2022-10-12 08:52:42 +02:00
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2023-09-19 14:06:53 +02:00
|
|
|
"cmp"
|
|
|
|
"slices"
|
2022-10-27 15:35:32 +05:30
|
|
|
"strings"
|
|
|
|
|
2023-07-06 10:00:36 +02:00
|
|
|
"github.com/kyverno/kyverno/api/kyverno"
|
2022-10-12 08:52:42 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
|
|
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-11-07 17:05:56 +01:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-10-12 08:52:42 +02:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
|
|
)
|
|
|
|
|
|
|
|
// webhook is the instance that aggregates the GVK of existing policies
|
|
|
|
// based on kind, failurePolicy and webhookTimeout
|
|
|
|
type webhook struct {
|
|
|
|
maxWebhookTimeout int32
|
|
|
|
failurePolicy admissionregistrationv1.FailurePolicyType
|
2023-03-10 14:24:55 +01:00
|
|
|
rules map[schema.GroupVersion]sets.Set[string]
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWebhook(timeout int32, failurePolicy admissionregistrationv1.FailurePolicyType) *webhook {
|
|
|
|
return &webhook{
|
|
|
|
maxWebhookTimeout: timeout,
|
|
|
|
failurePolicy: failurePolicy,
|
2023-03-10 14:24:55 +01:00
|
|
|
rules: map[schema.GroupVersion]sets.Set[string]{},
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:05:56 +01:00
|
|
|
func (wh *webhook) buildRulesWithOperations(ops ...admissionregistrationv1.OperationType) []admissionregistrationv1.RuleWithOperations {
|
|
|
|
var rules []admissionregistrationv1.RuleWithOperations
|
2023-03-10 14:24:55 +01:00
|
|
|
for gv, resources := range wh.rules {
|
|
|
|
// if we have pods, we add pods/ephemeralcontainers by default
|
2023-03-13 10:27:49 +01:00
|
|
|
if (gv.Group == "" || gv.Group == "*") && (gv.Version == "v1" || gv.Version == "*") && (resources.Has("pods") || resources.Has("*")) {
|
2023-01-06 19:10:35 +05:30
|
|
|
resources.Insert("pods/ephemeralcontainers")
|
|
|
|
}
|
2022-11-07 17:05:56 +01:00
|
|
|
rules = append(rules, admissionregistrationv1.RuleWithOperations{
|
|
|
|
Rule: admissionregistrationv1.Rule{
|
2023-03-10 14:24:55 +01:00
|
|
|
APIGroups: []string{gv.Group},
|
|
|
|
APIVersions: []string{gv.Version},
|
2022-12-21 23:33:51 +01:00
|
|
|
Resources: sets.List(resources),
|
2022-11-07 17:05:56 +01:00
|
|
|
},
|
|
|
|
Operations: ops,
|
|
|
|
})
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
2023-09-14 23:46:47 +02:00
|
|
|
less := func(a []string, b []string) (int, bool) {
|
2023-09-19 14:06:53 +02:00
|
|
|
if x := cmp.Compare(len(a), len(b)); x != 0 {
|
2023-09-14 23:46:47 +02:00
|
|
|
return x, true
|
2022-11-07 17:05:56 +01:00
|
|
|
}
|
|
|
|
for i := range a {
|
2023-09-19 14:06:53 +02:00
|
|
|
if x := cmp.Compare(a[i], b[i]); x != 0 {
|
2023-09-14 23:46:47 +02:00
|
|
|
return x, true
|
2022-11-07 17:05:56 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-14 23:46:47 +02:00
|
|
|
return 0, false
|
2022-11-07 17:05:56 +01:00
|
|
|
}
|
2023-09-14 23:46:47 +02:00
|
|
|
slices.SortFunc(rules, func(a admissionregistrationv1.RuleWithOperations, b admissionregistrationv1.RuleWithOperations) int {
|
|
|
|
if x, match := less(a.APIGroups, b.APIGroups); match {
|
|
|
|
return x
|
2022-11-07 17:05:56 +01:00
|
|
|
}
|
2023-09-14 23:46:47 +02:00
|
|
|
if x, match := less(a.APIVersions, b.APIVersions); match {
|
|
|
|
return x
|
2022-11-07 17:05:56 +01:00
|
|
|
}
|
2023-09-14 23:46:47 +02:00
|
|
|
if x, match := less(a.Resources, b.Resources); match {
|
|
|
|
return x
|
2022-11-07 17:05:56 +01:00
|
|
|
}
|
2023-09-14 23:46:47 +02:00
|
|
|
return 0
|
2022-11-07 17:05:56 +01:00
|
|
|
})
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:28:00 +01:00
|
|
|
func (wh *webhook) set(gvrs schema.GroupVersionResource) {
|
2023-03-13 15:44:39 +01:00
|
|
|
gv := gvrs.GroupVersion()
|
2023-03-10 14:24:55 +01:00
|
|
|
resources := wh.rules[gv]
|
|
|
|
if resources == nil {
|
2023-03-21 14:28:00 +01:00
|
|
|
wh.rules[gv] = sets.New(gvrs.Resource)
|
2023-03-10 14:24:55 +01:00
|
|
|
} else {
|
2023-03-21 14:28:00 +01:00
|
|
|
resources.Insert(gvrs.Resource)
|
2023-03-10 14:24:55 +01:00
|
|
|
}
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wh *webhook) isEmpty() bool {
|
2022-11-07 17:05:56 +01:00
|
|
|
return len(wh.rules) == 0
|
2022-10-12 08:52:42 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 14:17:37 +01:00
|
|
|
func objectMeta(name string, annotations map[string]string, owner ...metav1.OwnerReference) metav1.ObjectMeta {
|
2022-10-12 08:52:42 +02:00
|
|
|
return metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Labels: map[string]string{
|
2023-07-31 16:51:23 +02:00
|
|
|
kyverno.LabelWebhookManagedBy: kyverno.ValueKyvernoApp,
|
2022-10-12 08:52:42 +02:00
|
|
|
},
|
2023-03-15 14:17:37 +01:00
|
|
|
Annotations: annotations,
|
2022-10-12 08:52:42 +02:00
|
|
|
OwnerReferences: owner,
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 15:35:32 +05:30
|
|
|
|
|
|
|
func setRuleCount(rules []kyvernov1.Rule, status *kyvernov1.PolicyStatus) {
|
|
|
|
validateCount, generateCount, mutateCount, verifyImagesCount := 0, 0, 0, 0
|
|
|
|
for _, rule := range rules {
|
|
|
|
if !strings.HasPrefix(rule.Name, "autogen-") {
|
|
|
|
if rule.HasGenerate() {
|
|
|
|
generateCount += 1
|
|
|
|
}
|
|
|
|
if rule.HasValidate() {
|
|
|
|
validateCount += 1
|
|
|
|
}
|
|
|
|
if rule.HasMutate() {
|
|
|
|
mutateCount += 1
|
|
|
|
}
|
|
|
|
if rule.HasVerifyImages() {
|
|
|
|
verifyImagesCount += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.RuleCount.Validate = validateCount
|
|
|
|
status.RuleCount.Generate = generateCount
|
|
|
|
status.RuleCount.Mutate = mutateCount
|
|
|
|
status.RuleCount.VerifyImages = verifyImagesCount
|
|
|
|
}
|
2023-03-29 09:02:52 +02:00
|
|
|
|
|
|
|
func capTimeout(maxWebhookTimeout int32) int32 {
|
|
|
|
if maxWebhookTimeout > 30 {
|
|
|
|
return 30
|
|
|
|
}
|
|
|
|
return maxWebhookTimeout
|
|
|
|
}
|