99 "github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/internal/utils"
1010 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
1111 pluginTypes "github.com/argoproj/argo-rollouts/utils/plugin/types"
12+ "github.com/sirupsen/logrus"
1213 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1415)
@@ -19,10 +20,12 @@ const (
1920
2021func (r * RpcPlugin ) setHTTPRouteWeight (rollout * v1alpha1.Rollout , desiredWeight int32 , gatewayAPIConfig * GatewayAPITrafficRouting ) pluginTypes.RpcError {
2122 ctx := context .TODO ()
23+ clientset := r .TestClientset
2224 httpRouteClient := r .HTTPRouteClient
2325 if ! r .IsTest {
2426 gatewayClientV1 := r .GatewayAPIClientset .GatewayV1 ()
2527 httpRouteClient = gatewayClientV1 .HTTPRoutes (gatewayAPIConfig .Namespace )
28+ clientset = r .Clientset .CoreV1 ().ConfigMaps (gatewayAPIConfig .Namespace )
2629 }
2730 httpRoute , err := httpRouteClient .Get (ctx , gatewayAPIConfig .HTTPRoute , metav1.GetOptions {})
2831 if err != nil {
@@ -32,16 +35,63 @@ func (r *RpcPlugin) setHTTPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight
3235 }
3336 canaryServiceName := rollout .Spec .Strategy .Canary .CanaryService
3437 stableServiceName := rollout .Spec .Strategy .Canary .StableService
38+
39+ // Retrieve the managed routes from the configmap to determine which rules were added via setHTTPHeaderRoute
40+ managedRouteMap := make (ManagedRouteMap )
41+ configMap , err := utils .GetOrCreateConfigMap (gatewayAPIConfig .ConfigMap , utils.CreateConfigMapOptions {
42+ Clientset : clientset ,
43+ Ctx : ctx ,
44+ })
45+ if err != nil {
46+ return pluginTypes.RpcError {
47+ ErrorString : err .Error (),
48+ }
49+ }
50+ err = utils .GetConfigMapData (configMap , HTTPConfigMapKey , & managedRouteMap )
51+ if err != nil {
52+ return pluginTypes.RpcError {
53+ ErrorString : err .Error (),
54+ }
55+ }
56+ managedRuleIndices := make (map [int ]bool )
57+ for _ , managedRoute := range managedRouteMap {
58+ if idx , ok := managedRoute [httpRoute .Name ]; ok {
59+ managedRuleIndices [idx ] = true
60+ }
61+ }
62+
3563 routeRuleList := HTTPRouteRuleList (httpRoute .Spec .Rules )
36- canaryBackendRefs , err := getBackendRefs (canaryServiceName , routeRuleList )
64+ indexedCanaryBackendRefs , err := getIndexedBackendRefs (canaryServiceName , routeRuleList )
3765 if err != nil {
3866 return pluginTypes.RpcError {
3967 ErrorString : err .Error (),
4068 }
4169 }
70+ canaryBackendRefs := make ([]* HTTPBackendRef , 0 )
71+ for _ , indexedCanaryBackendRef := range indexedCanaryBackendRefs {
72+ // TODO - when setMirrorRoute is implemented, we would need to update the weight of the managed
73+ // canary backendRefs for mirror routes.
74+ // Ideally - these would be stored differently in the configmap from the managed header based routes
75+ // but that would mean a breaking change to the configmap structure
76+ if managedRuleIndices [indexedCanaryBackendRef .RuleIndex ] {
77+ r .LogCtx .WithFields (logrus.Fields {
78+ "rule" : httpRoute .Spec .Rules [indexedCanaryBackendRef .RuleIndex ],
79+ "index" : indexedCanaryBackendRef .RuleIndex ,
80+ "managedRouteMap" : managedRouteMap ,
81+ }).Info ("Skipping matched canary backendRef for weight adjustment since it is a part of a rule marked as a managed route" )
82+ continue
83+ }
84+ canaryBackendRefs = append (canaryBackendRefs , indexedCanaryBackendRef .Refs ... )
85+ }
86+
87+ // Update the weight of the canary backendRefs not owned by a rule marked as a managed route
4288 for _ , ref := range canaryBackendRefs {
4389 ref .Weight = & desiredWeight
4490 }
91+
92+ // Noted above, but any managed routes that would have a stableBackendRef would be updated with weight here.
93+ // Since this is not yet possible (all managed routes will only have a single canary backendRef),
94+ // we can avoid checking for managed route rule indexes here.
4595 stableBackendRefs , err := getBackendRefs (stableServiceName , routeRuleList )
4696 if err != nil {
4797 return pluginTypes.RpcError {
0 commit comments