-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fea(networkpolicy) test that pods have a matching networkpolicy
- Loading branch information
Showing
12 changed files
with
232 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package score | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
) | ||
|
||
func scorePodHasNetworkPolicy(allNetpols []networkingv1.NetworkPolicy) func(spec corev1.PodTemplateSpec) TestScore { | ||
return func(podSpec corev1.PodTemplateSpec) (score TestScore) { | ||
score.Name = "Pod Has NetworkPolicy" | ||
|
||
hasMatchingEgressNetpol := false | ||
hasMatchingIngressNetpol := false | ||
|
||
for _, netPol := range allNetpols { | ||
matchLabels := netPol.Spec.PodSelector.MatchLabels | ||
|
||
for labelKey, labelVal := range matchLabels { | ||
if podLabelVal, ok := podSpec.Labels[labelKey]; ok && podLabelVal == labelVal { | ||
|
||
for _, policyType := range netPol.Spec.PolicyTypes { | ||
if policyType == networkingv1.PolicyTypeIngress { | ||
hasMatchingIngressNetpol = true | ||
} | ||
if policyType == networkingv1.PolicyTypeEgress { | ||
hasMatchingEgressNetpol = true | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
if hasMatchingEgressNetpol && hasMatchingIngressNetpol { | ||
score.Grade = 10 | ||
} else if hasMatchingEgressNetpol && !hasMatchingIngressNetpol { | ||
score.Grade = 5 | ||
score.Comments = append(score.Comments, "The pod does not have a matching ingress network policy") | ||
} else if hasMatchingIngressNetpol && !hasMatchingEgressNetpol { | ||
score.Grade = 5 | ||
score.Comments = append(score.Comments, "The pod does not have a matching egress network policy") | ||
} else { | ||
score.Grade = 0 | ||
score.Comments = append(score.Comments, "The pod does not have a matching network policy") | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: testapp-netpol | ||
namespace: testspace | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
app: testapp | ||
egress: | ||
- ports: | ||
- port: 53 | ||
protocol: TCP | ||
- port: 53 | ||
protocol: UDP | ||
policyTypes: | ||
- Egress | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod-test-1 | ||
labels: | ||
app: testapp | ||
spec: | ||
containers: | ||
- name: foobar | ||
image: foo/bar:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: testapp-netpol | ||
namespace: testspace | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
app: testapp | ||
ingress: | ||
- ports: | ||
- port: 53 | ||
protocol: TCP | ||
- port: 53 | ||
protocol: UDP | ||
policyTypes: | ||
- Ingress | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod-test-1 | ||
labels: | ||
app: testapp | ||
spec: | ||
containers: | ||
- name: foobar | ||
image: foo/bar:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: testapp-netpol | ||
namespace: testspace | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
app: testapp | ||
egress: | ||
- ports: | ||
- port: 53 | ||
protocol: TCP | ||
- port: 53 | ||
protocol: UDP | ||
to: | ||
- namespaceSelector: | ||
matchLabels: | ||
service: kube-system | ||
policyTypes: | ||
- Ingress | ||
- Egress | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod-test-1 | ||
labels: | ||
app: testapp | ||
spec: | ||
containers: | ||
- name: foobar | ||
image: foo/bar:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: testapp-netpol | ||
namespace: testspace | ||
spec: | ||
podSelector: | ||
app: testapp | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod-test-1 | ||
labels: | ||
app: not-testapp | ||
spec: | ||
containers: | ||
- name: foobar | ||
image: foo/bar:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,5 @@ metadata: | |
spec: | ||
containers: | ||
- name: foobar | ||
resources: | ||
image: foo/bar:123 | ||
imagePullPolicy: Always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,5 @@ metadata: | |
spec: | ||
containers: | ||
- name: foobar | ||
resources: | ||
image: foo/bar:123 | ||
imagePullPolicy: Never |
Oops, something went wrong.