2019-03-19 21:32:31 +02:00
|
|
|
package webhooks
|
2019-03-21 15:57:30 +02:00
|
|
|
|
2019-03-19 21:32:31 +02:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
2019-03-21 15:57:30 +02:00
|
|
|
"github.com/nirmata/kube-policy/constants"
|
|
|
|
|
2019-03-19 21:32:31 +02:00
|
|
|
rest "k8s.io/client-go/rest"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
adm "k8s.io/api/admissionregistration/v1beta1"
|
|
|
|
admreg "k8s.io/client-go/kubernetes/typed/admissionregistration/v1beta1"
|
|
|
|
)
|
|
|
|
|
2019-03-21 15:57:30 +02:00
|
|
|
func RegisterMutationWebhook(config *rest.Config) error {
|
2019-03-19 21:32:31 +02:00
|
|
|
registrationClient, err := admreg.NewForConfig(config)
|
|
|
|
if err != nil {
|
2019-03-21 15:57:30 +02:00
|
|
|
return err
|
2019-03-19 21:32:31 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 15:57:30 +02:00
|
|
|
_, err = registrationClient.MutatingWebhookConfigurations().Create(constructWebhookConfig(config))
|
2019-03-19 21:32:31 +02:00
|
|
|
if err != nil {
|
2019-03-21 15:57:30 +02:00
|
|
|
return err
|
2019-03-19 21:32:31 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 15:57:30 +02:00
|
|
|
return nil
|
2019-03-19 21:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func constructWebhookConfig(config *rest.Config) *adm.MutatingWebhookConfiguration {
|
|
|
|
return &adm.MutatingWebhookConfiguration {
|
|
|
|
ObjectMeta: meta.ObjectMeta {
|
2019-03-21 15:57:30 +02:00
|
|
|
Name: constants.WebhookConfigName,
|
|
|
|
Labels: constants.WebhookConfigLabels,
|
2019-03-19 21:32:31 +02:00
|
|
|
},
|
|
|
|
Webhooks: []adm.Webhook {
|
|
|
|
adm.Webhook {
|
2019-03-21 15:57:30 +02:00
|
|
|
Name: constants.MutationWebhookName,
|
2019-03-19 21:32:31 +02:00
|
|
|
ClientConfig: adm.WebhookClientConfig {
|
2019-03-21 16:56:03 +02:00
|
|
|
Service: &adm.ServiceReference {
|
2019-03-21 15:57:30 +02:00
|
|
|
Namespace: constants.WebhookServiceNamespace,
|
|
|
|
Name: constants.WebhookServiceName,
|
|
|
|
Path: &constants.WebhookServicePath,
|
2019-03-19 21:32:31 +02:00
|
|
|
},
|
2019-03-20 12:37:05 +02:00
|
|
|
CABundle: ExtractCA(config),
|
2019-03-19 21:32:31 +02:00
|
|
|
},
|
|
|
|
Rules: []adm.RuleWithOperations {
|
|
|
|
adm.RuleWithOperations {
|
|
|
|
Operations: []adm.OperationType {
|
|
|
|
adm.Create,
|
|
|
|
},
|
|
|
|
Rule: adm.Rule {
|
|
|
|
APIGroups: []string {
|
|
|
|
"*",
|
|
|
|
},
|
|
|
|
APIVersions: []string {
|
|
|
|
"*",
|
|
|
|
},
|
|
|
|
Resources: []string {
|
|
|
|
"*/*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 12:37:05 +02:00
|
|
|
func ExtractCA(config *rest.Config) (result []byte) {
|
|
|
|
fileName := config.TLSClientConfig.CAFile
|
2019-03-19 21:32:31 +02:00
|
|
|
|
2019-03-21 16:56:03 +02:00
|
|
|
if fileName != "" {
|
2019-03-20 12:37:05 +02:00
|
|
|
result, err := ioutil.ReadFile(fileName)
|
|
|
|
|
2019-03-19 21:32:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-20 12:37:05 +02:00
|
|
|
return result
|
|
|
|
} else {
|
|
|
|
return config.TLSClientConfig.CAData
|
2019-03-19 21:32:31 +02:00
|
|
|
}
|
|
|
|
}
|