Skip to content

Draft - Update Namespaces with labels using policies #6063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: test.kyverno.io/v1alpha1
kind: Test
metadata:
name: assert-policy
spec:
assertions:
- policy: label-namespaces-tenant
exists: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: test.kyverno.io/v1alpha1
kind: Test
metadata:
name: label-namespaces-tenant
spec:
policies:
- policy.yaml
resources:
- resources/namespace-matching.yaml
- resources/namespace-nonmatching.yaml
results:
- kind: Namespace
name: demo-tenant # demo
policy: label-namespaces-tenant
rule: label-tenant-namespaces
result: pass
patchedResource: resources/expected-namespace-matching.yaml
- kind: Namespace
name: random-ns
policy: label-namespaces-tenant
rule: label-tenant-namespaces
result: skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Namespace
metadata:
name: demo-tenant
labels:
product: "openshift"
env: "production"
team: "fin-ops"
cost-center: "team"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: demo-tenant
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: random-ns
5 changes: 5 additions & 0 deletions components/cost-management/policies/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- update_namespace_labels.yaml
- kyverno_rbac.yaml
33 changes: 33 additions & 0 deletions components/cost-management/policies/kyverno_rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kyverno-read-kubearchiveconfig
labels:
rbac.kyverno.io/aggregate-to-admission-controller: "true"
rules:
- apiGroups:
- kubearchive.kubearchive.org
resources:
- kubearchiveconfigs
verbs:
- list
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kyverno-manage-kubearchiveconfig
labels:
rbac.kyverno.io/aggregate-to-background-controller: "true"
rules:
- apiGroups:
- kubearchive.kubearchive.org
resources:
- kubearchiveconfigs
verbs:
- create
- get
- list
- delete
- update
24 changes: 24 additions & 0 deletions components/cost-management/policies/update_namespace_labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: label-namespaces-tenant
spec:
rules:
- name: label-tenant-namespaces
match:
resources:
kinds:
- Namespace
selector:
matchLabels:
konflux-ci.dev/type: tenant
mutate:
patchStrategicMerge:
metadata:
labels:
product: "openshift" # Needs to be configured in the GitOps
env: "production"
team: "konflux-fin-ops" # Needs to be configured in the GitOps
cost-center: "konflux-team" # Needs to be configured in the GitOps
insights_cost_management_optimizations: "true"
Loading