Skip to content

Commit 02d680f

Browse files
committed
fix(trafficrouting): patch VirtualService when there is only one named route
Signed-off-by: Jean Morais <[email protected]>
1 parent d605486 commit 02d680f

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

rollout/trafficrouting/istio/istio.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,13 @@ func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations ..
903903
// getHttpRouteIndexesToPatch returns array indices of the httpRoutes which need to be patched when updating weights
904904
func getHttpRouteIndexesToPatch(routeNames []string, httpRoutes []VirtualServiceHTTPRoute) ([]int, error) {
905905
//We have no routes listed in spec.strategy.canary.trafficRouting.istio.virtualService.routes so find index
906-
//of the first empty named route
906+
//of the first empty named route.
907+
// If there is only one HTTPRoute defined in the VirtualService, then we can patch it without a name.
907908
if len(routeNames) == 0 {
909+
if len(httpRoutes) == 1 {
910+
return []int{0}, nil
911+
}
912+
908913
for i, route := range httpRoutes {
909914
if route.Name == "" {
910915
return []int{i}, nil

rollout/trafficrouting/istio/istio_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,3 +3156,45 @@ spec:
31563156
actions := client.Actions()
31573157
assert.Len(t, actions, 0)
31583158
}
3159+
3160+
func TestGetHttpRouteIndexesToPatch(t *testing.T) {
3161+
3162+
// Test case when the rollout has no managed routes defined
3163+
httpRoutes := []VirtualServiceHTTPRoute{
3164+
{Name: "foo", Match: nil},
3165+
{Name: "", Match: nil},
3166+
}
3167+
3168+
indexes, err := getHttpRouteIndexesToPatch([]string{}, httpRoutes)
3169+
assert.NoError(t, err)
3170+
assert.Equal(t, []int{1}, indexes)
3171+
3172+
// Test case when the rollout has managed routes defined
3173+
httpRoutes = []VirtualServiceHTTPRoute{
3174+
{Name: "foo", Match: nil},
3175+
{Name: "bar", Match: nil},
3176+
}
3177+
3178+
indexes, err = getHttpRouteIndexesToPatch([]string{"foo", "bar"}, httpRoutes)
3179+
assert.NoError(t, err)
3180+
assert.Equal(t, []int{0, 1}, indexes)
3181+
3182+
// Test case when the rollout has only one managed route defined
3183+
httpRoutes = []VirtualServiceHTTPRoute{
3184+
{Name: "foo", Match: nil},
3185+
}
3186+
3187+
indexes, err = getHttpRouteIndexesToPatch([]string{}, httpRoutes)
3188+
assert.NoError(t, err)
3189+
assert.Equal(t, []int{0}, indexes)
3190+
3191+
// Test case when http route is not found
3192+
httpRoutes = []VirtualServiceHTTPRoute{
3193+
{Name: "foo", Match: nil},
3194+
}
3195+
3196+
indexes, err = getHttpRouteIndexesToPatch([]string{"bar"}, httpRoutes)
3197+
assert.Equal(t, "HTTP Route 'bar' is not found in the defined Virtual Service.", err.Error())
3198+
assert.Nil(t, indexes)
3199+
3200+
}

0 commit comments

Comments
 (0)