Skip to content

Commit

Permalink
Improved PAAS monitoring handling
Browse files Browse the repository at this point in the history
* Rely on osc.edu/role label for service, ingress and pod
  • Loading branch information
treydock committed Apr 3, 2024
1 parent 91864c3 commit d1774b8
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 88 deletions.
2 changes: 1 addition & 1 deletion charts/kyverno-policies/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: kyverno-policies
description: OSC Kyverno policies deployment
type: application
version: 0.26.0
version: 0.27.0
appVersion: "v1.11.4"
maintainers:
- name: treydock
Expand Down
26 changes: 26 additions & 0 deletions charts/kyverno-policies/templates/add-annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-annotations
spec:
validationFailureAction: Enforce
background: true
rules:
- name: paas-disable-scrape
match:
any:
- resources:
kinds:
- Pod
- Service
namespaceSelector:
matchExpressions:
- key: osc.edu/role
operator: In
values:
- paas
mutate:
patchStrategicMerge:
metadata:
annotations:
prometheus.io/scrape: 'false'
4 changes: 3 additions & 1 deletion charts/kyverno-policies/templates/add-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ spec:
- resources:
kinds:
- Pod
- Service
- Ingress
namespaceSelector:
matchExpressions:
- key: osc.edu/role
Expand All @@ -22,4 +24,4 @@ spec:
patchStrategicMerge:
metadata:
labels:
role: paas
osc.edu/role: paas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: pod-role-validation
name: role-validation
spec:
background: false
validationFailureAction: Enforce
Expand All @@ -12,6 +12,8 @@ spec:
- resources:
kinds:
- Pod
- Service
- Ingress
namespaceSelector:
matchExpressions:
- key: osc.edu/role
Expand All @@ -23,4 +25,4 @@ spec:
pattern:
metadata:
labels:
role: "paas"
osc.edu/role: "paas"
2 changes: 1 addition & 1 deletion charts/prometheus/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: prometheus
description: OSC Prometheus deployment
type: application
version: 0.18.3
version: 0.18.4
appVersion: "v2.44.0"
maintainers:
- name: treydock
Expand Down
15 changes: 6 additions & 9 deletions charts/prometheus/templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ data:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_match_metrics,__meta_kubernetes_service_port_name]
action: drop
regex: 'true;.*[^metrics].*'
- source_labels: [__meta_kubernetes_service_label_role]
regex: 'paas'
action: drop
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
Expand Down Expand Up @@ -190,9 +187,6 @@ data:
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_role]
action: drop
regex: 'paas'
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: 'true'
Expand Down Expand Up @@ -280,6 +274,9 @@ data:
kubernetes_sd_configs:
- role: ingress
relabel_configs:
- source_labels: [__meta_kubernetes_ingress_annotation_prometheus_io_probe_skip]
action: drop
regex: 'true'
- source_labels: [__meta_kubernetes_ingress_scheme]
action: keep
regex: https
Expand Down Expand Up @@ -321,11 +318,11 @@ data:
target_label: receiver
- regex: "label_receiver"
action: labeldrop
- source_labels: [label_role]
- source_labels: [label_osc_edu_role]
regex: '(.+)'
replacement: '$1'
target_label: role
- regex: "label_role"
target_label: osc_edu_role
- regex: "label_osc_edu_role"
action: labeldrop
- job_name: kube-state-metrics-telemetry
metrics_path: /metrics
Expand Down
29 changes: 29 additions & 0 deletions tests/kyverno-policies/add-annotations/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: add-annotations
policies:
- policy.yaml
resources:
- resources.yaml
variables: variables.yaml
results:
- policy: add-annotations
rule: paas-disable-scrape
resources:
- test-paas-pod
patchedResource: test-paas-pod-mutated.yaml
kind: Pod
result: pass
- policy: add-annotations
rule: paas-disable-scrape
resources:
- test-paas-service
patchedResource: test-paas-service-mutated.yaml
kind: Service
result: pass
- policy: add-annotations
rule: paas-disable-scrape
resources:
- test-skip
- test-skip-webservice
kind: Pod
result: skip
45 changes: 45 additions & 0 deletions tests/kyverno-policies/add-annotations/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
apiVersion: v1
kind: Pod
metadata:
name: test-paas-pod
namespace: paas
spec:
containers:
- name: nginx
image: nginx:latest
---
apiVersion: v1
kind: Service
metadata:
name: test-paas-service
namespace: paas
spec:
ports:
- name: test
port: 8080
protocol: TCP
targetPort: http
selector:
app: test
type: ClusterIP
---
apiVersion: v1
kind: Pod
metadata:
name: test-skip
namespace: user-test
spec:
containers:
- name: nginx
image: nginx:latest
---
apiVersion: v1
kind: Pod
metadata:
name: test-skip-webservice
namespace: webservice
spec:
containers:
- name: nginx
image: nginx:latest
12 changes: 12 additions & 0 deletions tests/kyverno-policies/add-annotations/test-paas-pod-mutated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: v1
kind: Pod
metadata:
name: test-paas-pod
namespace: paas
annotations:
prometheus.io/scrape: 'false'
spec:
containers:
- name: nginx
image: nginx:latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
apiVersion: v1
kind: Service
metadata:
name: test-paas-service
namespace: paas
annotations:
prometheus.io/scrape: 'false'
spec:
ports:
- name: test
port: 8080
protocol: TCP
targetPort: http
selector:
app: test
type: ClusterIP
2 changes: 1 addition & 1 deletion tests/kyverno-policies/add-role/test-paas-mutated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
name: test-paas
namespace: paas
labels:
role: paas
osc.edu/role: paas
spec:
containers:
- name: nginx
Expand Down
29 changes: 0 additions & 29 deletions tests/kyverno-policies/pod-role-validation/kyverno-test.yaml

This file was deleted.

44 changes: 0 additions & 44 deletions tests/kyverno-policies/pod-role-validation/resources.yaml

This file was deleted.

51 changes: 51 additions & 0 deletions tests/kyverno-policies/role-validation/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
name: role-validation
policies:
- policy.yaml
resources:
- resources.yaml
variables: variables.yaml
results:
- policy: role-validation
rule: paas-require-role
resources:
- test-skip
- test-skip-webservice
kind: Pod
result: skip
- policy: role-validation
rule: paas-require-role
resources:
- test-pass-pod
kind: Pod
result: pass
- policy: role-validation
rule: paas-require-role
resources:
- test-pass-service
kind: Service
result: pass
- policy: role-validation
rule: paas-require-role
resources:
- test-pass-ingress
kind: Ingress
result: pass
- policy: role-validation
rule: paas-require-role
resources:
- test-fail-pod
kind: Pod
result: fail
- policy: role-validation
rule: paas-require-role
resources:
- test-fail-service
kind: Service
result: fail
- policy: role-validation
rule: paas-require-role
resources:
- test-fail-ingress
kind: Ingress
result: fail
Loading

0 comments on commit d1774b8

Please sign in to comment.