From 056c77dbd248b91e5b5c5b4ffc8767414f1280b9 Mon Sep 17 00:00:00 2001 From: Jiajing LU Date: Mon, 9 Dec 2024 19:25:49 +0800 Subject: [PATCH] Patch canary service selector from PodTemplateMetadata (#243) * patch canary service selector Signed-off-by: Megrez Lu * check null Signed-off-by: Megrez Lu * fix nil check Signed-off-by: Megrez Lu * remove len check Signed-off-by: Megrez Lu --------- Signed-off-by: Megrez Lu --- pkg/controller/rollout/rollout_progressing.go | 5 +++ pkg/feature/rollout_features.go | 7 ++-- pkg/trafficrouting/manager.go | 33 ++++++++++++------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/pkg/controller/rollout/rollout_progressing.go b/pkg/controller/rollout/rollout_progressing.go index 4798cf29..24be281a 100644 --- a/pkg/controller/rollout/rollout_progressing.go +++ b/pkg/controller/rollout/rollout_progressing.go @@ -600,11 +600,16 @@ func newTrafficRoutingContext(c *RolloutContext) *trafficrouting.TrafficRoutingC if c.Workload != nil { revisionLabelKey = c.Workload.RevisionLabelKey } + var selectorPatch map[string]string + if !c.Rollout.Spec.Strategy.DisableGenerateCanaryService() && c.Rollout.Spec.Strategy.Canary.PatchPodTemplateMetadata != nil { + selectorPatch = c.Rollout.Spec.Strategy.Canary.PatchPodTemplateMetadata.Labels + } return &trafficrouting.TrafficRoutingContext{ Key: fmt.Sprintf("Rollout(%s/%s)", c.Rollout.Namespace, c.Rollout.Name), Namespace: c.Rollout.Namespace, ObjectRef: c.Rollout.Spec.Strategy.GetTrafficRouting(), Strategy: currentStep.TrafficRoutingStrategy, + CanaryServiceSelectorPatch: selectorPatch, OwnerRef: *metav1.NewControllerRef(c.Rollout, rolloutControllerKind), RevisionLabelKey: revisionLabelKey, StableRevision: c.NewStatus.GetSubStatus().StableRevision, diff --git a/pkg/feature/rollout_features.go b/pkg/feature/rollout_features.go index d3bde30c..f45b30cc 100644 --- a/pkg/feature/rollout_features.go +++ b/pkg/feature/rollout_features.go @@ -28,11 +28,14 @@ const ( RolloutHistoryGate featuregate.Feature = "RolloutHistory" // AdvancedDeploymentGate enable advanced deployment controller. AdvancedDeploymentGate featuregate.Feature = "AdvancedDeployment" + // AppendServiceSelectorGate enable appending pod labels from PodTemplateMetadata to the canary service selector. + AppendServiceSelectorGate featuregate.Feature = "AppendPodSelector" ) var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{ - RolloutHistoryGate: {Default: false, PreRelease: featuregate.Alpha}, - AdvancedDeploymentGate: {Default: false, PreRelease: featuregate.Alpha}, + RolloutHistoryGate: {Default: false, PreRelease: featuregate.Alpha}, + AdvancedDeploymentGate: {Default: false, PreRelease: featuregate.Alpha}, + AppendServiceSelectorGate: {Default: false, PreRelease: featuregate.Alpha}, } func init() { diff --git a/pkg/trafficrouting/manager.go b/pkg/trafficrouting/manager.go index 243c5999..86bc6e90 100644 --- a/pkg/trafficrouting/manager.go +++ b/pkg/trafficrouting/manager.go @@ -21,13 +21,6 @@ import ( "fmt" "time" - "github.com/openkruise/rollouts/api/v1beta1" - "github.com/openkruise/rollouts/pkg/trafficrouting/network" - custom "github.com/openkruise/rollouts/pkg/trafficrouting/network/customNetworkProvider" - "github.com/openkruise/rollouts/pkg/trafficrouting/network/gateway" - "github.com/openkruise/rollouts/pkg/trafficrouting/network/ingress" - "github.com/openkruise/rollouts/pkg/util" - "github.com/openkruise/rollouts/pkg/util/grace" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -36,6 +29,16 @@ import ( "k8s.io/utils/integer" utilpointer "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/openkruise/rollouts/api/v1beta1" + "github.com/openkruise/rollouts/pkg/feature" + "github.com/openkruise/rollouts/pkg/trafficrouting/network" + custom "github.com/openkruise/rollouts/pkg/trafficrouting/network/customNetworkProvider" + "github.com/openkruise/rollouts/pkg/trafficrouting/network/gateway" + "github.com/openkruise/rollouts/pkg/trafficrouting/network/ingress" + "github.com/openkruise/rollouts/pkg/util" + utilfeature "github.com/openkruise/rollouts/pkg/util/feature" + "github.com/openkruise/rollouts/pkg/util/grace" ) var ( @@ -44,10 +47,11 @@ var ( type TrafficRoutingContext struct { // only for log info - Key string - Namespace string - ObjectRef []v1beta1.TrafficRoutingRef - Strategy v1beta1.TrafficRoutingStrategy + Key string + Namespace string + ObjectRef []v1beta1.TrafficRoutingRef + Strategy v1beta1.TrafficRoutingStrategy + CanaryServiceSelectorPatch map[string]string // OnlyTrafficRouting OnlyTrafficRouting bool OwnerRef metav1.OwnerReference @@ -447,6 +451,13 @@ func (m *Manager) createCanaryService(c *TrafficRoutingContext, cService string, for i := range canaryService.Spec.Ports { canaryService.Spec.Ports[i].NodePort = 0 } + for key, val := range c.CanaryServiceSelectorPatch { + if _, ok := canaryService.Spec.Selector[key]; ok { + canaryService.Spec.Selector[key] = val + } else if utilfeature.DefaultFeatureGate.Enabled(feature.AppendServiceSelectorGate) { + canaryService.Spec.Selector[key] = val + } + } err := m.Create(context.TODO(), canaryService) if err != nil && !errors.IsAlreadyExists(err) { klog.Errorf("%s create canary service(%s) failed: %s", c.Key, cService, err.Error())