Skip to content

Commit

Permalink
Merge pull request #463 from vshn/consistent-container-names
Browse files Browse the repository at this point in the history
Consistent job naming
  • Loading branch information
bastjan authored Jul 30, 2021
2 parents e3845ad + 9bbe92b commit 7e82781
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 13 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/archive_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (a *Archive) GetMetaObject() metav1.Object {
return a
}

// GetJobName returns the name of the underlying batch/v1 job.
func (a *Archive) GetJobName() string {
return a.GetType().String() + "-" + a.Name
}

func (*Archive) GetType() JobType {
return ArchiveType
}
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/backup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (*Backup) GetType() JobType {
return BackupType
}

// GetJobName returns the name of the underlying batch/v1 job.
func (b *Backup) GetJobName() string {
return b.GetType().String() + "-" + b.Name
}

// GetStatus retrieves the Status property
func (b *Backup) GetStatus() Status {
return b.Status
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/check_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (c *Check) GetMetaObject() metav1.Object {
return c
}

// GetJobName returns the name of the underlying batch/v1 job.
func (c *Check) GetJobName() string {
return c.GetType().String() + "-" + c.Name
}

func (c *Check) GetType() JobType {
return CheckType
}
Expand Down
26 changes: 26 additions & 0 deletions api/v1alpha1/job_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v1alpha1_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vshn/k8up/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestJobName(t *testing.T) {
subjects := map[string]v1alpha1.JobObject{
"archive-job-name": &v1alpha1.Archive{ObjectMeta: metav1.ObjectMeta{Name: "job-name"}},
"backup-job-name": &v1alpha1.Backup{ObjectMeta: metav1.ObjectMeta{Name: "job-name"}},
"check-job-name": &v1alpha1.Check{ObjectMeta: metav1.ObjectMeta{Name: "job-name"}},
"prune-job-name": &v1alpha1.Prune{ObjectMeta: metav1.ObjectMeta{Name: "job-name"}},
"restore-job-name": &v1alpha1.Restore{ObjectMeta: metav1.ObjectMeta{Name: "job-name"}},
"schedule-job-name": &v1alpha1.Schedule{ObjectMeta: metav1.ObjectMeta{Name: "job-name"}},
}

for expectedName, subject := range subjects {
t.Run(expectedName, func(t *testing.T) {
assert.Equal(t, expectedName, subject.GetJobName())
})
}
}
2 changes: 2 additions & 0 deletions api/v1alpha1/job_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type JobObject interface {
GetStatus() Status
SetStatus(s Status)
GetType() JobType
// GetJobName returns the name of the underlying batch/v1 job.
GetJobName() string
GetResources() corev1.ResourceRequirements
}

Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/prune_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func (p *Prune) GetMetaObject() metav1.Object {
return p
}

// GetJobName returns the name of the underlying batch/v1 job.
func (p *Prune) GetJobName() string {
return p.GetType().String() + "-" + p.Name
}

func (p *Prune) GetType() JobType {
return PruneType
}
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/restore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (r *Restore) GetMetaObject() metav1.Object {
return r
}

// GetJobName returns the name of the underlying batch/v1 job.
func (r *Restore) GetJobName() string {
return r.GetType().String() + "-" + r.Name
}

func (r *Restore) GetType() JobType {
return RestoreType
}
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/schedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (s *Schedule) GetMetaObject() metav1.Object {
return s
}

// GetJobName implements the JobObject interface.
func (s *Schedule) GetJobName() string {
return s.GetType().String() + "-" + s.Name
}

func (*Schedule) GetType() JobType {
return ScheduleType
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/backup_it_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (ts *BackupTestSuite) notifyObserverOfBackupJobStatusChange(status k8upObse
event := observer.GetJobByName(ts.BackupResource.Namespace + "/" + ts.BackupResource.Name)
event.Job = &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: ts.BackupResource.Name,
Name: ts.BackupResource.GetJobName(),
Namespace: ts.BackupResource.Namespace,
},
}
Expand Down
18 changes: 9 additions & 9 deletions controllers/check_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func (ts *CheckTestSuite) TestJobCleanup() {
successfulJobs := 2
failedJobs := 3
for i := 0; i < successfulJobs; i++ {
ts.whenJobCallbackIsInvoked(ts.CheckNames[i], observer.Succeeded)
ts.whenJobCallbackIsInvoked(ts.GivenChecks[i], observer.Succeeded)
}
for i := successfulJobs; i < successfulJobs+failedJobs; i++ {
ts.whenJobCallbackIsInvoked(ts.CheckNames[i], observer.Failed)
ts.whenJobCallbackIsInvoked(ts.GivenChecks[i], observer.Failed)
}

ts.expectCheckCleanupEventually((successfulJobs - ts.KeepSuccessful) + (failedJobs - ts.KeepFailed))
Expand Down Expand Up @@ -110,16 +110,16 @@ func (ts *CheckTestSuite) expectCheckCleanupEventually(expectedDeletes int) {
})
}

func (ts *CheckTestSuite) whenJobCallbackIsInvoked(checkName string, evtType observer.EventType) {
checkNSName := types.NamespacedName{Name: checkName, Namespace: ts.NS}
func (ts *CheckTestSuite) whenJobCallbackIsInvoked(check k8upv1a1.JobObject, evtType observer.EventType) {
checkNSName := types.NamespacedName{Name: check.GetJobName(), Namespace: ts.NS}

check := &batchv1.Job{}
ts.FetchResource(checkNSName, check)
childJob := &batchv1.Job{}
ts.FetchResource(checkNSName, childJob)

o := observer.GetObserver()
observableJob := o.GetJobByName(checkNSName.String())
observableJob.Event = evtType
observableJob.Job = check
observableJob.Job = childJob

eventChannel := o.GetUpdateChannel()
eventChannel <- observableJob
Expand All @@ -136,7 +136,7 @@ func (ts *CheckTestSuite) givenCheckResources(amount int) {
}

func (ts *CheckTestSuite) whenReconcile() (lastResult controllerruntime.Result) {
for _, checkName := range ts.CheckNames {
for _, check := range ts.GivenChecks {
controller := controllers.CheckReconciler{
Client: ts.Client,
Log: ts.Logger,
Expand All @@ -145,7 +145,7 @@ func (ts *CheckTestSuite) whenReconcile() (lastResult controllerruntime.Result)

key := types.NamespacedName{
Namespace: ts.NS,
Name: checkName,
Name: check.GetMetaObject().GetName(),
}
request := controllerruntime.Request{
NamespacedName: key,
Expand Down
2 changes: 1 addition & 1 deletion executor/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (g *generic) GetJobNamespace() string {
}

func (g *generic) GetJobNamespacedName() types.NamespacedName {
return types.NamespacedName{Namespace: g.Obj.GetMetaObject().GetNamespace(), Name: g.Obj.GetMetaObject().GetName()}
return types.NamespacedName{Namespace: g.Obj.GetMetaObject().GetNamespace(), Name: g.Obj.GetJobName()}
}

func (g *generic) GetJobType() k8upv1alpha1.JobType {
Expand Down
4 changes: 2 additions & 2 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewConfig(ctx context.Context, client client.Client, log logr.Logger, obj k
func GenerateGenericJob(obj k8upv1alpha1.JobObject, config Config) (*batchv1.Job, error) {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: obj.GetMetaObject().GetName(),
Name: obj.GetJobName(),
Namespace: obj.GetMetaObject().GetNamespace(),
Labels: map[string]string{
K8uplabel: "true",
Expand All @@ -65,7 +65,7 @@ func GenerateGenericJob(obj k8upv1alpha1.JobObject, config Config) (*batchv1.Job
RestartPolicy: corev1.RestartPolicyOnFailure,
Containers: []corev1.Container{
{
Name: obj.GetMetaObject().GetName(),
Name: obj.GetType().String(),
Image: cfg.Config.BackupImage,
Resources: config.Obj.GetResources(),
},
Expand Down

0 comments on commit 7e82781

Please sign in to comment.