From 3d7db80b3e31c359621ac16e32c850f3c2c4a653 Mon Sep 17 00:00:00 2001 From: Stepan Rakitin Date: Tue, 6 Feb 2024 15:24:39 +0100 Subject: [PATCH] Move route ref into its own struct Signed-off-by: Stepan Rakitin --- pkg/plugin/httproute.go | 4 ++-- pkg/plugin/plugin.go | 31 +++++++++++++++++-------------- pkg/plugin/plugin_test.go | 3 +-- pkg/plugin/tcproute.go | 4 ++-- pkg/plugin/types.go | 14 +++++++++++++- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/pkg/plugin/httproute.go b/pkg/plugin/httproute.go index a0a5c6c..67e54e0 100644 --- a/pkg/plugin/httproute.go +++ b/pkg/plugin/httproute.go @@ -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(), diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 6d52ee5..6323a5c 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -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" ) @@ -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 { @@ -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, }) } @@ -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 @@ -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 { diff --git a/pkg/plugin/plugin_test.go b/pkg/plugin/plugin_test.go index 6c258a8..98fd93c 100644 --- a/pkg/plugin/plugin_test.go +++ b/pkg/plugin/plugin_test.go @@ -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" @@ -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, diff --git a/pkg/plugin/tcproute.go b/pkg/plugin/tcproute.go index 2279dcb..a9c0b0e 100644 --- a/pkg/plugin/tcproute.go +++ b/pkg/plugin/tcproute.go @@ -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(), diff --git a/pkg/plugin/types.go b/pkg/plugin/types.go index e7f39c5..e3fa3ac 100644 --- a/pkg/plugin/types.go +++ b/pkg/plugin/types.go @@ -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 @@ -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"`