Update webhooks during registration
@@ -485,7 +485,7 @@ func (app *virtAPIApp) createWebhook() error {
registerWebhook := false
vmPath := vmValidatePath
- _, err := app.virtCli.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Get(virtWebhookValidator, metav1.GetOptions{})
+ webhookRegistration, err := app.virtCli.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Get(virtWebhookValidator, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
registerWebhook = true
@@ -494,16 +494,7 @@ func (app *virtAPIApp) createWebhook() error {
}
}
- // TODO update webhook if it is different than what we generate
- if registerWebhook {
- _, err := app.virtCli.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Create(&admissionregistrationv1beta1.ValidatingWebhookConfiguration{
- ObjectMeta: metav1.ObjectMeta{
- Name: virtWebhookValidator,
- Labels: map[string]string{
- v1.AppLabel: virtWebhookValidator,
- },
- },
- Webhooks: []admissionregistrationv1beta1.Webhook{
+ webHooks := []admissionregistrationv1beta1.Webhook{
{
Name: "virtualmachine-validator.kubevirt.io",
Rules: []admissionregistrationv1beta1.RuleWithOperations{{
@@ -523,11 +514,28 @@ func (app *virtAPIApp) createWebhook() error {
CABundle: app.signingCertBytes,
},
},
+ }
+
+ if registerWebhook {
+ _, err := app.virtCli.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Create(&admissionregistrationv1beta1.ValidatingWebhookConfiguration{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: virtWebhookValidator,
+ Labels: map[string]string{
+ v1.AppLabel: virtWebhookValidator,
},
+ },
+ Webhooks: webHooks,
})
if err != nil {
return err
}
+ } else {
+ // update registered webhook with our data
+ webhookRegistration.Webhooks = webHooks
+ _, err := app.virtCli.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Update(webhookRegistration)
+ if err != nil {
+ return err
+ }
}
http.HandleFunc(vmValidatePath, func(w http.ResponseWriter, r *http.Request) {
