Skip to content

Commit

Permalink
add postCreateHook
Browse files Browse the repository at this point in the history
Signed-off-by: mingzhou.swx <[email protected]>
  • Loading branch information
mingzhou.swx committed Oct 25, 2022
1 parent c5b63fa commit 9f5d61e
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 27 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"

LifecycleStateCreating LifecycleStateType = "Creating"
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"`
// PostCreated is the hook after Pod to be created and ready to be Normal.
PostCreated *LifecycleHook `json:"postCreated,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.

31 changes: 27 additions & 4 deletions config/crd/bases/apps.kruise.io_clonesets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,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
postCreated:
description: PostCreated 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
preDelete:
Expand All @@ -120,8 +142,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
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,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
postCreated:
description: PostCreated 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
preDelete:
Expand All @@ -124,8 +146,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
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,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
postCreated:
description: PostCreated 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
preDelete:
Expand All @@ -537,8 +559,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
type: object
Expand Down
60 changes: 56 additions & 4 deletions config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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
postCreated:
description: PostCreated 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
preDelete:
Expand All @@ -170,7 +194,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
type: object
Expand Down Expand Up @@ -565,7 +591,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
postCreated:
description: PostCreated 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
preDelete:
Expand All @@ -585,7 +635,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
type: object
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
k8s.io/component-base v0.22.6
k8s.io/component-helpers v0.22.6
k8s.io/cri-api v0.22.6
k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027
k8s.io/klog/v2 v2.9.0
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c
k8s.io/kubernetes v1.22.6
Expand Down Expand Up @@ -130,7 +131,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/cloud-provider v0.22.6 // indirect
k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027 // indirect
k8s.io/kube-scheduler v0.0.0 // indirect
k8s.io/mount-utils v0.22.6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
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.LifecycleStateCreating, 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.LifecycleStateCreating)(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.LifecycleStateCreating),
},
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.LifecycleStateCreating),
},
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.LifecycleStateCreating),
},
OwnerReferences: []metav1.OwnerReference{
{
Expand Down
9 changes: 8 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,13 @@ func (c *realControl) refreshPodState(cs *appsv1alpha1.CloneSet, coreControl clo

var state appspub.LifecycleStateType
switch lifecycle.GetPodLifecycleState(pod) {
case appspub.LifecycleStateCreating:
if cs.Spec.Lifecycle == nil ||
cs.Spec.Lifecycle.PostCreated == nil ||
lifecycle.IsPodAllHooked(cs.Spec.Lifecycle.PostCreated, 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 +242,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.LifecycleStateCreating, appspub.LifecycleStateNormal:
var err error
var updated bool
var gotPod *v1.Pod
Expand Down
Loading

0 comments on commit 9f5d61e

Please sign in to comment.