|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 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 controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" //nolint:revive |
| 23 | + . "github.com/onsi/gomega" //nolint:revive |
| 24 | + "go.uber.org/mock/gomock" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1" |
| 27 | + "sigs.k8s.io/cluster-api/test/framework" |
| 28 | + v1beta1conditions "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions" |
| 29 | + "sigs.k8s.io/cluster-api/util/patch" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 32 | + |
| 33 | + infrav1alpha1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha1" |
| 34 | + infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" |
| 35 | + "sigs.k8s.io/cluster-api-provider-openstack/pkg/scope" |
| 36 | +) |
| 37 | + |
| 38 | +var _ = Describe("OpenStackFloatingIPPool controller", func() { |
| 39 | + var ( |
| 40 | + testPool *infrav1alpha1.OpenStackFloatingIPPool |
| 41 | + testNamespace string |
| 42 | + poolReconciler *OpenStackFloatingIPPoolReconciler |
| 43 | + poolMockCtrl *gomock.Controller |
| 44 | + poolMockFactory *scope.MockScopeFactory |
| 45 | + testNum int |
| 46 | + ) |
| 47 | + |
| 48 | + BeforeEach(func() { |
| 49 | + testNum++ |
| 50 | + testNamespace = fmt.Sprintf("pool-test-%d", testNum) |
| 51 | + |
| 52 | + testPool = &infrav1alpha1.OpenStackFloatingIPPool{ |
| 53 | + TypeMeta: metav1.TypeMeta{ |
| 54 | + APIVersion: infrav1alpha1.SchemeGroupVersion.Group + "/" + infrav1alpha1.SchemeGroupVersion.Version, |
| 55 | + Kind: "OpenStackFloatingIPPool", |
| 56 | + }, |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: "test-pool", |
| 59 | + Namespace: testNamespace, |
| 60 | + }, |
| 61 | + Spec: infrav1alpha1.OpenStackFloatingIPPoolSpec{ |
| 62 | + IdentityRef: infrav1.OpenStackIdentityReference{ |
| 63 | + Name: "test-creds", |
| 64 | + CloudName: "openstack", |
| 65 | + }, |
| 66 | + ReclaimPolicy: infrav1alpha1.ReclaimDelete, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + input := framework.CreateNamespaceInput{ |
| 71 | + Creator: k8sClient, |
| 72 | + Name: testNamespace, |
| 73 | + } |
| 74 | + framework.CreateNamespace(ctx, input) |
| 75 | + |
| 76 | + poolMockCtrl = gomock.NewController(GinkgoT()) |
| 77 | + poolMockFactory = scope.NewMockScopeFactory(poolMockCtrl, "") |
| 78 | + poolReconciler = &OpenStackFloatingIPPoolReconciler{ |
| 79 | + Client: k8sClient, |
| 80 | + ScopeFactory: poolMockFactory, |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + AfterEach(func() { |
| 85 | + orphan := metav1.DeletePropagationOrphan |
| 86 | + deleteOptions := client.DeleteOptions{ |
| 87 | + PropagationPolicy: &orphan, |
| 88 | + } |
| 89 | + |
| 90 | + // Remove finalizers and delete openstackfloatingippool |
| 91 | + patchHelper, err := patch.NewHelper(testPool, k8sClient) |
| 92 | + Expect(err).To(BeNil()) |
| 93 | + testPool.SetFinalizers([]string{}) |
| 94 | + err = patchHelper.Patch(ctx, testPool) |
| 95 | + Expect(err).To(BeNil()) |
| 96 | + err = k8sClient.Delete(ctx, testPool, &deleteOptions) |
| 97 | + Expect(err).To(BeNil()) |
| 98 | + }) |
| 99 | + |
| 100 | + It("should set OpenStackAuthenticationSucceededCondition to False when credentials secret is missing", func() { |
| 101 | + testPool.SetName("missing-pool-credentials") |
| 102 | + testPool.Spec.IdentityRef = infrav1.OpenStackIdentityReference{ |
| 103 | + Type: "Secret", |
| 104 | + Name: "non-existent-secret", |
| 105 | + CloudName: "openstack", |
| 106 | + } |
| 107 | + |
| 108 | + err := k8sClient.Create(ctx, testPool) |
| 109 | + Expect(err).To(BeNil()) |
| 110 | + |
| 111 | + credentialsErr := fmt.Errorf("secret not found: non-existent-secret") |
| 112 | + poolMockFactory.SetClientScopeCreateError(credentialsErr) |
| 113 | + |
| 114 | + req := reconcile.Request{ |
| 115 | + NamespacedName: client.ObjectKey{ |
| 116 | + Name: testPool.Name, |
| 117 | + Namespace: testPool.Namespace, |
| 118 | + }, |
| 119 | + } |
| 120 | + result, err := poolReconciler.Reconcile(ctx, req) |
| 121 | + |
| 122 | + Expect(err).To(MatchError(credentialsErr)) |
| 123 | + Expect(result).To(Equal(reconcile.Result{})) |
| 124 | + |
| 125 | + // Fetch the updated OpenStackFloatingIPPool to verify the condition was set |
| 126 | + updatedPool := &infrav1alpha1.OpenStackFloatingIPPool{} |
| 127 | + Expect(k8sClient.Get(ctx, client.ObjectKey{Name: testPool.Name, Namespace: testPool.Namespace}, updatedPool)).To(Succeed()) |
| 128 | + |
| 129 | + // Verify OpenStackAuthenticationSucceededCondition is set to False |
| 130 | + Expect(v1beta1conditions.IsFalse(updatedPool, infrav1.OpenStackAuthenticationSucceeded)).To(BeTrue()) |
| 131 | + condition := v1beta1conditions.Get(updatedPool, infrav1.OpenStackAuthenticationSucceeded) |
| 132 | + Expect(condition).ToNot(BeNil()) |
| 133 | + Expect(condition.Reason).To(Equal(infrav1.OpenStackAuthenticationFailedReason)) |
| 134 | + Expect(condition.Severity).To(Equal(clusterv1beta1.ConditionSeverityError)) |
| 135 | + Expect(condition.Message).To(ContainSubstring("Failed to create OpenStack client scope")) |
| 136 | + }) |
| 137 | + |
| 138 | + It("should set OpenStackAuthenticationSucceededCondition to False when namespace is denied access to ClusterIdentity", func() { |
| 139 | + testPool.SetName("identity-access-denied-pool") |
| 140 | + testPool.Spec.IdentityRef = infrav1.OpenStackIdentityReference{ |
| 141 | + Type: "ClusterIdentity", |
| 142 | + Name: "test-cluster-identity", |
| 143 | + CloudName: "openstack", |
| 144 | + } |
| 145 | + |
| 146 | + err := k8sClient.Create(ctx, testPool) |
| 147 | + Expect(err).To(BeNil()) |
| 148 | + |
| 149 | + identityAccessErr := &scope.IdentityAccessDeniedError{ |
| 150 | + IdentityName: "test-cluster-identity", |
| 151 | + RequesterNamespace: testNamespace, |
| 152 | + } |
| 153 | + poolMockFactory.SetClientScopeCreateError(identityAccessErr) |
| 154 | + |
| 155 | + req := reconcile.Request{ |
| 156 | + NamespacedName: client.ObjectKey{ |
| 157 | + Name: testPool.Name, |
| 158 | + Namespace: testPool.Namespace, |
| 159 | + }, |
| 160 | + } |
| 161 | + result, err := poolReconciler.Reconcile(ctx, req) |
| 162 | + |
| 163 | + Expect(err).To(MatchError(identityAccessErr)) |
| 164 | + Expect(result).To(Equal(reconcile.Result{})) |
| 165 | + |
| 166 | + // Fetch the updated OpenStackFloatingIPPool to verify the condition was set |
| 167 | + updatedPool := &infrav1alpha1.OpenStackFloatingIPPool{} |
| 168 | + Expect(k8sClient.Get(ctx, client.ObjectKey{Name: testPool.Name, Namespace: testPool.Namespace}, updatedPool)).To(Succeed()) |
| 169 | + |
| 170 | + // Verify OpenStackAuthenticationSucceededCondition is set to False |
| 171 | + Expect(v1beta1conditions.IsFalse(updatedPool, infrav1.OpenStackAuthenticationSucceeded)).To(BeTrue()) |
| 172 | + condition := v1beta1conditions.Get(updatedPool, infrav1.OpenStackAuthenticationSucceeded) |
| 173 | + Expect(condition).ToNot(BeNil()) |
| 174 | + Expect(condition.Reason).To(Equal(infrav1.OpenStackAuthenticationFailedReason)) |
| 175 | + Expect(condition.Severity).To(Equal(clusterv1beta1.ConditionSeverityError)) |
| 176 | + Expect(condition.Message).To(ContainSubstring("Failed to create OpenStack client scope")) |
| 177 | + }) |
| 178 | +}) |
0 commit comments