10 "github.com/go-logr/logr"
12 onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
14 appsv1 "k8s.io/api/apps/v1"
15 corev1 "k8s.io/api/core/v1"
16 "k8s.io/apimachinery/pkg/api/errors"
17 "sigs.k8s.io/controller-runtime/pkg/client"
20 // Define the collectdPlugin finalizer for handling deletion
22 defaultWatchLabel = "app=collectd"
23 CollectdFinalizer = "finalizer.collectd.onap.org"
25 // WatchLabelsEnvVar is the constant for env variable WATCH_LABELS
26 // which is the labels where the watch activity happens.
27 // this value is empty if the operator is running with clusterScope.
28 WatchLabelsEnvVar = "WATCH_LABELS"
33 // ResourceMap to hold objects to update/reload
34 type ResourceMap struct {
35 ConfigMap *corev1.ConfigMap
36 DaemonSet *appsv1.DaemonSet
37 CollectdPlugins *[]onapv1alpha1.CollectdPlugin
40 // ComputeSHA256 returns hash of data as string
41 func ComputeSHA256(data []byte) string {
42 hash := sha256.Sum256(data)
43 return fmt.Sprintf("%x", hash)
46 // Contains checks if a string is contained in a list of strings
47 func Contains(list []string, s string) bool {
48 for _, v := range list {
56 // Remove removes a string from a list of string
57 func Remove(list []string, s string) []string {
58 for i, v := range list {
60 list = append(list[:i], list[i+1:]...)
66 // GetWatchLabels returns the labels the operator should be watching for changes
67 func GetWatchLabels() (string, error) {
68 labelSelector, found := os.LookupEnv(WatchLabelsEnvVar)
70 return defaultWatchLabel, fmt.Errorf("%s must be set", WatchLabelsEnvVar)
72 return labelSelector, nil
75 // GetCollectdPluginList returns the list of CollectdPlugin instances in the namespace ns
76 func GetCollectdPluginList(rc client.Client, ns string) (*onapv1alpha1.CollectdPluginList, error) {
77 // Get all collectd plugins in the current namespace to rebuild conf.
78 collectdPlugins := &onapv1alpha1.CollectdPluginList{}
79 cpOpts := &client.ListOptions{}
80 cpOpts.InNamespace(ns)
81 err := rc.List(context.TODO(), cpOpts, collectdPlugins)
85 return collectdPlugins, nil
88 // GetConfigMap returns the GetConfigMap in the namespace ns
89 func GetConfigMap(rc client.Client, reqLogger logr.Logger, ns string) (*corev1.ConfigMap, error) {
93 reqLogger.Info("Get ConfigMap for collectd.conf")
94 // Get all collectd plugins in the current namespace to rebuild conf.
95 cmList := &corev1.ConfigMapList{}
96 opts := &client.ListOptions{}
97 // Select ConfigMaps with label
98 labelSelector, err := GetWatchLabels()
100 reqLogger.Error(err, "Failed to get watch labels, continuing with default label")
102 opts.SetLabelSelector(labelSelector)
105 err = rc.List(context.TODO(), opts, cmList)
110 if cmList.Items == nil || len(cmList.Items) == 0 {
111 return nil, errors.NewNotFound(corev1.Resource("configmap"), "ConfigMap")
114 cm := &cmList.Items[0]
118 // GetCollectdGlobal returns the CollectdGlobal instance in the namespace ns
119 func GetCollectdGlobal(rc client.Client, ns string) (*onapv1alpha1.CollectdGlobal, error) {
120 // Get the CollectdGlobal instance in current namespace to rebuild conf.
121 cgList := &onapv1alpha1.CollectdGlobalList{}
122 cpOpts := &client.ListOptions{}
123 cpOpts.InNamespace(ns)
124 err := rc.List(context.TODO(), cpOpts, cgList)
128 if cgList.Items == nil || len(cgList.Items) == 0 {
131 collectdGlobals := &cgList.Items[0]
132 return collectdGlobals, nil
135 // GetPodList returns the list of pods in the namespace ns
136 func GetPodList(rc client.Client, ns string) ([]string, error) {
138 podList := &corev1.PodList{}
139 opts := &client.ListOptions{}
140 // Select ConfigMaps with label
141 labelSelector, _ := GetWatchLabels()
142 opts.SetLabelSelector(labelSelector)
144 err := rc.List(context.TODO(), opts, podList)
149 if podList.Items == nil || len(podList.Items) == 0 {
153 for _, pod := range podList.Items {
154 pods = append(pods, pod.Name)
159 // RebuildCollectdConf Get all collectd plugins and reconstruct, compute Hash and check for changes
160 func RebuildCollectdConf(rc client.Client, ns string, isDelete bool, delPlugin string) (string, error) {
161 var collectdConf, collectdGlobalConf string
162 // Get the collectd global in the current namespace to rebuild conf.
163 cg, err := GetCollectdGlobal(rc, ns)
169 collectdGlobalConf += cg.Spec.GlobalOptions + "\n"
172 // Get all collectd plugins in the current namespace to rebuild conf.
173 cp, err := GetCollectdPluginList(rc, ns)
178 loadPlugin := make(map[string]string)
179 if *cpList != nil && len(*cpList) > 0 {
180 for _, cp := range *cpList {
181 // using CollectdPlugin to set global options. If CollectdGlobal CRD is defined do not check for this
182 if cp.Spec.PluginName == "global" {
184 collectdGlobalConf += cp.Spec.PluginConf + "\n"
187 loadPlugin[cp.Spec.PluginName] = cp.Spec.PluginConf
193 delete(loadPlugin, delPlugin)
195 collectdConf += collectdGlobalConf
198 for cpName, cpConf := range loadPlugin {
199 collectdConf += "LoadPlugin" + " " + cpName + "\n"
200 collectdConf += cpConf + "\n"
203 collectdConf += "#Last line (collectd requires ā\\nā at the last line)\n"
205 return collectdConf, nil