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.
21 onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
23 appsv1 "k8s.io/api/apps/v1"
24 corev1 "k8s.io/api/core/v1"
25 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
28 var log = logf.Log.WithName("dsutils")
31 collectdContainerName = "collectd"
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
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:]...)
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:]...)
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 {
69 typesDBVolume := &corev1.ConfigMapVolumeSource{
70 LocalObjectReference: corev1.LocalObjectReference{Name: cm.Name},
72 vols := &ds.Spec.Template.Spec.Volumes
74 for i, vol := range *vols {
76 if vol.Name == typesDB {
77 (*vols)[i].ConfigMap = typesDBVolume
84 *vols = append(*vols, corev1.Volume{
86 VolumeSource: corev1.VolumeSource{
87 ConfigMap: typesDBVolume,
92 containers := &ds.Spec.Template.Spec.Containers
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:]...)
105 *vms = append(*vms, *typesVM...)
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)
128 log.V(1).Info(":::::Exiting findMountInfo:::::")