-
Notifications
You must be signed in to change notification settings - Fork 502
MCO-2152: MCO-2153: MCO-2154: Create MCO network policies for pods #6151
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
Open
HarshwardhanPatil07
wants to merge
5
commits into
openshift:main
Choose a base branch
from
HarshwardhanPatil07:mco-network-policies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee17801
Files Created:
HarshwardhanPatil07 f4e3a6a
MCO-2152: Move drift protection tests to MCO-2153 scope
HarshwardhanPatil07 7762f52
MCO-2153: Switch NetworkPolicy management to Server-Side Apply
HarshwardhanPatil07 7de6d83
MCO-2153: Add ValidatingAdmissionPolicy to block network policy delet…
HarshwardhanPatil07 a99acad
MCO-2153: Harden network policy e2e tests against silent regressions
HarshwardhanPatil07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
...fests/machineconfigcontroller/networkpolicy-deletion-guard-validatingadmissionpolicy.yaml
This file contains hidden or 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,21 @@ | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: ValidatingAdmissionPolicy | ||
| metadata: | ||
| name: "networkpolicy-prevent-deletion" | ||
| spec: | ||
| failurePolicy: Fail | ||
| matchConstraints: | ||
| matchPolicy: Equivalent | ||
| namespaceSelector: | ||
| matchLabels: | ||
| kubernetes.io/metadata.name: openshift-machine-config-operator | ||
| objectSelector: {} | ||
| resourceRules: | ||
| - apiGroups: ["networking.k8s.io"] | ||
| apiVersions: ["v1"] | ||
| operations: ["DELETE"] | ||
| resources: ["networkpolicies"] | ||
| scope: "Namespaced" | ||
| validations: | ||
| - expression: "!(oldObject.metadata.name in ['default-deny', 'allow-machine-config-operator', 'allow-machine-config-controller', 'allow-machine-os-builder'])" | ||
| message: "Deletion of MCO-managed NetworkPolicy resources in the openshift-machine-config-operator namespace is not allowed" |
7 changes: 7 additions & 0 deletions
7
...achineconfigcontroller/networkpolicy-deletion-guard-validatingadmissionpolicybinding.yaml
This file contains hidden or 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,7 @@ | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: ValidatingAdmissionPolicyBinding | ||
| metadata: | ||
| name: "networkpolicy-prevent-deletion-binding" | ||
| spec: | ||
| policyName: "networkpolicy-prevent-deletion" | ||
| validationActions: [Deny] |
This file contains hidden or 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,99 @@ | ||
| package operator | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" | ||
| corev1 "k8s.io/api/core/v1" | ||
| networkingv1 "k8s.io/api/networking/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/intstr" | ||
| metav1ac "k8s.io/client-go/applyconfigurations/meta/v1" | ||
| networkingv1ac "k8s.io/client-go/applyconfigurations/networking/v1" | ||
| ) | ||
|
|
||
| const networkPolicyFieldManager = "machine-config-operator" | ||
|
|
||
| func defaultDenyNetworkPolicy(namespace string) *networkingv1ac.NetworkPolicyApplyConfiguration { | ||
| return networkingv1ac.NetworkPolicy("default-deny", namespace). | ||
| WithSpec(networkingv1ac.NetworkPolicySpec(). | ||
| WithPodSelector(metav1ac.LabelSelector()). | ||
| WithPolicyTypes(networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress)) | ||
| } | ||
|
|
||
| func allowMCONetworkPolicy(namespace string) *networkingv1ac.NetworkPolicyApplyConfiguration { | ||
| return networkingv1ac.NetworkPolicy("allow-machine-config-operator", namespace). | ||
| WithSpec(networkingv1ac.NetworkPolicySpec(). | ||
| WithPodSelector(metav1ac.LabelSelector(). | ||
| WithMatchLabels(map[string]string{"k8s-app": "machine-config-operator"})). | ||
| WithIngress(networkingv1ac.NetworkPolicyIngressRule(). | ||
| WithPorts(networkingv1ac.NetworkPolicyPort(). | ||
| WithProtocol(corev1.ProtocolTCP). | ||
| WithPort(intstr.FromInt32(9001)))). | ||
| WithEgress(networkingv1ac.NetworkPolicyEgressRule()). | ||
| WithPolicyTypes(networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress)) | ||
| } | ||
|
|
||
| func allowMCCNetworkPolicy(namespace string) *networkingv1ac.NetworkPolicyApplyConfiguration { | ||
| return networkingv1ac.NetworkPolicy("allow-machine-config-controller", namespace). | ||
| WithSpec(networkingv1ac.NetworkPolicySpec(). | ||
| WithPodSelector(metav1ac.LabelSelector(). | ||
| WithMatchLabels(map[string]string{"k8s-app": "machine-config-controller"})). | ||
| WithIngress(networkingv1ac.NetworkPolicyIngressRule(). | ||
| WithPorts(networkingv1ac.NetworkPolicyPort(). | ||
| WithProtocol(corev1.ProtocolTCP). | ||
| WithPort(intstr.FromInt32(9001)))). | ||
| WithEgress(networkingv1ac.NetworkPolicyEgressRule()). | ||
| WithPolicyTypes(networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress)) | ||
| } | ||
|
|
||
| func allowMOBNetworkPolicy(namespace string) *networkingv1ac.NetworkPolicyApplyConfiguration { | ||
| return networkingv1ac.NetworkPolicy("allow-machine-os-builder", namespace). | ||
| WithSpec(networkingv1ac.NetworkPolicySpec(). | ||
| WithPodSelector(metav1ac.LabelSelector(). | ||
| WithMatchLabels(map[string]string{"k8s-app": "machine-os-builder"})). | ||
| WithIngress(networkingv1ac.NetworkPolicyIngressRule(). | ||
| WithPorts(networkingv1ac.NetworkPolicyPort(). | ||
| WithProtocol(corev1.ProtocolTCP). | ||
| WithPort(intstr.FromInt32(9001)))). | ||
| WithEgress(networkingv1ac.NetworkPolicyEgressRule()). | ||
| WithPolicyTypes(networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress)) | ||
| } | ||
|
|
||
| func (optr *Operator) syncNetworkPolicies(_ *renderConfig, _ *configv1.ClusterOperator) error { | ||
| ctx := context.TODO() | ||
| ns := ctrlcommon.MCONamespace | ||
| applyOpts := metav1.ApplyOptions{FieldManager: networkPolicyFieldManager, Force: true} | ||
|
|
||
| staticPolicies := []*networkingv1ac.NetworkPolicyApplyConfiguration{ | ||
| defaultDenyNetworkPolicy(ns), | ||
| allowMCONetworkPolicy(ns), | ||
| allowMCCNetworkPolicy(ns), | ||
| } | ||
|
|
||
| for _, policy := range staticPolicies { | ||
| if _, err := optr.kubeClient.NetworkingV1().NetworkPolicies(ns).Apply(ctx, policy, applyOpts); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| layeredMCPs, err := optr.getLayeredMachineConfigPools() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(layeredMCPs) > 0 { | ||
| if _, err := optr.kubeClient.NetworkingV1().NetworkPolicies(ns).Apply(ctx, allowMOBNetworkPolicy(ns), applyOpts); err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| err := optr.kubeClient.NetworkingV1().NetworkPolicies(ns).Delete(ctx, "allow-machine-os-builder", metav1.DeleteOptions{}) | ||
| if err != nil && !apierrors.IsNotFound(err) { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,190 @@ | ||
| package operator | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| mcfgv1 "github.com/openshift/api/machineconfiguration/v1" | ||
| fakeclientmachineconfigv1 "github.com/openshift/client-go/machineconfiguration/clientset/versioned/fake" | ||
| mcfginformers "github.com/openshift/client-go/machineconfiguration/informers/externalversions" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| networkingv1 "k8s.io/api/networking/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
| "k8s.io/utils/clock" | ||
| ) | ||
|
|
||
| func testRenderConfig() *renderConfig { | ||
| return &renderConfig{ | ||
| TargetNamespace: ctrlcommon.MCONamespace, | ||
| } | ||
| } | ||
|
|
||
| func testOperatorForNetworkPolicies(t *testing.T, pools []*mcfgv1.MachineConfigPool) *Operator { | ||
| t.Helper() | ||
|
|
||
| kubeClient := fake.NewClientset() | ||
| mcfgClient := fakeclientmachineconfigv1.NewSimpleClientset() | ||
| mcfgInformerFactory := mcfginformers.NewSharedInformerFactory(mcfgClient, 0) | ||
| mcpInformer := mcfgInformerFactory.Machineconfiguration().V1().MachineConfigPools() | ||
| moscInformer := mcfgInformerFactory.Machineconfiguration().V1().MachineOSConfigs() | ||
|
|
||
| for _, pool := range pools { | ||
| require.NoError(t, mcpInformer.Informer().GetIndexer().Add(pool)) | ||
| } | ||
|
|
||
| return &Operator{ | ||
| kubeClient: kubeClient, | ||
| mcpLister: mcpInformer.Lister(), | ||
| moscLister: moscInformer.Lister(), | ||
| libgoRecorder: events.NewInMemoryRecorder("test-operator", clock.RealClock{}), | ||
| } | ||
| } | ||
|
|
||
| func layeredPool(name string) *mcfgv1.MachineConfigPool { | ||
| return &mcfgv1.MachineConfigPool{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| Labels: map[string]string{ | ||
| ctrlcommon.LayeringEnabledPoolLabel: "", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func listNetworkPolicies(t *testing.T, optr *Operator) []networkingv1.NetworkPolicy { | ||
| t.Helper() | ||
| npList, err := optr.kubeClient.NetworkingV1().NetworkPolicies(ctrlcommon.MCONamespace).List(context.TODO(), metav1.ListOptions{}) | ||
| require.NoError(t, err) | ||
| return npList.Items | ||
| } | ||
|
|
||
| func findPolicy(policies []networkingv1.NetworkPolicy, name string) *networkingv1.NetworkPolicy { | ||
| for i := range policies { | ||
| if policies[i].Name == name { | ||
| return &policies[i] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func TestSyncNetworkPolicies_StaticPoliciesCreated(t *testing.T) { | ||
| optr := testOperatorForNetworkPolicies(t, nil) | ||
| config := testRenderConfig() | ||
|
|
||
| err := optr.syncNetworkPolicies(config, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| policies := listNetworkPolicies(t, optr) | ||
| names := make([]string, len(policies)) | ||
| for i, p := range policies { | ||
| names[i] = p.Name | ||
| } | ||
|
|
||
| assert.Contains(t, names, "default-deny", "expected default-deny policy") | ||
| assert.Contains(t, names, "allow-machine-config-operator", "expected allow-machine-config-operator policy") | ||
| assert.Contains(t, names, "allow-machine-config-controller", "expected allow-machine-config-controller policy") | ||
| assert.NotContains(t, names, "allow-machine-os-builder", "MOB policy should not be created without layered pools") | ||
| } | ||
|
|
||
| func TestSyncNetworkPolicies_DefaultDenySpec(t *testing.T) { | ||
| optr := testOperatorForNetworkPolicies(t, nil) | ||
| config := testRenderConfig() | ||
|
|
||
| err := optr.syncNetworkPolicies(config, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| policies := listNetworkPolicies(t, optr) | ||
| denyPolicy := findPolicy(policies, "default-deny") | ||
| require.NotNil(t, denyPolicy, "default-deny policy must exist") | ||
|
|
||
| assert.Empty(t, denyPolicy.Spec.PodSelector.MatchLabels, "default-deny should select all pods with empty selector") | ||
| assert.Contains(t, denyPolicy.Spec.PolicyTypes, networkingv1.PolicyTypeIngress, "default-deny must include Ingress policy type") | ||
| assert.Contains(t, denyPolicy.Spec.PolicyTypes, networkingv1.PolicyTypeEgress, "default-deny must include Egress policy type") | ||
| assert.Empty(t, denyPolicy.Spec.Ingress, "default-deny should have no ingress rules") | ||
| assert.Empty(t, denyPolicy.Spec.Egress, "default-deny should have no egress rules") | ||
| } | ||
|
|
||
| func TestSyncNetworkPolicies_AllowPolicySpecs(t *testing.T) { | ||
| optr := testOperatorForNetworkPolicies(t, nil) | ||
| config := testRenderConfig() | ||
|
|
||
| err := optr.syncNetworkPolicies(config, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| policies := listNetworkPolicies(t, optr) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| labelKey string | ||
| labelVal string | ||
| metricsPort int32 | ||
| }{ | ||
| {"allow-machine-config-operator", "k8s-app", "machine-config-operator", 9001}, | ||
| {"allow-machine-config-controller", "k8s-app", "machine-config-controller", 9001}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| policy := findPolicy(policies, tc.name) | ||
| require.NotNil(t, policy, "policy %s must exist", tc.name) | ||
|
|
||
| assert.Equal(t, tc.labelVal, policy.Spec.PodSelector.MatchLabels[tc.labelKey], | ||
| "podSelector should match %s=%s", tc.labelKey, tc.labelVal) | ||
| assert.Contains(t, policy.Spec.PolicyTypes, networkingv1.PolicyTypeIngress) | ||
| assert.Contains(t, policy.Spec.PolicyTypes, networkingv1.PolicyTypeEgress) | ||
|
|
||
| require.Len(t, policy.Spec.Egress, 1, "should have one egress rule (allow all)") | ||
| assert.Empty(t, policy.Spec.Egress[0].Ports, "egress rule should allow all ports") | ||
| assert.Empty(t, policy.Spec.Egress[0].To, "egress rule should allow all destinations") | ||
|
|
||
| require.Len(t, policy.Spec.Ingress, 1, "should have one ingress rule") | ||
| require.Len(t, policy.Spec.Ingress[0].Ports, 1, "ingress rule should have one port") | ||
| assert.Equal(t, tc.metricsPort, policy.Spec.Ingress[0].Ports[0].Port.IntVal, "ingress port should be metrics port") | ||
| assert.Empty(t, policy.Spec.Ingress[0].From, "ingress should not restrict source (kube-rbac-proxy handles auth)") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSyncNetworkPolicies_MOBPolicyWithLayeredPools(t *testing.T) { | ||
| pools := []*mcfgv1.MachineConfigPool{layeredPool("layered-worker")} | ||
| optr := testOperatorForNetworkPolicies(t, pools) | ||
| config := testRenderConfig() | ||
|
|
||
| err := optr.syncNetworkPolicies(config, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| policies := listNetworkPolicies(t, optr) | ||
| mobPolicy := findPolicy(policies, "allow-machine-os-builder") | ||
| require.NotNil(t, mobPolicy, "MOB policy should be created when layered pools exist") | ||
|
|
||
| assert.Equal(t, "machine-os-builder", mobPolicy.Spec.PodSelector.MatchLabels["k8s-app"]) | ||
| require.Len(t, mobPolicy.Spec.Egress, 1, "MOB should have one egress rule (allow all)") | ||
| require.Len(t, mobPolicy.Spec.Ingress, 1, "MOB should have one ingress rule") | ||
| } | ||
|
|
||
| func TestSyncNetworkPolicies_MOBPolicyDeletedWithoutLayeredPools(t *testing.T) { | ||
| pools := []*mcfgv1.MachineConfigPool{layeredPool("layered-worker")} | ||
| optr := testOperatorForNetworkPolicies(t, pools) | ||
| config := testRenderConfig() | ||
|
|
||
| err := optr.syncNetworkPolicies(config, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| policies := listNetworkPolicies(t, optr) | ||
| require.NotNil(t, findPolicy(policies, "allow-machine-os-builder"), "MOB policy should exist initially") | ||
|
|
||
| optr.mcpLister = testOperatorForNetworkPolicies(t, nil).mcpLister | ||
|
|
||
| err = optr.syncNetworkPolicies(config, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| policies = listNetworkPolicies(t, optr) | ||
| assert.Nil(t, findPolicy(policies, "allow-machine-os-builder"), "MOB policy should be deleted when no layered pools exist") | ||
| assert.NotNil(t, findPolicy(policies, "default-deny"), "static policies should still exist") | ||
| assert.NotNil(t, findPolicy(policies, "allow-machine-config-operator"), "static policies should still exist") | ||
| assert.NotNil(t, findPolicy(policies, "allow-machine-config-controller"), "static policies should still exist") | ||
| } | ||
|
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 curious if there's a downside to always having the MOSB network policies? What happens if we have all the network policies always in the cluster?
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 think it won't affect the functionality just that it will not be used if we have that condition. Happy to change if you think always on would be simpler