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

session: fix a logic error in scheduling #6018

Merged
merged 1 commit into from
Jan 17, 2023
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
2 changes: 1 addition & 1 deletion internal/controllers/core/session/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *Reconciler) k8sRuntimeTarget(mt *store.ManifestTarget, ci *v1alpha1.Ses

// Use the ctrl.Result to schedule a reconcile.
requeueAfter := k8sGracePeriod - graceSoFar
if result.RequeueAfter == 0 || result.RequeueAfter < requeueAfter {
if result.RequeueAfter == 0 || result.RequeueAfter > requeueAfter {
result.RequeueAfter = requeueAfter
}
}
Expand Down
106 changes: 79 additions & 27 deletions internal/controllers/core/session/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,7 @@ func TestExitControlCI_GracePeriod(t *testing.T) {
session.Spec.CI = &v1alpha1.SessionCISpec{K8sGracePeriod: &metav1.Duration{Duration: time.Minute}}
f.Update(&session)

m := manifestbuilder.New(f, "fe").WithK8sYAML(testyaml.SanchoYAML).Build()
f.upsertManifest(m)
f.Store.WithState(func(state *store.EngineState) {
mt := state.ManifestTargets["fe"]
mt.State.AddCompletedBuild(model.BuildRecord{
StartTime: f.clock.Now(),
FinishTime: f.clock.Now(),
})
mt.State.LastSuccessfulDeployTime = f.clock.Now()
mt.State.RuntimeState = store.NewK8sRuntimeStateWithPods(mt.Manifest, v1alpha1.Pod{
Name: "pod-a",
Status: "ErrImagePull",
Containers: []v1alpha1.Container{
{
Name: "c1",
State: v1alpha1.ContainerState{
Terminated: &v1alpha1.ContainerStateTerminated{
StartedAt: apis.NewTime(f.clock.Now()),
FinishedAt: apis.NewTime(f.clock.Now()),
Reason: "Error",
ExitCode: 127,
},
},
},
},
})
})
f.upsertFailingPod("fe")

f.MustReconcile(sessionKey)
f.requireNotDone()
Expand Down Expand Up @@ -613,6 +587,54 @@ func TestStatusDisabled(t *testing.T) {
}
}

func TestRequeueLongGracePeriod(t *testing.T) {
f := newFixture(t, store.EngineModeCI)

var session v1alpha1.Session
f.MustGet(types.NamespacedName{Name: "Tiltfile"}, &session)
session.Spec.CI = &v1alpha1.SessionCISpec{
Timeout: &metav1.Duration{Duration: time.Minute},
K8sGracePeriod: &metav1.Duration{Duration: 10 * time.Minute},
}
f.Update(&session)

f.upsertFailingPod("fe")

result, err := f.Reconcile(sessionKey)
require.NoError(t, err)
assert.Equal(t, time.Minute, result.RequeueAfter)

f.clock.Advance(50 * time.Second)

result, err = f.Reconcile(sessionKey)
require.NoError(t, err)
assert.Equal(t, 10*time.Second, result.RequeueAfter)
}

func TestRequeueLongTimeout(t *testing.T) {
f := newFixture(t, store.EngineModeCI)

var session v1alpha1.Session
f.MustGet(types.NamespacedName{Name: "Tiltfile"}, &session)
session.Spec.CI = &v1alpha1.SessionCISpec{
Timeout: &metav1.Duration{Duration: 10 * time.Minute},
K8sGracePeriod: &metav1.Duration{Duration: time.Minute},
}
f.Update(&session)

f.upsertFailingPod("fe")

result, err := f.Reconcile(sessionKey)
require.NoError(t, err)
assert.Equal(t, time.Minute, result.RequeueAfter)

f.clock.Advance(50 * time.Second)

result, err = f.Reconcile(sessionKey)
require.NoError(t, err)
assert.Equal(t, 10*time.Second, result.RequeueAfter)
}

type fixture struct {
*fake.ControllerFixture
tf *tempdir.TempDirFixture
Expand Down Expand Up @@ -700,6 +722,36 @@ func (f *fixture) Path() string {
return f.tf.Path()
}

func (f *fixture) upsertFailingPod(mn model.ManifestName) {
m := manifestbuilder.New(f, mn).WithK8sYAML(testyaml.SanchoYAML).Build()
f.upsertManifest(m)
f.Store.WithState(func(state *store.EngineState) {
mt := state.ManifestTargets[mn]
mt.State.AddCompletedBuild(model.BuildRecord{
StartTime: f.clock.Now(),
FinishTime: f.clock.Now(),
})
mt.State.LastSuccessfulDeployTime = f.clock.Now()
mt.State.RuntimeState = store.NewK8sRuntimeStateWithPods(mt.Manifest, v1alpha1.Pod{
Name: "pod-a",
Status: "ErrImagePull",
Containers: []v1alpha1.Container{
{
Name: "c1",
State: v1alpha1.ContainerState{
Terminated: &v1alpha1.ContainerStateTerminated{
StartedAt: apis.NewTime(f.clock.Now()),
FinishedAt: apis.NewTime(f.clock.Now()),
Reason: "Error",
ExitCode: 127,
},
},
},
},
})
})
}

func pod(podID k8s.PodID, ready bool) v1alpha1.Pod {
return v1alpha1.Pod{
Name: podID.String(),
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/core/session/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (r *Reconciler) makeLatestStatus(session *v1alpha1.Session, result *ctrl.Re
if ci != nil && ci.Timeout != nil && ci.Timeout.Duration > 0 {
timeout := ci.Timeout.Duration
requeueAfter := timeout - r.clock.Since(session.Status.StartTime.Time)
if result.RequeueAfter == 0 || result.RequeueAfter < requeueAfter {
if result.RequeueAfter == 0 || result.RequeueAfter > requeueAfter {
result.RequeueAfter = requeueAfter
}
}
Expand Down