Skip to content

Commit ef78697

Browse files
committed
CSPL-4022 Fixing tests and adding bus config to ingestor controller
1 parent 9f57abc commit ef78697

File tree

7 files changed

+160
-248
lines changed

7 files changed

+160
-248
lines changed

internal/controller/ingestorcluster_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ func (r *IngestorClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
140140
mgr.GetRESTMapper(),
141141
&enterpriseApi.IngestorCluster{},
142142
)).
143+
Watches(&enterpriseApi.BusConfiguration{},
144+
handler.EnqueueRequestForOwner(
145+
mgr.GetScheme(),
146+
mgr.GetRESTMapper(),
147+
&enterpriseApi.IngestorCluster{},
148+
)).
143149
WithOptions(controller.Options{
144150
MaxConcurrentReconciles: enterpriseApi.TotalWorker,
145151
}).

pkg/splunk/enterprise/busconfiguration.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,11 @@ func validateBusConfigurationSpec(ctx context.Context, c splcommon.ControllerCli
9595
}
9696

9797
func validateBusConfigurationInputs(cr *enterpriseApi.BusConfiguration) error {
98-
if cr.Spec == (enterpriseApi.BusConfigurationSpec{}) {
99-
return errors.New("bus configuration spec cannot be empty")
100-
}
101-
10298
// sqs_smartbus type is supported for now
10399
if cr.Spec.Type != "sqs_smartbus" {
104100
return errors.New("only sqs_smartbus type is supported in bus configuration")
105101
}
106102

107-
if cr.Spec.SQS == (enterpriseApi.SQSSpec{}) {
108-
return errors.New("bus configuration sqs cannot be empty")
109-
}
110-
111103
// Cannot be empty fields check
112104
cannotBeEmptyFields := []string{}
113105
if cr.Spec.SQS.QueueName == "" {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
Copyright 2025.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package enterprise
15+
16+
import (
17+
"context"
18+
"os"
19+
"path/filepath"
20+
"testing"
21+
22+
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
23+
"github.com/stretchr/testify/assert"
24+
appsv1 "k8s.io/api/apps/v1"
25+
corev1 "k8s.io/api/core/v1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29+
)
30+
31+
func init() {
32+
GetReadinessScriptLocation = func() string {
33+
fileLocation, _ := filepath.Abs("../../../" + readinessScriptLocation)
34+
return fileLocation
35+
}
36+
GetLivenessScriptLocation = func() string {
37+
fileLocation, _ := filepath.Abs("../../../" + livenessScriptLocation)
38+
return fileLocation
39+
}
40+
GetStartupScriptLocation = func() string {
41+
fileLocation, _ := filepath.Abs("../../../" + startupScriptLocation)
42+
return fileLocation
43+
}
44+
}
45+
46+
func TestApplyBusConfiguration(t *testing.T) {
47+
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
48+
49+
ctx := context.TODO()
50+
51+
scheme := runtime.NewScheme()
52+
_ = enterpriseApi.AddToScheme(scheme)
53+
_ = corev1.AddToScheme(scheme)
54+
_ = appsv1.AddToScheme(scheme)
55+
c := fake.NewClientBuilder().WithScheme(scheme).Build()
56+
57+
// Object definitions
58+
busConfig := &enterpriseApi.BusConfiguration{
59+
TypeMeta: metav1.TypeMeta{
60+
Kind: "BusConfiguration",
61+
APIVersion: "enterprise.splunk.com/v4",
62+
},
63+
ObjectMeta: metav1.ObjectMeta{
64+
Name: "busConfig",
65+
Namespace: "test",
66+
},
67+
Spec: enterpriseApi.BusConfigurationSpec{
68+
Type: "sqs_smartbus",
69+
SQS: enterpriseApi.SQSSpec{
70+
QueueName: "test-queue",
71+
AuthRegion: "us-west-2",
72+
Endpoint: "https://sqs.us-west-2.amazonaws.com",
73+
LargeMessageStorePath: "s3://ingestion/smartbus-test",
74+
LargeMessageStoreEndpoint: "https://s3.us-west-2.amazonaws.com",
75+
DeadLetterQueueName: "sqs-dlq-test",
76+
},
77+
},
78+
}
79+
c.Create(ctx, busConfig)
80+
81+
// ApplyBusConfiguration
82+
result, err := ApplyBusConfiguration(ctx, c, busConfig)
83+
assert.NoError(t, err)
84+
assert.True(t, result.Requeue)
85+
assert.NotEqual(t, enterpriseApi.PhaseError, busConfig.Status.Phase)
86+
assert.Equal(t, enterpriseApi.PhaseReady, busConfig.Status.Phase)
87+
}
88+
89+
func TestValidateBusConfigurationInputs(t *testing.T) {
90+
busConfig := enterpriseApi.BusConfiguration{
91+
TypeMeta: metav1.TypeMeta{
92+
Kind: "BusConfiguration",
93+
APIVersion: "enterprise.splunk.com/v4",
94+
},
95+
ObjectMeta: metav1.ObjectMeta{
96+
Name: "busConfig",
97+
},
98+
Spec: enterpriseApi.BusConfigurationSpec{
99+
Type: "othertype",
100+
SQS: enterpriseApi.SQSSpec{},
101+
},
102+
}
103+
104+
err := validateBusConfigurationInputs(&busConfig)
105+
assert.NotNil(t, err)
106+
assert.Equal(t, "only sqs_smartbus type is supported in bus configuration", err.Error())
107+
108+
busConfig.Spec.Type = "sqs_smartbus"
109+
110+
err = validateBusConfigurationInputs(&busConfig)
111+
assert.NotNil(t, err)
112+
assert.Equal(t, "bus configuration sqs queueName, authRegion, deadLetterQueueName cannot be empty", err.Error())
113+
114+
busConfig.Spec.SQS.AuthRegion = "us-west-2"
115+
116+
err = validateBusConfigurationInputs(&busConfig)
117+
assert.NotNil(t, err)
118+
assert.Equal(t, "bus configuration sqs queueName, deadLetterQueueName cannot be empty", err.Error())
119+
120+
busConfig.Spec.SQS.QueueName = "test-queue"
121+
busConfig.Spec.SQS.DeadLetterQueueName = "dlq-test"
122+
busConfig.Spec.SQS.AuthRegion = ""
123+
124+
err = validateBusConfigurationInputs(&busConfig)
125+
assert.NotNil(t, err)
126+
assert.Equal(t, "bus configuration sqs authRegion cannot be empty", err.Error())
127+
128+
busConfig.Spec.SQS.AuthRegion = "us-west-2"
129+
130+
err = validateBusConfigurationInputs(&busConfig)
131+
assert.NotNil(t, err)
132+
assert.Equal(t, "bus configuration sqs endpoint, largeMessageStoreEndpoint must start with https://", err.Error())
133+
134+
busConfig.Spec.SQS.Endpoint = "https://sqs.us-west-2.amazonaws.com"
135+
busConfig.Spec.SQS.LargeMessageStoreEndpoint = "https://s3.us-west-2.amazonaws.com"
136+
137+
err = validateBusConfigurationInputs(&busConfig)
138+
assert.NotNil(t, err)
139+
assert.Equal(t, "bus configuration sqs largeMessageStorePath must start with s3://", err.Error())
140+
141+
busConfig.Spec.SQS.LargeMessageStorePath = "ingestion/smartbus-test"
142+
143+
err = validateBusConfigurationInputs(&busConfig)
144+
assert.NotNil(t, err)
145+
assert.Equal(t, "bus configuration sqs largeMessageStorePath must start with s3://", err.Error())
146+
147+
busConfig.Spec.SQS.LargeMessageStorePath = "s3://ingestion/smartbus-test"
148+
149+
err = validateBusConfigurationInputs(&busConfig)
150+
assert.Nil(t, err)
151+
}

pkg/splunk/enterprise/indexercluster.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,67 +1143,9 @@ func validateIndexerClusterSpec(ctx context.Context, c splcommon.ControllerClien
11431143
return fmt.Errorf("multisite cluster does not support cluster manager to be located in a different namespace")
11441144
}
11451145

1146-
if busConfig != nil {
1147-
err := validateIndexerBusSpecificInputs(busConfig)
1148-
if err != nil {
1149-
return err
1150-
}
1151-
}
1152-
11531146
return validateCommonSplunkSpec(ctx, c, &cr.Spec.CommonSplunkSpec, cr)
11541147
}
11551148

1156-
func validateIndexerBusSpecificInputs(busConfig *enterpriseApi.BusConfiguration) error {
1157-
// Otherwise, it means that no Ingestion & Index separation is applied
1158-
if busConfig.Spec != (enterpriseApi.BusConfigurationSpec{}) {
1159-
if busConfig.Spec.Type != "sqs_smartbus" {
1160-
return errors.New("only sqs_smartbus type is supported in bus type")
1161-
}
1162-
1163-
if busConfig.Spec.SQS == (enterpriseApi.SQSSpec{}) {
1164-
return errors.New("bus sqs cannot be empty")
1165-
}
1166-
1167-
// Cannot be empty fields check
1168-
cannotBeEmptyFields := []string{}
1169-
if busConfig.Spec.SQS.QueueName == "" {
1170-
cannotBeEmptyFields = append(cannotBeEmptyFields, "queueName")
1171-
}
1172-
1173-
if busConfig.Spec.SQS.AuthRegion == "" {
1174-
cannotBeEmptyFields = append(cannotBeEmptyFields, "authRegion")
1175-
}
1176-
1177-
if busConfig.Spec.SQS.DeadLetterQueueName == "" {
1178-
cannotBeEmptyFields = append(cannotBeEmptyFields, "deadLetterQueueName")
1179-
}
1180-
1181-
if len(cannotBeEmptyFields) > 0 {
1182-
return errors.New("bus sqs " + strings.Join(cannotBeEmptyFields, ", ") + " cannot be empty")
1183-
}
1184-
1185-
// Have to start with https:// or s3:// checks
1186-
haveToStartWithHttps := []string{}
1187-
if !strings.HasPrefix(busConfig.Spec.SQS.Endpoint, "https://") {
1188-
haveToStartWithHttps = append(haveToStartWithHttps, "endpoint")
1189-
}
1190-
1191-
if !strings.HasPrefix(busConfig.Spec.SQS.LargeMessageStoreEndpoint, "https://") {
1192-
haveToStartWithHttps = append(haveToStartWithHttps, "largeMessageStoreEndpoint")
1193-
}
1194-
1195-
if len(haveToStartWithHttps) > 0 {
1196-
return errors.New("bus sqs " + strings.Join(haveToStartWithHttps, ", ") + " must start with https://")
1197-
}
1198-
1199-
if !strings.HasPrefix(busConfig.Spec.SQS.LargeMessageStorePath, "s3://") {
1200-
return errors.New("bus sqs largeMessageStorePath must start with s3://")
1201-
}
1202-
}
1203-
1204-
return nil
1205-
}
1206-
12071149
// helper function to get the list of IndexerCluster types in the current namespace
12081150
func getIndexerClusterList(ctx context.Context, c splcommon.ControllerClient, cr splcommon.MetaObject, listOpts []client.ListOption) (enterpriseApi.IndexerClusterList, error) {
12091151
reqLogger := log.FromContext(ctx)

pkg/splunk/enterprise/indexercluster_test.go

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,8 @@ func TestApplyIndexerClusterManager_BusConfig_Success(t *testing.T) {
23582358
APIVersion: "enterprise.splunk.com/v4",
23592359
},
23602360
ObjectMeta: metav1.ObjectMeta{
2361-
Name: "busConfig",
2361+
Name: "busConfig",
2362+
Namespace: "test",
23622363
},
23632364
Spec: enterpriseApi.BusConfigurationSpec{
23642365
Type: "sqs_smartbus",
@@ -2395,7 +2396,7 @@ func TestApplyIndexerClusterManager_BusConfig_Success(t *testing.T) {
23952396
Spec: enterpriseApi.IndexerClusterSpec{
23962397
Replicas: 1,
23972398
BusConfigurationRef: corev1.ObjectReference{
2398-
Name: busConfig.Name,
2399+
Name: busConfig.Name,
23992400
Namespace: busConfig.Namespace,
24002401
},
24012402
CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{
@@ -2550,67 +2551,3 @@ func mustReq(method, url, body string) *http.Request {
25502551
}
25512552
return r
25522553
}
2553-
2554-
func TestValidateIndexerSpecificInputs(t *testing.T) {
2555-
busConfig := enterpriseApi.BusConfiguration{
2556-
TypeMeta: metav1.TypeMeta{
2557-
Kind: "BusConfiguration",
2558-
APIVersion: "enterprise.splunk.com/v4",
2559-
},
2560-
ObjectMeta: metav1.ObjectMeta{
2561-
Name: "busConfig",
2562-
},
2563-
Spec: enterpriseApi.BusConfigurationSpec{
2564-
Type: "othertype",
2565-
SQS: enterpriseApi.SQSSpec{},
2566-
},
2567-
}
2568-
2569-
err := validateIndexerBusSpecificInputs(&busConfig)
2570-
assert.NotNil(t, err)
2571-
assert.Equal(t, "only sqs_smartbus type is supported in bus type", err.Error())
2572-
2573-
busConfig.Spec.Type = "sqs_smartbus"
2574-
2575-
err = validateIndexerBusSpecificInputs(&busConfig)
2576-
assert.NotNil(t, err)
2577-
assert.Equal(t, "bus sqs cannot be empty", err.Error())
2578-
2579-
busConfig.Spec.SQS.AuthRegion = "us-west-2"
2580-
2581-
err = validateIndexerBusSpecificInputs(&busConfig)
2582-
assert.NotNil(t, err)
2583-
assert.Equal(t, "bus sqs queueName, deadLetterQueueName cannot be empty", err.Error())
2584-
2585-
busConfig.Spec.SQS.QueueName = "test-queue"
2586-
busConfig.Spec.SQS.DeadLetterQueueName = "dlq-test"
2587-
busConfig.Spec.SQS.AuthRegion = ""
2588-
2589-
err = validateIndexerBusSpecificInputs(&busConfig)
2590-
assert.NotNil(t, err)
2591-
assert.Equal(t, "bus sqs authRegion cannot be empty", err.Error())
2592-
2593-
busConfig.Spec.SQS.AuthRegion = "us-west-2"
2594-
2595-
err = validateIndexerBusSpecificInputs(&busConfig)
2596-
assert.NotNil(t, err)
2597-
assert.Equal(t, "bus sqs endpoint, largeMessageStoreEndpoint must start with https://", err.Error())
2598-
2599-
busConfig.Spec.SQS.Endpoint = "https://sqs.us-west-2.amazonaws.com"
2600-
busConfig.Spec.SQS.LargeMessageStoreEndpoint = "https://s3.us-west-2.amazonaws.com"
2601-
2602-
err = validateIndexerBusSpecificInputs(&busConfig)
2603-
assert.NotNil(t, err)
2604-
assert.Equal(t, "bus sqs largeMessageStorePath must start with s3://", err.Error())
2605-
2606-
busConfig.Spec.SQS.LargeMessageStorePath = "ingestion/smartbus-test"
2607-
2608-
err = validateIndexerBusSpecificInputs(&busConfig)
2609-
assert.NotNil(t, err)
2610-
assert.Equal(t, "bus sqs largeMessageStorePath must start with s3://", err.Error())
2611-
2612-
busConfig.Spec.SQS.LargeMessageStorePath = "s3://ingestion/smartbus-test"
2613-
2614-
err = validateIndexerBusSpecificInputs(&busConfig)
2615-
assert.Nil(t, err)
2616-
}

0 commit comments

Comments
 (0)