Added Daemonset Status predicate
[demo.git] / vnfs / DAaaS / microservices / collectd-operator / pkg / controller / utils / collectdutils.go
index 0b3cf3f..b379d91 100644 (file)
@@ -5,14 +5,15 @@ import (
        "crypto/sha256"
        "fmt"
        "os"
+       "sort"
        "sync"
 
        "github.com/go-logr/logr"
 
        onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
 
+       appsv1 "k8s.io/api/apps/v1"
        corev1 "k8s.io/api/core/v1"
-       extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
        "k8s.io/apimachinery/pkg/api/errors"
        "sigs.k8s.io/controller-runtime/pkg/client"
 )
@@ -30,10 +31,13 @@ const (
 
 var lock sync.Mutex
 
+// ReconcileLock - Used to sync between global and plugin controller
+var ReconcileLock sync.Mutex
+
 // ResourceMap to hold objects to update/reload
 type ResourceMap struct {
        ConfigMap       *corev1.ConfigMap
-       DaemonSet       *extensionsv1beta1.DaemonSet
+       DaemonSet       *appsv1.DaemonSet
        CollectdPlugins *[]onapv1alpha1.CollectdPlugin
 }
 
@@ -72,14 +76,28 @@ func GetWatchLabels() (string, error) {
        return labelSelector, nil
 }
 
-// FindResourceMapForCR returns the configMap, collectd Daemonset and list of Collectd Plugins
-func FindResourceMapForCR(rc client.Client, reqLogger logr.Logger, ns string) (*ResourceMap, error) {
+// GetCollectdPluginList returns the list of CollectdPlugin instances in the namespace ns
+func GetCollectdPluginList(rc client.Client, ns string) (*onapv1alpha1.CollectdPluginList, error) {
+       // Get all collectd plugins in the current namespace to rebuild conf.
+       collectdPlugins := &onapv1alpha1.CollectdPluginList{}
+       cpOpts := &client.ListOptions{}
+       cpOpts.InNamespace(ns)
+       err := rc.List(context.TODO(), cpOpts, collectdPlugins)
+       if err != nil {
+               return nil, err
+       }
+       return collectdPlugins, nil
+}
+
+// GetConfigMap returns the GetConfigMap in the namespace ns
+func GetConfigMap(rc client.Client, reqLogger logr.Logger, ns string) (*corev1.ConfigMap, error) {
        lock.Lock()
        defer lock.Unlock()
+
+       reqLogger.Info("Get ConfigMap for collectd.conf")
+       // Get all collectd plugins in the current namespace to rebuild conf.
        cmList := &corev1.ConfigMapList{}
        opts := &client.ListOptions{}
-       rmap := &ResourceMap{}
-
        // Select ConfigMaps with label
        labelSelector, err := GetWatchLabels()
        if err != nil {
@@ -90,41 +108,15 @@ func FindResourceMapForCR(rc client.Client, reqLogger logr.Logger, ns string) (*
 
        err = rc.List(context.TODO(), opts, cmList)
        if err != nil {
-               return rmap, err
+               return nil, err
        }
 
        if cmList.Items == nil || len(cmList.Items) == 0 {
-               return rmap, errors.NewNotFound(corev1.Resource("configmap"), "ConfigMap")
+               return nil, errors.NewNotFound(corev1.Resource("configmap"), "ConfigMap")
        }
 
-       // Select DaemonSets with label
-       dsList := &extensionsv1beta1.DaemonSetList{}
-       err = rc.List(context.TODO(), opts, dsList)
-       if err != nil {
-               return rmap, err
-       }
-
-       if dsList.Items == nil || len(dsList.Items) == 0 {
-               return rmap, errors.NewNotFound(corev1.Resource("daemonset"), "DaemonSet")
-       }
-
-       rmap.ConfigMap = &cmList.Items[0]
-       rmap.DaemonSet = &dsList.Items[0]
-
-       return rmap, err
-}
-
-// GetCollectdPluginList returns the list of CollectdPlugin instances in the namespace ns
-func GetCollectdPluginList(rc client.Client, ns string) (*onapv1alpha1.CollectdPluginList, error) {
-       // Get all collectd plugins in the current namespace to rebuild conf.
-       collectdPlugins := &onapv1alpha1.CollectdPluginList{}
-       cpOpts := &client.ListOptions{}
-       cpOpts.InNamespace(ns)
-       err := rc.List(context.TODO(), cpOpts, collectdPlugins)
-       if err != nil {
-               return nil, err
-       }
-       return collectdPlugins, nil
+       cm := &cmList.Items[0]
+       return cm, nil
 }
 
 // GetCollectdGlobal returns the CollectdGlobal instance in the namespace ns
@@ -207,12 +199,19 @@ func RebuildCollectdConf(rc client.Client, ns string, isDelete bool, delPlugin s
                collectdConf += collectdGlobalConf
        }
 
-       for cpName, cpConf := range loadPlugin {
+       pluginKeys := make([]string, 0, len(loadPlugin))
+       for k := range loadPlugin {
+               pluginKeys = append(pluginKeys, k)
+       }
+       sort.Strings(pluginKeys)
+
+       for _, cpName := range pluginKeys {
+               cpConf := loadPlugin[cpName]
                collectdConf += "LoadPlugin" + " " + cpName + "\n"
                collectdConf += cpConf + "\n"
        }
 
-       collectdConf += "#Last line (collectd requires ā€˜\\nā€™ at the last line)\n"
+       collectdConf += "#Last line (collectd requires '\\n' at the last line)\n"
 
        return collectdConf, nil
 }