Skip to content

Commit

Permalink
Move route ref into its own struct
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Rakitin <[email protected]>
  • Loading branch information
svrakitin committed Feb 6, 2024
1 parent bd280b9 commit 3d7db80
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/plugin/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *RpcPlugin) setHTTPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight int32, additionalDestinations []v1alpha1.WeightDestination, gatewayAPIConfig *GatewayAPITrafficRouting, routeName string) pluginTypes.RpcError {
func (r *RpcPlugin) setHTTPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight int32, additionalDestinations []v1alpha1.WeightDestination, gatewayAPIConfig GatewayAPITrafficRouting) pluginTypes.RpcError {
ctx := context.TODO()
httpRouteClient := r.HTTPRouteClient
if !r.IsTest {
gatewayV1beta1 := r.Client.GatewayV1beta1()
httpRouteClient = gatewayV1beta1.HTTPRoutes(gatewayAPIConfig.Namespace)
}
httpRoute, err := httpRouteClient.Get(ctx, routeName, metav1.GetOptions{})
httpRoute, err := httpRouteClient.Get(ctx, gatewayAPIConfig.HTTPRoute, metav1.GetOptions{})
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
Expand Down
31 changes: 17 additions & 14 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/utils"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
pluginTypes "github.com/argoproj/argo-rollouts/utils/plugin/types"
"sigs.k8s.io/gateway-api/apis/v1beta1"
gatewayApiClientset "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
)

Expand All @@ -18,9 +17,6 @@ const (

GatewayAPIUpdateError = "GatewayAPIUpdateError"
GatewayAPIManifestError = "No routes configured. One of 'routes', 'tcpRoute' or 'httpRoute' should be set"

HTTPRouteKind = "HTTPRoute"
TCPRouteKind = "TCPRoute"
)

func (r *RpcPlugin) InitPlugin() pluginTypes.RpcError {
Expand Down Expand Up @@ -49,23 +45,24 @@ func (r *RpcPlugin) UpdateHash(rollout *v1alpha1.Rollout, canaryHash, stableHash

func (r *RpcPlugin) SetWeight(rollout *v1alpha1.Rollout, desiredWeight int32, additionalDestinations []v1alpha1.WeightDestination) pluginTypes.RpcError {
gatewayAPIConfig := &GatewayAPITrafficRouting{}
if err := json.Unmarshal(rollout.Spec.Strategy.Canary.TrafficRouting.Plugins[PluginName], gatewayAPIConfig); err != nil {
err := json.Unmarshal(rollout.Spec.Strategy.Canary.TrafficRouting.Plugins[PluginName], gatewayAPIConfig)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}

if gatewayAPIConfig.HTTPRoute != "" {
gatewayAPIConfig.Routes = append(gatewayAPIConfig.Routes, v1beta1.LocalObjectReference{
gatewayAPIConfig.Routes = append(gatewayAPIConfig.Routes, GatewayAPIRouteReference{
Kind: HTTPRouteKind,
Name: v1beta1.ObjectName(gatewayAPIConfig.HTTPRoute),
Name: gatewayAPIConfig.HTTPRoute,
})
}

if gatewayAPIConfig.TCPRoute != "" {
gatewayAPIConfig.Routes = append(gatewayAPIConfig.Routes, v1beta1.LocalObjectReference{
gatewayAPIConfig.Routes = append(gatewayAPIConfig.Routes, GatewayAPIRouteReference{
Kind: TCPRouteKind,
Name: v1beta1.ObjectName(gatewayAPIConfig.TCPRoute),
Name: gatewayAPIConfig.TCPRoute,
})
}

Expand All @@ -75,7 +72,7 @@ func (r *RpcPlugin) SetWeight(rollout *v1alpha1.Rollout, desiredWeight int32, ad
}
}

var err pluginTypes.RpcError
var rpcErr pluginTypes.RpcError
for _, ref := range gatewayAPIConfig.Routes {
if ref.Kind == "" {
// Assume HTTPRoute by default
Expand All @@ -84,20 +81,26 @@ func (r *RpcPlugin) SetWeight(rollout *v1alpha1.Rollout, desiredWeight int32, ad

switch ref.Kind {
case HTTPRouteKind:
err = r.setHTTPRouteWeight(rollout, desiredWeight, additionalDestinations, gatewayAPIConfig, string(ref.Name))
rpcErr = r.setHTTPRouteWeight(rollout, desiredWeight, additionalDestinations, GatewayAPITrafficRouting{
Namespace: gatewayAPIConfig.Namespace,
HTTPRoute: ref.Name,
})
case TCPRouteKind:
err = r.setTCPRouteWeight(rollout, desiredWeight, additionalDestinations, gatewayAPIConfig, string(ref.Name))
rpcErr = r.setTCPRouteWeight(rollout, desiredWeight, additionalDestinations, GatewayAPITrafficRouting{
Namespace: gatewayAPIConfig.Namespace,
TCPRoute: ref.Name,
})
default:
r.LogCtx.Warnf("unsupported kind %q for route %q, supported values: %q, %q", ref.Kind, ref.Name, HTTPRouteKind, TCPRouteKind)
}
if err.HasError() {
if rpcErr.HasError() {
return pluginTypes.RpcError{
ErrorString: fmt.Sprintf("set weight for %s %q: %s", ref.Kind, ref.Name, err),
}
}
}

return err
return rpcErr
}

func (r *RpcPlugin) SetHeaderRoute(rollout *v1alpha1.Rollout, headerRouting *v1alpha1.SetHeaderRoute) pluginTypes.RpcError {
Expand Down
3 changes: 1 addition & 2 deletions pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

log "github.com/sirupsen/logrus"
"sigs.k8s.io/gateway-api/apis/v1beta1"
gwFake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"

goPlugin "github.com/hashicorp/go-plugin"
Expand Down Expand Up @@ -131,7 +130,7 @@ func TestRunSuccessfully(t *testing.T) {
err := pluginInstance.SetWeight(newRollout(mocks.StableServiceName, mocks.CanaryServiceName,
&GatewayAPITrafficRouting{
Namespace: mocks.Namespace,
Routes: []v1beta1.LocalObjectReference{
Routes: []GatewayAPIRouteReference{
{
Kind: HTTPRouteKind,
Name: mocks.HTTPRouteName,
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/tcproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *RpcPlugin) setTCPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight int32, additionalDestinations []v1alpha1.WeightDestination, gatewayAPIConfig *GatewayAPITrafficRouting, routeName string) pluginTypes.RpcError {
func (r *RpcPlugin) setTCPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight int32, additionalDestinations []v1alpha1.WeightDestination, gatewayAPIConfig GatewayAPITrafficRouting) pluginTypes.RpcError {
ctx := context.TODO()
tcpRouteClient := r.TCPRouteClient
if !r.IsTest {
gatewayV1alpha2 := r.Client.GatewayV1alpha2()
tcpRouteClient = gatewayV1alpha2.TCPRoutes(gatewayAPIConfig.Namespace)
}
tcpRoute, err := tcpRouteClient.Get(ctx, routeName, metav1.GetOptions{})
tcpRoute, err := tcpRouteClient.Get(ctx, gatewayAPIConfig.TCPRoute, metav1.GetOptions{})
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
Expand Down
14 changes: 13 additions & 1 deletion pkg/plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
gatewayApiv1beta1 "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/typed/apis/v1beta1"
)

const (
HTTPRouteKind = "HTTPRoute"
TCPRouteKind = "TCPRoute"
)

type RpcPlugin struct {
IsTest bool
LogCtx *logrus.Entry
Expand All @@ -19,10 +24,17 @@ type RpcPlugin struct {
TCPRouteClient gatewayApiv1alpha2.TCPRouteInterface
}

type GatewayAPIRouteReference struct {
// Kind is kind of the route resource. For example "HTTPRoute" or "TCPRoute".
Kind string
// Name is the name of the route resource.
Name string
}

type GatewayAPITrafficRouting struct {
// Routes refers to HTTPRoute and TCPRoute resources used to route traffic
// to the service
Routes []v1beta1.LocalObjectReference `json:"routes,omitempty"`
Routes []GatewayAPIRouteReference `json:"routes,omitempty"`
// HTTPRoute refers to the name of the HTTPRoute used to route traffic to the
// service
HTTPRoute string `json:"httpRoute,omitempty"`
Expand Down

0 comments on commit 3d7db80

Please sign in to comment.