diff options
author | Dileep Ranganathan <dileep.ranganathan@intel.com> | 2019-08-10 15:15:49 -0700 |
---|---|---|
committer | Dileep Ranganathan <dileep.ranganathan@intel.com> | 2019-08-12 10:51:22 -0700 |
commit | ebc40b61c64d5f06b5fe2b4d886ed4e5748db6d3 (patch) | |
tree | 57aa45894e58677d17096c7416f44b4c24c25775 /vnfs/DAaaS/microservices/collectd-operator/pkg/controller | |
parent | d59e778f2b97c96fe7f6c3945f4bb9c91ff01e74 (diff) |
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 <dileep.ranganathan@intel.com>
Change-Id: Idb27d7981c72f67863f193bd5013f6907362ede9
Diffstat (limited to 'vnfs/DAaaS/microservices/collectd-operator/pkg/controller')
4 files changed, 130 insertions, 10 deletions
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 +} |