Skip to content

Commit 6269862

Browse files
authored
Merge pull request #1813 from splunk/appruntime-poc/inject_appruntime_supervisor_address_as_env
Appruntime poc/inject appruntime supervisor address as env
2 parents 20b8f5b + 5fc2b38 commit 6269862

8 files changed

Lines changed: 190 additions & 4 deletions

File tree

helm-chart/splunk-operator/templates/deployment.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ spec:
7878
- name: SPLUNK_GENERAL_TERMS
7979
value: {{ .Values.splunkOperator.splunkGeneralTerms | default "" }}
8080
{{- with .Values.extraEnvs }}
81-
{{- toYaml . | trim | nindent 10 }}
81+
{{- toYaml . | trim | nindent 12 }}
8282
{{- end }}
8383
ports:
8484
{{- range .Values.splunkOperator.service.ports }}
@@ -109,4 +109,4 @@ spec:
109109
{{- with .Values.splunkOperator.volumes }}
110110
volumes:
111111
{{- toYaml .| nindent 8 }}
112-
{{- end }}
112+
{{- end }}

helm-chart/splunk-operator/templates/rbac/clusterrole.yaml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ rules:
144144
- patch
145145
- update
146146
- watch
147+
- apiGroups:
148+
- enterprise.splunk.com
149+
resources:
150+
- appruntimes
151+
verbs:
152+
- create
153+
- delete
154+
- get
155+
- list
156+
- patch
157+
- update
158+
- watch
159+
- apiGroups:
160+
- enterprise.splunk.com
161+
resources:
162+
- appruntimes/status
163+
verbs:
164+
- get
165+
- patch
166+
- update
167+
- apiGroups:
168+
- enterprise.splunk.com
169+
resources:
170+
- appruntimes/finalizers
171+
verbs:
172+
- update
147173
- apiGroups:
148174
- enterprise.splunk.com
149175
resources:
@@ -430,4 +456,4 @@ rules:
430456
- get
431457
- patch
432458
- update
433-
{{- end }}
459+
{{- end }}

helm-chart/splunk-operator/templates/rbac/role.yaml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ rules:
144144
- patch
145145
- update
146146
- watch
147+
- apiGroups:
148+
- enterprise.splunk.com
149+
resources:
150+
- appruntimes
151+
verbs:
152+
- create
153+
- delete
154+
- get
155+
- list
156+
- patch
157+
- update
158+
- watch
159+
- apiGroups:
160+
- enterprise.splunk.com
161+
resources:
162+
- appruntimes/status
163+
verbs:
164+
- get
165+
- patch
166+
- update
167+
- apiGroups:
168+
- enterprise.splunk.com
169+
resources:
170+
- appruntimes/finalizers
171+
verbs:
172+
- update
147173
- apiGroups:
148174
- enterprise.splunk.com
149175
resources:
@@ -430,4 +456,4 @@ rules:
430456
- get
431457
- patch
432458
- update
433-
{{- end }}
459+
{{- end }}

internal/controller/appruntime_controller.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"k8s.io/apimachinery/pkg/labels"
1919
"k8s.io/apimachinery/pkg/runtime"
2020
"k8s.io/apimachinery/pkg/types"
21+
"k8s.io/apimachinery/pkg/util/intstr"
2122
"k8s.io/client-go/tools/record"
2223
ctrl "sigs.k8s.io/controller-runtime"
2324
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -271,6 +272,14 @@ func (r *AppRuntimeReconciler) createHeadlessService(ctx context.Context, ar *en
271272
svc.Labels = getCommonLabels(ar.Name)
272273
svc.Spec.Selector = svc.Labels
273274
svc.Spec.ClusterIP = corev1.ClusterIPNone
275+
svc.Spec.Ports = []corev1.ServicePort{
276+
{
277+
Name: "appruntime",
278+
Port: 9000,
279+
Protocol: corev1.ProtocolTCP,
280+
TargetPort: intstr.FromInt(9000),
281+
},
282+
}
274283
err = r.Create(ctx, svc)
275284
if err != nil {
276285
return nil, err
@@ -317,6 +326,13 @@ func (r *AppRuntimeReconciler) createPod(ctx context.Context, appRuntime *enterp
317326
Command: []string{
318327
"/usr/local/bin/entrypoint.sh",
319328
},
329+
Ports: []corev1.ContainerPort{
330+
{
331+
Name: "appruntime",
332+
ContainerPort: 9000,
333+
Protocol: corev1.ProtocolTCP,
334+
},
335+
},
320336
VolumeMounts: []corev1.VolumeMount{
321337
{
322338
Name: "pvc-etc",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
11
package controller
2+
3+
import (
4+
"context"
5+
6+
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/types"
13+
"k8s.io/client-go/kubernetes/scheme"
14+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
15+
)
16+
17+
var _ = Describe("AppRuntime Controller", func() {
18+
BeforeEach(func() {
19+
Expect(enterpriseApi.AddToScheme(scheme.Scheme)).To(Succeed())
20+
})
21+
22+
It("creates a headless service that exposes the appruntime port", func() {
23+
ctx := context.Background()
24+
ar := &enterpriseApi.AppRuntime{
25+
ObjectMeta: metav1.ObjectMeta{
26+
Name: "stack1-standalone-appruntime",
27+
Namespace: "test",
28+
},
29+
}
30+
31+
reconciler := AppRuntimeReconciler{
32+
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(ar).Build(),
33+
Scheme: scheme.Scheme,
34+
}
35+
36+
svc, err := reconciler.createHeadlessService(ctx, ar, types.NamespacedName{
37+
Name: getHeadlessName(ar.Name),
38+
Namespace: ar.Namespace,
39+
})
40+
Expect(err).NotTo(HaveOccurred())
41+
Expect(svc.Spec.ClusterIP).To(Equal(corev1.ClusterIPNone))
42+
Expect(svc.Spec.Ports).To(HaveLen(1))
43+
Expect(svc.Spec.Ports[0].Name).To(Equal("appruntime"))
44+
Expect(svc.Spec.Ports[0].Port).To(Equal(int32(9000)))
45+
Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(9000))
46+
})
47+
48+
It("creates appruntime pods with container port 9000", func() {
49+
ctx := context.Background()
50+
ar := &enterpriseApi.AppRuntime{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: "stack1-standalone-appruntime",
53+
Namespace: "test",
54+
},
55+
Spec: enterpriseApi.AppRuntimeSpec{
56+
Image: "supervisor:0.0.1",
57+
Replicas: 1,
58+
},
59+
}
60+
61+
reconciler := AppRuntimeReconciler{
62+
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(ar).Build(),
63+
Scheme: scheme.Scheme,
64+
}
65+
66+
err := reconciler.createPod(ctx, ar, types.NamespacedName{
67+
Name: getPodName(ar.Name, 0),
68+
Namespace: ar.Namespace,
69+
}, "splunk-stack1-standalone", 0)
70+
Expect(err).NotTo(HaveOccurred())
71+
72+
pod := &corev1.Pod{}
73+
err = reconciler.Get(ctx, types.NamespacedName{
74+
Name: getPodName(ar.Name, 0),
75+
Namespace: ar.Namespace,
76+
}, pod)
77+
Expect(err).NotTo(HaveOccurred())
78+
Expect(pod.Spec.Containers).NotTo(BeEmpty())
79+
Expect(pod.Spec.Containers[0].Ports).To(HaveLen(1))
80+
Expect(pod.Spec.Containers[0].Ports[0].Name).To(Equal("appruntime"))
81+
Expect(pod.Spec.Containers[0].Ports[0].ContainerPort).To(Equal(int32(9000)))
82+
})
83+
})

pkg/splunk/enterprise/configuration.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ func updateSplunkPodTemplateWithConfig(ctx context.Context, client splcommon.Con
951951
{Name: livenessProbeDriverPathEnv, Value: GetLivenessDriverFilePath()},
952952
{Name: "SPLUNK_GENERAL_TERMS", Value: os.Getenv("SPLUNK_GENERAL_TERMS")},
953953
{Name: "SPLUNK_SKIP_CLUSTER_BUNDLE_PUSH", Value: "true"},
954+
{
955+
Name: "SPLUNK_APPRUNTIME_HEADLESS_SERVICE_FQDN",
956+
Value: GetSplunkAppRuntimeServiceFQDN(cr.GetNamespace(), instanceType, cr.GetName()),
957+
},
954958
}
955959

956960
// update variables for licensing, if configured

pkg/splunk/enterprise/names.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ func GetSplunkServiceName(instanceType InstanceType, identifier string, isHeadle
253253
return result
254254
}
255255

256+
// GetSplunkAppRuntimeServiceName uses a template to name the headless Kubernetes Service for Splunk App Runtime instances.
257+
func GetSplunkAppRuntimeServiceName(instanceType InstanceType, identifier string) string {
258+
appRuntimeInstanceType := fmt.Sprintf("%s-appruntime", instanceType)
259+
return fmt.Sprintf(serviceTemplateStr, identifier, appRuntimeInstanceType, "headless")
260+
}
261+
262+
// GetSplunkAppRuntimeServiceFQDN returns the fully qualified domain name for the headless Kubernetes service for Splunk App Runtime instances.
263+
func GetSplunkAppRuntimeServiceFQDN(namespace string, instanceType InstanceType, identifier string) string {
264+
return splcommon.GetServiceFQDN(namespace, GetSplunkAppRuntimeServiceName(instanceType, identifier))
265+
}
266+
256267
// GetSplunkDefaultsName uses a template to name a Kubernetes ConfigMap for a SplunkEnterprise resource.
257268
func GetSplunkDefaultsName(identifier string, instanceType InstanceType) string {
258269
return fmt.Sprintf(defaultsTemplateStr, identifier, instanceType.ToKind())

pkg/splunk/enterprise/names_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ func TestGetSplunkServiceName(t *testing.T) {
7070
test("splunk-stack1-license-manager-service", SplunkLicenseManager, LicenseManagerRefName, false)
7171
}
7272

73+
func TestGetSplunkAppRuntimeServiceName(t *testing.T) {
74+
test := func(want string, instanceType InstanceType, identifier string) {
75+
got := GetSplunkAppRuntimeServiceName(instanceType, identifier)
76+
if got != want {
77+
t.Errorf("GetSplunkAppRuntimeServiceName(\"%s\",\"%s\") = %s; want %s",
78+
instanceType.ToString(), identifier, got, want)
79+
}
80+
}
81+
82+
test("splunk-t1-standalone-appruntime-headless", SplunkStandalone, "t1")
83+
test("splunk-t2-cluster-manager-appruntime-headless", SplunkClusterManager, "t2")
84+
}
85+
86+
func TestGetSplunkAppRuntimeServiceFQDN(t *testing.T) {
87+
want := "splunk-t1-search-head-appruntime-headless.splunktest.svc.cluster.local"
88+
got := GetSplunkAppRuntimeServiceFQDN("splunktest", SplunkSearchHead, "t1")
89+
if got != want {
90+
t.Errorf("GetSplunkAppRuntimeServiceFQDN() = %s; want %s", got, want)
91+
}
92+
}
93+
7394
func TestGetSplunkDefaultsName(t *testing.T) {
7495
got := GetSplunkDefaultsName("t1", SplunkSearchHead)
7596
want := "splunk-t1-search-head-defaults"

0 commit comments

Comments
 (0)