|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 operator |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | +) |
| 25 | + |
| 26 | +func makeKeystoneCRD(annotationNS, serviceNS string) *uns.Unstructured { |
| 27 | + obj := &uns.Unstructured{Object: map[string]interface{}{ |
| 28 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 29 | + "kind": "CustomResourceDefinition", |
| 30 | + "metadata": map[string]interface{}{ |
| 31 | + "name": "keystoneapis.keystone.openstack.org", |
| 32 | + "annotations": map[string]interface{}{ |
| 33 | + "cert-manager.io/inject-ca-from": annotationNS + "/keystone-operator-serving-cert", |
| 34 | + }, |
| 35 | + }, |
| 36 | + "spec": map[string]interface{}{ |
| 37 | + "conversion": map[string]interface{}{ |
| 38 | + "strategy": "Webhook", |
| 39 | + "webhook": map[string]interface{}{ |
| 40 | + "clientConfig": map[string]interface{}{ |
| 41 | + "service": map[string]interface{}{ |
| 42 | + "name": "keystone-operator-webhook-service", |
| 43 | + "namespace": serviceNS, |
| 44 | + "path": "/convert", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }} |
| 51 | + obj.SetGroupVersionKind(schema.GroupVersionKind{ |
| 52 | + Group: "apiextensions.k8s.io", Version: "v1", Kind: "CustomResourceDefinition", |
| 53 | + }) |
| 54 | + return obj |
| 55 | +} |
| 56 | + |
| 57 | +func TestRewriteCRDNamespaces_RewritesBothFields(t *testing.T) { |
| 58 | + obj := makeKeystoneCRD("keystone-operator-system", "keystone-operator-system") |
| 59 | + if err := rewriteCRDNamespaces(obj, "openstack-operators"); err != nil { |
| 60 | + t.Fatalf("unexpected error: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + annos := obj.GetAnnotations() |
| 64 | + got := annos["cert-manager.io/inject-ca-from"] |
| 65 | + want := "openstack-operators/keystone-operator-serving-cert" |
| 66 | + if got != want { |
| 67 | + t.Errorf("inject-ca-from: got %q, want %q", got, want) |
| 68 | + } |
| 69 | + |
| 70 | + svcNs, _, _ := uns.NestedString(obj.Object, "spec", "conversion", "webhook", "clientConfig", "service", "namespace") |
| 71 | + if svcNs != "openstack-operators" { |
| 72 | + t.Errorf("service.namespace: got %q, want %q", svcNs, "openstack-operators") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestRewriteCRDNamespaces_AlreadyCorrect(t *testing.T) { |
| 77 | + obj := makeKeystoneCRD("openstack-operators", "openstack-operators") |
| 78 | + if err := rewriteCRDNamespaces(obj, "openstack-operators"); err != nil { |
| 79 | + t.Fatalf("unexpected error: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + annos := obj.GetAnnotations() |
| 83 | + if got := annos["cert-manager.io/inject-ca-from"]; got != "openstack-operators/keystone-operator-serving-cert" { |
| 84 | + t.Errorf("annotation changed unexpectedly: %q", got) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestRewriteCRDNamespaces_AnnotationOnlyNoConversionSection(t *testing.T) { |
| 89 | + // CRD has the inject-ca-from annotation but no spec.conversion.webhook.clientConfig.service |
| 90 | + // (e.g. a CRD that uses cainjection for a non-conversion purpose). Service rewrite is a no-op. |
| 91 | + obj := &uns.Unstructured{Object: map[string]interface{}{ |
| 92 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 93 | + "kind": "CustomResourceDefinition", |
| 94 | + "metadata": map[string]interface{}{ |
| 95 | + "name": "somecrd.example.org", |
| 96 | + "annotations": map[string]interface{}{ |
| 97 | + "cert-manager.io/inject-ca-from": "source-ns/some-cert", |
| 98 | + }, |
| 99 | + }, |
| 100 | + "spec": map[string]interface{}{}, |
| 101 | + }} |
| 102 | + obj.SetGroupVersionKind(schema.GroupVersionKind{ |
| 103 | + Group: "apiextensions.k8s.io", Version: "v1", Kind: "CustomResourceDefinition", |
| 104 | + }) |
| 105 | + if err := rewriteCRDNamespaces(obj, "openstack-operators"); err != nil { |
| 106 | + t.Fatalf("unexpected error: %v", err) |
| 107 | + } |
| 108 | + annos := obj.GetAnnotations() |
| 109 | + if got := annos["cert-manager.io/inject-ca-from"]; got != "openstack-operators/some-cert" { |
| 110 | + t.Errorf("inject-ca-from not rewritten: got %q", got) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestRewriteCRDNamespaces_NoAnnotation(t *testing.T) { |
| 115 | + obj := &uns.Unstructured{Object: map[string]interface{}{ |
| 116 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 117 | + "kind": "CustomResourceDefinition", |
| 118 | + "metadata": map[string]interface{}{"name": "no-annotation.example.org"}, |
| 119 | + "spec": map[string]interface{}{}, |
| 120 | + }} |
| 121 | + obj.SetGroupVersionKind(schema.GroupVersionKind{ |
| 122 | + Group: "apiextensions.k8s.io", Version: "v1", Kind: "CustomResourceDefinition", |
| 123 | + }) |
| 124 | + if err := rewriteCRDNamespaces(obj, "openstack-operators"); err != nil { |
| 125 | + t.Fatalf("unexpected error: %v", err) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestRewriteCRDNamespaces_SkipsNonCRD(t *testing.T) { |
| 130 | + obj := &uns.Unstructured{Object: map[string]interface{}{ |
| 131 | + "apiVersion": "apps/v1", |
| 132 | + "kind": "Deployment", |
| 133 | + "metadata": map[string]interface{}{ |
| 134 | + "name": "test", |
| 135 | + "annotations": map[string]interface{}{ |
| 136 | + "cert-manager.io/inject-ca-from": "wrong-ns/some-cert", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }} |
| 140 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}) |
| 141 | + if err := rewriteCRDNamespaces(obj, "openstack-operators"); err != nil { |
| 142 | + t.Fatalf("unexpected error: %v", err) |
| 143 | + } |
| 144 | + |
| 145 | + // Non-CRD: annotation must NOT be touched |
| 146 | + if got := obj.GetAnnotations()["cert-manager.io/inject-ca-from"]; got != "wrong-ns/some-cert" { |
| 147 | + t.Errorf("non-CRD annotation was rewritten: %q", got) |
| 148 | + } |
| 149 | +} |
0 commit comments