Skip to content

Commit

Permalink
Add UT and return error if not deployer sts
Browse files Browse the repository at this point in the history
  • Loading branch information
akondur committed Dec 19, 2024
1 parent 502397d commit 478244a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/splunk/enterprise/searchheadcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package enterprise

import (
"context"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions pkg/splunk/enterprise/searchheadcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"os"
"path/filepath"
"reflect"
"runtime/debug"
"strings"
"testing"
Expand All @@ -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"
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 478244a

Please sign in to comment.