Skip to content

Commit 4bb675f

Browse files
authored
Refactor members and services status update (#1056)
* Refactor members and services status update Signed-off-by: Daniel Fan <[email protected]> * Update typo in log message Signed-off-by: Daniel Fan <[email protected]> * Re-construct if condition for better code readability Signed-off-by: Daniel Fan <[email protected]> --------- Signed-off-by: Daniel Fan <[email protected]>
1 parent 484e3ee commit 4bb675f

File tree

4 files changed

+69
-82
lines changed

4 files changed

+69
-82
lines changed

api/v1alpha1/operandrequest_types.go

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,16 @@ func (r *OperandRequest) GetRegistryKey(req Request) types.NamespacedName {
631631
}
632632

633633
// InitRequestStatus OperandConfig status.
634-
func (r *OperandRequest) InitRequestStatus() bool {
634+
func (r *OperandRequest) InitRequestStatus(mu sync.Locker) bool {
635635
isInitialized := true
636636
if r.Status.Phase == "" {
637637
isInitialized = false
638-
r.Status.Phase = ClusterPhaseNone
639638
}
639+
for _, member := range r.Status.Members {
640+
klog.V(2).Info("Cleaning the member status for Operand: ", member.Name)
641+
r.RemoveOperandPhase(member.Name, mu)
642+
}
643+
r.Status.Phase = ClusterPhaseNone
640644
return isInitialized
641645
}
642646

@@ -679,58 +683,6 @@ func (r *OperandRequest) UpdateLabels() bool {
679683
return isUpdated
680684
}
681685

682-
func (r *OperandRequest) CheckServiceStatus() bool {
683-
requeue := false
684-
monitoredServices := []string{"ibm-iam-operator", "ibm-idp-config-ui-operator", "ibm-mongodb-operator", "ibm-im-operator"}
685-
servicesRequested := false
686-
for _, serviceName := range monitoredServices {
687-
if foundOperand(r.Spec.Requests, serviceName) {
688-
servicesRequested = true
689-
break
690-
}
691-
}
692-
if servicesRequested {
693-
if len(r.Status.Services) == 0 {
694-
klog.Info("Waiting for status.services to be instantiated ...")
695-
requeue = true
696-
return requeue
697-
}
698-
var IMOrIAM string
699-
exists := false
700-
if foundOperand(r.Spec.Requests, "ibm-iam-operator") {
701-
IMOrIAM = "ibm-iam-operator"
702-
exists = true
703-
} else if foundOperand(r.Spec.Requests, "ibm-im-operator") {
704-
IMOrIAM = "ibm-im-operator"
705-
exists = true
706-
}
707-
708-
if exists {
709-
var imIndex int
710-
found := false
711-
for i, s := range r.Status.Services {
712-
if IMOrIAM == s.OperatorName { //eventually this should be changed to the variable but the operator name is still listed as iam in practice even when im is requested
713-
found = true
714-
imIndex = i
715-
break
716-
}
717-
}
718-
if found {
719-
if r.Status.Services[imIndex].Status != "Ready" {
720-
klog.Info("Waiting for IM service to be Ready ...")
721-
requeue = true
722-
return requeue
723-
}
724-
} else {
725-
klog.Info("Waiting for IM service status ...")
726-
requeue = true
727-
return requeue
728-
}
729-
}
730-
}
731-
return requeue
732-
}
733-
734686
// GetAllRegistryReconcileRequest gets all the Registry ReconcileRequest.
735687
func (r *OperandRequest) GetAllRegistryReconcileRequest() []reconcile.Request {
736688
rrs := []reconcile.Request{}

controllers/constant/constant.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ const (
9393

9494
//DefaultCRRetryNumber is the default maximum number of retry for reconciling a custom resource
9595
DefaultCRRetryNumber = 3
96+
97+
//StatusMonitoredServices is the annotation key for monitored services
98+
StatusMonitoredServices = "status-monitored-services"
9699
)

controllers/operandrequest/operandrequest_controller.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
144144
}
145145

146146
// Initialize the status for OperandRequest instance
147-
if !requestInstance.InitRequestStatus() {
147+
if !requestInstance.InitRequestStatus(&r.Mutex) {
148148
return ctrl.Result{Requeue: true}, nil
149149
}
150150

@@ -156,18 +156,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
156156
return ctrl.Result{Requeue: true}, err
157157
}
158158

159-
// Clean the phase of each operand under spec.request.operand
160-
for _, member := range requestInstance.Status.Members {
161-
klog.V(2).Info("Cleaning the member status for Operand: ", member.Name)
162-
requestInstance.RemoveOperandPhase(member.Name, &r.Mutex)
163-
}
164-
requestInstance.Status.Phase = operatorv1alpha1.ClusterPhaseNone
165-
166-
if err := r.Client.Status().Update(ctx, requestInstance); err != nil {
167-
klog.Errorf("failed to update the status of the OperandRequest %s: %v", req.NamespacedName.String(), err)
168-
return ctrl.Result{}, err
169-
}
170-
171159
// Reconcile Operators
172160
if err := r.reconcileOperator(ctx, requestInstance); err != nil {
173161
klog.Errorf("failed to reconcile Operators for OperandRequest %s: %v", req.NamespacedName.String(), err)
@@ -186,8 +174,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
186174
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
187175
}
188176

189-
//check if status.services is present (if a relevant service was requested), requeue again is im/iam is not ready yet
190-
if requestInstance.CheckServiceStatus() {
177+
//check if status.services is present (if a relevant service was requested), requeue again if service is not ready yet
178+
if isReady, err := r.ServiceStatusIsReady(ctx, requestInstance); !isReady || err != nil {
191179
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
192180
}
193181

@@ -416,7 +404,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
416404
UpdateFunc: func(e event.UpdateEvent) bool {
417405
oldObject := e.ObjectOld.(*operatorv1alpha1.OperandRegistry)
418406
newObject := e.ObjectNew.(*operatorv1alpha1.OperandRegistry)
419-
return !reflect.DeepEqual(oldObject.Spec, newObject.Spec)
407+
return !reflect.DeepEqual(oldObject.Spec, newObject.Spec) || !reflect.DeepEqual(oldObject.GetAnnotations(), newObject.GetAnnotations())
420408
},
421409
DeleteFunc: func(e event.DeleteEvent) bool {
422410
// Evaluates to false if the object has been confirmed deleted.

controllers/operandrequest/reconcile_operand.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,16 @@ func (r *Reconciler) reconcileCRwithConfig(ctx context.Context, service *operato
326326
merr.Add(err)
327327
continue
328328
}
329-
statusSpec, err := r.getOperandStatus(crFromALM)
329+
statusFromCR, err := r.getOperandStatus(crFromALM)
330330
if err != nil {
331331
return err
332332
}
333333
serviceKind := crFromALM.GetKind()
334-
if serviceKind != "OperandRequest" && statusSpec.ObjectName != "" {
334+
if serviceKind != "OperandRequest" && statusFromCR.ObjectName != "" {
335335
var resources []operatorv1alpha1.OperandStatus
336-
resources = append(resources, statusSpec)
337-
serviceSpec := newServiceStatus(operandName, operatorNamespace, resources)
338-
seterr := requestInstance.SetServiceStatus(ctx, serviceSpec, r.Client, mu)
339-
if seterr != nil {
336+
resources = append(resources, statusFromCR)
337+
serviceStatus := newServiceStatus(operandName, operatorNamespace, resources)
338+
if seterr := requestInstance.SetServiceStatus(ctx, serviceStatus, r.Client, mu); seterr != nil {
340339
return seterr
341340
}
342341
}
@@ -410,15 +409,15 @@ func (r *Reconciler) reconcileCRwithRequest(ctx context.Context, requestInstance
410409
if err := r.updateCustomResource(ctx, crFromRequest, requestKey.Namespace, operand.Kind, operand.Spec.Raw, map[string]interface{}{}, requestInstance); err != nil {
411410
return err
412411
}
413-
statusSpec, err := r.getOperandStatus(crFromRequest)
412+
statusFromCR, err := r.getOperandStatus(crFromRequest)
414413
if err != nil {
415414
return err
416415
}
417-
if operand.Kind != "OperandRequest" && statusSpec.ObjectName != "" {
416+
if operand.Kind != "OperandRequest" && statusFromCR.ObjectName != "" {
418417
var resources []operatorv1alpha1.OperandStatus
419-
resources = append(resources, statusSpec)
420-
serviceSpec := newServiceStatus(operand.Name, operatorNamespace, resources)
421-
seterr := requestInstance.SetServiceStatus(ctx, serviceSpec, r.Client, mu)
418+
resources = append(resources, statusFromCR)
419+
serviceStatus := newServiceStatus(operand.Name, operatorNamespace, resources)
420+
seterr := requestInstance.SetServiceStatus(ctx, serviceStatus, r.Client, mu)
422421
if seterr != nil {
423422
return seterr
424423
}
@@ -481,7 +480,6 @@ func newServiceStatus(operatorName string, namespace string, resources []operato
481480
}
482481
}
483482
serviceSpec.Status = status //TODO logic to determine readiness
484-
// serviceSpec.LastUpdateTime = time.Now().Format(time.RFC3339)
485483
serviceSpec.Resources = resources
486484
return serviceSpec
487485
}
@@ -1358,3 +1356,49 @@ func (r *Reconciler) setOwnerReferences(ctx context.Context, controlledRes *unst
13581356
}
13591357
return nil
13601358
}
1359+
1360+
func (r *Reconciler) ServiceStatusIsReady(ctx context.Context, requestInstance *operatorv1alpha1.OperandRequest) (bool, error) {
1361+
requestedServicesSet := make(map[string]struct{})
1362+
for _, req := range requestInstance.Spec.Requests {
1363+
registryKey := requestInstance.GetRegistryKey(req)
1364+
registryInstance, err := r.GetOperandRegistry(ctx, registryKey)
1365+
if err != nil {
1366+
klog.Errorf("Failed to get OperandRegistry %s, %v", registryKey, err)
1367+
return false, err
1368+
}
1369+
if registryInstance.Annotations != nil && registryInstance.Annotations[constant.StatusMonitoredServices] != "" {
1370+
monitoredServices := strings.Split(registryInstance.Annotations[constant.StatusMonitoredServices], ",")
1371+
for _, operand := range req.Operands {
1372+
if util.Contains(monitoredServices, operand.Name) {
1373+
requestedServicesSet[operand.Name] = struct{}{}
1374+
}
1375+
}
1376+
}
1377+
}
1378+
1379+
if len(requestedServicesSet) == 0 {
1380+
klog.V(2).Infof("No services to be monitored for OperandRequest %s/%s", requestInstance.Namespace, requestInstance.Name)
1381+
return true, nil
1382+
}
1383+
1384+
if len(requestInstance.Status.Services) == 0 {
1385+
klog.Infof("Waiting for status.services to be instantiated for OperandRequest %s/%s ...", requestInstance.Namespace, requestInstance.Name)
1386+
return false, nil
1387+
}
1388+
if len(requestedServicesSet) != len(requestInstance.Status.Services) {
1389+
klog.Infof("Waiting for status of all requested services to be instantiated for OperandRequest %s/%s ...", requestInstance.Namespace, requestInstance.Name)
1390+
return false, nil
1391+
}
1392+
1393+
serviceStatus := true
1394+
// wait for the status of the requested services to be ready
1395+
for _, s := range requestInstance.Status.Services {
1396+
if _, ok := requestedServicesSet[s.OperatorName]; ok {
1397+
if s.Status != "Ready" {
1398+
klog.Infof("Waiting for status of service %s to be Ready for OperandRequest %s/%s ...", s.OperatorName, requestInstance.Namespace, requestInstance.Name)
1399+
serviceStatus = false
1400+
}
1401+
}
1402+
}
1403+
return serviceStatus, nil
1404+
}

0 commit comments

Comments
 (0)