Skip to content

Commit fb66cd3

Browse files
authored
Merge pull request #681 from o11n/preservePredicates
Skipper: preserve Predicates
2 parents ce69a18 + e7da8c3 commit fb66cd3

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

pkg/router/skipper.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
"github.com/google/go-cmp/cmp"
910
"go.uber.org/zap"
@@ -176,7 +177,8 @@ func (skp *SkipperRouter) SetRoutes(canary *flaggerv1.Canary, primaryWeight, can
176177

177178
// Disable the canary-ingress route after the canary process
178179
if canaryWeight == 0 {
179-
iClone.Annotations[skipperpredicateAnnotationKey] = canaryRouteDisable
180+
// ensuring False() is at first place
181+
iClone.Annotations[skipperpredicateAnnotationKey] = insertPredicate(iClone.Annotations[skipperpredicateAnnotationKey], canaryRouteDisable)
180182
}
181183

182184
_, err = skp.kubeClient.NetworkingV1beta1().Ingresses(canary.Namespace).Update(
@@ -212,7 +214,7 @@ func (skp *SkipperRouter) makeAnnotations(annotations map[string]string, backend
212214
}
213215
annotations[skipperBackendWeightsAnnotationKey] = string(b)
214216
// adding more weight to canary route solves traffic bypassing through apexIngress
215-
annotations[skipperpredicateAnnotationKey] = canaryRouteWeight
217+
annotations[skipperpredicateAnnotationKey] = insertPredicate(annotations[skipperpredicateAnnotationKey], canaryRouteWeight)
216218

217219
return annotations
218220
}
@@ -233,3 +235,19 @@ func (skp *SkipperRouter) backendWeights(annotation map[string]string) (backendW
233235
func (skp *SkipperRouter) getIngressNames(name string) (apexName, canaryName string) {
234236
return name, fmt.Sprintf(canaryPatternf, name)
235237
}
238+
239+
func insertPredicate(raw, insert string) string {
240+
// ensuring it at first place
241+
predicates := []string{insert}
242+
for _, x := range strings.Split(raw, "&&") {
243+
predicate := strings.TrimSpace(x)
244+
// dropping conflicting predicates
245+
if predicate == "" ||
246+
predicate == canaryRouteWeight ||
247+
predicate == canaryRouteDisable {
248+
continue
249+
}
250+
predicates = append(predicates, predicate)
251+
}
252+
return strings.Join(predicates, " && ")
253+
}

pkg/router/skipper_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,49 @@ func TestSkipperRouter_GetSetRoutes(t *testing.T) {
105105
}
106106

107107
}
108+
109+
func Test_insertPredicate(t *testing.T) {
110+
tests := []struct {
111+
name string
112+
raw string
113+
insert string
114+
want string
115+
}{
116+
{
117+
name: "a few Predicates lined up",
118+
raw: `Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
119+
insert: "Weight(100)",
120+
want: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
121+
},
122+
{
123+
name: "adds Predicate if none is set",
124+
raw: "",
125+
insert: "Weight(100)",
126+
want: `Weight(100)`,
127+
},
128+
{
129+
name: "removes duplicated Predicate Weight(100)",
130+
raw: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
131+
insert: "Weight(100)",
132+
want: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
133+
},
134+
{
135+
name: "removes duplicated Predicate False() and reorders them",
136+
raw: `Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")&&False()`,
137+
insert: "False()",
138+
want: `False() && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
139+
},
140+
{
141+
name: "removes conflicting Predicate False()",
142+
raw: `Host(/^my-host-header\.example\.org$/) && False() && Method("GET") && Path("/hello")`,
143+
insert: "Weight(100)",
144+
want: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
145+
},
146+
}
147+
for _, tt := range tests {
148+
tt := tt
149+
t.Run(tt.name, func(t *testing.T) {
150+
assert.Equal(t, tt.want, insertPredicate(tt.raw, tt.insert))
151+
})
152+
}
153+
}

0 commit comments

Comments
 (0)