2020-11-26 16:07:06 -08:00
|
|
|
package webhookconfig
|
2019-10-30 13:39:19 -07:00
|
|
|
|
|
|
|
import (
|
2019-11-13 11:55:16 -08:00
|
|
|
"fmt"
|
2019-10-30 13:39:19 -07:00
|
|
|
"strconv"
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
dclient "github.com/kyverno/kyverno/pkg/dclient"
|
|
|
|
"github.com/kyverno/kyverno/pkg/event"
|
2019-10-30 13:39:19 -07:00
|
|
|
)
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
var deployName string = config.KyvernoDeploymentName
|
|
|
|
var deployNamespace string = config.KyvernoNamespace
|
2019-10-30 13:39:19 -07:00
|
|
|
|
|
|
|
const annCounter string = "kyverno.io/generationCounter"
|
2020-05-17 14:37:05 -07:00
|
|
|
const annWebhookStatus string = "kyverno.io/webhookActive"
|
2019-10-30 13:39:19 -07:00
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
//statusControl controls the webhook status
|
|
|
|
type statusControl struct {
|
2019-11-13 11:55:16 -08:00
|
|
|
client *dclient.Client
|
|
|
|
eventGen event.Interface
|
2020-03-17 11:05:20 -07:00
|
|
|
log logr.Logger
|
2019-10-30 13:39:19 -07:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
//success ...
|
|
|
|
func (vc statusControl) success() error {
|
2019-10-30 13:39:19 -07:00
|
|
|
return vc.setStatus("true")
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
//failure ...
|
|
|
|
func (vc statusControl) failure() error {
|
2019-10-30 13:39:19 -07:00
|
|
|
return vc.setStatus("false")
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
// NewStatusControl creates a new webhook status control
|
|
|
|
func newStatusControl(client *dclient.Client, eventGen event.Interface, log logr.Logger) *statusControl {
|
|
|
|
return &statusControl{
|
2019-11-13 11:55:16 -08:00
|
|
|
client: client,
|
|
|
|
eventGen: eventGen,
|
2020-03-17 11:05:20 -07:00
|
|
|
log: log,
|
2019-10-30 13:39:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:06 -08:00
|
|
|
func (vc statusControl) setStatus(status string) error {
|
2020-05-17 14:37:05 -07:00
|
|
|
logger := vc.log.WithValues("name", deployName, "namespace", deployNamespace)
|
2019-10-30 13:39:19 -07:00
|
|
|
var ann map[string]string
|
|
|
|
var err error
|
2020-08-07 09:47:33 +05:30
|
|
|
deploy, err := vc.client.GetResource("", "Deployment", deployNamespace, deployName)
|
2019-10-30 13:39:19 -07:00
|
|
|
if err != nil {
|
2020-05-17 14:37:05 -07:00
|
|
|
logger.Error(err, "failed to get deployment")
|
2019-10-30 13:39:19 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
ann = deploy.GetAnnotations()
|
|
|
|
if ann == nil {
|
|
|
|
ann = map[string]string{}
|
2020-05-17 14:37:05 -07:00
|
|
|
ann[annWebhookStatus] = status
|
2019-10-30 13:39:19 -07:00
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
|
|
|
deployStatus, ok := ann[annWebhookStatus]
|
2019-10-30 13:39:19 -07:00
|
|
|
if ok {
|
2020-05-17 14:37:05 -07:00
|
|
|
if deployStatus == status {
|
|
|
|
logger.V(4).Info(fmt.Sprintf("annotation %s already set to '%s'", annWebhookStatus, status))
|
2019-10-30 13:39:19 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
// set the status
|
2020-05-17 14:37:05 -07:00
|
|
|
logger.Info("updating deployment annotation", "key", annWebhookStatus, "val", status)
|
|
|
|
ann[annWebhookStatus] = status
|
2019-10-30 13:39:19 -07:00
|
|
|
deploy.SetAnnotations(ann)
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
// update counter
|
2020-08-07 09:47:33 +05:30
|
|
|
_, err = vc.client.UpdateResource("", "Deployment", deployNamespace, deploy, false)
|
2019-10-30 13:39:19 -07:00
|
|
|
if err != nil {
|
2020-05-17 14:37:05 -07:00
|
|
|
logger.Error(err, "failed to update deployment annotation", "key", annWebhookStatus, "val", status)
|
2019-10-30 13:39:19 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-11-13 11:55:16 -08:00
|
|
|
// create event on kyverno deployment
|
|
|
|
createStatusUpdateEvent(status, vc.eventGen)
|
2019-10-30 13:39:19 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:55:16 -08:00
|
|
|
func createStatusUpdateEvent(status string, eventGen event.Interface) {
|
|
|
|
e := event.Info{}
|
|
|
|
e.Kind = "Deployment"
|
2020-07-02 03:20:49 +05:30
|
|
|
e.Namespace = deployNamespace
|
|
|
|
e.Name = deployName
|
2019-11-13 11:55:16 -08:00
|
|
|
e.Reason = "Update"
|
|
|
|
e.Message = fmt.Sprintf("admission control webhook active status changed to %s", status)
|
|
|
|
eventGen.Add(e)
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
//IncrementAnnotation ...
|
2020-11-26 16:07:06 -08:00
|
|
|
func (vc statusControl) IncrementAnnotation() error {
|
2020-03-17 11:05:20 -07:00
|
|
|
logger := vc.log
|
2019-10-30 13:39:19 -07:00
|
|
|
var ann map[string]string
|
|
|
|
var err error
|
2020-08-07 09:47:33 +05:30
|
|
|
deploy, err := vc.client.GetResource("", "Deployment", deployNamespace, deployName)
|
2019-10-30 13:39:19 -07:00
|
|
|
if err != nil {
|
2020-07-02 03:20:49 +05:30
|
|
|
logger.Error(err, "failed to find Kyverno", "deployment", deployName, "namespace", deployNamespace)
|
2019-10-30 13:39:19 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
ann = deploy.GetAnnotations()
|
|
|
|
if ann == nil {
|
|
|
|
ann = map[string]string{}
|
2020-05-17 14:37:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ann[annCounter] == "" {
|
2019-10-30 13:39:19 -07:00
|
|
|
ann[annCounter] = "0"
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
counter, err := strconv.Atoi(ann[annCounter])
|
|
|
|
if err != nil {
|
2020-05-17 14:37:05 -07:00
|
|
|
logger.Error(err, "Failed to parse string", "name", annCounter, "value", ann[annCounter])
|
2019-10-30 13:39:19 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
// increment counter
|
|
|
|
counter++
|
|
|
|
ann[annCounter] = strconv.Itoa(counter)
|
2020-05-17 14:37:05 -07:00
|
|
|
|
|
|
|
logger.V(3).Info("updating webhook test annotation", "key", annCounter, "value", counter, "deployment", deployName, "namespace", deployNamespace)
|
2019-10-30 13:39:19 -07:00
|
|
|
deploy.SetAnnotations(ann)
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
// update counter
|
2020-08-07 09:47:33 +05:30
|
|
|
_, err = vc.client.UpdateResource("", "Deployment", deployNamespace, deploy, false)
|
2019-10-30 13:39:19 -07:00
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
logger.Error(err, fmt.Sprintf("failed to update annotation %s for deployment %s in namespace %s", annCounter, deployName, deployNamespace))
|
2019-10-30 13:39:19 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2019-10-30 13:39:19 -07:00
|
|
|
return nil
|
|
|
|
}
|