Patch canary service selector from PodTemplateMetadata (#243)

* patch canary service selector

Signed-off-by: Megrez Lu <lujiajing1126@gmail.com>

* check null

Signed-off-by: Megrez Lu <lujiajing1126@gmail.com>

* fix nil check

Signed-off-by: Megrez Lu <lujiajing1126@gmail.com>

* remove len check

Signed-off-by: Megrez Lu <lujiajing1126@gmail.com>

---------

Signed-off-by: Megrez Lu <lujiajing1126@gmail.com>
This commit is contained in:
Jiajing LU 2024-12-09 19:25:49 +08:00 committed by GitHub
parent 3f66aae0ae
commit 056c77dbd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 13 deletions

View File

@ -600,11 +600,16 @@ func newTrafficRoutingContext(c *RolloutContext) *trafficrouting.TrafficRoutingC
if c.Workload != nil { if c.Workload != nil {
revisionLabelKey = c.Workload.RevisionLabelKey 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{ return &trafficrouting.TrafficRoutingContext{
Key: fmt.Sprintf("Rollout(%s/%s)", c.Rollout.Namespace, c.Rollout.Name), Key: fmt.Sprintf("Rollout(%s/%s)", c.Rollout.Namespace, c.Rollout.Name),
Namespace: c.Rollout.Namespace, Namespace: c.Rollout.Namespace,
ObjectRef: c.Rollout.Spec.Strategy.GetTrafficRouting(), ObjectRef: c.Rollout.Spec.Strategy.GetTrafficRouting(),
Strategy: currentStep.TrafficRoutingStrategy, Strategy: currentStep.TrafficRoutingStrategy,
CanaryServiceSelectorPatch: selectorPatch,
OwnerRef: *metav1.NewControllerRef(c.Rollout, rolloutControllerKind), OwnerRef: *metav1.NewControllerRef(c.Rollout, rolloutControllerKind),
RevisionLabelKey: revisionLabelKey, RevisionLabelKey: revisionLabelKey,
StableRevision: c.NewStatus.GetSubStatus().StableRevision, StableRevision: c.NewStatus.GetSubStatus().StableRevision,

View File

@ -28,11 +28,14 @@ const (
RolloutHistoryGate featuregate.Feature = "RolloutHistory" RolloutHistoryGate featuregate.Feature = "RolloutHistory"
// AdvancedDeploymentGate enable advanced deployment controller. // AdvancedDeploymentGate enable advanced deployment controller.
AdvancedDeploymentGate featuregate.Feature = "AdvancedDeployment" 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{ var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
RolloutHistoryGate: {Default: false, PreRelease: featuregate.Alpha}, RolloutHistoryGate: {Default: false, PreRelease: featuregate.Alpha},
AdvancedDeploymentGate: {Default: false, PreRelease: featuregate.Alpha}, AdvancedDeploymentGate: {Default: false, PreRelease: featuregate.Alpha},
AppendServiceSelectorGate: {Default: false, PreRelease: featuregate.Alpha},
} }
func init() { func init() {

View File

@ -21,13 +21,6 @@ import (
"fmt" "fmt"
"time" "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" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -36,6 +29,16 @@ import (
"k8s.io/utils/integer" "k8s.io/utils/integer"
utilpointer "k8s.io/utils/pointer" utilpointer "k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client" "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 ( var (
@ -44,10 +47,11 @@ var (
type TrafficRoutingContext struct { type TrafficRoutingContext struct {
// only for log info // only for log info
Key string Key string
Namespace string Namespace string
ObjectRef []v1beta1.TrafficRoutingRef ObjectRef []v1beta1.TrafficRoutingRef
Strategy v1beta1.TrafficRoutingStrategy Strategy v1beta1.TrafficRoutingStrategy
CanaryServiceSelectorPatch map[string]string
// OnlyTrafficRouting // OnlyTrafficRouting
OnlyTrafficRouting bool OnlyTrafficRouting bool
OwnerRef metav1.OwnerReference OwnerRef metav1.OwnerReference
@ -447,6 +451,13 @@ func (m *Manager) createCanaryService(c *TrafficRoutingContext, cService string,
for i := range canaryService.Spec.Ports { for i := range canaryService.Spec.Ports {
canaryService.Spec.Ports[i].NodePort = 0 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) err := m.Create(context.TODO(), canaryService)
if err != nil && !errors.IsAlreadyExists(err) { if err != nil && !errors.IsAlreadyExists(err) {
klog.Errorf("%s create canary service(%s) failed: %s", c.Key, cService, err.Error()) klog.Errorf("%s create canary service(%s) failed: %s", c.Key, cService, err.Error())