X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=vnfs%2FDAaaS%2Fmicroservices%2Fcollectd-operator%2Fpkg%2Fcontroller%2Fcollectdplugin%2Fcollectdplugin_controller.go;h=3e99cdb37d92daca1880424335113e6cce51db81;hb=5115ed306a8f52fa806cf7f11b47d76953abb3ff;hp=a7990a0d6defc37ab70a30111aedea2860280d0d;hpb=83714f905f0483a84b8af8132a14ea1464bd0355;p=demo.git diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdplugin/collectdplugin_controller.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdplugin/collectdplugin_controller.go index a7990a0d..3e99cdb3 100644 --- a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdplugin/collectdplugin_controller.go +++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdplugin/collectdplugin_controller.go @@ -4,7 +4,8 @@ import ( "context" "crypto/sha256" "fmt" - "time" + "github.com/go-logr/logr" + "os" onapv1alpha1 "demo/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1" @@ -76,6 +77,17 @@ type ReconcileCollectdPlugin struct { scheme *runtime.Scheme } +// Define the collectdPlugin finalizer for handling deletion +const ( + defaultWatchLabel = "app=collectd" + collectdPluginFinalizer = "finalizer.collectdplugin.onap.org" + + // WatchLabelsEnvVar is the constant for env variable WATCH_LABELS + // which is the labels where the watch activity happens. + // this value is empty if the operator is running with clusterScope. + WatchLabelsEnvVar = "WATCH_LABELS" +) + // Reconcile reads that state of the cluster for a CollectdPlugin object and makes changes based on the state read // and what is in the CollectdPlugin.Spec // TODO(user): Modify this Reconcile function to implement your Controller logic. This example creates @@ -103,49 +115,71 @@ func (r *ReconcileCollectdPlugin) Reconcile(request reconcile.Request) (reconcil return reconcile.Result{}, err } - rmap, err := findResourceMapForCR(r, instance) - if err != nil { - reqLogger.Info("Skip reconcile: ConfigMap not found") + // Handle Delete CR for additional cleanup + isDelete, err := r.handleDelete(reqLogger, instance) + if isDelete { return reconcile.Result{}, err } + // Add finalizer for this CR + if !contains(instance.GetFinalizers(), collectdPluginFinalizer) { + if err := r.addFinalizer(reqLogger, instance); err != nil { + return reconcile.Result{}, err + } + return reconcile.Result{}, nil + } + err = r.handleCollectdPlugin(reqLogger, instance, false) + return reconcile.Result{}, err +} + +// handleCollectdPlugin regenerates the collectd conf on CR Create, Update, Delete events +func (r *ReconcileCollectdPlugin) handleCollectdPlugin(reqLogger logr.Logger, cr *onapv1alpha1.CollectdPlugin, isDelete bool) error { + + rmap, err := r.findResourceMapForCR(reqLogger, cr) + if err != nil { + reqLogger.Error(err, "Skip reconcile: Resources not found") + return err + } + cm := rmap.configMap ds := rmap.daemonSet collectPlugins := rmap.collectdPlugins reqLogger.V(1).Info("Found ResourceMap") - reqLogger.V(1).Info("ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name) - reqLogger.V(1).Info("DaemonSet.Namespace", ds.Namespace, "DaemonSet.Name", ds.Name) + reqLogger.V(1).Info(":::: ConfigMap Info ::::", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name) + reqLogger.V(1).Info(":::: DaemonSet Info ::::", "DaemonSet.Namespace", ds.Namespace, "DaemonSet.Name", ds.Name) - collectdConf, err := rebuildCollectdConf(collectPlugins) + collectdConf, err := rebuildCollectdConf(cr, collectPlugins, isDelete) //Restart Collectd Pods - - ts := time.Now().Format(time.RFC850) - reqLogger.V(1).Info("Timestamp : ", ts) + //Restart only if hash of configmap has changed. ds.Spec.Template.SetAnnotations(map[string]string{ - "daaas-random": ComputeSHA256([]byte(ts)), + "daaas-random": ComputeSHA256([]byte(collectdConf)), }) cm.SetAnnotations(map[string]string{ - "daaas-random": ComputeSHA256([]byte(ts)), + "daaas-random": ComputeSHA256([]byte(collectdConf)), }) cm.Data["node-collectd.conf"] = collectdConf // Update the ConfigMap with new Spec and reload DaemonSets reqLogger.Info("Updating the ConfigMap", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name) - log.Info("ConfigMap Data", "Map: ", cm.Data) + log.V(1).Info("ConfigMap Data", "Map: ", cm.Data) err = r.client.Update(context.TODO(), cm) if err != nil { - return reconcile.Result{}, err + reqLogger.Error(err, "Update the ConfigMap failed", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name) + return err } + reqLogger.Info("Reloading the Daemonset", "DaemonSet.Namespace", ds.Namespace, "DaemonSet.Name", ds.Name) err = r.client.Update(context.TODO(), ds) if err != nil { - return reconcile.Result{}, err + reqLogger.Error(err, "Update the DaemonSet failed", "DaemonSet.Namespace", ds.Namespace, "DaemonSet.Name", ds.Name) + return err } + r.updateStatus(cr) // Reconcile success - reqLogger.Info("Updated the ConfigMap", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name) - return reconcile.Result{}, nil + reqLogger.Info("Reconcile success!!") + return nil } // ComputeSHA256 returns hash of data as string @@ -154,25 +188,30 @@ func ComputeSHA256(data []byte) string { return fmt.Sprintf("%x", hash) } -// findConfigMapForCR returns the configMap used by collectd Daemonset -func findResourceMapForCR(r *ReconcileCollectdPlugin, cr *onapv1alpha1.CollectdPlugin) (ResourceMap, error) { +// findResourceMapForCR returns the configMap, collectd Daemonset and list of Collectd Plugins +func (r *ReconcileCollectdPlugin) findResourceMapForCR(reqLogger logr.Logger, cr *onapv1alpha1.CollectdPlugin) (ResourceMap, error) { cmList := &corev1.ConfigMapList{} opts := &client.ListOptions{} rmap := ResourceMap{} - // Select ConfigMaps with label app=collectd - opts.SetLabelSelector("app=collectd") + // Select ConfigMaps with label + labelSelector, err := getWatchLabels() + if err != nil { + reqLogger.Error(err, "Failed to get watch labels, continuing with default label") + } + opts.SetLabelSelector(labelSelector) opts.InNamespace(cr.Namespace) - err := r.client.List(context.TODO(), opts, cmList) + + err = r.client.List(context.TODO(), opts, cmList) if err != nil { return rmap, err } if cmList.Items == nil || len(cmList.Items) == 0 { - return rmap, err + return rmap, errors.NewNotFound(corev1.Resource("configmap"), "ConfigMap") } - // Select DaemonSets with label app=collectd + // Select DaemonSets with label dsList := &extensionsv1beta1.DaemonSetList{} err = r.client.List(context.TODO(), opts, dsList) if err != nil { @@ -180,7 +219,7 @@ func findResourceMapForCR(r *ReconcileCollectdPlugin, cr *onapv1alpha1.CollectdP } if dsList.Items == nil || len(dsList.Items) == 0 { - return rmap, err + return rmap, errors.NewNotFound(corev1.Resource("daemonset"), "DaemonSet") } // Get all collectd plugins in the current namespace to rebuild conf. @@ -199,7 +238,7 @@ func findResourceMapForCR(r *ReconcileCollectdPlugin, cr *onapv1alpha1.CollectdP } // Get all collectd plugins and reconstruct, compute Hash and check for changes -func rebuildCollectdConf(cpList *[]onapv1alpha1.CollectdPlugin) (string, error) { +func rebuildCollectdConf(cr *onapv1alpha1.CollectdPlugin, cpList *[]onapv1alpha1.CollectdPlugin, isDelete bool) (string, error) { var collectdConf string if *cpList == nil || len(*cpList) == 0 { return "", errors.NewNotFound(corev1.Resource("collectdplugin"), "CollectdPlugin") @@ -213,6 +252,10 @@ func rebuildCollectdConf(cpList *[]onapv1alpha1.CollectdPlugin) (string, error) } } + if isDelete { + delete(loadPlugin, cr.Spec.PluginName) + } + log.V(1).Info("::::::: Plugins Map ::::::: ", "PluginMap ", loadPlugin) for cpName, cpConf := range loadPlugin { @@ -220,7 +263,108 @@ func rebuildCollectdConf(cpList *[]onapv1alpha1.CollectdPlugin) (string, error) collectdConf += cpConf + "\n" } - collectdConf += "\n#Last line (collectd requires ‘\\n’ at the last line)" + collectdConf += "#Last line (collectd requires ‘\\n’ at the last line)\n" return collectdConf, nil } + +// Handle Delete CR event for additional cleanup +func (r *ReconcileCollectdPlugin) handleDelete(reqLogger logr.Logger, cr *onapv1alpha1.CollectdPlugin) (bool, error) { + // Check if the CollectdPlugin instance is marked to be deleted, which is + // indicated by the deletion timestamp being set. + isMarkedToBeDeleted := cr.GetDeletionTimestamp() != nil + if isMarkedToBeDeleted { + if contains(cr.GetFinalizers(), collectdPluginFinalizer) { + // Run finalization logic for collectdPluginFinalizer. If the + // finalization logic fails, don't remove the finalizer so + // that we can retry during the next reconciliation. + if err := r.finalizeCollectdPlugin(reqLogger, cr); err != nil { + return isMarkedToBeDeleted, err + } + + // Remove collectdPluginFinalizer. Once all finalizers have been + // removed, the object will be deleted. + cr.SetFinalizers(remove(cr.GetFinalizers(), collectdPluginFinalizer)) + err := r.client.Update(context.TODO(), cr) + if err != nil { + return isMarkedToBeDeleted, err + } + } + } + return isMarkedToBeDeleted, nil +} + +func (r *ReconcileCollectdPlugin) updateStatus(cr *onapv1alpha1.CollectdPlugin) error { + podList := &corev1.PodList{} + opts := &client.ListOptions{} + // Select ConfigMaps with label + labelSelector, _ := getWatchLabels() + opts.SetLabelSelector(labelSelector) + var pods []string + opts.InNamespace(cr.Namespace) + err := r.client.List(context.TODO(), opts, podList) + if err != nil { + return err + } + + if podList.Items == nil || len(podList.Items) == 0 { + return err + } + + for _, pod := range podList.Items { + pods = append(pods, pod.Name) + } + cr.Status.CollectdAgents = pods + err = r.client.Status().Update(context.TODO(), cr) + return err +} + +func (r *ReconcileCollectdPlugin) finalizeCollectdPlugin(reqLogger logr.Logger, cr *onapv1alpha1.CollectdPlugin) error { + // Cleanup by regenerating new collectd conf and rolling update of DaemonSet + if err := r.handleCollectdPlugin(reqLogger, cr, true); err != nil { + reqLogger.Error(err, "Finalize CollectdPlugin failed!!") + return err + } + reqLogger.Info("Successfully finalized CollectdPlugin!!") + return nil +} + +func (r *ReconcileCollectdPlugin) addFinalizer(reqLogger logr.Logger, cr *onapv1alpha1.CollectdPlugin) error { + reqLogger.Info("Adding Finalizer for the CollectdPlugin") + cr.SetFinalizers(append(cr.GetFinalizers(), collectdPluginFinalizer)) + + // Update CR + err := r.client.Update(context.TODO(), cr) + if err != nil { + reqLogger.Error(err, "Failed to update CollectdPlugin with finalizer") + return err + } + return nil +} + +func contains(list []string, s string) bool { + for _, v := range list { + if v == s { + return true + } + } + return false +} + +func remove(list []string, s string) []string { + for i, v := range list { + if v == s { + list = append(list[:i], list[i+1:]...) + } + } + return list +} + +// getWatchLabels returns the labels the operator should be watching for changes +func getWatchLabels() (string, error) { + labelSelector, found := os.LookupEnv(WatchLabelsEnvVar) + if !found { + return defaultWatchLabel, fmt.Errorf("%s must be set", WatchLabelsEnvVar) + } + return labelSelector, nil +}