patch canary service selector

Signed-off-by: Megrez Lu <lujiajing1126@gmail.com>
This commit is contained in:
Megrez Lu 2024-11-29 09:56:34 +08:00
parent 3f66aae0ae
commit 5e0c57688d
2 changed files with 16 additions and 4 deletions

View File

@ -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.GetRollingStyle() == v1beta1.CanaryRollingStyle {
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,

View File

@ -44,10 +44,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 +448,12 @@ 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 {
// TODO: add feature gate to support add more selector
if _, ok := canaryService.Spec.Selector[key]; ok {
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())