Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: support batch-id labeling for bluegreen strategy #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/controller/batchrelease/batchrelease_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

appsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
"github.com/openkruise/rollouts/api/v1beta1"
batchcontext "github.com/openkruise/rollouts/pkg/controller/batchrelease/context"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/control"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/control/bluegreenstyle"
bgcloneset "github.com/openkruise/rollouts/pkg/controller/batchrelease/control/bluegreenstyle/cloneset"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/openkruise/rollouts/pkg/controller/batchrelease/control/partitionstyle/daemonset"
partitiondeployment "github.com/openkruise/rollouts/pkg/controller/batchrelease/control/partitionstyle/deployment"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/control/partitionstyle/statefulset"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/labelpatch"
"github.com/openkruise/rollouts/pkg/util"
"github.com/openkruise/rollouts/pkg/util/errors"
apps "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -214,11 +216,13 @@ func (r *Executor) getReleaseController(release *v1beta1.BatchRelease, newStatus
case v1beta1.BlueGreenRollingStyle:
if targetRef.APIVersion == appsv1alpha1.GroupVersion.String() && targetRef.Kind == reflect.TypeOf(appsv1alpha1.CloneSet{}).Name() {
klog.InfoS("Using CloneSet bluegreen-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace)
return bluegreenstyle.NewControlPlane(bgcloneset.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil
return bluegreenstyle.NewControlPlane(bgcloneset.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk, labelpatch.DefaultRevisionConsistentFunc), nil
}
if targetRef.APIVersion == apps.SchemeGroupVersion.String() && targetRef.Kind == reflect.TypeOf(apps.Deployment{}).Name() {
klog.InfoS("Using Deployment bluegreen-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace)
return bluegreenstyle.NewControlPlane(bgdeplopyment.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil
return bluegreenstyle.NewControlPlane(bgdeplopyment.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk, func(pod *v1.Pod, ctx *batchcontext.BatchContext) bool {
return !util.IsConsistentWithRevision(pod, ctx.StableRevision)
}), nil
}

case v1beta1.CanaryRollingStyle:
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/batchrelease/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type BatchContext struct {
CurrentBatch int32 `json:"currentBatchIndex"`
// workload update revision
UpdateRevision string `json:"updateRevision,omitempty"`
// stable revision
StableRevision string `json:"stableRevision,omitempty"`

// workload replicas
Replicas int32 `json:"replicas"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package bluegreenstyle

import (
"github.com/openkruise/rollouts/api/v1beta1"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/context"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/control"
"github.com/openkruise/rollouts/pkg/controller/batchrelease/labelpatch"
"github.com/openkruise/rollouts/pkg/util"
Expand All @@ -41,14 +42,16 @@ type realBatchControlPlane struct {
type NewInterfaceFunc func(cli client.Client, key types.NamespacedName, gvk schema.GroupVersionKind) Interface

// NewControlPlane creates a new release controller with bluegreen-style to drive batch release state machine
func NewControlPlane(f NewInterfaceFunc, cli client.Client, recorder record.EventRecorder, release *v1beta1.BatchRelease, newStatus *v1beta1.BatchReleaseStatus, key types.NamespacedName, gvk schema.GroupVersionKind) *realBatchControlPlane {
func NewControlPlane(f NewInterfaceFunc, cli client.Client, recorder record.EventRecorder, release *v1beta1.BatchRelease,
newStatus *v1beta1.BatchReleaseStatus, key types.NamespacedName, gvk schema.GroupVersionKind,
consistentFunc labelpatch.RevisionConsistentFunc) *realBatchControlPlane {
return &realBatchControlPlane{
Client: cli,
EventRecorder: recorder,
newStatus: newStatus,
Interface: f(cli, key, gvk),
release: release.DeepCopy(),
patcher: labelpatch.NewLabelPatcher(cli, klog.KObj(release), release.Spec.ReleasePlan.Batches),
patcher: labelpatch.NewLabelPatcherWithRevisionConsistentFunc(cli, klog.KObj(release), release.Spec.ReleasePlan.Batches, consistentFunc),
}
}

Expand All @@ -72,6 +75,15 @@ func (rc *realBatchControlPlane) Initialize() error {
return err
}

func (rc *realBatchControlPlane) patchPodLabels(batchContext *context.BatchContext) error {
pods, err := rc.ListOwnedPods() // add pods to rc for patching pod batch labels
if err != nil {
return err
}
batchContext.Pods = pods
return rc.patcher.PatchPodBatchLabel(batchContext)
}

func (rc *realBatchControlPlane) UpgradeBatch() error {
controller, err := rc.BuildController()
if err != nil {
Expand All @@ -94,7 +106,7 @@ func (rc *realBatchControlPlane) UpgradeBatch() error {
return err
}

return nil
return rc.patchPodLabels(batchContext)
}

func (rc *realBatchControlPlane) CheckBatchReady() error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (rc *realController) CalculateBatchContext(release *v1beta1.BatchRelease) (
CurrentSurge: currentSurge,
DesiredSurge: desiredSurge,
UpdateRevision: release.Status.UpdateRevision,
StableRevision: release.Status.StableRevision,

Replicas: rc.Replicas,
UpdatedReplicas: rc.Status.UpdatedReplicas,
Expand Down
22 changes: 18 additions & 4 deletions pkg/controller/batchrelease/labelpatch/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,28 @@ type LabelPatcher interface {
PatchPodBatchLabel(ctx *batchcontext.BatchContext) error
}

// RevisionConsistentFunc determines if the pod corresponds to the revision of batchContext.
type RevisionConsistentFunc func(pod *corev1.Pod, ctx *batchcontext.BatchContext) bool

var DefaultRevisionConsistentFunc RevisionConsistentFunc = func(pod *corev1.Pod, ctx *batchcontext.BatchContext) bool {
return util.IsConsistentWithRevision(pod, ctx.UpdateRevision)
}

func NewLabelPatcher(cli client.Client, logKey klog.ObjectRef, batches []v1beta1.ReleaseBatch) *realPatcher {
return &realPatcher{Client: cli, logKey: logKey, batches: batches}
return NewLabelPatcherWithRevisionConsistentFunc(cli, logKey, batches, DefaultRevisionConsistentFunc)
}

func NewLabelPatcherWithRevisionConsistentFunc(cli client.Client, logKey klog.ObjectRef, batches []v1beta1.ReleaseBatch,
fn RevisionConsistentFunc) *realPatcher {
return &realPatcher{Client: cli, logKey: logKey, batches: batches,
revisionConsistents: fn}
}

type realPatcher struct {
client.Client
logKey klog.ObjectRef
batches []v1beta1.ReleaseBatch
logKey klog.ObjectRef
batches []v1beta1.ReleaseBatch
revisionConsistents RevisionConsistentFunc
}

func (r *realPatcher) PatchPodBatchLabel(ctx *batchcontext.BatchContext) error {
Expand All @@ -67,7 +81,7 @@ func (r *realPatcher) patchPodBatchLabel(pods []*corev1.Pod, ctx *batchcontext.B
continue
}
// we don't patch label for the active old revision pod
if !util.IsConsistentWithRevision(pod, ctx.UpdateRevision) {
if !r.revisionConsistents(pod, ctx) {
klog.InfoS("Pod is not consistent with revision, skip patching", "pod", klog.KObj(pod), "rollout", r.logKey)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/pod_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetPodConditionFromList(conditions []v1.PodCondition, conditionType v1.PodC
return -1, nil
}

// IsConsistentWithRevision return true iff pod is match the revision
// IsConsistentWithRevision return true if pod is match the revision
func IsConsistentWithRevision(pod *v1.Pod, revision string) bool {
if pod.Labels[appsv1.DefaultDeploymentUniqueLabelKey] != "" &&
strings.HasSuffix(revision, pod.Labels[appsv1.DefaultDeploymentUniqueLabelKey]) {
Expand Down
Loading
Loading