Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions pkg/splunk/enterprise/clustermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion pkg/splunk/enterprise/clustermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/splunk/enterprise/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading