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

Check if no. of failed pods exceeded BackOffLimit #500

Merged
merged 2 commits into from
Dec 8, 2020
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
4 changes: 2 additions & 2 deletions pkg/controller/command/command_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func (r *ReconcileCommand) checkDataMigrationCompleted(command *contrail.Command
err = r.client.Get(context.Background(), jobName, dataMigrationJob)
exists := err == nil
if exists {
if job.Status(dataMigrationJob.Status).JobComplete() {
if job.Job(*dataMigrationJob).JobCompleted() {
return true, true, nil
}
if job.Status(dataMigrationJob.Status).JobFailed() {
if job.Job(*dataMigrationJob).JobFailed() {
return true, false, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/swift/swift_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (r *ReconcileSwift) removeRingReconcilingJobs(swiftName types.NamespacedNam
err := r.client.Get(context.Background(), jobName, ringJob)
existingJob := err == nil
if existingJob {
if job.Status(ringJob.Status).JobPending() {
if job.Job(*ringJob).JobPending() {
// Wait until job finish executing to avoid breaking the ongoing ring reconciliation
return reconcile.Result{
Requeue: true,
Expand Down
26 changes: 14 additions & 12 deletions pkg/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,42 @@ import (
v1 "k8s.io/api/core/v1"
)

type Status batch.JobStatus
type Job batch.Job

func (s Status) JobPending() bool {
if len(s.Conditions) == 0 {
func (j Job) JobPending() bool {
if len(j.Status.Conditions) == 0 {
return true
}
for _, condition := range s.Conditions {
for _, condition := range j.Status.Conditions {
if condition.Status == v1.ConditionTrue && (condition.Type == batch.JobComplete || condition.Type == batch.JobFailed) {
return false
}
}
return true
}

func (s Status) JobComplete() bool {
if len(s.Conditions) == 0 {
func (j Job) JobCompleted() bool {
if len(j.Status.Conditions) == 0 {
return false
}
for _, condition := range s.Conditions {
for _, condition := range j.Status.Conditions {
if condition.Status == v1.ConditionTrue && condition.Type == batch.JobComplete {
return true
}
}
return false
}

func (s Status) JobFailed() bool {
if len(s.Conditions) == 0 {
return false
}
for _, condition := range s.Conditions {
func (j Job) JobFailed() bool {
for _, condition := range j.Status.Conditions {
if condition.Status == v1.ConditionTrue && condition.Type == batch.JobFailed {
return true
}
}
// This is a workaround for k8s bug that Status of Job may be updated with a delay
// and number of pods ran can exceed BackOffLimit specified in Job.Spec
if j.Spec.BackoffLimit != nil && *j.Spec.BackoffLimit <= j.Status.Failed {
return true
}
return false
}
62 changes: 40 additions & 22 deletions pkg/job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,80 @@ import (

func TestStatus_Pending(t *testing.T) {
tests := map[string]struct {
jobStatus batch.JobStatus
job batch.Job
expectedPending bool
}{
"no conditions": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{},
},
},
expectedPending: true,
},
"nil conditions": {
jobStatus: batch.JobStatus{
Conditions: nil,
job: batch.Job{
Status: batch.JobStatus{
Conditions: nil,
},
},
expectedPending: true,
},
"condition type Failed, status True": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobFailed, Status: v1.ConditionTrue}},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobFailed, Status: v1.ConditionTrue}},
},
},
expectedPending: false,
},
"condition type Failed, status False": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobFailed, Status: v1.ConditionFalse}},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobFailed, Status: v1.ConditionFalse}},
},
},
expectedPending: true,
},
"condition type fake, status true": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: "fake", Status: v1.ConditionTrue}},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: "fake", Status: v1.ConditionTrue}},
},
},
expectedPending: true,
},
"condition type fake, status false": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: "fake", Status: v1.ConditionFalse}},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: "fake", Status: v1.ConditionFalse}},
},
},
expectedPending: true,
},
"condition type Complete, status True": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobComplete, Status: v1.ConditionTrue}},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobComplete, Status: v1.ConditionTrue}},
},
},
expectedPending: false,
},
"condition type Complete, status False": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobComplete, Status: v1.ConditionFalse}},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{{Type: batch.JobComplete, Status: v1.ConditionFalse}},
},
},
expectedPending: true,
},
"multiple conditions": {
jobStatus: batch.JobStatus{
Conditions: []batch.JobCondition{
{Type: batch.JobComplete, Status: v1.ConditionFalse},
{Type: batch.JobFailed, Status: v1.ConditionTrue},
job: batch.Job{
Status: batch.JobStatus{
Conditions: []batch.JobCondition{
{Type: batch.JobComplete, Status: v1.ConditionFalse},
{Type: batch.JobFailed, Status: v1.ConditionTrue},
},
},
},
expectedPending: false,
Expand All @@ -76,7 +94,7 @@ func TestStatus_Pending(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// when
pending := job.Status(test.jobStatus).JobPending()
pending := job.Job(test.job).JobPending()
// then
assert.Equal(t, test.expectedPending, pending)
})
Expand Down
20 changes: 12 additions & 8 deletions test/e2e/ha/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ func TestHACommand(t *testing.T) {

t.Run("when upgrade to invalid image is performed", func(t *testing.T) {
badImage := "registry:5000/common-docker-third-party/contrail/busybox:1.31"
require.NoError(t, f.Client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: "command-ha"}, cluster))
containers := cluster.Spec.Services.Command.Spec.ServiceConfiguration.Containers
utils.GetContainerFromList("api", containers).Image = badImage
require.NoError(t, f.Client.Update(context.TODO(), cluster))
_, err = controllerutil.CreateOrUpdate(context.Background(), f.Client.Client, cluster, func() error {
containers := cluster.Spec.Services.Command.Spec.ServiceConfiguration.Containers
utils.GetContainerFromList("api", containers).Image = badImage
return nil
})
require.NoError(t, err)

t.Run("then command reports failed upgrade", func(t *testing.T) {
err := wait.Contrail{
Expand All @@ -200,10 +202,12 @@ func TestHACommand(t *testing.T) {

t.Run("when previous image is restored", func(t *testing.T) {
goodImage := "registry:5000/contrail-nightly/contrail-command:" + cemRelease
require.NoError(t, f.Client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: "command-ha"}, cluster))
containers := cluster.Spec.Services.Command.Spec.ServiceConfiguration.Containers
utils.GetContainerFromList("api", containers).Image = goodImage
require.NoError(t, f.Client.Update(context.TODO(), cluster))
_, err = controllerutil.CreateOrUpdate(context.Background(), f.Client.Client, cluster, func() error {
containers := cluster.Spec.Services.Command.Spec.ServiceConfiguration.Containers
utils.GetContainerFromList("api", containers).Image = goodImage
return nil
})
require.NoError(t, err)

t.Run("then command reports not upgrading state", func(t *testing.T) {
err := wait.Contrail{
Expand Down