Skip to content

Commit

Permalink
Patch canary service selector from PodTemplateMetadata (#243)
Browse files Browse the repository at this point in the history
* patch canary service selector

Signed-off-by: Megrez Lu <[email protected]>

* check null

Signed-off-by: Megrez Lu <[email protected]>

* fix nil check

Signed-off-by: Megrez Lu <[email protected]>

* remove len check

Signed-off-by: Megrez Lu <[email protected]>

---------

Signed-off-by: Megrez Lu <[email protected]>
  • Loading branch information
lujiajing1126 authored Dec 9, 2024
1 parent 3f66aae commit 056c77d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 5 additions & 0 deletions pkg/controller/rollout/rollout_progressing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions pkg/feature/rollout_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
33 changes: 22 additions & 11 deletions pkg/trafficrouting/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 056c77d

Please sign in to comment.