Skip to content

Commit

Permalink
Merge pull request #681 from o11n/preservePredicates
Browse files Browse the repository at this point in the history
Skipper: preserve Predicates
  • Loading branch information
stefanprodan authored Aug 29, 2020
2 parents ce69a18 + e7da8c3 commit fb66cd3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkg/router/skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

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

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

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

return annotations
}
Expand All @@ -233,3 +235,19 @@ func (skp *SkipperRouter) backendWeights(annotation map[string]string) (backendW
func (skp *SkipperRouter) getIngressNames(name string) (apexName, canaryName string) {
return name, fmt.Sprintf(canaryPatternf, name)
}

func insertPredicate(raw, insert string) string {
// ensuring it at first place
predicates := []string{insert}
for _, x := range strings.Split(raw, "&&") {
predicate := strings.TrimSpace(x)
// dropping conflicting predicates
if predicate == "" ||
predicate == canaryRouteWeight ||
predicate == canaryRouteDisable {
continue
}
predicates = append(predicates, predicate)
}
return strings.Join(predicates, " && ")
}
46 changes: 46 additions & 0 deletions pkg/router/skipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,49 @@ func TestSkipperRouter_GetSetRoutes(t *testing.T) {
}

}

func Test_insertPredicate(t *testing.T) {
tests := []struct {
name string
raw string
insert string
want string
}{
{
name: "a few Predicates lined up",
raw: `Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
insert: "Weight(100)",
want: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
},
{
name: "adds Predicate if none is set",
raw: "",
insert: "Weight(100)",
want: `Weight(100)`,
},
{
name: "removes duplicated Predicate Weight(100)",
raw: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
insert: "Weight(100)",
want: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
},
{
name: "removes duplicated Predicate False() and reorders them",
raw: `Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")&&False()`,
insert: "False()",
want: `False() && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
},
{
name: "removes conflicting Predicate False()",
raw: `Host(/^my-host-header\.example\.org$/) && False() && Method("GET") && Path("/hello")`,
insert: "Weight(100)",
want: `Weight(100) && Host(/^my-host-header\.example\.org$/) && Method("GET") && Path("/hello")`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, insertPredicate(tt.raw, tt.insert))
})
}
}

0 comments on commit fb66cd3

Please sign in to comment.