Skip to content

Commit

Permalink
adding cr specific configmap
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy <vivekr@splunk.com>
  • Loading branch information
vivekr-splunk committed Oct 28, 2024
1 parent d6ccab9 commit 51a47c4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/splunk/enterprise/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -229,10 +230,61 @@ func ApplySplunkConfig(ctx context.Context, client splcommon.ControllerClient, c
return nil, err
}
}
err = ReconcileCRSpecificConfigMap(ctx, client, cr)
if err != nil {
return nil, err
}

return namespaceScopedSecret, nil
}

// ReconcileCRSpecificConfigMap reconciles CR Specific config map exists and contains the ManualUpdate field set to "on"
func ReconcileCRSpecificConfigMap(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject) error {
reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("ReconcileCRSpecificConfigMap").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())

configMapName:= fmt.Sprintf("splunk-config-%s", cr.GetName())
namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: configMapName}

configMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
if err != nil {
if k8serrors.IsNotFound(err) {
// Create a new config map if it doesn't exist
configMap = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: cr.GetNamespace(),
},
Data: map[string]string{
"ManualUpdate": "on",
},
}
err = client.Create(ctx, configMap)
if err != nil {
scopedLog.Error(err, "Failed to create config map")
return err
}
scopedLog.Info("Created new config map with ManualUpdate set to 'on'")
return nil
}
scopedLog.Error(err, "Failed to get config map")
return err
}

// Check if the ManualUpdate field exists
if _, exists := configMap.Data["ManualUpdate"]; !exists {
configMap.Data["ManualUpdate"] = "on"
err = client.Update(ctx, configMap)
if err != nil {
scopedLog.Error(err, "Failed to update config map with ManualUpdate field")
return err
}
scopedLog.Info("Updated config map with ManualUpdate set to 'on'")
}

return nil
}

// getClusterMasterExtraEnv returns extra environment variables used by indexer clusters
func getClusterMasterExtraEnv(cr splcommon.MetaObject, spec *enterpriseApi.CommonSplunkSpec) []corev1.EnvVar {
return []corev1.EnvVar{
Expand Down

0 comments on commit 51a47c4

Please sign in to comment.