-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle IPPolicy CRD state transitions in a safer way (#260)
* Handle IPPolicy CRD state transitions in a safer way * Address review feedback Update tests and dry-up adding ip policy rules updates
- Loading branch information
Showing
4 changed files
with
252 additions
and
53 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
3 changes: 3 additions & 0 deletions
3
helm/ingress-controller/templates/crds/ingress.k8s.ngrok.com_ippolicies.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,75 @@ | ||
package controllers | ||
|
||
import ( | ||
"testing" | ||
|
||
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/v1alpha1" | ||
"github.com/ngrok/ngrok-api-go/v5" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func TestIPPolicyDiff(t *testing.T) { | ||
remoteRules := []*ngrok.IPPolicyRule{ | ||
{ID: "1", CIDR: "192.168.1.0/25", Action: IPPolicyRuleActionAllow, Description: "a"}, // 2. Rule changed from allow to deny | ||
{ID: "2", CIDR: "192.168.128.0/25", Action: IPPolicyRuleActionDeny, Description: "aa"}, // 3. Rule changed from deny to allow | ||
{ID: "3", CIDR: "172.16.0.0/16", Action: IPPolicyRuleActionAllow, Description: "aaa"}, // 5. Allow Rule that will no longer exist | ||
{ID: "4", CIDR: "172.17.0.0/16", Action: IPPolicyRuleActionDeny, Description: "aaaa"}, // 5. Deny Rule that will no longer exist | ||
{ID: "5", CIDR: "172.19.0.0/16", Action: IPPolicyRuleActionAllow, Description: "aaaaa"}, // 6. Just changing description | ||
} | ||
changedDescriptionRule := ingressv1alpha1.IPPolicyRule{CIDR: "172.19.0.0/16", Action: IPPolicyRuleActionAllow} | ||
changedDescriptionRule.Description = "b" | ||
|
||
specRules := []ingressv1alpha1.IPPolicyRule{ | ||
{CIDR: "192.168.1.0/25", Action: IPPolicyRuleActionDeny}, // 2. Rule changed from allow to deny | ||
{CIDR: "192.168.128.0/25", Action: IPPolicyRuleActionAllow}, // 3. Rule changed from deny to allow | ||
{CIDR: "10.0.0.0/8", Action: IPPolicyRuleActionDeny}, // 1. New Rule to be denied | ||
{CIDR: "172.18.0.0/16", Action: IPPolicyRuleActionAllow}, // 4. New Rule to be allowed | ||
changedDescriptionRule, // 6. Just changing description | ||
} | ||
|
||
diff := newIPPolicyDiff("test", remoteRules, specRules) | ||
|
||
assert.True(t, diff.Next()) | ||
assert.Empty(t, diff.NeedsDelete()) | ||
assert.Empty(t, diff.NeedsUpdate()) | ||
assert.Equal(t, []*ngrok.IPPolicyRuleCreate{ | ||
{IPPolicyID: "test", CIDR: specRules[2].CIDR, Action: pointer.String(IPPolicyRuleActionDeny)}}, | ||
diff.NeedsCreate(), | ||
) | ||
|
||
assert.True(t, diff.Next()) | ||
assert.Empty(t, diff.NeedsUpdate()) | ||
assert.Equal(t, []*ngrok.IPPolicyRule{remoteRules[0]}, diff.NeedsDelete()) | ||
assert.Equal(t, []*ngrok.IPPolicyRuleCreate{ | ||
{IPPolicyID: "test", CIDR: specRules[0].CIDR, Action: pointer.String(IPPolicyRuleActionDeny)}, | ||
}, diff.NeedsCreate()) | ||
|
||
assert.True(t, diff.Next()) | ||
assert.Empty(t, diff.NeedsUpdate()) | ||
assert.Equal(t, []*ngrok.IPPolicyRule{remoteRules[1]}, diff.NeedsDelete()) | ||
assert.Equal(t, []*ngrok.IPPolicyRuleCreate{ | ||
{IPPolicyID: "test", CIDR: specRules[1].CIDR, Action: pointer.String(IPPolicyRuleActionAllow)}, | ||
}, diff.NeedsCreate()) | ||
|
||
assert.True(t, diff.Next()) | ||
assert.Empty(t, diff.NeedsUpdate()) | ||
assert.Equal(t, []*ngrok.IPPolicyRule{}, diff.NeedsDelete()) | ||
assert.Equal(t, []*ngrok.IPPolicyRuleCreate{ | ||
{IPPolicyID: "test", CIDR: specRules[3].CIDR, Action: pointer.String(IPPolicyRuleActionAllow)}, | ||
}, diff.NeedsCreate()) | ||
|
||
assert.True(t, diff.Next()) | ||
assert.Empty(t, diff.NeedsUpdate()) | ||
assert.Equal(t, []*ngrok.IPPolicyRule{remoteRules[2], remoteRules[3]}, diff.NeedsDelete()) | ||
assert.Equal(t, []*ngrok.IPPolicyRuleCreate{}, diff.NeedsCreate()) | ||
|
||
assert.True(t, diff.Next()) | ||
assert.Empty(t, diff.NeedsDelete()) | ||
assert.Empty(t, diff.NeedsCreate()) | ||
assert.Equal(t, []*ngrok.IPPolicyRuleUpdate{ | ||
{ID: "5", CIDR: pointer.String("172.19.0.0/16"), Description: pointer.String("b"), Metadata: pointer.String("")}, | ||
}, diff.NeedsUpdate()) | ||
|
||
assert.False(t, diff.Next()) | ||
} |