|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package endpoints |
| 18 | + |
| 19 | +import ( |
| 20 | + . "github.com/onsi/ginkgo/v2" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | + |
| 25 | + operatorv1alpha1 "github.com/jumpstarter-dev/jumpstarter-controller/deploy/operator/api/v1alpha1" |
| 26 | +) |
| 27 | + |
| 28 | +// createTestJumpstarterSpec creates a JumpstarterSpec with the given baseDomain for testing |
| 29 | +func createTestJumpstarterSpec(baseDomain string) *operatorv1alpha1.JumpstarterSpec { |
| 30 | + return &operatorv1alpha1.JumpstarterSpec{ |
| 31 | + BaseDomain: baseDomain, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +var _ = Describe("detectOpenShiftBaseDomain", func() { |
| 36 | + // Note: These tests require OpenShift CRDs to be available in the test environment. |
| 37 | + // They will be skipped if the CRDs are not present, which is expected in non-OpenShift environments. |
| 38 | + |
| 39 | + Context("when OpenShift is available", func() { |
| 40 | + BeforeEach(func() { |
| 41 | + // Check if OpenShift CRDs are available |
| 42 | + ingress := &unstructured.Unstructured{} |
| 43 | + ingress.SetGroupVersionKind(schema.GroupVersionKind{ |
| 44 | + Group: "config.openshift.io", |
| 45 | + Version: "v1", |
| 46 | + Kind: "Ingress", |
| 47 | + }) |
| 48 | + ingress.SetName("cluster") |
| 49 | + ingress.Object["spec"] = map[string]interface{}{ |
| 50 | + "domain": "test-check.apps.example.com", |
| 51 | + } |
| 52 | + |
| 53 | + // Try to create a test Ingress object to check if the CRD is available |
| 54 | + err := k8sClient.Create(ctx, ingress) |
| 55 | + if err != nil { |
| 56 | + Skip("Skipping OpenShift baseDomain auto-detection tests: OpenShift CRDs not available in test environment") |
| 57 | + } |
| 58 | + // Clean up test object |
| 59 | + _ = k8sClient.Delete(ctx, ingress) |
| 60 | + }) |
| 61 | + |
| 62 | + Context("when OpenShift Ingress cluster config exists", func() { |
| 63 | + It("should successfully auto-detect baseDomain", func() { |
| 64 | + // Create a mock OpenShift Ingress cluster config |
| 65 | + ingress := &unstructured.Unstructured{} |
| 66 | + ingress.SetGroupVersionKind(schema.GroupVersionKind{ |
| 67 | + Group: "config.openshift.io", |
| 68 | + Version: "v1", |
| 69 | + Kind: "Ingress", |
| 70 | + }) |
| 71 | + ingress.SetName("cluster") |
| 72 | + ingress.Object["spec"] = map[string]interface{}{ |
| 73 | + "domain": "apps.example.com", |
| 74 | + } |
| 75 | + |
| 76 | + // Create the Ingress object in the cluster |
| 77 | + err := k8sClient.Create(ctx, ingress) |
| 78 | + Expect(err).NotTo(HaveOccurred()) |
| 79 | + |
| 80 | + // Test auto-detection |
| 81 | + detectedDomain := detectOpenShiftBaseDomain(cfg) |
| 82 | + Expect(detectedDomain).To(Equal("apps.example.com")) |
| 83 | + |
| 84 | + // Cleanup |
| 85 | + err = k8sClient.Delete(ctx, ingress) |
| 86 | + Expect(err).NotTo(HaveOccurred()) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + Context("when OpenShift Ingress cluster config has empty domain", func() { |
| 91 | + It("should return empty string", func() { |
| 92 | + // Create a mock OpenShift Ingress cluster config with empty domain |
| 93 | + ingress := &unstructured.Unstructured{} |
| 94 | + ingress.SetGroupVersionKind(schema.GroupVersionKind{ |
| 95 | + Group: "config.openshift.io", |
| 96 | + Version: "v1", |
| 97 | + Kind: "Ingress", |
| 98 | + }) |
| 99 | + ingress.SetName("cluster") |
| 100 | + ingress.Object["spec"] = map[string]interface{}{ |
| 101 | + "domain": "", |
| 102 | + } |
| 103 | + |
| 104 | + // Create the Ingress object in the cluster |
| 105 | + err := k8sClient.Create(ctx, ingress) |
| 106 | + Expect(err).NotTo(HaveOccurred()) |
| 107 | + |
| 108 | + // Test auto-detection with empty domain |
| 109 | + detectedDomain := detectOpenShiftBaseDomain(cfg) |
| 110 | + Expect(detectedDomain).To(Equal("")) |
| 111 | + |
| 112 | + // Cleanup |
| 113 | + err = k8sClient.Delete(ctx, ingress) |
| 114 | + Expect(err).NotTo(HaveOccurred()) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + Context("when OpenShift Ingress cluster config has no spec.domain", func() { |
| 119 | + It("should return empty string", func() { |
| 120 | + // Create a mock OpenShift Ingress cluster config without domain field |
| 121 | + ingress := &unstructured.Unstructured{} |
| 122 | + ingress.SetGroupVersionKind(schema.GroupVersionKind{ |
| 123 | + Group: "config.openshift.io", |
| 124 | + Version: "v1", |
| 125 | + Kind: "Ingress", |
| 126 | + }) |
| 127 | + ingress.SetName("cluster") |
| 128 | + ingress.Object["spec"] = map[string]interface{}{ |
| 129 | + // domain field is missing |
| 130 | + } |
| 131 | + |
| 132 | + // Create the Ingress object in the cluster |
| 133 | + err := k8sClient.Create(ctx, ingress) |
| 134 | + Expect(err).NotTo(HaveOccurred()) |
| 135 | + |
| 136 | + // Test auto-detection when domain field is missing |
| 137 | + detectedDomain := detectOpenShiftBaseDomain(cfg) |
| 138 | + Expect(detectedDomain).To(Equal("")) |
| 139 | + |
| 140 | + // Cleanup |
| 141 | + err = k8sClient.Delete(ctx, ingress) |
| 142 | + Expect(err).NotTo(HaveOccurred()) |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + Context("when OpenShift Ingress cluster config does not exist", func() { |
| 148 | + It("should return empty string", func() { |
| 149 | + // Try to auto-detect when no Ingress config exists |
| 150 | + // This test will work even without OpenShift CRDs because it just checks the fallback behavior |
| 151 | + detectedDomain := detectOpenShiftBaseDomain(cfg) |
| 152 | + Expect(detectedDomain).To(Equal("")) |
| 153 | + }) |
| 154 | + }) |
| 155 | +}) |
| 156 | + |
| 157 | +var _ = Describe("DefaultBaseDomain in Reconciler", func() { |
| 158 | + Context("when baseDomain is auto-detected", func() { |
| 159 | + It("should apply default baseDomain in ApplyDefaults when spec.BaseDomain is empty", func() { |
| 160 | + reconciler := NewReconciler(k8sClient, k8sClient.Scheme(), cfg) |
| 161 | + |
| 162 | + // Manually set a default baseDomain for testing |
| 163 | + reconciler.DefaultBaseDomain = "apps.example.com" |
| 164 | + |
| 165 | + // Create a spec with empty baseDomain |
| 166 | + spec := createTestJumpstarterSpec("") |
| 167 | + |
| 168 | + // Apply defaults with a namespace |
| 169 | + reconciler.ApplyDefaults(spec, "test-namespace") |
| 170 | + |
| 171 | + // Should use the default baseDomain with namespace prefix |
| 172 | + Expect(spec.BaseDomain).To(Equal("jumpstarter.test-namespace.apps.example.com")) |
| 173 | + }) |
| 174 | + |
| 175 | + It("should not override user-provided baseDomain", func() { |
| 176 | + reconciler := NewReconciler(k8sClient, k8sClient.Scheme(), cfg) |
| 177 | + |
| 178 | + // Set a default baseDomain |
| 179 | + reconciler.DefaultBaseDomain = "apps.example.com" |
| 180 | + |
| 181 | + // Create a spec with user-provided baseDomain |
| 182 | + spec := createTestJumpstarterSpec("user.custom.domain") |
| 183 | + |
| 184 | + // Apply defaults |
| 185 | + reconciler.ApplyDefaults(spec, "test-namespace") |
| 186 | + |
| 187 | + // Should keep the user-provided baseDomain |
| 188 | + Expect(spec.BaseDomain).To(Equal("user.custom.domain")) |
| 189 | + }) |
| 190 | + |
| 191 | + It("should not set baseDomain when DefaultBaseDomain is empty", func() { |
| 192 | + reconciler := NewReconciler(k8sClient, k8sClient.Scheme(), cfg) |
| 193 | + |
| 194 | + // No default baseDomain set (simulating non-OpenShift cluster) |
| 195 | + reconciler.DefaultBaseDomain = "" |
| 196 | + |
| 197 | + // Create a spec with empty baseDomain |
| 198 | + spec := createTestJumpstarterSpec("") |
| 199 | + |
| 200 | + // Apply defaults |
| 201 | + reconciler.ApplyDefaults(spec, "test-namespace") |
| 202 | + |
| 203 | + // baseDomain should remain empty |
| 204 | + Expect(spec.BaseDomain).To(Equal("")) |
| 205 | + }) |
| 206 | + }) |
| 207 | +}) |
0 commit comments