1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 18:38:40 +00:00

feat: support generating VAPs in case of matching resources in specific namespaces ()

Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Mariam Fahmy 2024-05-08 19:09:47 +08:00 committed by GitHub
parent 84f70ef944
commit 60e347bedb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 65 additions and 17 deletions
pkg/validatingadmissionpolicy
test/conformance/chainsaw/generate-validating-admission-policy/clusterpolicy/standard
generate/cpol-match-resource-in-specific-namespace
skip-generate/cpol-match-resource-in-specific-namespace

View file

@ -115,7 +115,20 @@ func translateResource(discoveryClient dclient.IDiscovery, matchResources *admis
}
matchResources.ResourceRules = *rules
matchResources.NamespaceSelector = res.NamespaceSelector
if len(res.Namespaces) > 0 {
namespaceSelector := &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "kubernetes.io/metadata.name",
Operator: "In",
Values: res.Namespaces,
},
},
}
matchResources.NamespaceSelector = namespaceSelector
} else {
matchResources.NamespaceSelector = res.NamespaceSelector
}
matchResources.ObjectSelector = res.Selector
return nil
}

View file

@ -90,8 +90,8 @@ func CanGenerateVAP(spec *kyvernov1.Spec) (bool, string) {
func checkResources(resource kyvernov1.ResourceDescription) (bool, string) {
var msg string
if len(resource.Namespaces) != 0 || len(resource.Annotations) != 0 {
msg = "skip generating ValidatingAdmissionPolicy: Namespaces / Annotations in resource description is not applicable."
if len(resource.Annotations) != 0 {
msg = "skip generating ValidatingAdmissionPolicy: Annotations in resource description is not applicable."
return false, msg
}
if resource.Name != "" && wildcard.ContainsWildcard(resource.Name) {

View file

@ -30,7 +30,7 @@ func Test_Check_Resources(t *testing.T) {
]
}
`),
expected: false,
expected: true,
},
{
name: "resource-with-annotations",

View file

@ -0,0 +1,35 @@
apiVersion: admissionregistration.k8s.io/v1alpha1
kind: ValidatingAdmissionPolicy
metadata:
labels:
app.kubernetes.io/managed-by: kyverno
name: disallow-host-path-t4
ownerReferences:
- apiVersion: kyverno.io/v1
kind: ClusterPolicy
name: disallow-host-path-t4
spec:
failurePolicy: Fail
matchConstraints:
namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values:
- production
- staging
resourceRules:
- apiGroups:
- apps
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- deployments
validations:
- expression: '!has(object.spec.template.spec.volumes) || object.spec.template.spec.volumes.all(volume,
!has(volume.hostPath))'
message: HostPath volumes are forbidden. The field spec.template.spec.volumes[*].hostPath
must be unset.

View file

@ -4,4 +4,10 @@ metadata:
labels:
app.kubernetes.io/managed-by: kyverno
name: disallow-host-path-t4-binding
spec: {}
ownerReferences:
- apiVersion: kyverno.io/v1
kind: ClusterPolicy
name: disallow-host-path-t4
spec:
policyName: disallow-host-path-t4
validationActions: [Audit, Warn]

View file

@ -1,7 +0,0 @@
apiVersion: admissionregistration.k8s.io/v1alpha1
kind: ValidatingAdmissionPolicy
metadata:
labels:
app.kubernetes.io/managed-by: kyverno
name: disallow-host-path-t4
spec: {}