Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(trafficrouting): patch VirtualService when there is only one named route #4055

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion rollout/trafficrouting/istio/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,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
Expand Down
44 changes: 44 additions & 0 deletions rollout/trafficrouting/istio/istio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3156,3 +3156,47 @@ spec:
actions := client.Actions()
assert.Len(t, actions, 0)
}

func TestGetHttpRouteIndexesToPatch(t *testing.T) {
t.Run("the rollout has no managed routes defined", func(t *testing.T) {
httpRoutes := []VirtualServiceHTTPRoute{
{Name: "foo", Match: nil},
{Name: "", Match: nil},
}

indexes, err := getHttpRouteIndexesToPatch([]string{}, httpRoutes)
assert.NoError(t, err)
assert.Equal(t, []int{1}, indexes)
})

t.Run("the rollout has managed routes defined", func(t *testing.T) {
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)
})

t.Run("the rollout has only one managed route defined", func(t *testing.T) {
httpRoutes := []VirtualServiceHTTPRoute{
{Name: "foo", Match: nil},
}

indexes, err := getHttpRouteIndexesToPatch([]string{}, httpRoutes)
assert.NoError(t, err)
assert.Equal(t, []int{0}, indexes)
})

t.Run("http route is not found", func(t *testing.T) {
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)
})
}
Loading