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

Add preNormal hook for Advanced StatefulSet #1858

Merged
merged 2 commits into from
Dec 25, 2024
Merged
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
3 changes: 2 additions & 1 deletion pkg/controller/cloneset/sync/cloneset_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
testingclock "k8s.io/utils/clock/testing"

apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand All @@ -34,7 +35,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
testingclock "k8s.io/utils/clock/testing"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

Expand Down
34 changes: 23 additions & 11 deletions pkg/controller/statefulset/stateful_set_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,11 +770,8 @@
}

func (ssc *defaultStatefulSetControl) refreshPodState(set *appsv1beta1.StatefulSet, pod *v1.Pod, updateRevision string) (bool, time.Duration, error) {
if set.Spec.UpdateStrategy.RollingUpdate == nil {
return false, 0, nil
}
opts := &inplaceupdate.UpdateOptions{}
if set.Spec.UpdateStrategy.RollingUpdate.InPlaceUpdateStrategy != nil {
if set.Spec.UpdateStrategy.RollingUpdate != nil && set.Spec.UpdateStrategy.RollingUpdate.InPlaceUpdateStrategy != nil {
opts.GracePeriodSeconds = set.Spec.UpdateStrategy.RollingUpdate.InPlaceUpdateStrategy.GracePeriodSeconds
}
opts = inplaceupdate.SetOptionsDefaults(opts)
Expand All @@ -788,6 +785,12 @@

var state appspub.LifecycleStateType
switch lifecycle.GetPodLifecycleState(pod) {
case appspub.LifecycleStatePreparingNormal:
if set.Spec.Lifecycle == nil ||
set.Spec.Lifecycle.PreNormal == nil ||
lifecycle.IsPodAllHooked(set.Spec.Lifecycle.PreNormal, pod) {
state = appspub.LifecycleStateNormal
}
case appspub.LifecycleStatePreparingUpdate:
// when pod updated to PreparingUpdate state to wait lifecycle blocker to remove,
// then rollback, do not need update pod inplace since it is the update revision,
Expand Down Expand Up @@ -859,7 +862,7 @@
if ssc.inplaceControl.CanUpdateInPlace(oldRevision, updateRevision, opts) {
state := lifecycle.GetPodLifecycleState(pod)
switch state {
case "", appspub.LifecycleStateNormal:
case "", appspub.LifecycleStatePreparingNormal, appspub.LifecycleStateNormal:
var err error
var updated bool
if set.Spec.Lifecycle != nil && lifecycle.IsPodHooked(set.Spec.Lifecycle.InPlaceUpdate, pod) {
Expand Down Expand Up @@ -1112,7 +1115,15 @@
return true, false, err
}
}
lifecycle.SetPodLifecycle(appspub.LifecycleStateNormal)(replicas[i])
// asts ut invoke updateStatefulset once by once,
// so we can update pod into normal state to avoid changing so many ut cases
state := appspub.LifecycleStatePreparingNormal
if set.Spec.Lifecycle == nil ||
set.Spec.Lifecycle.PreNormal == nil ||
lifecycle.IsPodAllHooked(set.Spec.Lifecycle.PreNormal, replicas[i]) {
state = appspub.LifecycleStateNormal
}
lifecycle.SetPodLifecycle(state)(replicas[i])
if err := ssc.podControl.CreateStatefulPod(ctx, set, replicas[i]); err != nil {
msg := fmt.Sprintf("StatefulPodControl failed to create Pod error: %s", err)
condition := NewStatefulsetCondition(appsv1beta1.FailedCreatePod, v1.ConditionTrue, "", msg)
Expand Down Expand Up @@ -1156,12 +1167,13 @@
}

// Update InPlaceUpdateReady condition for pod
if res := ssc.inplaceControl.Refresh(replicas[i], nil); res.RefreshErr != nil {
logger.Error(res.RefreshErr, "StatefulSet failed to update pod condition for inplace",
_, duration, err := ssc.refreshPodState(set, replicas[i], status.UpdateRevision)
if err != nil {
logger.Error(err, "StatefulSet failed to update pod condition for inplace",

Check warning on line 1172 in pkg/controller/statefulset/stateful_set_control.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/statefulset/stateful_set_control.go#L1172

Added line #L1172 was not covered by tests
"statefulSet", klog.KObj(set), "pod", klog.KObj(replicas[i]))
return true, false, res.RefreshErr
} else if res.DelayDuration > 0 {
durationStore.Push(getStatefulSetKey(set), res.DelayDuration)
return true, false, err

Check warning on line 1174 in pkg/controller/statefulset/stateful_set_control.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/statefulset/stateful_set_control.go#L1174

Added line #L1174 was not covered by tests
} else if duration > 0 {
durationStore.Push(getStatefulSetKey(set), duration)

Check warning on line 1176 in pkg/controller/statefulset/stateful_set_control.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/statefulset/stateful_set_control.go#L1176

Added line #L1176 was not covered by tests
}

// If we have a Pod that has been created but is not running and available we can not make progress.
Expand Down
Loading
Loading