Skip to content

Commit 5326bec

Browse files
fix: modify cloned ingress instead of original ingress (#1282)
1 parent 9d95af7 commit 5326bec

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed

pkg/controller/prune.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ func pruneProhibitedIngress(c *AppGwIngressController, appGw *n.ApplicationGatew
4646
// Mutate the list of Ingresses by removing ones that AGIC should not be creating configuration.
4747
for idx, ingress := range ingressList {
4848
klog.V(5).Infof("Original Ingress[%d] Rules: %+v", idx, ingress.Spec.Rules)
49-
ingressList[idx].Spec.Rules = brownfield.PruneIngressRules(ingress, cbCtx.ProhibitedTargets)
50-
klog.V(5).Infof("Sanitized Ingress[%d] Rules: %+v", idx, ingress.Spec.Rules)
49+
50+
ingressClone := ingressList[idx].DeepCopy()
51+
ingressClone.Spec.Rules = brownfield.PruneIngressRules(ingress, cbCtx.ProhibitedTargets)
52+
ingressList[idx] = ingressClone
53+
54+
klog.V(5).Infof("Sanitized Ingress[%d] Rules: %+v", idx, ingressList[idx].Spec.Rules)
5155
}
5256

5357
return ingressList

pkg/controller/prune_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/annotations"
2020
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/appgw"
21+
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/environment"
2122
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests"
2223
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests/fixtures"
2324
)
@@ -202,4 +203,62 @@ var _ = Describe("prune function tests", func() {
202203
Expect(prunedIngresses).To(ContainElement(ingressValid2))
203204
})
204205
})
206+
207+
Context("ensure pruneProhibitedIngress prunes ingress", func() {
208+
env := environment.GetFakeEnv()
209+
env.EnableBrownfieldDeployment = true
210+
ingress := fixtures.GetIngressWithProhibitedTargetConflict()
211+
cbCtx := &appgw.ConfigBuilderContext{
212+
IngressList: []*networking.Ingress{
213+
ingress,
214+
},
215+
ServiceList: []*v1.Service{
216+
tests.NewServiceFixture(),
217+
},
218+
ProhibitedTargets: fixtures.GetAzureIngressProhibitedTargets(),
219+
DefaultAddressPoolID: to.StringPtr("xx"),
220+
DefaultHTTPSettingsID: to.StringPtr("yy"),
221+
EnvVariables: env,
222+
}
223+
appGw := fixtures.GetAppGateway()
224+
225+
validateOldIngress := func(oldIngress *networking.Ingress) {
226+
// should have two rules
227+
Expect(len(oldIngress.Spec.Rules)).To(Equal(2))
228+
229+
// should have rule 1 with OldHost as host and no paths
230+
Expect(oldIngress.Spec.Rules[0].Host).To(Equal(tests.OtherHost))
231+
Expect(len(oldIngress.Spec.Rules[0].HTTP.Paths)).To(Equal(0))
232+
233+
// should have rule 2 with Host as host and 2 path rules: /foo /fox
234+
Expect(oldIngress.Spec.Rules[1].Host).To(Equal(tests.Host))
235+
Expect(len(oldIngress.Spec.Rules[1].HTTP.Paths)).To(Equal(2))
236+
Expect(oldIngress.Spec.Rules[1].HTTP.Paths[0].Path).To(Equal(fixtures.PathFoo))
237+
Expect(oldIngress.Spec.Rules[1].HTTP.Paths[1].Path).To(Equal(fixtures.PathFox))
238+
}
239+
240+
It("removes the ingress rules without modifying the original ingress", func() {
241+
Expect(len(cbCtx.IngressList)).To(Equal(1))
242+
243+
// Get pointer to the old ingress object
244+
oldIngress := cbCtx.IngressList[0]
245+
246+
// Validate that ingress follows the requirement
247+
validateOldIngress(oldIngress)
248+
249+
// Prune: test.OtherHost and /fox are prohibited
250+
_ = pruneProhibitedIngress(controller, &appGw, cbCtx, cbCtx.IngressList)
251+
252+
// Validate old ingress is the same as before
253+
validateOldIngress(oldIngress)
254+
255+
// Validate new ingress
256+
newIngress := cbCtx.IngressList[0]
257+
258+
Expect(len(newIngress.Spec.Rules)).To(Equal(1), "should have only 1 rule after pruning")
259+
Expect(len(newIngress.Spec.Rules[0].HTTP.Paths)).To(Equal(1), "Rule should have only 1 path rule left")
260+
Expect(oldIngress.Spec.Rules[1].Host).To(Equal(tests.Host), "Host for that path should be tests.Host")
261+
Expect(oldIngress.Spec.Rules[1].HTTP.Paths[0].Path).To(Equal(fixtures.PathFoo), "Path should /foo; /fox should be removed")
262+
})
263+
})
205264
})

pkg/tests/fixtures/ingress.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,52 @@ func GetIngress() *networking.Ingress {
6060
},
6161
}
6262
}
63+
64+
// GetIngressWithProhibitedTargetConflict returns ingress with /foo and /fox as paths
65+
func GetIngressWithProhibitedTargetConflict() *networking.Ingress {
66+
return &networking.Ingress{
67+
Spec: networking.IngressSpec{
68+
Rules: []networking.IngressRule{
69+
{
70+
// Rule with no Paths
71+
Host: tests.OtherHost,
72+
IngressRuleValue: networking.IngressRuleValue{
73+
HTTP: &networking.HTTPIngressRuleValue{},
74+
},
75+
},
76+
{
77+
// Rule with Paths
78+
Host: tests.Host,
79+
IngressRuleValue: networking.IngressRuleValue{
80+
HTTP: &networking.HTTPIngressRuleValue{
81+
Paths: []networking.HTTPIngressPath{
82+
{
83+
Path: PathFoo,
84+
Backend: networking.IngressBackend{
85+
Service: &networking.IngressServiceBackend{
86+
Name: tests.ServiceName,
87+
Port: networking.ServiceBackendPort{
88+
Number: 80,
89+
},
90+
},
91+
},
92+
},
93+
{
94+
Path: PathFox,
95+
Backend: networking.IngressBackend{
96+
Service: &networking.IngressServiceBackend{
97+
Name: tests.ServiceName,
98+
Port: networking.ServiceBackendPort{
99+
Number: 443,
100+
},
101+
},
102+
},
103+
},
104+
},
105+
},
106+
},
107+
},
108+
},
109+
},
110+
}
111+
}

0 commit comments

Comments
 (0)