Skip to content

Commit

Permalink
Allow Traffic shaping through header based routing for ALB. Increase …
Browse files Browse the repository at this point in the history
…coverage

Signed-off-by: Andrii Perenesenko <[email protected]>
  • Loading branch information
perenesenko committed Aug 29, 2022
1 parent 634f501 commit 5fef2dc
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
46 changes: 45 additions & 1 deletion rollout/trafficrouting/alb/alb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,9 @@ func TestVerifyWeightWithAdditionalDestinations(t *testing.T) {

func TestSetHeaderRoute(t *testing.T) {
ro := fakeRollout(STABLE_SVC, CANARY_SVC, nil, "ingress", 443)
ro.Spec.Strategy.Canary.TrafficRouting.ManagedRoutes = []v1alpha1.MangedRoutes{
{Name: "header-route"},
}
i := ingress("ingress", STABLE_SVC, CANARY_SVC, "action1", 443, 10, ro.Name, false)
client := fake.NewSimpleClientset(i)
k8sI := kubeinformers.NewSharedInformerFactory(client, 0)
Expand Down Expand Up @@ -899,6 +902,11 @@ func TestSetHeaderRoute(t *testing.T) {
})
assert.Nil(t, err)
assert.Len(t, client.Actions(), 1)

// no managed routes, no changes expected
err = r.RemoveManagedRoutes()
assert.Nil(t, err)
assert.Len(t, client.Actions(), 1)
}

func TestRemoveManagedRoutes(t *testing.T) {
Expand All @@ -917,6 +925,36 @@ func TestRemoveManagedRoutes(t *testing.T) {
i.Annotations["alb.ingress.kubernetes.io/actions.header-route"] = "{}"
i.Annotations["alb.ingress.kubernetes.io/conditions.header-route"] = "{}"
i.Annotations[ingressutil.ManagedAnnotations] = managedByValue.String()
i.Spec.Rules = []extensionsv1beta1.IngressRule{
{
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
{
Backend: extensionsv1beta1.IngressBackend{
ServiceName: "action1",
ServicePort: intstr.Parse("use-annotation"),
},
},
},
},
},
},
{
IngressRuleValue: extensionsv1beta1.IngressRuleValue{
HTTP: &extensionsv1beta1.HTTPIngressRuleValue{
Paths: []extensionsv1beta1.HTTPIngressPath{
{
Backend: extensionsv1beta1.IngressBackend{
ServiceName: "header-route",
ServicePort: intstr.Parse("use-annotation"),
},
},
},
},
},
},
}

client := fake.NewSimpleClientset(i)
k8sI := kubeinformers.NewSharedInformerFactory(client, 0)
Expand All @@ -934,9 +972,15 @@ func TestRemoveManagedRoutes(t *testing.T) {
})
assert.NoError(t, err)

err = r.RemoveManagedRoutes()
err = r.SetHeaderRoute(&v1alpha1.SetHeaderRoute{
Name: "header-route",
})
assert.Nil(t, err)
assert.Len(t, client.Actions(), 1)

err = r.RemoveManagedRoutes()
assert.Nil(t, err)
assert.Len(t, client.Actions(), 2)
}

func TestSetMirrorRoute(t *testing.T) {
Expand Down
52 changes: 52 additions & 0 deletions utils/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,55 @@ func getExtensionsIngress() *extensionsv1beta1.Ingress {
},
}
}

func TestManagedALBAnnotations(t *testing.T) {
emptyJson, _ := NewManagedALBAnnotations("")
assert.NotNil(t, emptyJson)
assert.Equal(t, 0, len(emptyJson))
assert.Equal(t, "{}", emptyJson.String())

_, err := NewManagedALBAnnotations("invalid json")
assert.Error(t, err)

json := "{\"rollouts-demo\":[\"alb.ingress.kubernetes.io/actions.action1\", \"alb.ingress.kubernetes.io/actions.header-action\", \"alb.ingress.kubernetes.io/conditions.header-action\"]}"
actual, err := NewManagedALBAnnotations(json)
assert.NoError(t, err)

rolloutsDemoAnnotation := actual["rollouts-demo"]
assert.NotNil(t, rolloutsDemoAnnotation)
assert.Equal(t, 3, len(rolloutsDemoAnnotation))
}

func TestALBHeaderBasedActionAnnotationKey(t *testing.T) {
r := &v1alpha1.Rollout{
Spec: v1alpha1.RolloutSpec{
Strategy: v1alpha1.RolloutStrategy{
Canary: &v1alpha1.CanaryStrategy{
TrafficRouting: &v1alpha1.RolloutTrafficRouting{
ALB: &v1alpha1.ALBTrafficRouting{
AnnotationPrefix: "alb.ingress.kubernetes.io",
},
},
},
},
},
}
assert.Equal(t, "alb.ingress.kubernetes.io/actions.route", ALBHeaderBasedActionAnnotationKey(r, "route"))
}

func TestALBHeaderBasedConditionAnnotationKey(t *testing.T) {
r := &v1alpha1.Rollout{
Spec: v1alpha1.RolloutSpec{
Strategy: v1alpha1.RolloutStrategy{
Canary: &v1alpha1.CanaryStrategy{
TrafficRouting: &v1alpha1.RolloutTrafficRouting{
ALB: &v1alpha1.ALBTrafficRouting{
AnnotationPrefix: "alb.ingress.kubernetes.io",
},
},
},
},
},
}
assert.Equal(t, "alb.ingress.kubernetes.io/conditions.route", ALBHeaderBasedConditionAnnotationKey(r, "route"))
}

0 comments on commit 5fef2dc

Please sign in to comment.