-
Notifications
You must be signed in to change notification settings - Fork 64
✨OPRUN-3873: Add e2e tests for NetworkPolicies #2013
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for olmv1 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
test/e2e/network_policy_test.go
Outdated
}, | ||
{ | ||
Ports: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(9443)}}, | ||
Justification: "Permits Kubernetes API server to reach catalogd's admission webhook for CRD validation, ensuring integrity of catalog resources.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a mutating webhook that sets a label for the metadata.name that can be used by selectors in ClusterExtension objects to choose specific catalogs by name.
Justification: "Permits Kubernetes API server to reach catalogd's admission webhook for CRD validation, ensuring integrity of catalog resources.", | |
Justification: "Permits Kubernetes API server to reach catalogd's mutating admission webhook, ensuring integrity of catalog resources.", |
test/e2e/network_policy_test.go
Outdated
}, | ||
{ | ||
Ports: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(8443)}}, | ||
Justification: "Enables operator-controller to query catalog metadata from catalogd. This is a core function for bundle resolution and operator discovery.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Justification: "Enables operator-controller to query catalog metadata from catalogd. This is a core function for bundle resolution and operator discovery.", | |
Justification: "Enables clients (e.g. operator-controller) to query catalog metadata from catalogd. This is a core function for bundle resolution and operator discovery.", |
test/e2e/network_policy_test.go
Outdated
EgressRules: []EgressRule{ | ||
{ | ||
// Empty Ports and To means allow all egress | ||
Justification: "Permits catalogd to fetch catalog images from various container registries and communicate with the Kubernetes API server for its operational needs.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Justification: "Permits catalogd to fetch catalog images from various container registries and communicate with the Kubernetes API server for its operational needs.", | |
Justification: "Permits catalogd to fetch catalog images from arbitrary container registries and communicate with the Kubernetes API server for its operational needs.", |
test/e2e/network_policy_test.go
Outdated
EgressRules: []EgressRule{ | ||
{ | ||
// Empty Ports and To means allow all egress | ||
Justification: "Enables operator-controller to pull bundle images, connect to catalogd's HTTPS server for metadata, and interact with the Kubernetes API server.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Justification: "Enables operator-controller to pull bundle images, connect to catalogd's HTTPS server for metadata, and interact with the Kubernetes API server.", | |
Justification: "Enables operator-controller to pull bundle images from arbitrary image registries, connect to catalogd's HTTPS server for metadata, and interact with the Kubernetes API server.", |
@joelanford incorporated your suggestions..also fyi had to change the struct definitions a bit because I realized the policies are defined as a single ingress/egress policy with multiple ports, instead of multiple ingress/egress policies, for each NetworkPolicy object. PTAL, thanks! |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2013 +/- ##
==========================================
- Coverage 69.17% 69.16% -0.02%
==========================================
Files 79 79
Lines 7037 7037
==========================================
- Hits 4868 4867 -1
- Misses 1887 1888 +1
Partials 282 282
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
5496cd9
to
ec22c0a
Compare
test/e2e/network_policy_test.go
Outdated
{ | ||
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(8443)}}, | ||
Justification: "Allows Prometheus to scrape metrics from catalogd, which is essential for monitoring its performance and health.", | ||
}, | ||
{ | ||
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(9443)}}, | ||
Justification: "Permits Kubernetes API server to reach catalogd's mutating admission webhook, ensuring integrity of catalog resources.", | ||
}, | ||
{ | ||
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(7443)}}, | ||
Justification: "Enables clients (eg. operator-controller) to query catalog metadata from catalogd, which is a core function for bundle resolution and operator discovery.", | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ | |
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(8443)}}, | |
Justification: "Allows Prometheus to scrape metrics from catalogd, which is essential for monitoring its performance and health.", | |
}, | |
{ | |
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(9443)}}, | |
Justification: "Permits Kubernetes API server to reach catalogd's mutating admission webhook, ensuring integrity of catalog resources.", | |
}, | |
{ | |
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(7443)}}, | |
Justification: "Enables clients (eg. operator-controller) to query catalog metadata from catalogd, which is a core function for bundle resolution and operator discovery.", | |
}, | |
{ | |
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(7443)}}, | |
Justification: "Allows Prometheus to scrape metrics from catalogd, which is essential for monitoring its performance and health.", | |
}, | |
{ | |
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(9443)}}, | |
Justification: "Permits Kubernetes API server to reach catalogd's mutating admission webhook, ensuring integrity of catalog resources.", | |
}, | |
{ | |
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(8443)}}, | |
Justification: "Enables clients (eg. operator-controller) to query catalog metadata from catalogd, which is a core function for bundle resolution and operator discovery.", | |
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a great catch thank you @dtfranz!! Updated the PR, PTAL
test/e2e/network_policy_test.go
Outdated
Justification: "Permits Kubernetes API server to reach catalogd's mutating admission webhook, ensuring integrity of catalog resources.", | ||
}, | ||
{ | ||
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(7443)}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe move the ports to consts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏽 moved them to constants
test/e2e/network_policy_test.go
Outdated
operatorManagerSelector = "control-plane=operator-controller-controller-manager" | ||
) | ||
|
||
type PortWithJustification struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: do these need to be exported? (PortWithJustification, IngressRule, EgressRule, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, un-exported them
test/e2e/network_policy_test.go
Outdated
}, | ||
{ | ||
Port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(9443)}}, | ||
Justification: "Permits Kubernetes API server to reach catalogd's mutating admission webhook, ensuring integrity of catalog resources.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kube apiserver and dns ports could change downstream (or in different installations). We might want a way to configure those =S
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hhhmm really good point.
So I was looking at the manifests downstream: looks like we have the same ports configured for now (see catalogd's network policy and operator-controller's network policy).
We could configure this using a env vars:
# Makefile
export E2E_CATALOGD_METRICS_PORT ?= 7443
export E2E_SERVER_PORT ?= 8443
and then read those env vars in the test, which would enable us to configure these variable in the Makefile downstream (or anywhere else).
But it doesn't look like we have to do that for now?
This comment did make me look into my usage of
const olmv1systemNamespace = olmv1-system
and now I switched to an existing helper function getComponentNamespace
from the metrics_test.go
file, so thank you for that :)
func intOrStrPtr(port int32) *intstr.IntOrString { | ||
val := intstr.FromInt(int(port)) | ||
return &val | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func intOrStrPtr(port int32) *intstr.IntOrString { | |
val := intstr.FromInt(int(port)) | |
return &val | |
} |
We do not need the helper.
We can use k8s.io/apimachinery/pkg/util/intstr (since we import this lib already)
and then something like Port: intstr.FromInt(NUMBER)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@camilamacedo86 I did try that first but fyi Port
is of type *intstr.IntOrString
but intstr.FromInt(Number)
returns a intstr.IntOrString
, and you can't really do a &intstr.FromInt(Number)
. Which is why I decided to store the intstr.IntOrString
returned by FromInt
in a variable, and then return the address of that variable from the helper function, so that that can be used for setting Port
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @anik120
We are using it here:
port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(catalogdMetricsPort)}},
We do not need to create a helper for this one. We can
&intstr.IntOrString{Type: intstr.Int, IntVal: PORT_NUMBER}
Like
port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: &intstr.IntOrString{Type: intstr.Int, IntVal: catalogdMetricsPort}}},
ports: []portWithJustification{ | ||
{ | ||
port: []networkingv1.NetworkPolicyPort{{Protocol: ptr.To(corev1.ProtocolTCP), Port: intOrStrPtr(catalogdMetricsPort)}}, | ||
justification: "Allows Prometheus to scrape metrics from catalogd, which is essential for monitoring its performance and health.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really working?
@dtfranz did you not need to apply the NP to scrape the metrics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure it will work without the additional NP if prometheus lives in another namespace.
@joelanford @perdasilva regarding the discussion about possibly switching this to a unit test, I see the Quickstart target in the Makefile, and ack the discussion about using steps from that target (separated out) to generate the manifests and before starting the unit tests, and then consuming the generated manifests from the unit test. I don't see a natural home for the unit test though. Maybe that's actually a good signal that this should be a e2e test instead. Thoughts? |
Description
This PR introduces a new e2e test to continuously validate the NetworkPolicy objects for the catalogd and operator-controller components.
The new test,
TestNetworkPolicyJustifications
, achieves the following:Documents NetworkPolicies deployed: A registry of all allowed network policies (
allowedNetworkPolicies
) is defined using the RFC: OLMv1 Static Network Policies. Every rule within these policies is explicitly registered in the registry with a clear, written justification of a minimum length. This ensures that every rule's purpose is documented and understood.Prevents Regressions: The test validates that the NetworkPolicy objects deployed in the cluster exactly match the definitions in a registry (
allowedNetworkPolicies
). This prevents undocumented or accidental changes to our network rules.Improves Security Posture: Enforces thoughtful consideration for any change to existing network rules, improving overall security posture. Also ensures that other, unrelated network policies won't inadvertently break existing functionalities.
Reviewer Checklist