-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #462 from vshn/improve-monitoring
Clean histories for successful and failed jobs separately
- Loading branch information
Showing
55 changed files
with
1,528 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package v1alpha1_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/vshn/k8up/api/v1alpha1" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type limiter interface { | ||
GetSuccessfulJobsHistoryLimit() *int | ||
GetFailedJobsHistoryLimit() *int | ||
} | ||
|
||
var historyLimitTestCases = map[string]func(successful, failed, deprecatedKeep *int) limiter{ | ||
"Archive": func(successful, failed, deprecatedKeep *int) limiter { | ||
return &v1alpha1.Archive{Spec: v1alpha1.ArchiveSpec{RestoreSpec: &v1alpha1.RestoreSpec{SuccessfulJobsHistoryLimit: successful, FailedJobsHistoryLimit: failed, KeepJobs: deprecatedKeep}}} | ||
}, | ||
"Backup": func(successful, failed, deprecatedKeep *int) limiter { | ||
return &v1alpha1.Backup{Spec: v1alpha1.BackupSpec{SuccessfulJobsHistoryLimit: successful, FailedJobsHistoryLimit: failed, KeepJobs: deprecatedKeep}} | ||
}, | ||
"Check": func(successful, failed, deprecatedKeep *int) limiter { | ||
return &v1alpha1.Check{Spec: v1alpha1.CheckSpec{SuccessfulJobsHistoryLimit: successful, FailedJobsHistoryLimit: failed, KeepJobs: deprecatedKeep}} | ||
}, | ||
"Prune": func(successful, failed, deprecatedKeep *int) limiter { | ||
return &v1alpha1.Prune{Spec: v1alpha1.PruneSpec{SuccessfulJobsHistoryLimit: successful, FailedJobsHistoryLimit: failed, KeepJobs: deprecatedKeep}} | ||
}, | ||
"Restore": func(successful, failed, deprecatedKeep *int) limiter { | ||
return &v1alpha1.Restore{Spec: v1alpha1.RestoreSpec{SuccessfulJobsHistoryLimit: successful, FailedJobsHistoryLimit: failed, KeepJobs: deprecatedKeep}} | ||
}, | ||
} | ||
|
||
func TestHistoryLimits(t *testing.T) { | ||
for name, createSpec := range historyLimitTestCases { | ||
t.Run(name, func(t *testing.T) { | ||
failedLimit := 1 | ||
successLimit := 2 | ||
keepJobs := 3 | ||
|
||
t.Run("JobsHistoryLimit", func(t *testing.T) { | ||
limits := createSpec(&successLimit, &failedLimit, &keepJobs) | ||
assert.Equal(t, *limits.GetFailedJobsHistoryLimit(), failedLimit) | ||
assert.Equal(t, *limits.GetSuccessfulJobsHistoryLimit(), successLimit) | ||
}) | ||
t.Run("fallback to deprecated KeepJobs", func(t *testing.T) { | ||
limits := createSpec(nil, nil, &keepJobs) | ||
assert.Equal(t, *limits.GetFailedJobsHistoryLimit(), keepJobs) | ||
assert.Equal(t, *limits.GetSuccessfulJobsHistoryLimit(), keepJobs) | ||
}) | ||
t.Run("no fallback value", func(t *testing.T) { | ||
limits := createSpec(nil, nil, nil) | ||
var nilInt *int | ||
assert.Equal(t, limits.GetFailedJobsHistoryLimit(), nilInt) | ||
assert.Equal(t, limits.GetSuccessfulJobsHistoryLimit(), nilInt) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// +k8s:deepcopy-gen=false | ||
|
||
// JobObject is an interface that must be implemented by all CRDs that implement a job. | ||
type JobObject interface { | ||
GetMetaObject() metav1.Object | ||
GetRuntimeObject() runtime.Object | ||
GetStatus() Status | ||
SetStatus(s Status) | ||
GetType() JobType | ||
GetResources() corev1.ResourceRequirements | ||
} | ||
|
||
// +k8s:deepcopy-gen=false | ||
|
||
// JobObjectList is a sortable list of job objects | ||
type JobObjectList []JobObject | ||
|
||
func (jo JobObjectList) Len() int { return len(jo) } | ||
func (jo JobObjectList) Swap(i, j int) { jo[i], jo[j] = jo[j], jo[i] } | ||
|
||
func (jo JobObjectList) Less(i, j int) bool { | ||
if jo[i].GetMetaObject().GetCreationTimestamp().Time.Equal(jo[j].GetMetaObject().GetCreationTimestamp().Time) { | ||
return jo[i].GetMetaObject().GetName() < jo[j].GetMetaObject().GetName() | ||
} | ||
return jo[i].GetMetaObject().GetCreationTimestamp().Time.Before(jo[j].GetMetaObject().GetCreationTimestamp().Time) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package v1alpha1_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/vshn/k8up/api/v1alpha1" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestGetObjectList(t *testing.T) { | ||
|
||
type jobObjectList interface { | ||
GetJobObjects() v1alpha1.JobObjectList | ||
} | ||
|
||
testCases := map[string]struct { | ||
desc string | ||
createList func(itemName1, itemName2 string) jobObjectList | ||
}{ | ||
"Archive": { | ||
createList: func(itemName1, itemName2 string) jobObjectList { | ||
return &v1alpha1.ArchiveList{ | ||
Items: []v1alpha1.Archive{ | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName1}}, | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName2}}, | ||
}, | ||
} | ||
}, | ||
}, | ||
"Backup": { | ||
createList: func(itemName1, itemName2 string) jobObjectList { | ||
return &v1alpha1.BackupList{ | ||
Items: []v1alpha1.Backup{ | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName1}}, | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName2}}, | ||
}, | ||
} | ||
}, | ||
}, | ||
"Check": { | ||
createList: func(itemName1, itemName2 string) jobObjectList { | ||
return &v1alpha1.CheckList{ | ||
Items: []v1alpha1.Check{ | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName1}}, | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName2}}, | ||
}, | ||
} | ||
}, | ||
}, | ||
"Prune": { | ||
createList: func(itemName1, itemName2 string) jobObjectList { | ||
return &v1alpha1.PruneList{ | ||
Items: []v1alpha1.Prune{ | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName1}}, | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName2}}, | ||
}, | ||
} | ||
}, | ||
}, | ||
"Restore": { | ||
createList: func(itemName1, itemName2 string) jobObjectList { | ||
return &v1alpha1.RestoreList{ | ||
Items: []v1alpha1.Restore{ | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName1}}, | ||
{ObjectMeta: v1.ObjectMeta{Name: itemName2}}, | ||
}, | ||
} | ||
}, | ||
}, | ||
} | ||
for name, tC := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
name1 := "obj1" | ||
name2 := "obj2" | ||
list := tC.createList(name1, name2).GetJobObjects() | ||
assert.Equal(t, name1, list[0].GetMetaObject().GetName()) | ||
assert.Equal(t, name2, list[1].GetMetaObject().GetName()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.