|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package specs |
| 21 | + |
| 22 | +import ( |
| 23 | + cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1" |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + rbacv1 "k8s.io/api/rbac/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("SetControllerReference", func() { |
| 32 | + It("should set the owner reference from the owner's TypeMeta", func() { |
| 33 | + owner := &cnpgv1.Cluster{ |
| 34 | + TypeMeta: metav1.TypeMeta{ |
| 35 | + APIVersion: "postgresql.cnpg.io/v1", |
| 36 | + Kind: "Cluster", |
| 37 | + }, |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "my-cluster", |
| 40 | + Namespace: "default", |
| 41 | + UID: types.UID("test-uid"), |
| 42 | + }, |
| 43 | + } |
| 44 | + controlled := &rbacv1.Role{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{ |
| 46 | + Name: "my-role", |
| 47 | + Namespace: "default", |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + Expect(SetControllerReference(owner, controlled)).To(Succeed()) |
| 52 | + Expect(controlled.OwnerReferences).To(HaveLen(1)) |
| 53 | + Expect(controlled.OwnerReferences[0].APIVersion).To(Equal("postgresql.cnpg.io/v1")) |
| 54 | + Expect(controlled.OwnerReferences[0].Kind).To(Equal("Cluster")) |
| 55 | + Expect(controlled.OwnerReferences[0].Name).To(Equal("my-cluster")) |
| 56 | + Expect(controlled.OwnerReferences[0].UID).To(Equal(types.UID("test-uid"))) |
| 57 | + Expect(*controlled.OwnerReferences[0].Controller).To(BeTrue()) |
| 58 | + Expect(*controlled.OwnerReferences[0].BlockOwnerDeletion).To(BeTrue()) |
| 59 | + }) |
| 60 | + |
| 61 | + It("should work with a custom CNPG API group", func() { |
| 62 | + owner := &cnpgv1.Cluster{ |
| 63 | + TypeMeta: metav1.TypeMeta{ |
| 64 | + APIVersion: "mycompany.io/v1", |
| 65 | + Kind: "Cluster", |
| 66 | + }, |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "my-cluster", |
| 69 | + UID: types.UID("test-uid"), |
| 70 | + }, |
| 71 | + } |
| 72 | + controlled := &rbacv1.Role{} |
| 73 | + |
| 74 | + Expect(SetControllerReference(owner, controlled)).To(Succeed()) |
| 75 | + Expect(controlled.OwnerReferences[0].APIVersion).To(Equal("mycompany.io/v1")) |
| 76 | + }) |
| 77 | + |
| 78 | + It("should fail when the owner has no GVK set", func() { |
| 79 | + owner := &cnpgv1.Cluster{ |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: "my-cluster", |
| 82 | + }, |
| 83 | + } |
| 84 | + controlled := &rbacv1.Role{} |
| 85 | + |
| 86 | + err := SetControllerReference(owner, controlled) |
| 87 | + Expect(err).To(HaveOccurred()) |
| 88 | + Expect(err.Error()).To(ContainSubstring("has no GVK set")) |
| 89 | + }) |
| 90 | +}) |
0 commit comments