Added Daemonset Status predicate
[demo.git] / vnfs / DAaaS / microservices / collectd-operator / pkg / controller / collectdglobal / collectdglobal_controller.go
index 0d3e2bb..8c6023b 100644 (file)
@@ -12,6 +12,7 @@ import (
 
        onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
        collectdutils "collectd-operator/pkg/controller/utils"
+       dspredicate "collectd-operator/pkg/controller/utils"
        dsutils "collectd-operator/pkg/controller/utils"
 
        appsv1 "k8s.io/api/apps/v1"
@@ -69,22 +70,13 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
                                        log.Error(err, "Failed to get watch labels, continuing with default label")
                                }
                                rcp := r.(*ReconcileCollectdGlobal)
-                               // Select the Daemonset with labelSelector (Defautl  is app=collectd)
+                               // Select the Daemonset with labelSelector (Default  is app=collectd)
                                if a.Meta.GetLabels()[labels[0]] == labels[1] {
                                        var requests []reconcile.Request
                                        cg, err := collectdutils.GetCollectdGlobal(rcp.client, a.Meta.GetNamespace())
                                        if err != nil || cg == nil {
                                                log.V(1).Info("No CollectdGlobal CR instance Exist")
-                                               cpList, err := collectdutils.GetCollectdPluginList(rcp.client, a.Meta.GetNamespace())
-                                               if err != nil || cpList == nil || cpList.Items == nil || len(cpList.Items) == 0 {
-                                                       log.V(1).Info("No CollectdPlugin CR instance Exist")
-                                                       return nil
-                                               }
-                                               for _, cp := range cpList.Items {
-                                                       requests = append(requests, reconcile.Request{
-                                                               NamespacedName: client.ObjectKey{Namespace: cp.Namespace, Name: cp.Name}})
-                                               }
-                                               return requests
+                                               return nil
                                        }
                                        requests = append(requests, reconcile.Request{
                                                NamespacedName: client.ObjectKey{Namespace: cg.Namespace, Name: cg.Name}})
@@ -92,7 +84,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
                                }
                                return nil
                        }),
-               }, predicate.GenerationChangedPredicate{})
+               }, dspredicate.DaemonSetStatusChangedPredicate{})
        if err != nil {
                return err
        }
@@ -224,10 +216,13 @@ func (r *ReconcileCollectdGlobal) handleCollectdGlobal(reqLogger logr.Logger, cr
                panic(fmt.Errorf("Update failed: %v", retryErr))
        }
 
-       err := r.updateStatus(cr)
-       if err != nil {
-               reqLogger.Error(err, "Unable to update status")
+       retryErr = retry.RetryOnConflict(retry.DefaultRetry, func() error {
+               err := r.updateStatus(cr)
                return err
+       })
+
+       if retryErr != nil {
+               panic(fmt.Errorf("Update failed: %v", retryErr))
        }
        // Reconcile success
        reqLogger.Info("Reconcile success!!")
@@ -266,22 +261,29 @@ func (r *ReconcileCollectdGlobal) handleDelete(reqLogger logr.Logger, cr *onapv1
 }
 
 func (r *ReconcileCollectdGlobal) updateStatus(cr *onapv1alpha1.CollectdGlobal) error {
-       switch cr.Status.Status {
+       // Fetch the CollectdGlobal instance
+       instance := &onapv1alpha1.CollectdGlobal{}
+       key := types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name}
+       err := r.client.Get(context.TODO(), key, instance)
+       if err != nil {
+               return err
+       }
+       switch instance.Status.Status {
        case onapv1alpha1.Initial:
-               cr.Status.Status = onapv1alpha1.Created
+               instance.Status.Status = onapv1alpha1.Created
        case onapv1alpha1.Created, onapv1alpha1.Enabled:
-               pods, err := collectdutils.GetPodList(r.client, cr.Namespace)
+               pods, err := collectdutils.GetPodList(r.client, instance.Namespace)
                if err != nil {
                        return err
                }
-               if !reflect.DeepEqual(pods, cr.Status.CollectdAgents) {
-                       cr.Status.CollectdAgents = pods
-                       cr.Status.Status = onapv1alpha1.Enabled
+               if !reflect.DeepEqual(pods, instance.Status.CollectdAgents) {
+                       instance.Status.CollectdAgents = pods
+                       instance.Status.Status = onapv1alpha1.Enabled
                }
        case onapv1alpha1.Deleting, onapv1alpha1.Deprecated:
                return nil
        }
-       err := r.client.Status().Update(context.TODO(), cr)
+       err = r.client.Status().Update(context.TODO(), instance)
        return err
 }
 
@@ -301,11 +303,12 @@ func (r *ReconcileCollectdGlobal) addFinalizer(reqLogger logr.Logger, cr *onapv1
        // Update status from Initial to Created
        // Since addFinalizer will be executed only once,
        // the status will be changed from Initial state to Created
-       updateErr := r.updateStatus(cr)
-       if updateErr != nil {
-               reqLogger.Error(updateErr, "Failed to update status from Initial state")
-       }
+       // updateErr := r.updateStatus(cr)
+       // if updateErr != nil {
+       //      reqLogger.Error(updateErr, "Failed to update status from Initial state")
+       // }
        // Update CR
+       cr.Status.Status = onapv1alpha1.Created
        err := r.client.Update(context.TODO(), cr)
        if err != nil {
                reqLogger.Error(err, "Failed to update CollectdGlobal with finalizer")