Skip to content

Commit

Permalink
Recover the policy name
Browse files Browse the repository at this point in the history
Signed-off-by: kerthcet <kerthcet@gmail.com>
  • Loading branch information
kerthcet committed Sep 13, 2024
1 parent 3892afa commit 1b9e7b7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions api/leaderworkerset/v1/leaderworkerset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ const (
type RestartPolicyType string

const (
// DefaultRestartPolicy will recreate all the pods in the group if
// RecreateGroupOnPodRestart will recreate all the pods in the group if
// 1. Any individual pod in the group is recreated; 2. Any containers/init-containers
// in a pod is restarted. This is to ensure all pods/containers in the group will be
// started in the same time.
DefaultRestartPolicy RestartPolicyType = "RecreateGroupOnPodRestart"
RecreateGroupOnPodRestart RestartPolicyType = "RecreateGroupOnPodRestart"

// Default will follow the same behavior as the StatefulSet where only the failed pod
// will be restarted on failure and other pods in the group will not be impacted.
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/leaderworkerset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestLeaderStatefulSetApplyConfig(t *testing.T) {
}).
WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).
Size(1).
RestartPolicy(leaderworkerset.DefaultRestartPolicy).Obj(),
RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).Obj(),
wantApplyConfig: &appsapplyv1.StatefulSetApplyConfiguration{
TypeMetaApplyConfiguration: metaapplyv1.TypeMetaApplyConfiguration{
Kind: ptr.To[string]("StatefulSet"),
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestLeaderStatefulSetApplyConfig(t *testing.T) {
}).
WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).
Size(2).
RestartPolicy(leaderworkerset.DefaultRestartPolicy).Obj(),
RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).Obj(),
wantApplyConfig: &appsapplyv1.StatefulSetApplyConfiguration{
TypeMetaApplyConfiguration: metaapplyv1.TypeMetaApplyConfiguration{
Kind: ptr.To[string]("StatefulSet"),
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestLeaderStatefulSetApplyConfig(t *testing.T) {
WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).
LeaderTemplateSpec(testutils.MakeLeaderPodSpec()).
Size(2).
RestartPolicy(leaderworkerset.DefaultRestartPolicy).Obj(),
RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).Obj(),
wantApplyConfig: &appsapplyv1.StatefulSetApplyConfiguration{
TypeMetaApplyConfiguration: metaapplyv1.TypeMetaApplyConfiguration{
Kind: ptr.To[string]("StatefulSet"),
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestLeaderStatefulSetApplyConfig(t *testing.T) {
}).
WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).
Size(1).
RestartPolicy(leaderworkerset.DefaultRestartPolicy).Obj(),
RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).Obj(),
wantApplyConfig: &appsapplyv1.StatefulSetApplyConfiguration{
TypeMetaApplyConfiguration: metaapplyv1.TypeMetaApplyConfiguration{
Kind: ptr.To[string]("StatefulSet"),
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestLeaderStatefulSetApplyConfig(t *testing.T) {
WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).
LeaderTemplateSpec(testutils.MakeLeaderPodSpec()).
Size(2).
RestartPolicy(leaderworkerset.DefaultRestartPolicy).Obj(),
RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).Obj(),
wantApplyConfig: &appsapplyv1.StatefulSetApplyConfiguration{
TypeMetaApplyConfiguration: metaapplyv1.TypeMetaApplyConfiguration{
Kind: ptr.To[string]("StatefulSet"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}

func (r *PodReconciler) handleRestartPolicy(ctx context.Context, pod corev1.Pod, leaderWorkerSet leaderworkerset.LeaderWorkerSet) (bool, error) {
if leaderWorkerSet.Spec.LeaderWorkerTemplate.RestartPolicy != leaderworkerset.DefaultRestartPolicy {
if leaderWorkerSet.Spec.LeaderWorkerTemplate.RestartPolicy != leaderworkerset.RecreateGroupOnPodRestart {
return false, nil
}
// the leader pod will be deleted if the worker pod is deleted or any containes were restarted
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhooks/leaderworkerset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ webhook.CustomDefaulter = &LeaderWorkerSetWebhook{}
func (r *LeaderWorkerSetWebhook) Default(ctx context.Context, obj runtime.Object) error {
lws := obj.(*v1.LeaderWorkerSet)
if lws.Spec.LeaderWorkerTemplate.RestartPolicy == "" {
lws.Spec.LeaderWorkerTemplate.RestartPolicy = v1.DefaultRestartPolicy
lws.Spec.LeaderWorkerTemplate.RestartPolicy = v1.RecreateGroupOnPodRestart
}

if lws.Spec.LeaderWorkerTemplate.RestartPolicy == v1.DeprecatedDefaultRestartPolicy {
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var _ = ginkgo.Describe("leaderWorkerSet e2e tests", func() {
})

ginkgo.It("Can create/update a lws with size=1", func() {
lws = testing.BuildLeaderWorkerSet(ns.Name).Replica(4).MaxSurge(1).Size(1).RestartPolicy(v1.DefaultRestartPolicy).Obj()
lws = testing.BuildLeaderWorkerSet(ns.Name).Replica(4).MaxSurge(1).Size(1).RestartPolicy(v1.RecreateGroupOnPodRestart).Obj()
testing.MustCreateLws(ctx, k8sClient, lws)

testing.ExpectValidLeaderStatefulSet(ctx, k8sClient, lws, 4)
Expand Down Expand Up @@ -262,8 +262,8 @@ var _ = ginkgo.Describe("leaderWorkerSet e2e tests", func() {
}
})

ginkgo.It("Pod restart will delete the pod group when restart policy is DefaultRestartPolicy", func() {
lws = testing.BuildLeaderWorkerSet(ns.Name).Replica(1).Size(3).RestartPolicy(v1.DefaultRestartPolicy).Obj()
ginkgo.It("Pod restart will delete the pod group when restart policy is RecreateGroupOnPodRestart", func() {
lws = testing.BuildLeaderWorkerSet(ns.Name).Replica(1).Size(3).RestartPolicy(v1.RecreateGroupOnPodRestart).Obj()
testing.MustCreateLws(ctx, k8sClient, lws)
testing.ExpectLeaderWorkerSetAvailable(ctx, k8sClient, lws, "All replicas are ready")

Expand Down
4 changes: 2 additions & 2 deletions test/integration/controllers/leaderworkerset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ var _ = ginkgo.Describe("LeaderWorkerSet controller", func() {
},
},
}),
ginkgo.Entry("Pod restart will delete the pod group when restart policy is DefaultRestartPolicy", &testCase{
ginkgo.Entry("Pod restart will delete the pod group when restart policy is RecreateGroupOnPodRestart", &testCase{
makeLeaderWorkerSet: func(nsName string) *testing.LeaderWorkerSetWrapper {
return testing.BuildLeaderWorkerSet(nsName).RestartPolicy(leaderworkerset.DefaultRestartPolicy).Replica(1).Size(3)
return testing.BuildLeaderWorkerSet(nsName).RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).Replica(1).Size(3)
},
updates: []*update{
{
Expand Down
12 changes: 6 additions & 6 deletions test/integration/webhooks/leaderworkerset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _ = ginkgo.Describe("leaderworkerset defaulting, creation and update", func(
return lwsWrapper
},
getExpectedLWS: func(lws *leaderworkerset.LeaderWorkerSet) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(1).RestartPolicy(leaderworkerset.DefaultRestartPolicy)
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(1).RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart)
},
}),
ginkgo.Entry("apply defaulting logic for size", &testDefaultingCase{
Expand All @@ -81,23 +81,23 @@ var _ = ginkgo.Describe("leaderworkerset defaulting, creation and update", func(
return lwsWrapper
},
getExpectedLWS: func(lws *leaderworkerset.LeaderWorkerSet) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).Size(1).RestartPolicy(leaderworkerset.DefaultRestartPolicy)
return testutils.BuildLeaderWorkerSet(ns.Name).Size(1).RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart)
},
}),
ginkgo.Entry("defaulting logic won't apply when shouldn't", &testDefaultingCase{
makeLeaderWorkerSet: func(ns *corev1.Namespace) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(2).Size(2)
},
getExpectedLWS: func(lws *leaderworkerset.LeaderWorkerSet) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(2).Size(2).LeaderTemplateSpec(testutils.MakeLeaderPodSpec()).WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).RestartPolicy(leaderworkerset.DefaultRestartPolicy)
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(2).Size(2).LeaderTemplateSpec(testutils.MakeLeaderPodSpec()).WorkerTemplateSpec(testutils.MakeWorkerPodSpec()).RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart)
},
}),
ginkgo.Entry("defaulting logic applies when leaderworkertemplate.restartpolicy is not set", &testDefaultingCase{
makeLeaderWorkerSet: func(ns *corev1.Namespace) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(2).Size(2).RestartPolicy("")
},
getExpectedLWS: func(lws *leaderworkerset.LeaderWorkerSet) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(2).Size(2).RestartPolicy(leaderworkerset.DefaultRestartPolicy)
return testutils.BuildLeaderWorkerSet(ns.Name).Replica(2).Size(2).RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart)
},
}),
ginkgo.Entry("defaulting logic won't apply when leaderworkertemplate.restartpolicy is set", &testDefaultingCase{
Expand Down Expand Up @@ -147,7 +147,7 @@ var _ = ginkgo.Describe("leaderworkerset defaulting, creation and update", func(
return testutils.BuildLeaderWorkerSet(ns.Name).RolloutStrategy(leaderworkerset.RolloutStrategy{}) // unset rollout strategy
},
getExpectedLWS: func(lws *leaderworkerset.LeaderWorkerSet) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).RestartPolicy(leaderworkerset.DefaultRestartPolicy).RolloutStrategy(leaderworkerset.RolloutStrategy{
return testutils.BuildLeaderWorkerSet(ns.Name).RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).RolloutStrategy(leaderworkerset.RolloutStrategy{
Type: leaderworkerset.RollingUpdateStrategyType,
RollingUpdateConfiguration: &leaderworkerset.RollingUpdateConfiguration{
MaxUnavailable: intstr.FromInt32(1),
Expand All @@ -167,7 +167,7 @@ var _ = ginkgo.Describe("leaderworkerset defaulting, creation and update", func(
},
getExpectedLWS: func(lws *leaderworkerset.LeaderWorkerSet) *testutils.LeaderWorkerSetWrapper {
return testutils.BuildLeaderWorkerSet(ns.Name).
RestartPolicy(leaderworkerset.DefaultRestartPolicy).
RestartPolicy(leaderworkerset.RecreateGroupOnPodRestart).
RolloutStrategy(leaderworkerset.RolloutStrategy{
Type: leaderworkerset.RollingUpdateStrategyType,
RollingUpdateConfiguration: &leaderworkerset.RollingUpdateConfiguration{
Expand Down

0 comments on commit 1b9e7b7

Please sign in to comment.