diff --git a/rollout/trafficrouting/istio/istio.go b/rollout/trafficrouting/istio/istio.go index 9fd9402c09..a0055cf056 100644 --- a/rollout/trafficrouting/istio/istio.go +++ b/rollout/trafficrouting/istio/istio.go @@ -904,8 +904,13 @@ func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations .. // getHttpRouteIndexesToPatch returns array indices of the httpRoutes which need to be patched when updating weights func getHttpRouteIndexesToPatch(routeNames []string, httpRoutes []VirtualServiceHTTPRoute) ([]int, error) { //We have no routes listed in spec.strategy.canary.trafficRouting.istio.virtualService.routes so find index - //of the first empty named route + //of the first empty named route. + // If there is only one HTTPRoute defined in the VirtualService, then we can patch it without a name. if len(routeNames) == 0 { + if len(httpRoutes) == 1 { + return []int{0}, nil + } + for i, route := range httpRoutes { if route.Name == "" { return []int{i}, nil diff --git a/rollout/trafficrouting/istio/istio_test.go b/rollout/trafficrouting/istio/istio_test.go index 32050fe2b4..a316e7af2b 100644 --- a/rollout/trafficrouting/istio/istio_test.go +++ b/rollout/trafficrouting/istio/istio_test.go @@ -3156,3 +3156,45 @@ spec: actions := client.Actions() assert.Len(t, actions, 0) } + +func TestGetHttpRouteIndexesToPatch(t *testing.T) { + + // Test case when the rollout has no managed routes defined + httpRoutes := []VirtualServiceHTTPRoute{ + {Name: "foo", Match: nil}, + {Name: "", Match: nil}, + } + + indexes, err := getHttpRouteIndexesToPatch([]string{}, httpRoutes) + assert.NoError(t, err) + assert.Equal(t, []int{1}, indexes) + + // Test case when the rollout has managed routes defined + httpRoutes = []VirtualServiceHTTPRoute{ + {Name: "foo", Match: nil}, + {Name: "bar", Match: nil}, + } + + indexes, err = getHttpRouteIndexesToPatch([]string{"foo", "bar"}, httpRoutes) + assert.NoError(t, err) + assert.Equal(t, []int{0, 1}, indexes) + + // Test case when the rollout has only one managed route defined + httpRoutes = []VirtualServiceHTTPRoute{ + {Name: "foo", Match: nil}, + } + + indexes, err = getHttpRouteIndexesToPatch([]string{}, httpRoutes) + assert.NoError(t, err) + assert.Equal(t, []int{0}, indexes) + + // Test case when http route is not found + httpRoutes = []VirtualServiceHTTPRoute{ + {Name: "foo", Match: nil}, + } + + indexes, err = getHttpRouteIndexesToPatch([]string{"bar"}, httpRoutes) + assert.Equal(t, "HTTP Route 'bar' is not found in the defined Virtual Service.", err.Error()) + assert.Nil(t, indexes) + +}