-
Notifications
You must be signed in to change notification settings - Fork 277
fix: [NPM] policy translation correctness and limits #4859
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
Isaiah Raya (rayaisaiah)
wants to merge
28
commits into
master
Choose a base branch
from
isaiahraya/npm-translation-hardening
base: master
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
28 commits
Select commit
Hold shift + click to select a range
4639811
fix: [NPM] compile multi-value namespaceSelector NotIn as a single co…
rayaisaiah 1cb58e8
fix: [NPM] scope negation-only namespaceSelector matches to cluster n…
rayaisaiah f691b00
fix: [NPM] bound namespaceSelector flattening
rayaisaiah 68b9c34
fix: [NPM] canonicalize ipBlock CIDRs and stop reporting failed trans…
rayaisaiah affea42
chore: [NPM] address lint findings in the changed files
rayaisaiah a41a3e1
test: [NPM] cover every negation-only namespaceSelector operator and …
rayaisaiah eae6cc9
fix: [NPM] bound generated rules per policy and keep CIDR canonicaliz…
rayaisaiah caa37b3
fix: [NPM] keep the rule budget off the direct-rule path and make two…
rayaisaiah 4390359
fix: [NPM] check the rule budget per port and record translation erro…
rayaisaiah d33f5b8
chore: [NPM] address lint findings in the changed files
rayaisaiah 92249a8
fix: [NPM] make the rule ceiling exact and refuse an except NPM canno…
rayaisaiah 5817c7a
fix: [NPM] bound a rule that lists ports and no peers
rayaisaiah 2bb7202
chore: [NPM] keep the new error messages bounded and conventional
rayaisaiah 54c7fb0
fix: [NPM] let a policy land exactly on the rule ceiling
rayaisaiah 10cb1a7
fix: [NPM] bound the matches a namespaceSelector expands into
rayaisaiah e26f3d0
fix: [NPM] bound a selector that carries only matchLabels
rayaisaiah 63cd697
fix: [NPM] bound a selector across its branches and keep the rule cei…
rayaisaiah 5388807
fix: [NPM] account for default drops at the ACL boundary
rayaisaiah cf44fd5
fix: [NPM] distinguish aggregate namespace membership
rayaisaiah 0344b13
fix: [NPM] retain CIDR error details during translation
rayaisaiah 4ab92bb
fix: [NPM] classify policy errors and namespace debug matches
rayaisaiah bc1278f
fix: [NPM] retain combined peer conditions in v2 diagnostics
rayaisaiah 0ebf283
fix: [NPM] evaluate all v2 namespace conditions consistently
rayaisaiah 3a70439
fix: [NPM] use explicit version mode in peer diagnostics
rayaisaiah 10ec9b6
fix: [NPM] reject invalid exclusions before set programming
rayaisaiah 103ef3e
test: [NPM] share diagnostic label constants
rayaisaiah c645df8
fix: [NPM] preserve invalid-exclusion error classification
rayaisaiah c180e45
fix: [NPM] preserve alternative branches in v2 diagnostics
rayaisaiah 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
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
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
211 changes: 211 additions & 0 deletions
211
npm/pkg/controlplane/controllers/v2/networkPolicyController_retry_test.go
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,211 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/Azure/azure-container-networking/npm/pkg/controlplane/translation" | ||
| dpmocks "github.com/Azure/azure-container-networking/npm/pkg/dataplane/mocks" | ||
| "github.com/Azure/azure-container-networking/npm/util" | ||
| "github.com/golang/mock/gomock" | ||
| "github.com/stretchr/testify/require" | ||
| networkingv1 "k8s.io/api/networking/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/util/workqueue" | ||
| ) | ||
|
|
||
| func newNetPolQueueFixture(t *testing.T, policy *networkingv1.NetworkPolicy, dp *dpmocks.MockGenericDataplane, npmLite bool) *netPolFixture { | ||
| t.Helper() | ||
| f := newNetPolFixture(t) | ||
| f.netPolLister = append(f.netPolLister, policy) | ||
| f.kubeobjects = append(f.kubeobjects, policy) | ||
| f.newNetPolController(nil, dp, npmLite) | ||
| f.netPolController.workqueue.ShutDown() | ||
| // Zero-delay retries make incorrect requeueing observable without sleeps. | ||
| f.netPolController.workqueue = workqueue.NewTypedRateLimitingQueue[any](workqueue.NewTypedItemFastSlowRateLimiter[any](0, 0, 1)) | ||
| t.Cleanup(f.netPolController.workqueue.ShutDown) | ||
| return f | ||
| } | ||
|
|
||
| func TestFullNPMTranslationFailureWaitsForPolicyChange(t *testing.T) { | ||
| oversized := netPolWithCIDR("192.0.2.0/24") | ||
| selector := &metav1.LabelSelector{} | ||
| for i := 0; i < 19; i++ { | ||
| selector.MatchExpressions = append(selector.MatchExpressions, metav1.LabelSelectorRequirement{ | ||
| Key: fmt.Sprintf("key%d", i), Operator: metav1.LabelSelectorOpIn, Values: []string{"a", "b"}, | ||
| }) | ||
| } | ||
| oversized.Spec.Ingress[0].From = []networkingv1.NetworkPolicyPeer{{NamespaceSelector: selector}} | ||
| invalidWithExcept := netPolWithCIDR("192.0.2.0/33") | ||
| invalidWithExcept.Spec.Ingress[0].From[0].IPBlock.Except = []string{"192.0.2.1/32"} | ||
| unknownOperator := netPolWithCIDR("192.0.2.0/24") | ||
| unknownOperator.Spec.Ingress[0].From = []networkingv1.NetworkPolicyPeer{{ | ||
| NamespaceSelector: &metav1.LabelSelector{MatchExpressions: []metav1.LabelSelectorRequirement{{ | ||
| Key: "tenant", Operator: "Unknown", | ||
| }}}, | ||
| }} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| policy *networkingv1.NetworkPolicy | ||
| cause error | ||
| }{ | ||
| {"malformed CIDR", netPolWithCIDR("192.0.2.0/33"), util.ErrInvalidCIDR}, | ||
| {"malformed CIDR with Except", invalidWithExcept, util.ErrInvalidCIDR}, | ||
| {"selector expansion", oversized, translation.ErrTooManyFlattenedSelectors}, | ||
| {"unsupported operator", unknownOperator, translation.ErrUnsupportedMatchExpressionOperator}, | ||
| } | ||
| if !util.IsWindowsDP() { | ||
| tests = append(tests, struct { | ||
| name string | ||
| policy *networkingv1.NetworkPolicy | ||
| cause error | ||
| }{"unsupported family", netPolWithCIDR("2001:db8::/32"), util.ErrUnsupportedIPFamily}) | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| test.policy.ResourceVersion = "1" | ||
| ctrl := gomock.NewController(t) | ||
| dp := dpmocks.NewMockGenericDataplane(ctrl) | ||
| f := newNetPolQueueFixture(t, test.policy, dp, false) | ||
| c := f.netPolController | ||
| key := getKey(test.policy, t) | ||
|
|
||
| _, err := c.syncAddAndUpdateNetPol(test.policy) | ||
| require.ErrorIs(t, err, test.cause) | ||
| require.ErrorIs(t, err, errNetPolTranslationFailure) | ||
| require.ErrorContains(t, err, key) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
|
|
||
| c.addNetworkPolicy(test.policy) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Zero(t, c.workqueue.NumRequeues(key)) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
|
|
||
| c.updateNetworkPolicy(test.policy, test.policy.DeepCopy()) | ||
| require.Zero(t, c.workqueue.Len(), "an informer resync must not repeat the rejection") | ||
|
|
||
| corrected := test.policy.DeepCopy() | ||
| corrected.ResourceVersion = "2" | ||
| corrected.Spec = netPolWithCIDR("10.0.0.0/0").Spec | ||
| require.NoError(t, f.kubeInformer.Networking().V1().NetworkPolicies().Informer().GetIndexer().Update(corrected)) | ||
| dp.EXPECT().UpdatePolicy(gomock.Any()).Return(nil).Times(1) | ||
| c.updateNetworkPolicy(test.policy, corrected) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Equal(t, &corrected.Spec, c.rawNpSpecMap[key]) | ||
| require.Zero(t, c.workqueue.NumRequeues(key)) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestFullNPMDataplaneFailureStillRetries(t *testing.T) { | ||
| policy := netPolWithCIDR("10.0.0.0/0") | ||
| ctrl := gomock.NewController(t) | ||
| dp := dpmocks.NewMockGenericDataplane(ctrl) | ||
| f := newNetPolQueueFixture(t, policy, dp, false) | ||
| c := f.netPolController | ||
| key := getKey(policy, t) | ||
|
|
||
| gomock.InOrder( | ||
| dp.EXPECT().UpdatePolicy(gomock.Any()).Return(fmt.Errorf("programming policy: %w", translation.ErrUnsupportedIPAddress)), | ||
| dp.EXPECT().UpdatePolicy(gomock.Any()).Return(nil), | ||
| ) | ||
| c.addNetworkPolicy(policy) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Equal(t, 1, c.workqueue.NumRequeues(key)) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
|
|
||
| require.True(t, c.processNextWorkItem()) | ||
| require.Zero(t, c.workqueue.NumRequeues(key)) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| require.Equal(t, &policy.Spec, c.rawNpSpecMap[key]) | ||
| } | ||
|
|
||
| func TestFullNPMRejectedUpdateRetainsAppliedPolicyUntilDeletion(t *testing.T) { | ||
| policy := netPolWithCIDR("192.0.2.0/24") | ||
| policy.ResourceVersion = "1" | ||
| ctrl := gomock.NewController(t) | ||
| dp := dpmocks.NewMockGenericDataplane(ctrl) | ||
| f := newNetPolQueueFixture(t, policy, dp, false) | ||
| c := f.netPolController | ||
| key := getKey(policy, t) | ||
|
|
||
| dp.EXPECT().UpdatePolicy(gomock.Any()).Return(nil).Times(1) | ||
| c.addNetworkPolicy(policy) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Equal(t, &policy.Spec, c.rawNpSpecMap[key]) | ||
|
|
||
| rejected := policy.DeepCopy() | ||
| rejected.ResourceVersion = "2" | ||
| rejected.Spec.Ingress[0].From[0].IPBlock.CIDR = "192.0.2.0/33" | ||
| indexer := f.kubeInformer.Networking().V1().NetworkPolicies().Informer().GetIndexer() | ||
| require.NoError(t, indexer.Update(rejected)) | ||
| c.updateNetworkPolicy(policy, rejected) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Equal(t, &policy.Spec, c.rawNpSpecMap[key]) | ||
| require.Zero(t, c.workqueue.NumRequeues(key)) | ||
| require.Zero(t, c.workqueue.Len()) | ||
|
|
||
| require.NoError(t, indexer.Delete(rejected)) | ||
| dp.EXPECT().RemovePolicy(key).Return(nil).Times(1) | ||
| c.deleteNetworkPolicy(rejected) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| } | ||
|
|
||
| func TestFullNPMRejectedCreateCanBeDeleted(t *testing.T) { | ||
| policy := netPolWithCIDR("invalid") | ||
| ctrl := gomock.NewController(t) | ||
| dp := dpmocks.NewMockGenericDataplane(ctrl) | ||
| f := newNetPolQueueFixture(t, policy, dp, false) | ||
| c := f.netPolController | ||
|
|
||
| c.addNetworkPolicy(policy) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
|
|
||
| require.NoError(t, f.kubeInformer.Networking().V1().NetworkPolicies().Informer().GetIndexer().Delete(policy)) | ||
| c.deleteNetworkPolicy(policy) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| } | ||
|
|
||
| func TestLiteTranslationFailureHandlingIsUnchanged(t *testing.T) { | ||
| policy := netPolWithCIDR("invalid") | ||
| ctrl := gomock.NewController(t) | ||
| dp := dpmocks.NewMockGenericDataplane(ctrl) | ||
| f := newNetPolQueueFixture(t, policy, dp, true) | ||
| c := f.netPolController | ||
| key := getKey(policy, t) | ||
|
|
||
| _, err := c.syncAddAndUpdateNetPol(policy) | ||
| if util.IsWindowsDP() { | ||
| require.NoError(t, err, "the legacy Lite direct-address limitation stays suppressed") | ||
| } else { | ||
| require.ErrorIs(t, err, util.ErrInvalidCIDR) | ||
| require.NotErrorIs(t, err, errNetPolTranslationFailure) | ||
| } | ||
|
|
||
| c.addNetworkPolicy(policy) | ||
| require.True(t, c.processNextWorkItem()) | ||
| require.Empty(t, c.rawNpSpecMap) | ||
| if util.IsWindowsDP() { | ||
| require.Zero(t, c.workqueue.NumRequeues(key)) | ||
| require.Zero(t, c.workqueue.Len()) | ||
| } else { | ||
| require.Equal(t, 1, c.workqueue.NumRequeues(key)) | ||
| require.Equal(t, 1, c.workqueue.Len()) | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.