mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 23:46:56 +00:00
Functions for parsing metadata moved to utils. Changed login of mutation webhook according to last changes.
75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package webhooks
|
|
|
|
import (
|
|
types "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
|
"k8s.io/api/admission/v1beta1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
var supportedKinds = [...]string{
|
|
"ConfigMap",
|
|
"CronJob",
|
|
"DaemonSet",
|
|
"Deployment",
|
|
"Endpoint",
|
|
"HorizontalPodAutoscaler",
|
|
"Ingress",
|
|
"Job",
|
|
"LimitRange",
|
|
"Namespace",
|
|
"NetworkPolicy",
|
|
"PersistentVolumeClaim",
|
|
"PodDisruptionBudget",
|
|
"PodTemplate",
|
|
"ResourceQuota",
|
|
"Secret",
|
|
"Service",
|
|
"StatefulSet",
|
|
}
|
|
|
|
func kindIsSupported(kind string) bool {
|
|
for _, k := range supportedKinds {
|
|
if k == kind {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Checks for admission if kind is supported
|
|
func AdmissionIsRequired(request *v1beta1.AdmissionRequest) bool {
|
|
// Here you can make additional hardcoded checks
|
|
return kindIsSupported(request.Kind.Kind)
|
|
}
|
|
|
|
// Checks requests kind, name and labels to fit the policy
|
|
func IsRuleApplicableToRequest(policyResource types.PolicyResource, request *v1beta1.AdmissionRequest) bool {
|
|
if policyResource.Kind != request.Kind.Kind {
|
|
return false
|
|
}
|
|
|
|
if request.Object.Raw != nil {
|
|
meta := parseMetadataFromObject(request.Object.Raw)
|
|
name := parseNameFromMetadata(meta)
|
|
|
|
if policyResource.Name != nil && *policyResource.Name != name {
|
|
return false
|
|
}
|
|
|
|
if policyResource.Selector != nil {
|
|
selector, err := metav1.LabelSelectorAsSelector(policyResource.Selector)
|
|
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
labelMap := parseLabelsFromMetadata(meta)
|
|
|
|
if !selector.Matches(labelMap) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|