|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package clusterserviceversion |
| 16 | + |
| 17 | +import ( |
| 18 | + . "github.com/onsi/ginkgo" |
| 19 | + . "github.com/onsi/gomega" |
| 20 | + admissionregv1 "k8s.io/api/admissionregistration/v1" |
| 21 | + appsv1 "k8s.io/api/apps/v1" |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + |
| 24 | + "github.com/operator-framework/operator-sdk/internal/generate/collector" |
| 25 | +) |
| 26 | + |
| 27 | +var _ = Describe("findMatchingDeploymentAndServiceForWebhook", func() { |
| 28 | + |
| 29 | + var ( |
| 30 | + c *collector.Manifests |
| 31 | + wcc admissionregv1.WebhookClientConfig |
| 32 | + |
| 33 | + depName1 = "dep-name-1" |
| 34 | + depName2 = "dep-name-2" |
| 35 | + serviceName1 = "service-name-1" |
| 36 | + serviceName2 = "service-name-2" |
| 37 | + ) |
| 38 | + |
| 39 | + BeforeEach(func() { |
| 40 | + c = &collector.Manifests{} |
| 41 | + wcc = admissionregv1.WebhookClientConfig{} |
| 42 | + wcc.Service = &admissionregv1.ServiceReference{} |
| 43 | + }) |
| 44 | + |
| 45 | + Context("webhook config has a matching service name", func() { |
| 46 | + By("parsing one deployment and one service with one label") |
| 47 | + It("returns the first service and deployment", func() { |
| 48 | + labels := map[string]string{"operator-name": "test-operator"} |
| 49 | + c.Deployments = []appsv1.Deployment{newDeployment(depName1, labels)} |
| 50 | + c.Services = []corev1.Service{newService(serviceName1, labels)} |
| 51 | + wcc.Service.Name = serviceName1 |
| 52 | + depName, serviceName := findMatchingDeploymentAndServiceForWebhook(c, wcc) |
| 53 | + Expect(depName).To(Equal(depName1)) |
| 54 | + Expect(serviceName).To(Equal(serviceName1)) |
| 55 | + }) |
| 56 | + |
| 57 | + By("parsing two deployments and two services with non-intersecting labels") |
| 58 | + It("returns the first service and deployment", func() { |
| 59 | + labels1 := map[string]string{"operator-name": "test-operator"} |
| 60 | + labels2 := map[string]string{"foo": "bar"} |
| 61 | + c.Deployments = []appsv1.Deployment{ |
| 62 | + newDeployment(depName1, labels1), |
| 63 | + newDeployment(depName2, labels2), |
| 64 | + } |
| 65 | + c.Services = []corev1.Service{ |
| 66 | + newService(serviceName1, labels1), |
| 67 | + newService(serviceName2, labels2), |
| 68 | + } |
| 69 | + wcc.Service.Name = serviceName1 |
| 70 | + depName, serviceName := findMatchingDeploymentAndServiceForWebhook(c, wcc) |
| 71 | + Expect(depName).To(Equal(depName1)) |
| 72 | + Expect(serviceName).To(Equal(serviceName1)) |
| 73 | + }) |
| 74 | + |
| 75 | + By("parsing two deployments and two services with a label subset") |
| 76 | + It("returns the first service and second deployment", func() { |
| 77 | + labels1 := map[string]string{"operator-name": "test-operator"} |
| 78 | + labels2 := map[string]string{"operator-name": "test-operator", "foo": "bar"} |
| 79 | + c.Deployments = []appsv1.Deployment{ |
| 80 | + newDeployment(depName2, labels2), |
| 81 | + newDeployment(depName1, labels1), |
| 82 | + } |
| 83 | + c.Services = []corev1.Service{newService(serviceName1, labels1)} |
| 84 | + wcc.Service.Name = serviceName1 |
| 85 | + depName, serviceName := findMatchingDeploymentAndServiceForWebhook(c, wcc) |
| 86 | + Expect(depName).To(Equal(depName2)) |
| 87 | + Expect(serviceName).To(Equal(serviceName1)) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + Context("webhook config does not have a matching service", func() { |
| 92 | + By("parsing one deployment and one service with one label") |
| 93 | + It("returns neither service nor deployment name", func() { |
| 94 | + labels := map[string]string{"operator-name": "test-operator"} |
| 95 | + c.Deployments = []appsv1.Deployment{newDeployment(depName1, labels)} |
| 96 | + c.Services = []corev1.Service{newService(serviceName1, labels)} |
| 97 | + wcc.Service.Name = serviceName2 |
| 98 | + depName, serviceName := findMatchingDeploymentAndServiceForWebhook(c, wcc) |
| 99 | + Expect(depName).To(BeEmpty()) |
| 100 | + Expect(serviceName).To(BeEmpty()) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + Context("webhook config has a matching service but labels do not match", func() { |
| 105 | + By("parsing one deployment and one service with one label") |
| 106 | + It("returns the first service and no deployment", func() { |
| 107 | + labels1 := map[string]string{"operator-name": "test-operator"} |
| 108 | + labels2 := map[string]string{"foo": "bar"} |
| 109 | + c.Deployments = []appsv1.Deployment{newDeployment(depName1, labels1)} |
| 110 | + c.Services = []corev1.Service{newService(serviceName1, labels2)} |
| 111 | + wcc.Service.Name = serviceName1 |
| 112 | + depName, serviceName := findMatchingDeploymentAndServiceForWebhook(c, wcc) |
| 113 | + Expect(depName).To(BeEmpty()) |
| 114 | + Expect(serviceName).To(Equal(serviceName1)) |
| 115 | + }) |
| 116 | + |
| 117 | + By("parsing one deployment and one service with two intersecting labels") |
| 118 | + It("returns the first service and no deployment", func() { |
| 119 | + labels1 := map[string]string{"operator-name": "test-operator", "foo": "bar"} |
| 120 | + labels2 := map[string]string{"foo": "bar", "baz": "bat"} |
| 121 | + c.Deployments = []appsv1.Deployment{newDeployment(depName1, labels1)} |
| 122 | + c.Services = []corev1.Service{newService(serviceName1, labels2)} |
| 123 | + wcc.Service.Name = serviceName1 |
| 124 | + depName, serviceName := findMatchingDeploymentAndServiceForWebhook(c, wcc) |
| 125 | + Expect(depName).To(BeEmpty()) |
| 126 | + Expect(serviceName).To(Equal(serviceName1)) |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +func newDeployment(name string, labels map[string]string) appsv1.Deployment { |
| 132 | + dep := appsv1.Deployment{} |
| 133 | + dep.SetName(name) |
| 134 | + dep.Spec.Template.SetLabels(labels) |
| 135 | + return dep |
| 136 | +} |
| 137 | + |
| 138 | +func newService(name string, labels map[string]string) corev1.Service { |
| 139 | + s := corev1.Service{} |
| 140 | + s.SetName(name) |
| 141 | + s.Spec.Selector = labels |
| 142 | + return s |
| 143 | +} |
0 commit comments