Kubernetes Operator that creates Service Endpoints from Secrets
1/*
2Copyright 2025.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package controller
18
19import (
20 "context"
21
22 . "github.com/onsi/ginkgo/v2"
23 . "github.com/onsi/gomega"
24 "k8s.io/apimachinery/pkg/api/errors"
25 "k8s.io/apimachinery/pkg/types"
26 "sigs.k8s.io/controller-runtime/pkg/reconcile"
27
28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30 appsv1 "github.com/evanjarrett/secret-service-operator/api/v1"
31)
32
33var _ = Describe("SecretService Controller", func() {
34 Context("When reconciling a resource", func() {
35 const resourceName = "test-resource"
36
37 ctx := context.Background()
38
39 typeNamespacedName := types.NamespacedName{
40 Name: resourceName,
41 Namespace: "default", // TODO(user):Modify as needed
42 }
43 secretservice := &appsv1.SecretService{}
44
45 BeforeEach(func() {
46 By("creating the custom resource for the Kind SecretService")
47 err := k8sClient.Get(ctx, typeNamespacedName, secretservice)
48 if err != nil && errors.IsNotFound(err) {
49 resource := &appsv1.SecretService{
50 ObjectMeta: metav1.ObjectMeta{
51 Name: resourceName,
52 Namespace: "default",
53 },
54 Spec: appsv1.SecretServiceSpec{
55 SecretName: "test-secret",
56 },
57 }
58 Expect(k8sClient.Create(ctx, resource)).To(Succeed())
59 }
60 })
61
62 AfterEach(func() {
63 // TODO(user): Cleanup logic after each test, like removing the resource instance.
64 resource := &appsv1.SecretService{}
65 err := k8sClient.Get(ctx, typeNamespacedName, resource)
66 Expect(err).NotTo(HaveOccurred())
67
68 By("Cleanup the specific resource instance SecretService")
69 Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
70 })
71 It("should successfully reconcile the resource", func() {
72 By("Reconciling the created resource")
73 controllerReconciler := &SecretServiceReconciler{
74 Client: k8sClient,
75 Scheme: k8sClient.Scheme(),
76 }
77
78 _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
79 NamespacedName: typeNamespacedName,
80 })
81 Expect(err).NotTo(HaveOccurred())
82 // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
83 // Example: If you expect a certain status condition after reconciliation, verify it here.
84 })
85 })
86})