Skip to content

Commit

Permalink
fix(admission server): improve reconcile() error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
  • Loading branch information
wdhif committed Sep 5, 2024
1 parent 749d651 commit 3aa0e8c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
52 changes: 28 additions & 24 deletions pkg/clusteragent/admission/controllers/webhook/controller_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,38 +188,42 @@ func (c *ControllerV1) reconcile() error {
return err
}

validatingWebhook, err := c.validatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Validating Webhook %s was not found, creating it", c.config.getWebhookName())
err := c.createValidatingWebhook(secret)
if c.config.mutationEnabled {
mutatingWebhook, err := c.mutatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Mutating Webhook %s was not found, creating it", c.config.getWebhookName())
err := c.createMutatingWebhook(secret)
if err != nil {
_ = log.Errorf("Failed to create Mutating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("Mutating Webhook %s was found, updating it", c.config.getWebhookName())
err := c.updateMutatingWebhook(secret, mutatingWebhook)
if err != nil {
_ = log.Errorf("Failed to create Validating Webhook %s: %v", c.config.getWebhookName(), err)
_ = log.Errorf("Failed to update Mutating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("Validating Webhook %s was found, updating it", c.config.getWebhookName())
err := c.updateValidatingWebhook(secret, validatingWebhook)
if err != nil {
_ = log.Errorf("Failed to update Validating Webhook %s: %v", c.config.getWebhookName(), err)
}
}

mutatingWebhook, err := c.mutatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Mutating Webhook %s was not found, creating it", c.config.getWebhookName())
err := c.createMutatingWebhook(secret)
if c.config.validationEnabled {
validatingWebhook, err := c.validatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Validating Webhook %s was not found, creating it", c.config.getWebhookName())
err := c.createValidatingWebhook(secret)
if err != nil {
_ = log.Errorf("Failed to create Validating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("Validating Webhook %s was found, updating it", c.config.getWebhookName())
err := c.updateValidatingWebhook(secret, validatingWebhook)
if err != nil {
_ = log.Errorf("Failed to create Mutating Webhook %s: %v", c.config.getWebhookName(), err)
_ = log.Errorf("Failed to update Validating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("Mutating Webhook %s was found, updating it", c.config.getWebhookName())
err := c.updateMutatingWebhook(secret, mutatingWebhook)
if err != nil {
_ = log.Errorf("Failed to update Mutating Webhook %s: %v", c.config.getWebhookName(), err)
}
}

return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,45 +183,47 @@ func (c *ControllerV1beta1) handleWebhookUpdate(oldObj, newObj interface{}) {

// reconcile creates/updates the webhook object on new events.
func (c *ControllerV1beta1) reconcile() error {
var err error

secret, err := c.getSecret()
if err != nil {
return err
}

validatingWebhook, err := c.validatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Webhook %s was not found, creating it", c.config.getWebhookName())
err = c.createValidatingWebhook(secret)
if c.config.mutationEnabled {
mutatingWebhook, err := c.mutatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Webhook %s was not found, creating it", c.config.getWebhookName())
err = c.createMutatingWebhook(secret)
if err != nil {
_ = log.Errorf("Failed to create Mutating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("The Webhook %s was found, updating it", c.config.getWebhookName())
err = c.updateMutatingWebhook(secret, mutatingWebhook)
if err != nil {
_ = log.Errorf("Failed to create Validating Webhook %s: %v", c.config.getWebhookName(), err)
_ = log.Errorf("Failed to update Mutating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("The Webhook %s was found, updating it", c.config.getWebhookName())
err = c.updateValidatingWebhook(secret, validatingWebhook)
if err != nil {
_ = log.Errorf("Failed to update Validating Webhook %s: %v", c.config.getWebhookName(), err)
}
}

mutatingWebhook, err := c.mutatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Webhook %s was not found, creating it", c.config.getWebhookName())
err = c.createMutatingWebhook(secret)
if c.config.validationEnabled {
validatingWebhook, err := c.validatingWebhooksLister.Get(c.config.getWebhookName())
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Webhook %s was not found, creating it", c.config.getWebhookName())
err = c.createValidatingWebhook(secret)
if err != nil {
_ = log.Errorf("Failed to create Validating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("The Webhook %s was found, updating it", c.config.getWebhookName())
err = c.updateValidatingWebhook(secret, validatingWebhook)
if err != nil {
_ = log.Errorf("Failed to create Mutating Webhook %s: %v", c.config.getWebhookName(), err)
_ = log.Errorf("Failed to update Validating Webhook %s: %v", c.config.getWebhookName(), err)
}
}
} else {
log.Debugf("The Webhook %s was found, updating it", c.config.getWebhookName())
err = c.updateMutatingWebhook(secret, mutatingWebhook)
if err != nil {
_ = log.Errorf("Failed to update Mutating Webhook %s: %v", c.config.getWebhookName(), err)
}
}

return err
Expand Down

0 comments on commit 3aa0e8c

Please sign in to comment.