Collectd Operator Unit Tests
[demo.git] / vnfs / DAaaS / microservices / collectd-operator / pkg / controller / utils / dsutils.go
1 /*
2 Copyright 2019 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package utils
15
16 import (
17         "path/filepath"
18         "strconv"
19         "strings"
20
21         onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
22
23         appsv1 "k8s.io/api/apps/v1"
24         corev1 "k8s.io/api/core/v1"
25         logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
26 )
27
28 var log = logf.Log.WithName("dsutils")
29
30 const (
31         collectdContainerName = "collectd"
32
33         // canonical label for the volume created for TypesDB
34         // reason - a DNS-1123 label must consist of lower case alphanumeric characters
35         //                      or '-', and must start and end with an alphanumeric character
36         typesDB = "types-db"
37 )
38
39 // RemoveTypesDB - removes TypesDB volumes and volume mounts from collectd pods.
40 func RemoveTypesDB(ds *appsv1.DaemonSet) {
41         vols := &ds.Spec.Template.Spec.Volumes
42         for i := 0; i < len(*vols); i++ {
43                 if (*vols)[i].Name == typesDB {
44                         *vols = append((*vols)[:i], (*vols)[i+1:]...)
45                         i--
46                 }
47         }
48
49         containers := &ds.Spec.Template.Spec.Containers
50         for j, container := range *containers {
51                 if container.Name == collectdContainerName {
52                         vms := &(*containers)[j].VolumeMounts
53                         for i := 0; i < len(*vms); i++ {
54                                 if (*vms)[i].Name == typesDB {
55                                         *vms = append((*vms)[:i], (*vms)[i+1:]...)
56                                         i--
57                                 }
58                         }
59                 }
60         }
61 }
62
63 // UpsertTypesDB - Insert/Update TypesDB volumes and volume mounts to collectd pods.
64 func UpsertTypesDB(ds *appsv1.DaemonSet, cm *corev1.ConfigMap, cr *onapv1alpha1.CollectdGlobal) {
65         typesVM := findMountInfo(cr)
66         if *typesVM == nil || len(*typesVM) == 0 {
67                 return
68         }
69         typesDBVolume := &corev1.ConfigMapVolumeSource{
70                 LocalObjectReference: corev1.LocalObjectReference{Name: cm.Name},
71         }
72         vols := &ds.Spec.Template.Spec.Volumes
73         var hasUpdated bool
74         for i, vol := range *vols {
75                 // Update case
76                 if vol.Name == typesDB {
77                         (*vols)[i].ConfigMap = typesDBVolume
78                         hasUpdated = true
79                 }
80         }
81
82         if !hasUpdated {
83                 //Insert case
84                 *vols = append(*vols, corev1.Volume{
85                         Name: typesDB,
86                         VolumeSource: corev1.VolumeSource{
87                                 ConfigMap: typesDBVolume,
88                         },
89                 })
90         }
91
92         containers := &ds.Spec.Template.Spec.Containers
93
94         for j, container := range *containers {
95                 if container.Name == collectdContainerName {
96                         vms := &(*containers)[j].VolumeMounts
97                         for i := 0; i < len(*vms); i++ {
98                                 // Update case (Equivalent to remove and add)
99                                 if (*vms)[i].Name == typesDB {
100                                         *vms = append((*vms)[:i], (*vms)[i+1:]...)
101                                         i--
102                                 }
103                         }
104
105                         *vms = append(*vms, *typesVM...)
106                 }
107         }
108 }
109
110 func findMountInfo(cr *onapv1alpha1.CollectdGlobal) *[]corev1.VolumeMount {
111         log.V(1).Info(":::::Entering findMountInfo:::::")
112         var typesVM []corev1.VolumeMount
113         globalOpts := strings.Split(cr.Spec.GlobalOptions, "\n")
114         log.V(1).Info(":::::findMountInfo:::::", "GlobalOptions", globalOpts)
115         for i, globalOpt := range globalOpts {
116                 log.V(1).Info(":::::For Loop:::::", "Item No:", i, "LineEntry:", globalOpt)
117                 s := strings.Fields(globalOpt)
118                 log.V(1).Info(":::::s:::::", "s:", s)
119                 if s != nil && len(s) != 0 && s[0] == "TypesDB" {
120                         path, _ := strconv.Unquote(s[1])
121                         _, file := filepath.Split(path)
122                         log.V(1).Info(":::::file:::::", "s[1]:", path, "file:", file)
123                         vm := corev1.VolumeMount{Name: typesDB, MountPath: path, SubPath: file}
124                         typesVM = append(typesVM, vm)
125                         log.V(1).Info(":::::TypesVM:::::", "TypesVM:", typesVM)
126                 }
127         }
128         log.V(1).Info(":::::Exiting findMountInfo:::::")
129         return &typesVM
130 }