Skip to content

Commit

Permalink
Merge pull request #94 from Abirdcfly/fixorgstatus
Browse files Browse the repository at this point in the history
fix: org status sometimes cannot be changed to Deployed
  • Loading branch information
bjwswang authored Feb 10, 2023
2 parents bb75c7e + e88040a commit bfce67e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 56 deletions.
17 changes: 11 additions & 6 deletions config/samples/example-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,19 @@ kubectl create -f config/samples/orgs/org1.yaml --dry-run=client -o json |
kubectl create --token=${Admin1Token} -f -
function waitOrgReady() {
orgName=$1
wantFedName=$2
START_TIME=$(date +%s)
while true; do
status=$(kubectl get org $orgName --ignore-not-found=true -o json | jq -r .status.type)
if [ "$status" == "Deployed" ]; then
break
fi

# TODO after finish https://github.com/bestchains/fabric-operator/issues/79, remove check ca status
caStatus=$(kubectl get ibpca -n $orgName $orgName --ignore-not-found=true -o json | jq -r '.status.type')
if [ "$caStatus" == "Deployed" ]; then
if [[ $fedName != "" ]]; then
getFedName=$(kubectl get org $orgName --ignore-not-found=true -o json | jq -r '.status.federations[0].name')
if [[ $wantFedName == $getFedName ]]; then
break
else
continue
fi
fi
break
fi

Expand Down Expand Up @@ -313,6 +316,8 @@ function waitFed() {
done
}
waitFed federation-sample "Exist"
waitOrgReady "org1" "federation-sample"
waitOrgReady "org1" "federation-sample"

info "4.3 create federation create proposal for fed=federation-sample"

Expand Down
5 changes: 4 additions & 1 deletion controllers/organization/organization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func add(mgr manager.Manager, r *ReconcileOrganization) error {
caFuncs := predicate.Funcs{
UpdateFunc: r.CAUpdateFunc,
}
err = c.Watch(&source.Kind{Type: &current.IBPCA{}}, &handler.EnqueueRequestForObject{}, caFuncs)
err = c.Watch(&source.Kind{Type: &current.IBPCA{}}, &handler.EnqueueRequestForOwner{
OwnerType: &current.Organization{},
IsController: true,
}, caFuncs)
if err != nil {
return err
}
Expand Down
25 changes: 9 additions & 16 deletions controllers/organization/predict.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,20 @@ func (r *ReconcileOrganization) AddFed(m current.Member, federation *current.Fed

// CA related predict funcs
func (r *ReconcileOrganization) CAUpdateFunc(e event.UpdateEvent) bool {
var err error

update := Update{}
oldCA := e.ObjectOld.(*current.IBPCA)
newCA := e.ObjectNew.(*current.IBPCA)
log.Info(fmt.Sprintf("Update event detected for ibpca '%s'", oldCA.GetName()))

org := &current.Organization{}
err = r.client.Get(context.TODO(), types.NamespacedName{
Name: newCA.GetOrganization().Name,
}, org)
if err != nil {
log.Error(err, fmt.Sprintf("failed to get organization %s`", newCA.GetOrganization().Name))
if reflect.DeepEqual(oldCA.Status.CRStatus, newCA.Status.CRStatus) {
return false
}
// sync to CAStatus
err = r.SetStatus(org, &newCA.Status.CRStatus)
if err != nil {
log.Error(err, fmt.Sprintf("set organization %s to %s", org.GetName(), newCA.Status.Type))
}

return false
log.Info(fmt.Sprintf("Update event detected for ibpca '%s'", oldCA.GetName()))
orgName := oldCA.GetOrganization().Name

update.caStatusUpdated = true
r.PushUpdate(orgName, update)

return true
}

func (r *ReconcileOrganization) UpdateStatus(organization current.NamespacedName, newStatus current.CRStatus) error {
Expand Down
15 changes: 12 additions & 3 deletions controllers/organization/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
type Update struct {
specUpdated bool

adminUpdated bool
tokenUpdated bool
adminUpdated bool
tokenUpdated bool
caStatusUpdated bool

// cache orgigin admin
adminTransfered string
Expand Down Expand Up @@ -63,6 +64,10 @@ func (u *Update) TokenUpdated() bool {
return u.tokenUpdated
}

func (u *Update) CAStatusUpdated() bool {
return u.caStatusUpdated
}

// GetUpdateStackWithTrues is a helper method to print updates that have been detected
func (u *Update) GetUpdateStackWithTrues() string {
stack := ""
Expand All @@ -88,7 +93,11 @@ func (u *Update) GetUpdateStackWithTrues() string {
}

if u.ClientsRemoved() != "" {
stack += "clientsRemoved"
stack += "clientsRemoved "
}

if u.CAStatusUpdated() {
stack += "caStatusUpdated "
}

if len(stack) == 0 {
Expand Down
107 changes: 89 additions & 18 deletions pkg/offering/base/organization/mocks/update.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions pkg/offering/base/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

Expand All @@ -53,6 +54,7 @@ type Update interface {
AdminTransfered() string
ClientsUpdated() bool
ClientsRemoved() string
CAStatusUpdated() bool
}

type Override interface {
Expand All @@ -73,7 +75,7 @@ type Organization interface {
PreReconcileChecks(instance *current.Organization, update Update) error
Initialize(instance *current.Organization, update Update) error
ReconcileManagers(instance *current.Organization, update Update) error
CheckStates(instance *current.Organization) (common.Result, error)
CheckStates(instance *current.Organization, update Update) (common.Result, error)
Reconcile(instance *current.Organization, update Update) (common.Result, error)
}

Expand Down Expand Up @@ -162,7 +164,7 @@ func (organization *BaseOrganization) Reconcile(instance *current.Organization,
return common.Result{}, errors.Wrap(err, "failed to reconcile managers")
}

return organization.CheckStates(instance)
return organization.CheckStates(instance, update)
}

// PreReconcileChecks on Organization upon Update
Expand Down Expand Up @@ -322,13 +324,24 @@ func (organization *BaseOrganization) ReconcileUsers(instance *current.Organizat
}

// CheckStates on Organization
func (organization *BaseOrganization) CheckStates(instance *current.Organization) (common.Result, error) {
return common.Result{
Status: &current.CRStatus{
Type: current.Deploying,
Version: version.Operator,
},
}, nil
func (organization *BaseOrganization) CheckStates(instance *current.Organization, update Update) (common.Result, error) {
if update.CAStatusUpdated() && instance.Status.Type != current.Error {
ca := &current.IBPCA{}
ca.Namespace = instance.GetCA().Namespace
ca.Name = instance.GetCA().Name
if err := organization.Client.Get(context.TODO(), client.ObjectKeyFromObject(ca), ca); err == nil {
return common.Result{Status: &ca.Status.CRStatus}, nil
}
}
if !instance.HasType() {
return common.Result{
Status: &current.CRStatus{
Type: current.Deploying,
Version: version.Operator,
},
}, nil
}
return common.Result{}, nil
}

// GetLabels from instance.GetLabels
Expand Down
Loading

0 comments on commit bfce67e

Please sign in to comment.