8 onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
10 corev1 "k8s.io/api/core/v1"
11 extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
12 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
15 var log = logf.Log.WithName("dsutils")
18 collectdContainerName = "collectd"
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
26 // RemoveTypesDB - removes TypesDB volumes and volume mounts from collectd pods.
27 func RemoveTypesDB(ds *extensionsv1beta1.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:]...)
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:]...)
50 // UpsertTypesDB - Insert/Update TypesDB volumes and volume mounts to collectd pods.
51 func UpsertTypesDB(ds *extensionsv1beta1.DaemonSet, cm *corev1.ConfigMap, cr *onapv1alpha1.CollectdGlobal) {
52 typesVM := findMountInfo(cr)
53 if *typesVM == nil || len(*typesVM) == 0 {
56 typesDBVolume := &corev1.ConfigMapVolumeSource{
57 LocalObjectReference: corev1.LocalObjectReference{Name: cm.Name},
59 vols := &ds.Spec.Template.Spec.Volumes
61 for i, vol := range *vols {
63 if vol.Name == typesDB {
64 (*vols)[i].ConfigMap = typesDBVolume
71 *vols = append(*vols, corev1.Volume{
73 VolumeSource: corev1.VolumeSource{
74 ConfigMap: typesDBVolume,
79 containers := &ds.Spec.Template.Spec.Containers
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:]...)
92 *vms = append(*vms, *typesVM...)
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)
115 log.V(1).Info(":::::Exiting findMountInfo:::::")