MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / checkWebhookSetup

Function checkWebhookSetup

tests/utils/operator/webhooks.go:139–192  ·  view source on GitHub ↗

checkWebhookSetup ensures that the operator has finished the webhook setup. Webhook configuration names are resolved via configuration.Current, so ENABLE_WEBHOOK_NAMESPACE_SUFFIX and OPERATOR_NAMESPACE must be set in the test process environment to match the operator deployment under test.

(
	ctx context.Context,
	crudClient client.Client,
	namespace string,
)

Source from the content-addressed store, hash-verified

137// ENABLE_WEBHOOK_NAMESPACE_SUFFIX and OPERATOR_NAMESPACE must be set in the
138// test process environment to match the operator deployment under test.
139func checkWebhookSetup(
140 ctx context.Context,
141 crudClient client.Client,
142 namespace string,
143) error {
144 // Check CA
145 secret := &corev1.Secret{}
146 secretNamespacedName := types.NamespacedName{
147 Namespace: namespace,
148 Name: controller.WebhookSecretName,
149 }
150 err := objects.Get(ctx, crudClient, secretNamespacedName, secret)
151 if err != nil {
152 return err
153 }
154
155 ca := secret.Data["tls.crt"]
156
157 mutatingWebhookConfig, err := getCNPGsMutatingWebhookConf(ctx, crudClient)
158 if err != nil {
159 return err
160 }
161
162 for _, webhook := range mutatingWebhookConfig.Webhooks {
163 if !bytes.Equal(webhook.ClientConfig.CABundle, ca) {
164 return fmt.Errorf("CA bundle mismatch in %v: secret %v/%v (ca len=%d) does not match webhook config (bundle len=%d)",
165 configuration.Current.GetMutatingWebhookConfigurationName(),
166 secret.Namespace,
167 secret.Name,
168 len(ca),
169 len(webhook.ClientConfig.CABundle),
170 )
171 }
172 }
173
174 validatingWebhookConfig, err := getCNPGsValidatingWebhookConf(ctx, crudClient)
175 if err != nil {
176 return err
177 }
178
179 for _, webhook := range validatingWebhookConfig.Webhooks {
180 if !bytes.Equal(webhook.ClientConfig.CABundle, ca) {
181 return fmt.Errorf("CA bundle mismatch in %v: secret %v/%v (ca len=%d) does not match webhook config (bundle len=%d)",
182 configuration.Current.GetValidatingWebhookConfigurationName(),
183 secret.Namespace,
184 secret.Name,
185 len(ca),
186 len(webhook.ClientConfig.CABundle),
187 )
188 }
189 }
190
191 return nil
192}
193
194// getCNPGsMutatingWebhookConf get the MutatingWebhook linked to the operator.
195// The lookup name is resolved from configuration.Current, which reads the test

Callers 1

IsReadyFunction · 0.85

Tested by

no test coverage detected