diff --git a/pkg/splunk/enterprise/clustermanager.go b/pkg/splunk/enterprise/clustermanager.go index d58654ffb..68be1a9db 100644 --- a/pkg/splunk/enterprise/clustermanager.go +++ b/pkg/splunk/enterprise/clustermanager.go @@ -136,7 +136,7 @@ func ApplyClusterManager(ctx context.Context, client splcommon.ControllerClient, // check if deletion has been requested if cr.ObjectMeta.DeletionTimestamp != nil { if cr.Spec.MonitoringConsoleRef.Name != "" { - extraEnv, _ := VerifyCMisMultisiteCall(ctx, cr, namespaceScopedSecret) + extraEnv, _ := GetCMMultisiteEnvVarsCall(ctx, cr, namespaceScopedSecret) _, err = ApplyMonitoringConsoleEnvConfigMap(ctx, client, cr.GetNamespace(), cr.GetName(), cr.Spec.MonitoringConsoleRef.Name, extraEnv, false) if err != nil { return result, err @@ -187,7 +187,7 @@ func ApplyClusterManager(ctx context.Context, client splcommon.ControllerClient, } //make changes to respective mc configmap when changing/removing mcRef from spec - extraEnv, err := VerifyCMisMultisiteCall(ctx, cr, namespaceScopedSecret) + extraEnv, _ := GetCMMultisiteEnvVarsCall(ctx, cr, namespaceScopedSecret) err = validateMonitoringConsoleRef(ctx, client, statefulSet, extraEnv) if err != nil { return result, err @@ -445,23 +445,30 @@ func getClusterManagerList(ctx context.Context, c splcommon.ControllerClient, cr return numOfObjects, nil } -// VerifyCMisMultisite checks if its a multisite used also in mock -var VerifyCMisMultisiteCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { - var err error +// GetCMMultisiteEnvVarsCall checks if cluster is multisite and returns appropriate environment variables +// If it fails to connect to the cluster manager (e.g., pod not ready yet), it returns basic env vars as fallback +// This function is used also in mock tests +var GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { reqLogger := log.FromContext(ctx) scopedLog := reqLogger.WithName("Verify if Multisite Indexer Cluster").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace()) + + extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) + mgr := clusterManagerPodManager{log: scopedLog, cr: cr, secrets: namespaceScopedSecret, newSplunkClient: splclient.NewSplunkClient} cm := mgr.getClusterManagerClient(cr) clusterInfo, err := cm.GetClusterInfo(false) if err != nil { - return nil, err + scopedLog.Error(err, "Failed to get cluster info from cluster manager pod, using basic environment variables") + return extraEnv, err } - multiSite := clusterInfo.MultiSite - extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) - if multiSite == "true" { - extraEnv = append(extraEnv, corev1.EnvVar{Name: "SPLUNK_SITE", Value: "site0"}, corev1.EnvVar{Name: "SPLUNK_MULTISITE_MASTER", Value: GetSplunkServiceName(SplunkClusterManager, cr.GetName(), false)}) + + if clusterInfo.MultiSite == "true" { + extraEnv = append(extraEnv, + corev1.EnvVar{Name: "SPLUNK_SITE", Value: "site0"}, + corev1.EnvVar{Name: "SPLUNK_MULTISITE_MASTER", Value: GetSplunkServiceName(SplunkClusterManager, cr.GetName(), false)}) } - return extraEnv, err + + return extraEnv, nil } // changeClusterManagerAnnotations updates the splunk/image-tag field of the ClusterManager annotations to trigger the reconcile loop diff --git a/pkg/splunk/enterprise/clustermanager_test.go b/pkg/splunk/enterprise/clustermanager_test.go index 92eb22e7e..d1360ea8e 100644 --- a/pkg/splunk/enterprise/clustermanager_test.go +++ b/pkg/splunk/enterprise/clustermanager_test.go @@ -55,6 +55,11 @@ func TestApplyClusterManager(t *testing.T) { return nil } + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) + return extraEnv, nil + } + ctx := context.TODO() funcCalls := []spltest.MockFuncCall{ {MetaName: "*v1.Secret-test-splunk-test-secret"}, @@ -542,6 +547,12 @@ func TestClusterManagerSpecNotCreatedWithoutGeneralTerms(t *testing.T) { func TestApplyClusterManagerWithSmartstore(t *testing.T) { os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com") + + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) + return extraEnv, nil + } + ctx := context.TODO() funcCalls := []spltest.MockFuncCall{ {MetaName: "*v1.Secret-test-splunk-test-secret"}, @@ -874,6 +885,12 @@ func TestPushManagerAppsBundle(t *testing.T) { func TestAppFrameworkApplyClusterManagerShouldNotFail(t *testing.T) { os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com") initGlobalResourceTracker() + + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) + return extraEnv, nil + } + ctx := context.TODO() cm := enterpriseApi.ClusterManager{ ObjectMeta: metav1.ObjectMeta{ @@ -948,6 +965,12 @@ func TestAppFrameworkApplyClusterManagerShouldNotFail(t *testing.T) { func TestApplyClusterManagerDeletion(t *testing.T) { os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com") + + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) + return extraEnv, nil + } + ctx := context.TODO() cm := enterpriseApi.ClusterManager{ ObjectMeta: metav1.ObjectMeta{ @@ -1471,6 +1494,12 @@ func TestCheckIfsmartstoreConfigMapUpdatedToPod(t *testing.T) { func TestIsClusterManagerReadyForUpgrade(t *testing.T) { os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com") + + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) + return extraEnv, nil + } + ctx := context.TODO() sch := pkgruntime.NewScheme() @@ -1678,7 +1707,7 @@ func TestChangeClusterManagerAnnotations(t *testing.T) { debug.PrintStack() } - VerifyCMisMultisiteCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) return extraEnv, err } diff --git a/pkg/splunk/enterprise/upgrade_test.go b/pkg/splunk/enterprise/upgrade_test.go index 00adac306..b501527fd 100644 --- a/pkg/splunk/enterprise/upgrade_test.go +++ b/pkg/splunk/enterprise/upgrade_test.go @@ -417,7 +417,7 @@ func TestUpgradePathValidation(t *testing.T) { t.Errorf("shc is not in ready state") } - VerifyCMisMultisiteCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { + GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) { extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec) return extraEnv, err }