Fix issue with concurrent CR creation
[demo.git] / vnfs / DAaaS / microservices / collectd-operator / pkg / controller / utils / dsutils.go
1 package utils
2
3 import (
4         "path/filepath"
5         "strconv"
6         "strings"
7
8         onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
9
10         appsv1 "k8s.io/api/apps/v1"
11         corev1 "k8s.io/api/core/v1"
12         logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
13 )
14
15 var log = logf.Log.WithName("dsutils")
16
17 const (
18         collectdContainerName = "collectd"
19
20         // canonical label for the volume created for TypesDB
21         // reason - a DNS-1123 label must consist of lower case alphanumeric characters
22         //                      or '-', and must start and end with an alphanumeric character
23         typesDB = "types-db"
24 )
25
26 // RemoveTypesDB - removes TypesDB volumes and volume mounts from collectd pods.
27 func RemoveTypesDB(ds *appsv1.DaemonSet) {
28         vols := &ds.Spec.Template.Spec.Volumes
29         for i := 0; i < len(*vols); i++ {
30                 if (*vols)[i].Name == typesDB {
31                         *vols = append((*vols)[:i], (*vols)[i+1:]...)
32                         i--
33                 }
34         }
35
36         containers := &ds.Spec.Template.Spec.Containers
37         for j, container := range *containers {
38                 if container.Name == collectdContainerName {
39                         vms := &(*containers)[j].VolumeMounts
40                         for i := 0; i < len(*vms); i++ {
41                                 if (*vms)[i].Name == typesDB {
42                                         *vms = append((*vms)[:i], (*vms)[i+1:]...)
43                                         i--
44                                 }
45                         }
46                 }
47         }
48 }
49
50 // UpsertTypesDB - Insert/Update TypesDB volumes and volume mounts to collectd pods.
51 func UpsertTypesDB(ds *appsv1.DaemonSet, cm *corev1.ConfigMap, cr *onapv1alpha1.CollectdGlobal) {
52         typesVM := findMountInfo(cr)
53         if *typesVM == nil || len(*typesVM) == 0 {
54                 return
55         }
56         typesDBVolume := &corev1.ConfigMapVolumeSource{
57                 LocalObjectReference: corev1.LocalObjectReference{Name: cm.Name},
58         }
59         vols := &ds.Spec.Template.Spec.Volumes
60         var hasUpdated bool
61         for i, vol := range *vols {
62                 // Update case
63                 if vol.Name == typesDB {
64                         (*vols)[i].ConfigMap = typesDBVolume
65                         hasUpdated = true
66                 }
67         }
68
69         if !hasUpdated {
70                 //Insert case
71                 *vols = append(*vols, corev1.Volume{
72                         Name: typesDB,
73                         VolumeSource: corev1.VolumeSource{
74                                 ConfigMap: typesDBVolume,
75                         },
76                 })
77         }
78
79         containers := &ds.Spec.Template.Spec.Containers
80
81         for j, container := range *containers {
82                 if container.Name == collectdContainerName {
83                         vms := &(*containers)[j].VolumeMounts
84                         for i := 0; i < len(*vms); i++ {
85                                 // Update case (Equivalent to remove and add)
86                                 if (*vms)[i].Name == typesDB {
87                                         *vms = append((*vms)[:i], (*vms)[i+1:]...)
88                                         i--
89                                 }
90                         }
91
92                         *vms = append(*vms, *typesVM...)
93                 }
94         }
95 }
96
97 func findMountInfo(cr *onapv1alpha1.CollectdGlobal) *[]corev1.VolumeMount {
98         log.V(1).Info(":::::Entering findMountInfo:::::")
99         var typesVM []corev1.VolumeMount
100         globalOpts := strings.Split(cr.Spec.GlobalOptions, "\n")
101         log.V(1).Info(":::::findMountInfo:::::", "GlobalOptions", globalOpts)
102         for i, globalOpt := range globalOpts {
103                 log.V(1).Info(":::::For Loop:::::", "Item No:", i, "LineEntry:", globalOpt)
104                 s := strings.Fields(globalOpt)
105                 log.V(1).Info(":::::s:::::", "s:", s)
106                 if s != nil && len(s) != 0 && s[0] == "TypesDB" {
107                         path, _ := strconv.Unquote(s[1])
108                         _, file := filepath.Split(path)
109                         log.V(1).Info(":::::file:::::", "s[1]:", path, "file:", file)
110                         vm := corev1.VolumeMount{Name: typesDB, MountPath: path, SubPath: file}
111                         typesVM = append(typesVM, vm)
112                         log.V(1).Info(":::::TypesVM:::::", "TypesVM:", typesVM)
113                 }
114         }
115         log.V(1).Info(":::::Exiting findMountInfo:::::")
116         return &typesVM
117 }