From 478244ad053623129cb46b0b9670cf95152b1532 Mon Sep 17 00:00:00 2001 From: Arjun Kondur Date: Thu, 19 Dec 2024 10:07:14 -0600 Subject: [PATCH] Add UT and return error if not deployer sts --- pkg/splunk/enterprise/searchheadcluster.go | 15 ++++++- .../enterprise/searchheadcluster_test.go | 42 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/pkg/splunk/enterprise/searchheadcluster.go b/pkg/splunk/enterprise/searchheadcluster.go index 8c31a3d07..f2cb9d8c3 100644 --- a/pkg/splunk/enterprise/searchheadcluster.go +++ b/pkg/splunk/enterprise/searchheadcluster.go @@ -17,6 +17,7 @@ package enterprise import ( "context" + "errors" "fmt" "reflect" "strings" @@ -653,7 +654,12 @@ func getSearchHeadStatefulSet(ctx context.Context, client splcommon.ControllerCl // CSPL-3652 Configure deployer resources if configured // Use default otherwise -func setDeployerResources(cr *enterpriseApi.SearchHeadCluster, podTemplate *corev1.PodTemplateSpec) { +// Make sure to set the resources ONLY for the deployer +func setDeployerResources(cr *enterpriseApi.SearchHeadCluster, podTemplate *corev1.PodTemplateSpec) error { + // Break out if this is not a deployer + if !strings.Contains("deployer", podTemplate.Labels["app.kubernetes.io/name"]) { + return errors.New("not a deployer, skipping setting resources") + } depRes := cr.Spec.DeployerResourceSpec for i := range podTemplate.Spec.Containers { if len(depRes.Requests) != 0 { @@ -664,6 +670,8 @@ func setDeployerResources(cr *enterpriseApi.SearchHeadCluster, podTemplate *core podTemplate.Spec.Containers[i].Resources.Limits = cr.Spec.DeployerResourceSpec.Limits } } + + return nil } // getDeployerStatefulSet returns a Kubernetes StatefulSet object for a Splunk Enterprise license manager. @@ -674,7 +682,10 @@ func getDeployerStatefulSet(ctx context.Context, client splcommon.ControllerClie } // CSPL-3562 - Set deployer resources if configured - setDeployerResources(cr, &ss.Spec.Template) + err = setDeployerResources(cr, &ss.Spec.Template) + if err != nil { + return ss, err + } // Setup App framework staging volume for apps setupAppsStagingVolume(ctx, client, cr, &ss.Spec.Template, &cr.Spec.AppFrameworkConfig) diff --git a/pkg/splunk/enterprise/searchheadcluster_test.go b/pkg/splunk/enterprise/searchheadcluster_test.go index 0ce729aa3..d63f04b58 100644 --- a/pkg/splunk/enterprise/searchheadcluster_test.go +++ b/pkg/splunk/enterprise/searchheadcluster_test.go @@ -22,6 +22,7 @@ import ( "net/http" "os" "path/filepath" + "reflect" "runtime/debug" "strings" "testing" @@ -31,6 +32,7 @@ import ( appsv1 "k8s.io/api/apps/v1" 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" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -1892,3 +1894,43 @@ func TestSearchHeadClusterWithReadyState(t *testing.T) { t.Errorf("Unexpected error while running reconciliation for search head cluster with app framework. Error=%v", err) } } + +func TestSetDeployerResources(t *testing.T) { + ctx := context.TODO() + client := spltest.NewMockClient() + depResSpec := corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("4"), + corev1.ResourceMemory: resource.MustParse("14Gi"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("2"), + corev1.ResourceMemory: resource.MustParse("7Gi"), + }, + } + + shc := enterpriseApi.SearchHeadCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stack1", + Namespace: "test", + }, + Spec: enterpriseApi.SearchHeadClusterSpec{ + DeployerResourceSpec: depResSpec, + }, + } + + // Get deployer STS and set resources + depSts, err := getSplunkStatefulSet(ctx, client, &shc, &shc.Spec.CommonSplunkSpec, SplunkDeployer, 1, getSearchHeadExtraEnv(&shc, shc.Spec.Replicas)) + if err != nil { + t.Errorf("Failed to get deployer statefulset due to error=%s", err) + } + setDeployerResources(&shc, &depSts.Spec.Template) + if !reflect.DeepEqual(depResSpec.Limits, depSts.Spec.Template.Spec.Containers[0].Resources.Limits) { + t.Errorf("Failed to set deployer resources properly, limits are off") + } + + // Verify deployer resources are set properly + if !reflect.DeepEqual(depResSpec.Requests, depSts.Spec.Template.Spec.Containers[0].Resources.Requests) { + t.Errorf("Failed to set deployer resources properly, requests are off") + } +}