diff --git a/pkg/splunk/controller/configmap.go b/pkg/splunk/controller/configmap.go index 311629792..ba7ee6451 100644 --- a/pkg/splunk/controller/configmap.go +++ b/pkg/splunk/controller/configmap.go @@ -49,3 +49,22 @@ func ApplyConfigMap(client splcommon.ControllerClient, configMap *corev1.ConfigM return err } + +// getConfigMap gets the ConfigMap resource in a given namespace +func getConfigMap(client splcommon.ControllerClient, namespacedName types.NamespacedName) (corev1.ConfigMap, error) { + var configMap corev1.ConfigMap + err := client.Get(context.TODO(), namespacedName, &configMap) + if err != nil { + return configMap, err + } + return configMap, nil +} + +// GetConfigMapResourceVersion gets the Resource version of a configMap +func GetConfigMapResourceVersion(client splcommon.ControllerClient, namespacedName types.NamespacedName) (string, error) { + configMap, err := getConfigMap(client, namespacedName) + if err != nil { + return "", err + } + return configMap.ResourceVersion, nil +} diff --git a/pkg/splunk/enterprise/configuration.go b/pkg/splunk/enterprise/configuration.go index c0891824c..1927ec040 100644 --- a/pkg/splunk/enterprise/configuration.go +++ b/pkg/splunk/enterprise/configuration.go @@ -21,10 +21,12 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" enterprisev1 "github.com/splunk/splunk-operator/pkg/apis/enterprise/v1alpha3" splcommon "github.com/splunk/splunk-operator/pkg/splunk/common" + splctrl "github.com/splunk/splunk-operator/pkg/splunk/controller" splutil "github.com/splunk/splunk-operator/pkg/splunk/util" logf "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -477,14 +479,27 @@ func updateSplunkPodTemplateWithConfig(client splcommon.ControllerClient, podTem // add inline defaults to all splunk containers other than MC(where CR spec defaults are not needed) if spec.Defaults != "" && instanceType != SplunkMonitoringConsole { + configMapName := GetSplunkDefaultsName(cr.GetName(), instanceType) addSplunkVolumeToTemplate(podTemplateSpec, "defaults", corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: GetSplunkDefaultsName(cr.GetName(), instanceType), + Name: configMapName, }, DefaultMode: &configMapVolDefaultMode, }, }) + + scopedLog := log.WithName("updateSplunkPodTemplateWithConfig").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace()) + namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: configMapName} + + // We will update the annotation for resource version in the pod template spec + // so that any change in the ConfigMap will lead to recycle of the pod. + configMapResourceVersion, err := splctrl.GetConfigMapResourceVersion(client, namespacedName) + if err == nil { + podTemplateSpec.ObjectMeta.Annotations["defaultConfigRev"] = configMapResourceVersion + } else { + scopedLog.Error(err, "Updation of default configMap annotation failed") + } } // update security context