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

feat: support multiple routes per rollout #34

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
40 changes: 35 additions & 5 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plugin

import (
"encoding/json"
"fmt"

"github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/utils"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
Expand All @@ -15,7 +16,7 @@ const (
PluginName = "argoproj-labs/gatewayAPI"

GatewayAPIUpdateError = "GatewayAPIUpdateError"
GatewayAPIManifestError = "httpRoute and tcpRoute fields are empty. tcpRoute or httpRoute should be set"
GatewayAPIManifestError = "No routes configured. One of 'tcpRoutes', 'httpRoutes', 'tcpRoute' or 'httpRoute' should be set"
)

func (r *RpcPlugin) InitPlugin() pluginTypes.RpcError {
Expand Down Expand Up @@ -50,15 +51,44 @@ func (r *RpcPlugin) SetWeight(rollout *v1alpha1.Rollout, desiredWeight int32, ad
ErrorString: err.Error(),
}
}

if gatewayAPIConfig.HTTPRoute != "" {
return r.setHTTPRouteWeight(rollout, desiredWeight, additionalDestinations, &gatewayAPIConfig)
gatewayAPIConfig.HTTPRoutes = append(gatewayAPIConfig.HTTPRoutes, gatewayAPIConfig.HTTPRoute)
}

if gatewayAPIConfig.TCPRoute != "" {
return r.setTCPRouteWeight(rollout, desiredWeight, additionalDestinations, &gatewayAPIConfig)
gatewayAPIConfig.TCPRoutes = append(gatewayAPIConfig.TCPRoutes, gatewayAPIConfig.TCPRoute)
}
return pluginTypes.RpcError{
ErrorString: GatewayAPIManifestError,

if len(gatewayAPIConfig.HTTPRoutes) == 0 && len(gatewayAPIConfig.TCPRoutes) == 0 {
svrakitin marked this conversation as resolved.
Show resolved Hide resolved
return pluginTypes.RpcError{
ErrorString: GatewayAPIManifestError,
}
}

for _, name := range gatewayAPIConfig.HTTPRoutes {
err := r.setHTTPRouteWeight(rollout, desiredWeight, additionalDestinations, &GatewayAPITrafficRouting{
Namespace: gatewayAPIConfig.Namespace,
HTTPRoute: name,
})
if err.HasError() {
return pluginTypes.RpcError{
ErrorString: fmt.Sprintf("set weight for HTTPRoute %q: %s", name, err),
}
}
svrakitin marked this conversation as resolved.
Show resolved Hide resolved
}
for _, name := range gatewayAPIConfig.TCPRoutes {
err := r.setTCPRouteWeight(rollout, desiredWeight, additionalDestinations, &GatewayAPITrafficRouting{
Namespace: gatewayAPIConfig.Namespace,
TCPRoute: name,
})
if err.HasError() {
return pluginTypes.RpcError{
ErrorString: fmt.Sprintf("set weight for TCPRoute %q: %s", name, err),
}
}
}
return pluginTypes.RpcError{}
}

func (r *RpcPlugin) SetHeaderRoute(rollout *v1alpha1.Rollout, headerRouting *v1alpha1.SetHeaderRoute) pluginTypes.RpcError {
Expand Down
41 changes: 27 additions & 14 deletions pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,50 @@ func TestRunSuccessfully(t *testing.T) {
}
t.Run("SetHTTPRouteWeight", func(t *testing.T) {
var desiredWeight int32 = 30
err := pluginInstance.SetWeight(newRollout(mocks.StableServiceName, mocks.CanaryServiceName, mocks.HTTPRoute, mocks.HTTPRouteName), desiredWeight, []v1alpha1.WeightDestination{})
err := pluginInstance.SetWeight(newRollout(mocks.StableServiceName, mocks.CanaryServiceName, &GatewayAPITrafficRouting{
Namespace: mocks.Namespace,
HTTPRoute: mocks.HTTPRouteName,
}), desiredWeight, []v1alpha1.WeightDestination{})

assert.Empty(t, err.Error())
assert.Equal(t, 100-desiredWeight, *(rpcPluginImp.UpdatedHTTPRouteMock.Spec.Rules[0].BackendRefs[0].Weight))
assert.Equal(t, desiredWeight, *(rpcPluginImp.UpdatedHTTPRouteMock.Spec.Rules[0].BackendRefs[1].Weight))
})
t.Run("SetTCPRouteWeight", func(t *testing.T) {
var desiredWeight int32 = 30
err := pluginInstance.SetWeight(newRollout(mocks.StableServiceName, mocks.CanaryServiceName, mocks.TCPRoute, mocks.TCPRouteName), desiredWeight, []v1alpha1.WeightDestination{})
err := pluginInstance.SetWeight(newRollout(mocks.StableServiceName, mocks.CanaryServiceName,
&GatewayAPITrafficRouting{
Namespace: mocks.Namespace,
TCPRoute: mocks.TCPRouteName,
}), desiredWeight, []v1alpha1.WeightDestination{})

assert.Empty(t, err.Error())
assert.Equal(t, 100-desiredWeight, *(rpcPluginImp.UpdatedTCPRouteMock.Spec.Rules[0].BackendRefs[0].Weight))
assert.Equal(t, desiredWeight, *(rpcPluginImp.UpdatedTCPRouteMock.Spec.Rules[0].BackendRefs[1].Weight))
})
t.Run("SetWeightViaRoutes", func(t *testing.T) {
var desiredWeight int32 = 30
err := pluginInstance.SetWeight(newRollout(mocks.StableServiceName, mocks.CanaryServiceName,
&GatewayAPITrafficRouting{
Namespace: mocks.Namespace,
HTTPRoutes: []string{mocks.HTTPRouteName},
TCPRoutes: []string{mocks.TCPRouteName},
}), desiredWeight, []v1alpha1.WeightDestination{})

assert.Empty(t, err.Error())
assert.Equal(t, 100-desiredWeight, *(rpcPluginImp.UpdatedHTTPRouteMock.Spec.Rules[0].BackendRefs[0].Weight))
assert.Equal(t, desiredWeight, *(rpcPluginImp.UpdatedHTTPRouteMock.Spec.Rules[0].BackendRefs[1].Weight))
assert.Equal(t, 100-desiredWeight, *(rpcPluginImp.UpdatedTCPRouteMock.Spec.Rules[0].BackendRefs[0].Weight))
assert.Equal(t, desiredWeight, *(rpcPluginImp.UpdatedTCPRouteMock.Spec.Rules[0].BackendRefs[1].Weight))
})

// Canceling should cause an exit
cancel()
<-closeCh
}

func newRollout(stableSvc, canarySvc, routeType, routeName string) *v1alpha1.Rollout {
gatewayAPIConfig := GatewayAPITrafficRouting{
Namespace: mocks.Namespace,
}
switch routeType {
case mocks.HTTPRoute:
gatewayAPIConfig.HTTPRoute = routeName
case mocks.TCPRoute:
gatewayAPIConfig.TCPRoute = routeName
}
encodedGatewayAPIConfig, err := json.Marshal(gatewayAPIConfig)
func newRollout(stableSvc, canarySvc string, config *GatewayAPITrafficRouting) *v1alpha1.Rollout {
encodedConfig, err := json.Marshal(config)
if err != nil {
log.Fatal(err)
}
Expand All @@ -150,7 +163,7 @@ func newRollout(stableSvc, canarySvc, routeType, routeName string) *v1alpha1.Rol
CanaryService: canarySvc,
TrafficRouting: &v1alpha1.RolloutTrafficRouting{
Plugins: map[string]json.RawMessage{
PluginName: encodedGatewayAPIConfig,
PluginName: encodedConfig,
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type RpcPlugin struct {
}

type GatewayAPITrafficRouting struct {
// HTTPRoutes refer to names of HTTPRoute resources used to route traffic to the
// service
HTTPRoutes []string `json:"httpRoutes,omitempty"`
// TCPRoutes refer to names of TCPRoute resources used to route traffic to the
// service
TCPRoutes []string `json:"tcpRoutes,omitempty"`
// HTTPRoute refers to the name of the HTTPRoute used to route traffic to the
// service
HTTPRoute string `json:"httpRoute,omitempty"`
Expand Down