Skip to content

Commit 3314a75

Browse files
authored
Merge pull request #1629 from splunk/CSPL-4281-add-error-handling-for-cm-multisite-check
CSPL-4281 Add fallback error handling for cluster manager multisite configuration
2 parents b5d8704 + de6b4ea commit 3314a75

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

pkg/splunk/enterprise/clustermanager.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func ApplyClusterManager(ctx context.Context, client splcommon.ControllerClient,
136136
// check if deletion has been requested
137137
if cr.ObjectMeta.DeletionTimestamp != nil {
138138
if cr.Spec.MonitoringConsoleRef.Name != "" {
139-
extraEnv, _ := VerifyCMisMultisiteCall(ctx, cr, namespaceScopedSecret)
139+
extraEnv, _ := GetCMMultisiteEnvVarsCall(ctx, cr, namespaceScopedSecret)
140140
_, err = ApplyMonitoringConsoleEnvConfigMap(ctx, client, cr.GetNamespace(), cr.GetName(), cr.Spec.MonitoringConsoleRef.Name, extraEnv, false)
141141
if err != nil {
142142
return result, err
@@ -187,7 +187,7 @@ func ApplyClusterManager(ctx context.Context, client splcommon.ControllerClient,
187187
}
188188

189189
//make changes to respective mc configmap when changing/removing mcRef from spec
190-
extraEnv, err := VerifyCMisMultisiteCall(ctx, cr, namespaceScopedSecret)
190+
extraEnv, _ := GetCMMultisiteEnvVarsCall(ctx, cr, namespaceScopedSecret)
191191
err = validateMonitoringConsoleRef(ctx, client, statefulSet, extraEnv)
192192
if err != nil {
193193
return result, err
@@ -446,23 +446,30 @@ func getClusterManagerList(ctx context.Context, c splcommon.ControllerClient, cr
446446
return numOfObjects, nil
447447
}
448448

449-
// VerifyCMisMultisite checks if its a multisite used also in mock
450-
var VerifyCMisMultisiteCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
451-
var err error
449+
// GetCMMultisiteEnvVarsCall checks if cluster is multisite and returns appropriate environment variables
450+
// If it fails to connect to the cluster manager (e.g., pod not ready yet), it returns basic env vars as fallback
451+
// This function is used also in mock tests
452+
var GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
452453
reqLogger := log.FromContext(ctx)
453454
scopedLog := reqLogger.WithName("Verify if Multisite Indexer Cluster").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())
455+
456+
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
457+
454458
mgr := clusterManagerPodManager{log: scopedLog, cr: cr, secrets: namespaceScopedSecret, newSplunkClient: splclient.NewSplunkClient}
455459
cm := mgr.getClusterManagerClient(cr)
456460
clusterInfo, err := cm.GetClusterInfo(false)
457461
if err != nil {
458-
return nil, err
462+
scopedLog.Error(err, "Failed to get cluster info from cluster manager pod, using basic environment variables")
463+
return extraEnv, err
459464
}
460-
multiSite := clusterInfo.MultiSite
461-
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
462-
if multiSite == "true" {
463-
extraEnv = append(extraEnv, corev1.EnvVar{Name: "SPLUNK_SITE", Value: "site0"}, corev1.EnvVar{Name: "SPLUNK_MULTISITE_MASTER", Value: GetSplunkServiceName(SplunkClusterManager, cr.GetName(), false)})
465+
466+
if clusterInfo.MultiSite == "true" {
467+
extraEnv = append(extraEnv,
468+
corev1.EnvVar{Name: "SPLUNK_SITE", Value: "site0"},
469+
corev1.EnvVar{Name: "SPLUNK_MULTISITE_MASTER", Value: GetSplunkServiceName(SplunkClusterManager, cr.GetName(), false)})
464470
}
465-
return extraEnv, err
471+
472+
return extraEnv, nil
466473
}
467474

468475
// changeClusterManagerAnnotations updates the splunk/image-tag field of the ClusterManager annotations to trigger the reconcile loop

pkg/splunk/enterprise/clustermanager_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func TestApplyClusterManager(t *testing.T) {
5555
return nil
5656
}
5757

58+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
59+
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
60+
return extraEnv, nil
61+
}
62+
5863
ctx := context.TODO()
5964
funcCalls := []spltest.MockFuncCall{
6065
{MetaName: "*v1.Secret-test-splunk-test-secret"},
@@ -542,6 +547,12 @@ func TestClusterManagerSpecNotCreatedWithoutGeneralTerms(t *testing.T) {
542547

543548
func TestApplyClusterManagerWithSmartstore(t *testing.T) {
544549
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
550+
551+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
552+
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
553+
return extraEnv, nil
554+
}
555+
545556
ctx := context.TODO()
546557
funcCalls := []spltest.MockFuncCall{
547558
{MetaName: "*v1.Secret-test-splunk-test-secret"},
@@ -874,6 +885,12 @@ func TestPushManagerAppsBundle(t *testing.T) {
874885
func TestAppFrameworkApplyClusterManagerShouldNotFail(t *testing.T) {
875886
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
876887
initGlobalResourceTracker()
888+
889+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
890+
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
891+
return extraEnv, nil
892+
}
893+
877894
ctx := context.TODO()
878895
cm := enterpriseApi.ClusterManager{
879896
ObjectMeta: metav1.ObjectMeta{
@@ -948,6 +965,12 @@ func TestAppFrameworkApplyClusterManagerShouldNotFail(t *testing.T) {
948965

949966
func TestApplyClusterManagerDeletion(t *testing.T) {
950967
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
968+
969+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
970+
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
971+
return extraEnv, nil
972+
}
973+
951974
ctx := context.TODO()
952975
cm := enterpriseApi.ClusterManager{
953976
ObjectMeta: metav1.ObjectMeta{
@@ -1471,6 +1494,12 @@ func TestCheckIfsmartstoreConfigMapUpdatedToPod(t *testing.T) {
14711494

14721495
func TestIsClusterManagerReadyForUpgrade(t *testing.T) {
14731496
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
1497+
1498+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
1499+
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
1500+
return extraEnv, nil
1501+
}
1502+
14741503
ctx := context.TODO()
14751504

14761505
sch := pkgruntime.NewScheme()
@@ -1678,7 +1707,7 @@ func TestChangeClusterManagerAnnotations(t *testing.T) {
16781707
debug.PrintStack()
16791708
}
16801709

1681-
VerifyCMisMultisiteCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
1710+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
16821711
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
16831712
return extraEnv, err
16841713
}

pkg/splunk/enterprise/upgrade_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func TestUpgradePathValidation(t *testing.T) {
417417
t.Errorf("shc is not in ready state")
418418
}
419419

420-
VerifyCMisMultisiteCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
420+
GetCMMultisiteEnvVarsCall = func(ctx context.Context, cr *enterpriseApi.ClusterManager, namespaceScopedSecret *corev1.Secret) ([]corev1.EnvVar, error) {
421421
extraEnv := getClusterManagerExtraEnv(cr, &cr.Spec.CommonSplunkSpec)
422422
return extraEnv, err
423423
}

0 commit comments

Comments
 (0)