mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 15:37:19 +00:00
85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package webhookconfig
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/golang/glog"
|
|
"github.com/nirmata/kyverno/pkg/config"
|
|
admregapi "k8s.io/api/admissionregistration/v1beta1"
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
func (wrc *WebhookRegistrationClient) contructDebugMutatingWebhookConfig(caData []byte) *admregapi.MutatingWebhookConfiguration {
|
|
url := fmt.Sprintf("https://%s%s", wrc.serverIP, config.MutatingWebhookServicePath)
|
|
glog.V(4).Infof("Debug MutatingWebhookConfig is registered with url %s\n", url)
|
|
|
|
return &admregapi.MutatingWebhookConfiguration{
|
|
ObjectMeta: v1.ObjectMeta{
|
|
Name: config.MutatingWebhookConfigurationDebugName,
|
|
},
|
|
Webhooks: []admregapi.Webhook{
|
|
generateDebugWebhook(
|
|
config.MutatingWebhookName,
|
|
url,
|
|
caData,
|
|
true,
|
|
wrc.timeoutSeconds,
|
|
"*/*",
|
|
"*",
|
|
"*",
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update},
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (wrc *WebhookRegistrationClient) constructMutatingWebhookConfig(caData []byte) *admregapi.MutatingWebhookConfiguration {
|
|
return &admregapi.MutatingWebhookConfiguration{
|
|
ObjectMeta: v1.ObjectMeta{
|
|
Name: config.MutatingWebhookConfigurationName,
|
|
OwnerReferences: []v1.OwnerReference{
|
|
wrc.constructOwner(),
|
|
},
|
|
},
|
|
Webhooks: []admregapi.Webhook{
|
|
generateWebhook(
|
|
config.MutatingWebhookName,
|
|
config.MutatingWebhookServicePath,
|
|
caData,
|
|
false,
|
|
wrc.timeoutSeconds,
|
|
"*/*",
|
|
"*",
|
|
"*",
|
|
[]admregapi.OperationType{admregapi.Create, admregapi.Update},
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
//GetResourceMutatingWebhookConfigName provi
|
|
func (wrc *WebhookRegistrationClient) GetResourceMutatingWebhookConfigName() string {
|
|
if wrc.serverIP != "" {
|
|
return config.MutatingWebhookConfigurationDebugName
|
|
}
|
|
return config.MutatingWebhookConfigurationName
|
|
}
|
|
|
|
//RemoveResourceMutatingWebhookConfiguration removes mutating webhook configuration for all resources
|
|
func (wrc *WebhookRegistrationClient) RemoveResourceMutatingWebhookConfiguration() error {
|
|
|
|
configName := wrc.GetResourceMutatingWebhookConfigName()
|
|
// delete webhook configuration
|
|
err := wrc.client.DeleteResouce(MutatingWebhookConfigurationKind, "", configName, false)
|
|
if errors.IsNotFound(err) {
|
|
glog.V(4).Infof("resource webhook configuration %s does not exits, so not deleting", configName)
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
glog.V(4).Infof("failed to delete resource webhook configuration %s: %v", configName, err)
|
|
return err
|
|
}
|
|
glog.V(4).Infof("deleted resource webhook configuration %s", configName)
|
|
return nil
|
|
}
|