Skip to content

Commit

Permalink
add pre-normal-hook for cloneset
Browse files Browse the repository at this point in the history
Signed-off-by: mingzhou.swx <mingzhou.swx@alibaba-inc.com>
  • Loading branch information
mingzhou.swx committed Oct 26, 2022
1 parent a639a6f commit ab3d832
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 28 deletions.
4 changes: 4 additions & 0 deletions apis/apps/pub/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
LifecycleStateKey = "lifecycle.apps.kruise.io/state"
LifecycleTimestampKey = "lifecycle.apps.kruise.io/timestamp"

LifecycleStatePreparingNormal LifecycleStateType = "PreparingNormal"
LifecycleStateNormal LifecycleStateType = "Normal"
LifecycleStatePreparingUpdate LifecycleStateType = "PreparingUpdate"
LifecycleStateUpdating LifecycleStateType = "Updating"
Expand All @@ -35,6 +36,8 @@ type Lifecycle struct {
PreDelete *LifecycleHook `json:"preDelete,omitempty"`
// InPlaceUpdate is the hook before Pod to update and after Pod has been updated.
InPlaceUpdate *LifecycleHook `json:"inPlaceUpdate,omitempty"`
// PreNormal is the hook after Pod to be created and ready to be Normal.
PreNormal *LifecycleHook `json:"preNormal,omitempty"`
}

type LifecycleHook struct {
Expand All @@ -43,6 +46,7 @@ type LifecycleHook struct {
// MarkPodNotReady = true means:
// - Pod will be set to 'NotReady' at preparingDelete/preparingUpdate state.
// - Pod will be restored to 'Ready' at Updated state if it was set to 'NotReady' at preparingUpdate state.
// Currently, MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete hook.
// Default to false.
MarkPodNotReady bool `json:"markPodNotReady,omitempty"`
}
5 changes: 5 additions & 0 deletions apis/apps/pub/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/apps/v1alpha1/cloneset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type CloneSetSpec struct {
// Defaults to 0 (pod will be considered available as soon as it is ready)
MinReadySeconds int32 `json:"minReadySeconds,omitempty"`

// Lifecycle defines the lifecycle hooks for Pods pre-delete, in-place update.
// Lifecycle defines the lifecycle hooks for Pods post create, pre-delete, in-place update.
Lifecycle *appspub.Lifecycle `json:"lifecycle,omitempty"`
}

Expand Down
35 changes: 29 additions & 6 deletions config/crd/bases/apps.kruise.io_clonesets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ spec:
description: CloneSetSpec defines the desired state of CloneSet
properties:
lifecycle:
description: Lifecycle defines the lifecycle hooks for Pods pre-delete,
in-place update.
description: Lifecycle defines the lifecycle hooks for Pods post create,
pre-delete, in-place update.
properties:
inPlaceUpdate:
description: InPlaceUpdate is the hook before Pod to update and
Expand All @@ -101,8 +101,9 @@ spec:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Default
to false.'
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preDelete:
Expand All @@ -120,8 +121,30 @@ spec:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Default
to false.'
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preNormal:
description: PreNormal is the hook after Pod to be created and
ready to be Normal.
properties:
finalizersHandler:
items:
type: string
type: array
labelsHandler:
additionalProperties:
type: string
type: object
markPodNotReady:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
type: object
Expand Down
31 changes: 27 additions & 4 deletions config/crd/bases/apps.kruise.io_daemonsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ spec:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Default
to false.'
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preDelete:
Expand All @@ -124,8 +125,30 @@ spec:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Default
to false.'
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preNormal:
description: PreNormal is the hook after Pod to be created and
ready to be Normal.
properties:
finalizersHandler:
items:
type: string
type: array
labelsHandler:
additionalProperties:
type: string
type: object
markPodNotReady:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
type: object
Expand Down
31 changes: 27 additions & 4 deletions config/crd/bases/apps.kruise.io_statefulsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,9 @@ spec:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Default
to false.'
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preDelete:
Expand All @@ -537,8 +538,30 @@ spec:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Default
to false.'
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preNormal:
description: PreNormal is the hook after Pod to be created and
ready to be Normal.
properties:
finalizersHandler:
items:
type: string
type: array
labelsHandler:
additionalProperties:
type: string
type: object
markPodNotReady:
description: 'MarkPodNotReady = true means: - Pod will be
set to ''NotReady'' at preparingDelete/preparingUpdate state.
- Pod will be restored to ''Ready'' at Updated state if
it was set to ''NotReady'' at preparingUpdate state. Currently,
MarkPodNotReady only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
type: object
Expand Down
62 changes: 57 additions & 5 deletions config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ spec:
Pod will be set to ''NotReady'' at preparingDelete/preparingUpdate
state. - Pod will be restored to ''Ready'' at
Updated state if it was set to ''NotReady''
at preparingUpdate state. Default to false.'
at preparingUpdate state. Currently, MarkPodNotReady
only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preDelete:
Expand All @@ -170,7 +172,31 @@ spec:
Pod will be set to ''NotReady'' at preparingDelete/preparingUpdate
state. - Pod will be restored to ''Ready'' at
Updated state if it was set to ''NotReady''
at preparingUpdate state. Default to false.'
at preparingUpdate state. Currently, MarkPodNotReady
only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preNormal:
description: PreNormal is the hook after Pod to be
created and ready to be Normal.
properties:
finalizersHandler:
items:
type: string
type: array
labelsHandler:
additionalProperties:
type: string
type: object
markPodNotReady:
description: 'MarkPodNotReady = true means: -
Pod will be set to ''NotReady'' at preparingDelete/preparingUpdate
state. - Pod will be restored to ''Ready'' at
Updated state if it was set to ''NotReady''
at preparingUpdate state. Currently, MarkPodNotReady
only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
type: object
Expand Down Expand Up @@ -546,7 +572,7 @@ spec:
properties:
lifecycle:
description: Lifecycle defines the lifecycle hooks for
Pods pre-delete, in-place update.
Pods post create, pre-delete, in-place update.
properties:
inPlaceUpdate:
description: InPlaceUpdate is the hook before Pod
Expand All @@ -565,7 +591,9 @@ spec:
Pod will be set to ''NotReady'' at preparingDelete/preparingUpdate
state. - Pod will be restored to ''Ready'' at
Updated state if it was set to ''NotReady''
at preparingUpdate state. Default to false.'
at preparingUpdate state. Currently, MarkPodNotReady
only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preDelete:
Expand All @@ -585,7 +613,31 @@ spec:
Pod will be set to ''NotReady'' at preparingDelete/preparingUpdate
state. - Pod will be restored to ''Ready'' at
Updated state if it was set to ''NotReady''
at preparingUpdate state. Default to false.'
at preparingUpdate state. Currently, MarkPodNotReady
only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
preNormal:
description: PreNormal is the hook after Pod to be
created and ready to be Normal.
properties:
finalizersHandler:
items:
type: string
type: array
labelsHandler:
additionalProperties:
type: string
type: object
markPodNotReady:
description: 'MarkPodNotReady = true means: -
Pod will be set to ''NotReady'' at preparingDelete/preparingUpdate
state. - Pod will be restored to ''Ready'' at
Updated state if it was set to ''NotReady''
at preparingUpdate state. Currently, MarkPodNotReady
only takes effect on InPlaceUpdate & PreDelete
hook. Default to false.'
type: boolean
type: object
type: object
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/cloneset/sync/cloneset_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func (r *realControl) managePreparingDelete(cs *appsv1alpha1.CloneSet, pods, pod
continue
}

klog.V(3).Infof("CloneSet %s patch pod %s lifecycle from PreparingDelete to Normal",
klog.V(3).Infof("CloneSet %s patch pod %s lifecycle from PreparingDelete to Creating",
clonesetutils.GetControllerKey(cs), pod.Name)
if updated, gotPod, err := r.lifecycleControl.UpdatePodLifecycle(pod, appspub.LifecycleStateNormal, false); err != nil {
if updated, gotPod, err := r.lifecycleControl.UpdatePodLifecycle(pod, appspub.LifecycleStatePreparingNormal, false); err != nil {
return modified, err
} else if updated {
modified = true
Expand Down Expand Up @@ -214,7 +214,7 @@ func (r *realControl) createPods(
if clonesetutils.EqualToRevisionHash("", pod, currentRevision) {
cs = currentCS
}
lifecycle.SetPodLifecycle(appspub.LifecycleStateNormal)(pod)
lifecycle.SetPodLifecycle(appspub.LifecycleStatePreparingNormal)(pod)

var createErr error
if createErr = r.createOnePod(cs, pod, existingPVCNames); createErr != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/cloneset/sync/cloneset_scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestCreatePods(t *testing.T) {
apps.ControllerRevisionHashLabelKey: "revision_abc",
apps.DefaultDeploymentUniqueLabelKey: "revision_abc",
"foo": "bar",
appspub.LifecycleStateKey: string(appspub.LifecycleStateNormal),
appspub.LifecycleStateKey: string(appspub.LifecycleStatePreparingNormal),
},
OwnerReferences: []metav1.OwnerReference{
{
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestCreatePods(t *testing.T) {
apps.ControllerRevisionHashLabelKey: "revision_xyz",
apps.DefaultDeploymentUniqueLabelKey: "revision_xyz",
"foo": "bar",
appspub.LifecycleStateKey: string(appspub.LifecycleStateNormal),
appspub.LifecycleStateKey: string(appspub.LifecycleStatePreparingNormal),
},
OwnerReferences: []metav1.OwnerReference{
{
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestCreatePods(t *testing.T) {
apps.ControllerRevisionHashLabelKey: "revision_xyz",
apps.DefaultDeploymentUniqueLabelKey: "revision_xyz",
"foo": "bar",
appspub.LifecycleStateKey: string(appspub.LifecycleStateNormal),
appspub.LifecycleStateKey: string(appspub.LifecycleStatePreparingNormal),
},
OwnerReferences: []metav1.OwnerReference{
{
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/cloneset/sync/cloneset_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func (c *realControl) refreshPodState(cs *appsv1alpha1.CloneSet, coreControl clo

var state appspub.LifecycleStateType
switch lifecycle.GetPodLifecycleState(pod) {
case appspub.LifecycleStatePreparingNormal:
if cs.Spec.Lifecycle == nil ||
cs.Spec.Lifecycle.PreNormal == nil ||
lifecycle.IsPodAllHooked(cs.Spec.Lifecycle.PreNormal, pod) {
state = appspub.LifecycleStateNormal
}
case appspub.LifecycleStateUpdating:
if opts.CheckPodUpdateCompleted(pod) == nil {
if cs.Spec.Lifecycle != nil && !lifecycle.IsPodAllHooked(cs.Spec.Lifecycle.InPlaceUpdate, pod) {
Expand Down Expand Up @@ -235,7 +241,7 @@ func (c *realControl) updatePod(cs *appsv1alpha1.CloneSet, coreControl clonesetc

if c.inplaceControl.CanUpdateInPlace(oldRevision, updateRevision, coreControl.GetUpdateOptions()) {
switch state := lifecycle.GetPodLifecycleState(pod); state {
case "", appspub.LifecycleStateNormal:
case "", appspub.LifecycleStatePreparingNormal, appspub.LifecycleStateNormal:
var err error
var updated bool
var gotPod *v1.Pod
Expand Down
Loading

0 comments on commit ab3d832

Please sign in to comment.