aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/DAaaS/microservices/collectd-operator/pkg/controller/utils/dsutils.go
blob: 1892b5256b810923adfc45ccff6bd1347803d92c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright 2019 Intel Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
	"path/filepath"
	"strconv"
	"strings"

	onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"

	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

var log = logf.Log.WithName("dsutils")

const (
	collectdContainerName = "collectd"

	// canonical label for the volume created for TypesDB
	// reason - a DNS-1123 label must consist of lower case alphanumeric characters
	//			or '-', and must start and end with an alphanumeric character
	typesDB = "types-db"
)

// RemoveTypesDB - removes TypesDB volumes and volume mounts from collectd pods.
func RemoveTypesDB(ds *appsv1.DaemonSet) {
	vols := &ds.Spec.Template.Spec.Volumes
	for i := 0; i < len(*vols); i++ {
		if (*vols)[i].Name == typesDB {
			*vols = append((*vols)[:i], (*vols)[i+1:]...)
			i--
		}
	}

	containers := &ds.Spec.Template.Spec.Containers
	for j, container := range *containers {
		if container.Name == collectdContainerName {
			vms := &(*containers)[j].VolumeMounts
			for i := 0; i < len(*vms); i++ {
				if (*vms)[i].Name == typesDB {
					*vms = append((*vms)[:i], (*vms)[i+1:]...)
					i--
				}
			}
		}
	}
}

// UpsertTypesDB - Insert/Update TypesDB volumes and volume mounts to collectd pods.
func UpsertTypesDB(ds *appsv1.DaemonSet, cm *corev1.ConfigMap, cr *onapv1alpha1.CollectdGlobal) {
	typesVM := findMountInfo(cr)
	if *typesVM == nil || len(*typesVM) == 0 {
		return
	}
	typesDBVolume := &corev1.ConfigMapVolumeSource{
		LocalObjectReference: corev1.LocalObjectReference{Name: cm.Name},
	}
	vols := &ds.Spec.Template.Spec.Volumes
	var hasUpdated bool
	for i, vol := range *vols {
		// Update case
		if vol.Name == typesDB {
			(*vols)[i].ConfigMap = typesDBVolume
			hasUpdated = true
		}
	}

	if !hasUpdated {
		//Insert case
		*vols = append(*vols, corev1.Volume{
			Name: typesDB,
			VolumeSource: corev1.VolumeSource{
				ConfigMap: typesDBVolume,
			},
		})
	}

	containers := &ds.Spec.Template.Spec.Containers

	for j, container := range *containers {
		if container.Name == collectdContainerName {
			vms := &(*containers)[j].VolumeMounts
			for i := 0; i < len(*vms); i++ {
				// Update case (Equivalent to remove and add)
				if (*vms)[i].Name == typesDB {
					*vms = append((*vms)[:i], (*vms)[i+1:]...)
					i--
				}
			}

			*vms = append(*vms, *typesVM...)
		}
	}
}

func findMountInfo(cr *onapv1alpha1.CollectdGlobal) *[]corev1.VolumeMount {
	log.V(1).Info(":::::Entering findMountInfo:::::")
	var typesVM []corev1.VolumeMount
	globalOpts := strings.Split(cr.Spec.GlobalOptions, "\n")
	log.V(1).Info(":::::findMountInfo:::::", "GlobalOptions", globalOpts)
	for i, globalOpt := range globalOpts {
		log.V(1).Info(":::::For Loop:::::", "Item No:", i, "LineEntry:", globalOpt)
		s := strings.Fields(globalOpt)
		log.V(1).Info(":::::s:::::", "s:", s)
		if s != nil && len(s) != 0 && s[0] == "TypesDB" {
			path, _ := strconv.Unquote(s[1])
			_, file := filepath.Split(path)
			log.V(1).Info(":::::file:::::", "s[1]:", path, "file:", file)
			vm := corev1.VolumeMount{Name: typesDB, MountPath: path, SubPath: file}
			typesVM = append(typesVM, vm)
			log.V(1).Info(":::::TypesVM:::::", "TypesVM:", typesVM)
		}
	}
	log.V(1).Info(":::::Exiting findMountInfo:::::")
	return &typesVM
}