|
9 | 9 |
|
10 | 10 | log "github.com/sirupsen/logrus"
|
11 | 11 | admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
|
12 |
| - k8serrors "k8s.io/apimachinery/pkg/api/errors" |
13 | 12 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
14 | 13 | "k8s.io/apimachinery/pkg/labels"
|
15 | 14 | )
|
@@ -72,80 +71,107 @@ func (i *StrategyDeploymentInstaller) createOrUpdateWebhook(caPEM []byte, desc v
|
72 | 71 | }
|
73 | 72 |
|
74 | 73 | func (i *StrategyDeploymentInstaller) createOrUpdateMutatingWebhook(ogNamespacelabelSelector *metav1.LabelSelector, caPEM []byte, desc v1alpha1.WebhookDescription) error {
|
75 |
| - webhooks := []admissionregistrationv1.MutatingWebhook{ |
76 |
| - desc.GetMutatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM), |
| 74 | + webhookLabels := ownerutil.OwnerLabel(i.owner, i.owner.GetObjectKind().GroupVersionKind().Kind) |
| 75 | + webhookLabels[WebhookDescKey] = desc.GenerateName |
| 76 | + webhookSelector := labels.SelectorFromSet(webhookLabels).String() |
| 77 | + |
| 78 | + existingWebhooks, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().MutatingWebhookConfigurations().List(context.TODO(), metav1.ListOptions{LabelSelector: webhookSelector}) |
| 79 | + if err != nil { |
| 80 | + return err |
77 | 81 | }
|
78 |
| - existingHook, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().MutatingWebhookConfigurations().Get(context.TODO(), desc.Name, metav1.GetOptions{}) |
79 |
| - if err == nil { |
80 |
| - // Check if the only owners are this CSV or in this CSV's replacement chain |
81 |
| - if ownerutil.Adoptable(i.owner, existingHook.GetOwnerReferences()) { |
82 |
| - ownerutil.AddNonBlockingOwner(existingHook, i.owner) |
83 |
| - } |
84 | 82 |
|
85 |
| - // Update the list of webhooks |
86 |
| - existingHook.Webhooks = webhooks |
| 83 | + if len(existingWebhooks.Items) == 0 { |
| 84 | + // Create a MutatingWebhookConfiguration |
| 85 | + webhook := admissionregistrationv1.MutatingWebhookConfiguration{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{ |
| 87 | + GenerateName: desc.GenerateName + "-", |
| 88 | + Namespace: i.owner.GetNamespace(), |
| 89 | + Labels: ownerutil.OwnerLabel(i.owner, i.owner.GetObjectKind().GroupVersionKind().Kind), |
| 90 | + }, |
| 91 | + Webhooks: []admissionregistrationv1.MutatingWebhook{ |
| 92 | + desc.GetMutatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM), |
| 93 | + }, |
| 94 | + } |
| 95 | + addWebhookLabels(&webhook, desc) |
87 | 96 |
|
88 |
| - // Attempt an update |
89 |
| - if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().MutatingWebhookConfigurations().Update(context.TODO(), existingHook, metav1.UpdateOptions{}); err != nil { |
90 |
| - log.Warnf("could not update MutatingWebhookConfiguration %s", existingHook.GetName()) |
| 97 | + if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().MutatingWebhookConfigurations().Create(context.TODO(), &webhook, metav1.CreateOptions{}); err != nil { |
| 98 | + log.Errorf("Webhooks: Error creating MutatingWebhookConfiguration: %v", err) |
91 | 99 | return err
|
92 | 100 | }
|
93 |
| - } else if k8serrors.IsNotFound(err) { |
94 |
| - hook := admissionregistrationv1.MutatingWebhookConfiguration{ |
95 |
| - ObjectMeta: metav1.ObjectMeta{Name: desc.Name, |
96 |
| - Namespace: i.owner.GetNamespace(), |
97 |
| - }, |
98 |
| - Webhooks: webhooks, |
| 101 | + return nil |
| 102 | + } |
| 103 | + for _, webhook := range existingWebhooks.Items { |
| 104 | + // Update the list of webhooks |
| 105 | + webhook.Webhooks = []admissionregistrationv1.MutatingWebhook{ |
| 106 | + desc.GetMutatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM), |
99 | 107 | }
|
100 |
| - // Add an owner |
101 |
| - ownerutil.AddNonBlockingOwner(&hook, i.owner) |
102 |
| - if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().MutatingWebhookConfigurations().Create(context.TODO(), &hook, metav1.CreateOptions{}); err != nil { |
103 |
| - log.Errorf("Webhooks: Error creating mutating MutatingVebhookConfiguration: %v", err) |
| 108 | + |
| 109 | + // Attempt an update |
| 110 | + if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().MutatingWebhookConfigurations().Update(context.TODO(), &webhook, metav1.UpdateOptions{}); err != nil { |
| 111 | + log.Warnf("could not update MutatingWebhookConfiguration %s", webhook.GetName()) |
104 | 112 | return err
|
105 | 113 | }
|
106 |
| - } else { |
107 |
| - return err |
108 | 114 | }
|
109 | 115 |
|
110 | 116 | return nil
|
111 | 117 | }
|
112 | 118 |
|
113 | 119 | func (i *StrategyDeploymentInstaller) createOrUpdateValidatingWebhook(ogNamespacelabelSelector *metav1.LabelSelector, caPEM []byte, desc v1alpha1.WebhookDescription) error {
|
114 |
| - webhooks := []admissionregistrationv1.ValidatingWebhook{ |
115 |
| - desc.GetValidatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM), |
| 120 | + webhookLabels := ownerutil.OwnerLabel(i.owner, i.owner.GetObjectKind().GroupVersionKind().Kind) |
| 121 | + webhookLabels[WebhookDescKey] = desc.GenerateName |
| 122 | + webhookSelector := labels.SelectorFromSet(webhookLabels).String() |
| 123 | + |
| 124 | + existingWebhooks, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().ValidatingWebhookConfigurations().List(context.TODO(), metav1.ListOptions{LabelSelector: webhookSelector}) |
| 125 | + if err != nil { |
| 126 | + return err |
116 | 127 | }
|
117 |
| - existingHook, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().ValidatingWebhookConfigurations().Get(context.TODO(), desc.Name, metav1.GetOptions{}) |
118 |
| - if err == nil { |
119 |
| - // Check if the only owners are this CSV or in this CSV's replacement chain |
120 |
| - if ownerutil.Adoptable(i.owner, existingHook.GetOwnerReferences()) { |
121 |
| - ownerutil.AddNonBlockingOwner(existingHook, i.owner) |
122 |
| - } |
123 | 128 |
|
124 |
| - // Update the list of webhooks |
125 |
| - existingHook.Webhooks = webhooks |
| 129 | + if len(existingWebhooks.Items) == 0 { |
| 130 | + // Create a ValidatingWebhookConfiguration |
| 131 | + webhook := admissionregistrationv1.ValidatingWebhookConfiguration{ |
| 132 | + ObjectMeta: metav1.ObjectMeta{ |
| 133 | + GenerateName: desc.GenerateName + "-", |
| 134 | + Namespace: i.owner.GetNamespace(), |
| 135 | + Labels: ownerutil.OwnerLabel(i.owner, i.owner.GetObjectKind().GroupVersionKind().Kind), |
| 136 | + }, |
| 137 | + Webhooks: []admissionregistrationv1.ValidatingWebhook{ |
| 138 | + desc.GetValidatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM), |
| 139 | + }, |
| 140 | + } |
| 141 | + addWebhookLabels(&webhook, desc) |
126 | 142 |
|
127 |
| - // Attempt an update |
128 |
| - if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().ValidatingWebhookConfigurations().Update(context.TODO(), existingHook, metav1.UpdateOptions{}); err != nil { |
129 |
| - log.Warnf("could not update ValidatingWebhookConfiguration %s", existingHook.GetName()) |
| 143 | + if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().ValidatingWebhookConfigurations().Create(context.TODO(), &webhook, metav1.CreateOptions{}); err != nil { |
| 144 | + log.Errorf("Webhooks: Error creating ValidatingWebhookConfiguration: %v", err) |
130 | 145 | return err
|
131 | 146 | }
|
132 |
| - } else if k8serrors.IsNotFound(err) { |
133 |
| - // Create a ValidatingWebhookConfiguration |
134 |
| - hook := admissionregistrationv1.ValidatingWebhookConfiguration{ |
135 |
| - ObjectMeta: metav1.ObjectMeta{Name: desc.Name, |
136 |
| - Namespace: i.owner.GetNamespace(), |
137 |
| - }, |
138 |
| - Webhooks: webhooks, |
| 147 | + return nil |
| 148 | + } |
| 149 | + for _, webhook := range existingWebhooks.Items { |
| 150 | + // Update the list of webhooks |
| 151 | + webhook.Webhooks = []admissionregistrationv1.ValidatingWebhook{ |
| 152 | + desc.GetValidatingWebhook(i.owner.GetNamespace(), ogNamespacelabelSelector, caPEM), |
139 | 153 | }
|
140 | 154 |
|
141 |
| - // Add an owner |
142 |
| - ownerutil.AddNonBlockingOwner(&hook, i.owner) |
143 |
| - if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().ValidatingWebhookConfigurations().Create(context.TODO(), &hook, metav1.CreateOptions{}); err != nil { |
144 |
| - log.Errorf("Webhooks: Error create creating ValidationVebhookConfiguration: %v", err) |
| 155 | + // Attempt an update |
| 156 | + if _, err := i.strategyClient.GetOpClient().KubernetesInterface().AdmissionregistrationV1().ValidatingWebhookConfigurations().Update(context.TODO(), &webhook, metav1.UpdateOptions{}); err != nil { |
| 157 | + log.Warnf("could not update ValidatingWebhookConfiguration %s", webhook.GetName()) |
145 | 158 | return err
|
146 | 159 | }
|
147 |
| - } else { |
148 |
| - return err |
149 | 160 | }
|
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +const WebhookDescKey = "webhookDescriptionGenerateName" |
| 166 | + |
| 167 | +// addWebhookLabels adds webhook labels to an object |
| 168 | +func addWebhookLabels(object metav1.Object, webhookDesc v1alpha1.WebhookDescription) error { |
| 169 | + labels := object.GetLabels() |
| 170 | + if labels == nil { |
| 171 | + labels = map[string]string{} |
| 172 | + } |
| 173 | + labels[WebhookDescKey] = webhookDesc.GenerateName |
| 174 | + object.SetLabels(labels) |
| 175 | + |
150 | 176 | return nil
|
151 | 177 | }
|
0 commit comments