Skip to content

Commit

Permalink
Disallow LoadBalancer, NodePort and ExternalName services for PAAS
Browse files Browse the repository at this point in the history
  • Loading branch information
treydock committed May 26, 2024
1 parent 2b0dd9c commit 6558910
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 103 deletions.
25 changes: 0 additions & 25 deletions charts/kyverno-policies/templates/no-loadbalancers.yaml

This file was deleted.

59 changes: 59 additions & 0 deletions charts/kyverno-policies/templates/service-types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# https://github.com/kyverno/policies/blob/main/other/restrict_loadbalancer.yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: service-types
spec:
background: true
validationFailureAction: Enforce
rules:
- name: no-load-balancer
match:
any:
- resources:
kinds:
- Service
exclude:
any:
- resources:
namespaces:
- ingress-nginx
validate:
message: "Service of type LoadBalancer is not allowed."
pattern:
spec:
type: "!LoadBalancer"
- name: no-external-name
match:
any:
- resources:
kinds:
- Service
namespaceSelector:
matchExpressions:
- key: osc.edu/role
operator: In
values:
- paas
validate:
message: "Service of type ExternalName is not allowed."
pattern:
spec:
type: "!ExternalName"
- name: no-node-port
match:
any:
- resources:
kinds:
- Service
namespaceSelector:
matchExpressions:
- key: osc.edu/role
operator: In
values:
- paas
validate:
message: "Service of type NodePort is not allowed."
pattern:
spec:
type: "!NodePort"
28 changes: 0 additions & 28 deletions tests/kyverno-policies/no-loadbalancers/kyverno-test.yaml

This file was deleted.

50 changes: 0 additions & 50 deletions tests/kyverno-policies/no-loadbalancers/resources.yaml

This file was deleted.

65 changes: 65 additions & 0 deletions tests/kyverno-policies/service-types/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: service-types
policies:
- policy.yaml
resources:
- resources.yaml
variables: variables.yaml
results:
- policy: service-types
rule: no-load-balancer
resources:
- ingress-nginx/load-balancer-skip
kind: Service
result: skip
- policy: service-types
rule: no-load-balancer
resources:
- test/load-balancer-pass
kind: Service
result: pass
- policy: service-types
rule: no-load-balancer
resources:
- test/load-balancer-fail
kind: Service
result: fail
- policy: service-types
rule: no-external-name
resources:
- webservice/external-name-skip
kind: Service
result: skip
- policy: service-types
rule: no-external-name
resources:
- paas/external-name-pass
kind: Service
result: pass
- policy: service-types
rule: no-external-name
resources:
- paas/external-name-fail
kind: Service
result: fail
- policy: service-types
rule: no-node-port
resources:
- webservice/node-port-skip
kind: Service
result: skip
- policy: service-types
rule: no-node-port
resources:
- paas/node-port-pass
kind: Service
result: pass
- policy: service-types
rule: no-node-port
resources:
- paas/node-port-fail
kind: Service
result: fail
134 changes: 134 additions & 0 deletions tests/kyverno-policies/service-types/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
apiVersion: v1
kind: Service
metadata:
name: load-balancer-skip
namespace: ingress-nginx
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
name: load-balancer-pass
namespace: test
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: load-balancer-fail
namespace: test
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
name: external-name-skip
namespace: webservice
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: ExternalName
---
apiVersion: v1
kind: Service
metadata:
name: external-name-pass
namespace: paas
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: external-name-fail
namespace: paas
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: ExternalName
---
apiVersion: v1
kind: Service
metadata:
name: node-port-skip
namespace: webservice
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: node-port-pass
namespace: paas
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: node-port-fail
namespace: paas
spec:
ports:
- name: http-port
port: 80
protocol: TCP
targetPort: http-port
selector:
app: web
type: NodePort
11 changes: 11 additions & 0 deletions tests/kyverno-policies/service-types/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Values
metadata:
name: values
namespaceSelector:
- name: webservice
labels:
osc.edu/role: webservice
- name: paas
labels:
osc.edu/role: paas

0 comments on commit 6558910

Please sign in to comment.