summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/Makefile2
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/deploy/crds/onap_v1alpha1_collectdplugin_crd.yaml3
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/go.mod2
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdplugin_types.go2
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go1
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go10
-rw-r--r--vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdplugin/collectdplugin_controller.go149
7 files changed, 136 insertions, 33 deletions
diff --git a/vnfs/DAaaS/microservices/collectd-operator/Makefile b/vnfs/DAaaS/microservices/collectd-operator/Makefile
index 1c9311ea..07989148 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/Makefile
+++ b/vnfs/DAaaS/microservices/collectd-operator/Makefile
@@ -41,6 +41,8 @@ all: build test cover
debug:
@echo "Deleting ${COP_LOCAL} binary"
@rm -rf ${COP_LOCAL}
+ operator-sdk generate k8s --verbose
+ operator-sdk generate openapi --verbose
@echo "Building ${COP_LOCAL} binary"
@go build -o ${COP_LOCAL} -mod=vendor demo/vnfs/DAaaS/microservices/collectd-operator/cmd/manager
diff --git a/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/onap_v1alpha1_collectdplugin_crd.yaml b/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/onap_v1alpha1_collectdplugin_crd.yaml
index 9b944f48..e59c74f7 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/onap_v1alpha1_collectdplugin_crd.yaml
+++ b/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/onap_v1alpha1_collectdplugin_crd.yaml
@@ -52,11 +52,8 @@ spec:
items:
type: string
type: array
- collectdConf:
- type: object
required:
- collectdAgents
- - collectdConf
type: object
version: v1alpha1
versions:
diff --git a/vnfs/DAaaS/microservices/collectd-operator/go.mod b/vnfs/DAaaS/microservices/collectd-operator/go.mod
index dfaa4f8e..4554bbaf 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/go.mod
+++ b/vnfs/DAaaS/microservices/collectd-operator/go.mod
@@ -8,7 +8,7 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/emicklei/go-restful v2.8.1+incompatible // indirect
github.com/evanphx/json-patch v4.2.0+incompatible // indirect
- github.com/go-logr/logr v0.1.0 // indirect
+ github.com/go-logr/logr v0.1.0
github.com/go-logr/zapr v0.1.0 // indirect
github.com/go-openapi/spec v0.19.0
github.com/golang/groupcache v0.0.0-20180924190550-6f2cf27854a4 // indirect
diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdplugin_types.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdplugin_types.go
index d29498e8..5f1c0a83 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdplugin_types.go
+++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdplugin_types.go
@@ -1,7 +1,6 @@
package v1alpha1
import (
- corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@@ -26,7 +25,6 @@ type CollectdPluginStatus struct {
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
// CollectdAgents are the collectd pods in the Daemonset
CollectdAgents []string `json:"collectdAgents"`
- CollectdConf corev1.ConfigMap `json:"collectdConf"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go
index 0fe1dcc3..b7e2b34e 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go
+++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go
@@ -93,7 +93,6 @@ func (in *CollectdPluginStatus) DeepCopyInto(out *CollectdPluginStatus) {
*out = make([]string, len(*in))
copy(*out, *in)
}
- in.CollectdConf.DeepCopyInto(&out.CollectdConf)
return
}
diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go
index 97bd060e..d5821249 100644
--- a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go
+++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go
@@ -109,16 +109,10 @@ func schema_pkg_apis_onap_v1alpha1_CollectdPluginStatus(ref common.ReferenceCall
},
},
},
- "collectdConf": {
- SchemaProps: spec.SchemaProps{
- Ref: ref("k8s.io/api/core/v1.ConfigMap"),
- },
- },
},
- Required: []string{"collectdAgents", "collectdConf"},
+ Required: []string{"collectdAgents"},
},
},
- Dependencies: []string{
- "k8s.io/api/core/v1.ConfigMap"},
+ Dependencies: []string{},
}
}
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..5bab455e 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,7 @@ import (
"context"
"crypto/sha256"
"fmt"
- "time"
+ "github.com/go-logr/logr"
onapv1alpha1 "demo/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1"
@@ -76,6 +76,9 @@ type ReconcileCollectdPlugin struct {
scheme *runtime.Scheme
}
+// Define the collectdPlugin finalizer for handling deletion
+const collectdPluginFinalizer = "finalizer.collectdplugin.onap.org"
+
// 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 +106,69 @@ 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
+ }
+ }
+ err = r.handleCollectdPlugin(reqLogger, instance)
+ 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) error {
+
+ rmap, err := r.findResourceMapForCR(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)
//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
}
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,8 +177,8 @@ 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(cr *onapv1alpha1.CollectdPlugin) (ResourceMap, error) {
cmList := &corev1.ConfigMapList{}
opts := &client.ListOptions{}
rmap := ResourceMap{}
@@ -224,3 +247,93 @@ func rebuildCollectdConf(cpList *[]onapv1alpha1.CollectdPlugin) (string, error)
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{}
+ opts.SetLabelSelector("app=collectd")
+ 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); 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
+}