From ebc40b61c64d5f06b5fe2b4d886ed4e5748db6d3 Mon Sep 17 00:00:00 2001 From: Dileep Ranganathan Date: Sat, 10 Aug 2019 15:15:49 -0700 Subject: [PATCH] Collectd Operator - Added TypesDB feature support Added support for typesDB. CollectGlobal CR can now add, update, delete TypesDB and custom TypesDB to the conf. This is achieved via creating a configmap for the types.db files and then passing the configmap name in the CR Spec. Issue-ID: ONAPARC-461 Signed-off-by: Dileep Ranganathan Change-Id: Idb27d7981c72f67863f193bd5013f6907362ede9 --- .../DAaaS/microservices/collectd-operator/Makefile | 1 - .../crds => examples/collectd}/collectdglobal.yaml | 0 .../collectd}/cpu_collectdplugin_cr.yaml | 0 .../examples/collectd/kustomization.yaml | 5 + .../collectd}/memory_collectdplugin_cr.yaml | 0 .../collectd}/prometheus_collectdplugin_cr.yaml | 0 .../examples/collectd/resources/types.db | 3 + .../examples/collectd/resources/vcmts.types.db | 3 + .../pkg/apis/onap/v1alpha1/collectdglobal_types.go | 4 +- .../pkg/apis/onap/v1alpha1/collectdplugin_types.go | 11 +- .../collectdglobal/collectdglobal_controller.go | 12 ++- .../collectdplugin/collectdplugin_controller.go | 7 +- .../pkg/controller/utils/collectdutils.go | 4 +- .../pkg/controller/utils/dsutils.go | 117 +++++++++++++++++++++ 14 files changed, 148 insertions(+), 19 deletions(-) rename vnfs/DAaaS/microservices/collectd-operator/{deploy/crds => examples/collectd}/collectdglobal.yaml (100%) rename vnfs/DAaaS/microservices/collectd-operator/{deploy/crds => examples/collectd}/cpu_collectdplugin_cr.yaml (100%) create mode 100644 vnfs/DAaaS/microservices/collectd-operator/examples/collectd/kustomization.yaml rename vnfs/DAaaS/microservices/collectd-operator/{deploy/crds => examples/collectd}/memory_collectdplugin_cr.yaml (100%) rename vnfs/DAaaS/microservices/collectd-operator/{deploy/crds => examples/collectd}/prometheus_collectdplugin_cr.yaml (100%) create mode 100644 vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/types.db create mode 100644 vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/vcmts.types.db create mode 100644 vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/dsutils.go diff --git a/vnfs/DAaaS/microservices/collectd-operator/Makefile b/vnfs/DAaaS/microservices/collectd-operator/Makefile index 7effedf0..52c779cf 100644 --- a/vnfs/DAaaS/microservices/collectd-operator/Makefile +++ b/vnfs/DAaaS/microservices/collectd-operator/Makefile @@ -26,7 +26,6 @@ build: clean GOOS=linux GOARCH=amd64 operator-sdk generate k8s --verbose operator-sdk generate openapi --verbose - #@go build -o ${COP} -gcflags all=-trimpath=${ROOTPATH} -asmflags all=-trimpath=${ROOTPATH} collectd-operator/cmd/manager @go build -o ${COP_LOCAL} collectd-operator/cmd/manager @operator-sdk build ${IMAGE_NAME} --verbose diff --git a/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/collectdglobal.yaml b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/collectdglobal.yaml similarity index 100% rename from vnfs/DAaaS/microservices/collectd-operator/deploy/crds/collectdglobal.yaml rename to vnfs/DAaaS/microservices/collectd-operator/examples/collectd/collectdglobal.yaml diff --git a/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/cpu_collectdplugin_cr.yaml b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/cpu_collectdplugin_cr.yaml similarity index 100% rename from vnfs/DAaaS/microservices/collectd-operator/deploy/crds/cpu_collectdplugin_cr.yaml rename to vnfs/DAaaS/microservices/collectd-operator/examples/collectd/cpu_collectdplugin_cr.yaml diff --git a/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/kustomization.yaml b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/kustomization.yaml new file mode 100644 index 00000000..17ff7ae2 --- /dev/null +++ b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/kustomization.yaml @@ -0,0 +1,5 @@ +configMapGenerator: +- name: typesdb-configmap + files: + - resources/types.db + - resources/vbng.types.db diff --git a/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/memory_collectdplugin_cr.yaml b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/memory_collectdplugin_cr.yaml similarity index 100% rename from vnfs/DAaaS/microservices/collectd-operator/deploy/crds/memory_collectdplugin_cr.yaml rename to vnfs/DAaaS/microservices/collectd-operator/examples/collectd/memory_collectdplugin_cr.yaml diff --git a/vnfs/DAaaS/microservices/collectd-operator/deploy/crds/prometheus_collectdplugin_cr.yaml b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/prometheus_collectdplugin_cr.yaml similarity index 100% rename from vnfs/DAaaS/microservices/collectd-operator/deploy/crds/prometheus_collectdplugin_cr.yaml rename to vnfs/DAaaS/microservices/collectd-operator/examples/collectd/prometheus_collectdplugin_cr.yaml diff --git a/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/types.db b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/types.db new file mode 100644 index 00000000..15da66df --- /dev/null +++ b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/types.db @@ -0,0 +1,3 @@ +DDDDDD 111111 +EEEEEE wdasdsadsa +dsadsadas sdsadsa diff --git a/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/vcmts.types.db b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/vcmts.types.db new file mode 100644 index 00000000..33f5c05f --- /dev/null +++ b/vnfs/DAaaS/microservices/collectd-operator/examples/collectd/resources/vcmts.types.db @@ -0,0 +1,3 @@ +111111 KKKKKK +222222 LLLLLL +333333 MMMMMM diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdglobal_types.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdglobal_types.go index 1094271d..d5c69fbd 100644 --- a/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdglobal_types.go +++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/apis/onap/v1alpha1/collectdglobal_types.go @@ -12,8 +12,8 @@ type CollectdGlobalSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file // Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html - GlobalOptions string `json:"globalOptions"` - ConfigMap string `json:"configMap,omitempty"` + GlobalOptions string `json:"globalOptions"` + ConfigMap string `json:"configMap,omitempty"` } // CollectdGlobalStatus defines the observed state of CollectdGlobal 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 065f14c4..eb338639 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 @@ -4,18 +4,17 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) - const ( //Initial indicates the initial status of CollectdPlugin - Initial = "" + Initial = "" //Created indicates the status of CollectdPlugin after first reconcile - Created = "Created" + Created = "Created" //Enabled indicates the status of CollectdPlugin after all the pods are reloaded - Enabled = "Enabled" + Enabled = "Enabled" //Deleting state - Deleting = "Deleting" + Deleting = "Deleting" //Deprecated state when a plugin with same name is created. Old plugin gets deprecated and deleted eventually. - Deprecated = "Deprecated" + Deprecated = "Deprecated" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdglobal/collectdglobal_controller.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdglobal/collectdglobal_controller.go index c48c2f5e..539b680d 100644 --- a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdglobal/collectdglobal_controller.go +++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/collectdglobal/collectdglobal_controller.go @@ -12,6 +12,7 @@ import ( onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1" collectdutils "collectd-operator/pkg/controller/utils" + dsutils "collectd-operator/pkg/controller/utils" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -213,7 +214,7 @@ func (r *ReconcileCollectdGlobal) handleCollectdGlobal(reqLogger logr.Logger, cr ds.Spec.Template.SetAnnotations(map[string]string{ "daaas-random": collectdutils.ComputeSHA256([]byte(collectdConf)), }) - r.handleAdditonalConfigMap(reqLogger, cr, ds) + r.handleTypesDB(reqLogger, cr, ds, isDelete) updateErr := r.client.Update(context.TODO(), ds) return updateErr }) @@ -311,7 +312,12 @@ func (r *ReconcileCollectdGlobal) addFinalizer(reqLogger logr.Logger, cr *onapv1 return nil } -func (r *ReconcileCollectdGlobal) handleAdditonalConfigMap(reqLogger logr.Logger, cr *onapv1alpha1.CollectdGlobal, ds *extensionsv1beta1.DaemonSet) error { +func (r *ReconcileCollectdGlobal) handleTypesDB(reqLogger logr.Logger, cr *onapv1alpha1.CollectdGlobal, ds *extensionsv1beta1.DaemonSet, isDelete bool) error { + if isDelete || cr.Spec.ConfigMap == "" { + dsutils.RemoveTypesDB(ds) + return nil + } + cm := &corev1.ConfigMap{} key := types.NamespacedName{Namespace: cr.Namespace, Name: cr.Spec.ConfigMap} err := r.client.Get(context.TODO(), key, cm) @@ -319,6 +325,6 @@ func (r *ReconcileCollectdGlobal) handleAdditonalConfigMap(reqLogger logr.Logger reqLogger.Info("Error getting TypesDB") return nil } - // TODO: Implement Types.DB mounting + dsutils.UpsertTypesDB(ds, cm, cr) return nil } 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 0e3af8d1..98d770a1 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 @@ -29,7 +29,6 @@ import ( var log = logf.Log.WithName("controller_collectdplugin") - // Add creates a new CollectdPlugin Controller and adds it to the Manager. The Manager will set fields on the Controller // and Start it when the Manager is Started. func Add(mgr manager.Manager) error { @@ -61,7 +60,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { err = c.Watch( &source.Kind{Type: &appsv1.DaemonSet{}}, &handler.EnqueueRequestsFromMapFunc{ - ToRequests: handler.ToRequestsFunc(func (a handler.MapObject) []reconcile.Request { + ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request { labelSelector, err := collectdutils.GetWatchLabels() labels := strings.Split(labelSelector, "=") if err != nil { @@ -69,7 +68,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { } rcp := r.(*ReconcileCollectdPlugin) // Select the Daemonset with labelSelector (Defautl is app=collectd) - if a.Meta.GetLabels()[labels[0]] == labels[1] { + if a.Meta.GetLabels()[labels[0]] == labels[1] { var requests []reconcile.Request cpList, err := collectdutils.GetCollectdPluginList(rcp.client, a.Meta.GetNamespace()) if err != nil { @@ -91,7 +90,6 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { return nil } - // blank assignment to verify that ReconcileCollectdPlugin implements reconcile.Reconciler var _ reconcile.Reconciler = &ReconcileCollectdPlugin{} @@ -103,7 +101,6 @@ type ReconcileCollectdPlugin struct { scheme *runtime.Scheme } - // 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 diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/collectdutils.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/collectdutils.go index b3ee8056..0b3cf3fd 100644 --- a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/collectdutils.go +++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/collectdutils.go @@ -19,7 +19,7 @@ import ( // Define the collectdPlugin finalizer for handling deletion const ( - defaultWatchLabel = "app=collectd" + defaultWatchLabel = "app=collectd" CollectdFinalizer = "finalizer.collectd.onap.org" // WatchLabelsEnvVar is the constant for env variable WATCH_LABELS @@ -200,7 +200,7 @@ func RebuildCollectdConf(rc client.Client, ns string, isDelete bool, delPlugin s } } } - + if isDelete { delete(loadPlugin, delPlugin) } else { diff --git a/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/dsutils.go b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/dsutils.go new file mode 100644 index 00000000..d6c60cce --- /dev/null +++ b/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/dsutils.go @@ -0,0 +1,117 @@ +package utils + +import ( + "path/filepath" + "strings" + "strconv" + + onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1" + + corev1 "k8s.io/api/core/v1" + extensionsv1beta1 "k8s.io/api/extensions/v1beta1" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" +) + +var log = logf.Log.WithName("dsutils") + +const ( + collectdContainerName = "collectd" + + // canonical label for the volume created for TypesDB + // reason - a DNS-1123 label must consist of lower case alphanumeric characters + // or '-', and must start and end with an alphanumeric character + typesDB = "types-db" +) + +// RemoveTypesDB - removes TypesDB volumes and volume mounts from collectd pods. +func RemoveTypesDB(ds *extensionsv1beta1.DaemonSet) { + vols := &ds.Spec.Template.Spec.Volumes + for i:=0; i < len(*vols); i++ { + if (*vols)[i].Name == typesDB { + *vols = append((*vols)[:i], (*vols)[i+1:]...) + i-- + } + } + + containers := &ds.Spec.Template.Spec.Containers + for j, container := range *containers { + if container.Name == collectdContainerName { + vms := &(*containers)[j].VolumeMounts + for i:=0; i < len(*vms); i++ { + if (*vms)[i].Name == typesDB { + *vms = append((*vms)[:i], (*vms)[i+1:]...) + i-- + } + } + } + } +} + +// UpsertTypesDB - Insert/Update TypesDB volumes and volume mounts to collectd pods. +func UpsertTypesDB(ds *extensionsv1beta1.DaemonSet, cm *corev1.ConfigMap, cr *onapv1alpha1.CollectdGlobal) { + typesVM := findMountInfo(cr) + if *typesVM == nil || len(*typesVM) == 0 { + return + } + typesDBVolume := &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{Name: cm.Name}, + } + vols := &ds.Spec.Template.Spec.Volumes + var hasUpdated bool + for i, vol := range *vols { + // Update case + if vol.Name == typesDB { + (*vols)[i].ConfigMap = typesDBVolume + hasUpdated = true + } + } + + if !hasUpdated { + //Insert case + *vols = append(*vols, corev1.Volume{ + Name: typesDB, + VolumeSource: corev1.VolumeSource{ + ConfigMap: typesDBVolume, + }, + }) + } + + containers := &ds.Spec.Template.Spec.Containers + + for j, container := range *containers { + if container.Name == collectdContainerName { + vms := &(*containers)[j].VolumeMounts + for i:=0; i < len(*vms); i++ { + // Update case (Equivalent to remove and add) + if (*vms)[i].Name == typesDB { + *vms = append((*vms)[:i], (*vms)[i+1:]...) + i-- + } + } + + *vms = append(*vms, *typesVM...) + } + } +} + +func findMountInfo(cr *onapv1alpha1.CollectdGlobal) *[]corev1.VolumeMount { + log.V(1).Info(":::::Entering findMountInfo:::::") + var typesVM []corev1.VolumeMount + globalOpts := strings.Split(cr.Spec.GlobalOptions, "\n") + log.V(1).Info(":::::findMountInfo:::::", "GlobalOptions", globalOpts) + for i, globalOpt := range globalOpts { + log.V(1).Info(":::::For Loop:::::", "Item No:", i, "LineEntry:", globalOpt) + s := strings.Fields(globalOpt) + log.V(1).Info(":::::s:::::", "s:", s) + if s != nil && len(s) != 0 && s[0] == "TypesDB" { + path,_ := strconv.Unquote(s[1]) + _, file := filepath.Split(path) + log.V(1).Info(":::::file:::::", "s[1]:", path, "file:", file) + vm := corev1.VolumeMount{Name: typesDB, MountPath: path, SubPath: file} + typesVM = append(typesVM, vm) + log.V(1).Info(":::::TypesVM:::::", "TypesVM:", typesVM) + } + } + log.V(1).Info(":::::Exiting findMountInfo:::::") + return &typesVM +} -- 2.16.6