github.com/percona/percona-xtradb-cluster-operator@v1.14.0/pkg/controller/pxc/service_test.go (about) 1 package pxc 2 3 import ( 4 "context" 5 "strings" 6 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 appsv1 "k8s.io/api/apps/v1" 10 corev1 "k8s.io/api/core/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 "k8s.io/apimachinery/pkg/types" 13 "sigs.k8s.io/controller-runtime/pkg/client" 14 "sigs.k8s.io/controller-runtime/pkg/reconcile" 15 16 "github.com/percona/percona-xtradb-cluster-operator/pkg/pxc" 17 ) 18 19 var _ = Describe("Service labels and annotations", Ordered, func() { 20 ctx := context.Background() 21 const ns = "svc-ls-an" 22 namespace := &corev1.Namespace{ 23 ObjectMeta: metav1.ObjectMeta{ 24 Name: ns, 25 Namespace: ns, 26 }, 27 } 28 crName := ns + "-cr" 29 crNamespacedName := types.NamespacedName{Name: crName, Namespace: ns} 30 cr, err := readDefaultCR(crName, ns) 31 It("should read default cr.yaml", func() { 32 Expect(err).NotTo(HaveOccurred()) 33 }) 34 35 BeforeAll(func() { 36 By("Creating the Namespace to perform the tests") 37 err := k8sClient.Create(ctx, namespace) 38 Expect(err).To(Not(HaveOccurred())) 39 }) 40 41 AfterAll(func() { 42 // TODO(user): Attention if you improve this code by adding other context test you MUST 43 // be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations 44 By("Deleting the Namespace to perform the tests") 45 _ = k8sClient.Delete(ctx, namespace) 46 }) 47 48 Context("Create Percona XtraDB cluster", func() { 49 It("Should create PerconaXtraDBCluster", func() { 50 Expect(k8sClient.Create(ctx, cr)).Should(Succeed()) 51 }) 52 }) 53 54 It("should reconcile PerconaXtraDBCluster", func() { 55 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 56 NamespacedName: crNamespacedName, 57 }) 58 Expect(err).To(Succeed()) 59 }) 60 61 checkLabelsAndAnnotations := func(services []*corev1.Service) { 62 Context("update service labels manually", func() { 63 It("should update service labels manually", func() { 64 for i := range services { 65 svc := new(corev1.Service) 66 67 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 68 69 svc.Labels["manual-label"] = "test" 70 svc.Labels["ignored-label"] = "test" 71 svc.Annotations["manual-annotation"] = "test" 72 svc.Annotations["ignored-annotation"] = "test" 73 Expect(k8sClient.Update(ctx, svc)).To(Succeed()) 74 } 75 }) 76 77 It("should reconcile PerconaXtraDBCluster", func() { 78 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 79 NamespacedName: crNamespacedName, 80 }) 81 Expect(err).To(Succeed()) 82 }) 83 It("should check if manual labels and annotations are still there", func() { 84 for i := range services { 85 svc := new(corev1.Service) 86 87 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 88 89 Expect(svc.Labels["manual-label"]).To(Equal("test")) 90 Expect(svc.Annotations["manual-annotation"]).To(Equal("test")) 91 Expect(svc.Labels["ignored-label"]).To(Equal("test")) 92 Expect(svc.Annotations["ignored-annotation"]).To(Equal("test")) 93 } 94 }) 95 }) 96 97 Context("set service labels and annotations", func() { 98 It("should update cr", func() { 99 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(cr), cr)).To(Succeed()) 100 cr.Spec.IgnoreAnnotations = []string{"ignored-annotation"} 101 cr.Spec.IgnoreLabels = []string{"ignored-label"} 102 cr.Spec.PXC.Expose.Labels = map[string]string{"cr-label": "test"} 103 cr.Spec.PXC.Expose.Annotations = map[string]string{"cr-annotation": "test"} 104 cr.Spec.HAProxy.ExposePrimary.Labels = map[string]string{"cr-label": "test"} 105 cr.Spec.HAProxy.ExposePrimary.Annotations = map[string]string{"cr-annotation": "test"} 106 cr.Spec.HAProxy.ExposeReplicas.Labels = map[string]string{"cr-label": "test"} 107 cr.Spec.HAProxy.ExposeReplicas.Annotations = map[string]string{"cr-annotation": "test"} 108 cr.Spec.ProxySQL.Expose.Labels = map[string]string{"cr-label": "test"} 109 cr.Spec.ProxySQL.Expose.Annotations = map[string]string{"cr-annotation": "test"} 110 Expect(k8sClient.Update(ctx, cr)).Should(Succeed()) 111 }) 112 It("should reconcile PerconaXtraDBCluster", func() { 113 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 114 NamespacedName: crNamespacedName, 115 }) 116 Expect(err).To(Succeed()) 117 }) 118 It("check labels and annotations", func() { 119 for i := range services { 120 svc := new(corev1.Service) 121 122 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 123 124 Expect(svc.Labels["manual-label"]).To(Equal("")) 125 Expect(svc.Annotations["manual-annotation"]).To(Equal("")) 126 Expect(svc.Labels["ignored-label"]).To(Equal("test")) 127 Expect(svc.Annotations["ignored-annotation"]).To(Equal("test")) 128 Expect(svc.Labels["cr-label"]).To(Equal("test")) 129 Expect(svc.Annotations["cr-annotation"]).To(Equal("test")) 130 } 131 }) 132 }) 133 Context("remove ignored labels and annotations", func() { 134 It("should update cr", func() { 135 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(cr), cr)).To(Succeed()) 136 cr.Spec.IgnoreAnnotations = []string{} 137 cr.Spec.IgnoreLabels = []string{} 138 Expect(k8sClient.Update(ctx, cr)).Should(Succeed()) 139 }) 140 It("should reconcile PerconaXtraDBCluster", func() { 141 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 142 NamespacedName: crNamespacedName, 143 }) 144 Expect(err).To(Succeed()) 145 }) 146 It("should check if there are no ignored labels and annotations", func() { 147 for i := range services { 148 svc := new(corev1.Service) 149 150 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 151 152 Expect(svc.Labels["ignored-label"]).To(Equal("")) 153 Expect(svc.Annotations["ignored-annotation"]).To(Equal("")) 154 Expect(svc.Labels["cr-label"]).To(Equal("test")) 155 Expect(svc.Annotations["cr-annotation"]).To(Equal("test")) 156 } 157 }) 158 }) 159 } 160 161 services := []*corev1.Service{ 162 pxc.NewServicePXC(cr), 163 pxc.NewServiceHAProxy(cr), 164 pxc.NewServiceHAProxyReplicas(cr), 165 } 166 167 Context("check haproxy cluster", func() { 168 checkLabelsAndAnnotations(services) 169 }) 170 171 It("should delete services", func() { 172 for _, svc := range services { 173 Expect(k8sClient.Delete(ctx, svc)).To(Succeed()) 174 } 175 }) 176 177 It("should switch to ProxySQL and remove serviceLabels, serviceAnnotations", func() { 178 haproxySts := &appsv1.StatefulSet{ 179 ObjectMeta: metav1.ObjectMeta{ 180 Name: cr.Name + "-haproxy", 181 Namespace: cr.Namespace, 182 }, 183 } 184 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(haproxySts), haproxySts)).To(Succeed()) 185 Expect(k8sClient.Delete(ctx, haproxySts)).To(Succeed()) 186 187 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(cr), cr)).To(Succeed()) 188 cr.Spec.HAProxy.Enabled = false 189 cr.Spec.ProxySQL.Enabled = true 190 191 cr.Spec.PXC.Expose.Labels = nil 192 cr.Spec.PXC.Expose.Annotations = nil 193 cr.Spec.HAProxy.ExposePrimary.Labels = nil 194 cr.Spec.HAProxy.ExposePrimary.Annotations = nil 195 cr.Spec.HAProxy.ExposeReplicas.Labels = nil 196 cr.Spec.HAProxy.ExposeReplicas.Annotations = nil 197 cr.Spec.ProxySQL.Expose.Labels = nil 198 cr.Spec.ProxySQL.Expose.Annotations = nil 199 Expect(k8sClient.Update(ctx, cr)).To(Succeed()) 200 }) 201 It("should reconcile PerconaXtraDBCluster", func() { 202 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 203 NamespacedName: crNamespacedName, 204 }) 205 Expect(err).To(Succeed()) 206 }) 207 208 Context("check proxysql cluster", func() { 209 checkLabelsAndAnnotations([]*corev1.Service{ 210 pxc.NewServicePXC(cr), 211 pxc.NewServiceProxySQL(cr), 212 }) 213 }) 214 }) 215 216 var _ = Describe("Service labels and annotations for 1.13.0", Ordered, func() { 217 ctx := context.Background() 218 const ns = "svc-ls-an-1-13-0" 219 namespace := &corev1.Namespace{ 220 ObjectMeta: metav1.ObjectMeta{ 221 Name: ns, 222 Namespace: ns, 223 }, 224 } 225 crName := ns + "-cr" 226 crNamespacedName := types.NamespacedName{Name: crName, Namespace: ns} 227 cr, err := readDefaultCR(crName, ns) 228 It("should read default cr.yaml", func() { 229 Expect(err).NotTo(HaveOccurred()) 230 }) 231 cr.Spec.CRVersion = "1.13.0" 232 233 BeforeAll(func() { 234 By("Creating the Namespace to perform the tests") 235 err := k8sClient.Create(ctx, namespace) 236 Expect(err).To(Not(HaveOccurred())) 237 }) 238 239 AfterAll(func() { 240 // TODO(user): Attention if you improve this code by adding other context test you MUST 241 // be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations 242 By("Deleting the Namespace to perform the tests") 243 _ = k8sClient.Delete(ctx, namespace) 244 }) 245 246 Context("Create Percona XtraDB cluster", func() { 247 It("Should create PerconaXtraDBCluster", func() { 248 Expect(k8sClient.Create(ctx, cr)).Should(Succeed()) 249 }) 250 }) 251 252 It("should reconcile PerconaXtraDBCluster", func() { 253 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 254 NamespacedName: crNamespacedName, 255 }) 256 Expect(err).To(Succeed()) 257 }) 258 259 checkLabelsAndAnnotations := func(services []*corev1.Service) { 260 Context("update service labels manually", func() { 261 It("should update service labels manually", func() { 262 for i := range services { 263 svc := new(corev1.Service) 264 265 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 266 267 svc.Labels["manual-label"] = "test" 268 svc.Labels["ignored-label"] = "test" 269 svc.Annotations["manual-annotation"] = "test" 270 svc.Annotations["ignored-annotation"] = "test" 271 Expect(k8sClient.Update(ctx, svc)).To(Succeed()) 272 } 273 }) 274 275 It("should reconcile PerconaXtraDBCluster", func() { 276 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 277 NamespacedName: crNamespacedName, 278 }) 279 Expect(err).To(Succeed()) 280 }) 281 It("should check if manual labels and annotations are still there", func() { 282 for i := range services { 283 svc := new(corev1.Service) 284 285 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 286 287 Expect(svc.Labels["manual-label"]).To(Equal("test")) 288 Expect(svc.Annotations["manual-annotation"]).To(Equal("test")) 289 Expect(svc.Labels["ignored-label"]).To(Equal("test")) 290 Expect(svc.Annotations["ignored-annotation"]).To(Equal("test")) 291 } 292 }) 293 }) 294 295 Context("set service labels and annotations", func() { 296 It("should update cr", func() { 297 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(cr), cr)).To(Succeed()) 298 cr.Spec.IgnoreAnnotations = []string{"ignored-annotation"} 299 cr.Spec.IgnoreLabels = []string{"ignored-label"} 300 301 cr.Spec.HAProxy.ServiceLabels = map[string]string{"cr-label": "test"} 302 cr.Spec.HAProxy.ServiceAnnotations = map[string]string{"cr-annotation": "test"} 303 cr.Spec.HAProxy.ReplicasServiceLabels = map[string]string{"cr-label": "test"} 304 cr.Spec.HAProxy.ReplicasServiceAnnotations = map[string]string{"cr-annotation": "test"} 305 cr.Spec.ProxySQL.ServiceLabels = map[string]string{"cr-label": "test"} 306 cr.Spec.ProxySQL.ServiceAnnotations = map[string]string{"cr-annotation": "test"} 307 Expect(k8sClient.Update(ctx, cr)).Should(Succeed()) 308 }) 309 It("should reconcile PerconaXtraDBCluster", func() { 310 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 311 NamespacedName: crNamespacedName, 312 }) 313 Expect(err).To(Succeed()) 314 }) 315 It("check labels and annotations", func() { 316 for i := range services { 317 svc := new(corev1.Service) 318 319 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 320 321 if strings.Contains(svc.Name, "pxc") { 322 Expect(svc.Labels["manual-label"]).To(Equal("test")) 323 Expect(svc.Annotations["manual-annotation"]).To(Equal("test")) 324 Expect(svc.Labels["ignored-label"]).To(Equal("test")) 325 Expect(svc.Annotations["ignored-annotation"]).To(Equal("test")) 326 327 Expect(svc.Labels["cr-label"]).To(BeEmpty()) 328 Expect(svc.Annotations["cr-annotation"]).To(BeEmpty()) 329 } else { 330 Expect(svc.Labels["manual-label"]).To(Equal("")) 331 Expect(svc.Annotations["manual-annotation"]).To(Equal("")) 332 Expect(svc.Labels["ignored-label"]).To(Equal("test")) 333 Expect(svc.Annotations["ignored-annotation"]).To(Equal("test")) 334 Expect(svc.Labels["cr-label"]).To(Equal("test")) 335 Expect(svc.Annotations["cr-annotation"]).To(Equal("test")) 336 } 337 } 338 }) 339 }) 340 Context("remove ignored labels and annotations", func() { 341 It("should update cr", func() { 342 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(cr), cr)).To(Succeed()) 343 cr.Spec.IgnoreAnnotations = []string{} 344 cr.Spec.IgnoreLabels = []string{} 345 Expect(k8sClient.Update(ctx, cr)).Should(Succeed()) 346 }) 347 It("should reconcile PerconaXtraDBCluster", func() { 348 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 349 NamespacedName: crNamespacedName, 350 }) 351 Expect(err).To(Succeed()) 352 }) 353 It("should check if there are no ignored labels and annotations", func() { 354 for i := range services { 355 svc := new(corev1.Service) 356 357 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(services[i]), svc)).To(Succeed()) 358 359 if strings.Contains(svc.Name, "pxc") { 360 Expect(svc.Labels["ignored-label"]).To(Equal("test")) 361 Expect(svc.Annotations["ignored-annotation"]).To(Equal("test")) 362 Expect(svc.Labels["cr-label"]).To(BeEmpty()) 363 Expect(svc.Annotations["cr-annotation"]).To(BeEmpty()) 364 } else { 365 Expect(svc.Labels["ignored-label"]).To(Equal("")) 366 Expect(svc.Annotations["ignored-annotation"]).To(Equal("")) 367 Expect(svc.Labels["cr-label"]).To(Equal("test")) 368 Expect(svc.Annotations["cr-annotation"]).To(Equal("test")) 369 } 370 } 371 }) 372 }) 373 } 374 375 services := []*corev1.Service{ 376 pxc.NewServicePXC(cr), 377 pxc.NewServiceHAProxy(cr), 378 pxc.NewServiceHAProxyReplicas(cr), 379 } 380 381 Context("check haproxy cluster", func() { 382 checkLabelsAndAnnotations(services) 383 }) 384 385 It("should delete services", func() { 386 for _, svc := range services { 387 Expect(k8sClient.Delete(ctx, svc)).To(Succeed()) 388 } 389 }) 390 391 It("should switch to ProxySQL and remove serviceLabels, serviceAnnotations", func() { 392 haproxySts := &appsv1.StatefulSet{ 393 ObjectMeta: metav1.ObjectMeta{ 394 Name: cr.Name + "-haproxy", 395 Namespace: cr.Namespace, 396 }, 397 } 398 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(haproxySts), haproxySts)).To(Succeed()) 399 Expect(k8sClient.Delete(ctx, haproxySts)).To(Succeed()) 400 401 Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(cr), cr)).To(Succeed()) 402 cr.Spec.HAProxy.Enabled = false 403 cr.Spec.ProxySQL.Enabled = true 404 405 cr.Spec.HAProxy.ServiceLabels = nil 406 cr.Spec.HAProxy.ServiceAnnotations = nil 407 cr.Spec.HAProxy.ReplicasServiceLabels = nil 408 cr.Spec.HAProxy.ReplicasServiceAnnotations = nil 409 cr.Spec.ProxySQL.ServiceLabels = nil 410 cr.Spec.ProxySQL.ServiceAnnotations = nil 411 Expect(k8sClient.Update(ctx, cr)).To(Succeed()) 412 }) 413 It("should reconcile PerconaXtraDBCluster", func() { 414 _, err := reconciler().Reconcile(ctx, reconcile.Request{ 415 NamespacedName: crNamespacedName, 416 }) 417 Expect(err).To(Succeed()) 418 }) 419 420 Context("check proxysql cluster", func() { 421 checkLabelsAndAnnotations([]*corev1.Service{ 422 pxc.NewServicePXC(cr), 423 pxc.NewServiceProxySQL(cr), 424 }) 425 }) 426 })