Skip to content
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
13 changes: 6 additions & 7 deletions internal/job/createfinbackup/createfinbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func (c *CreateFinBackup) Perform() error {
}

// getJobCreationTimestamp retrieves the Job creation timestamp from the
// batch.kubernetes.io/cronjob-scheduled-timestamp annotation
// batch.kubernetes.io/cronjob-scheduled-timestamp annotation.
// If the annotation is not found, it falls back to the Job's CreationTimestamp.
func (c *CreateFinBackup) getJobCreationTimestamp(ctx context.Context) (time.Time, error) {
job := &batchv1.Job{}
if err := c.client.Get(ctx, types.NamespacedName{Namespace: c.jobNamespace, Name: c.jobName}, job); err != nil {
Expand All @@ -79,18 +80,16 @@ func (c *CreateFinBackup) getJobCreationTimestamp(ctx context.Context) (time.Tim

tsStr, ok := job.Annotations["batch.kubernetes.io/cronjob-scheduled-timestamp"]
if !ok {
return time.Time{}, fmt.Errorf(
"job %s/%s missing annotation batch.kubernetes.io/cronjob-scheduled-timestamp",
c.jobNamespace,
c.jobName,
)
if job.CreationTimestamp.IsZero() {
return time.Time{}, fmt.Errorf("job %s/%s has no creation timestamp available", c.jobNamespace, c.jobName)
}
return job.CreationTimestamp.Time, nil
}

jobCreatedAt, err := time.Parse(time.RFC3339, tsStr)
if err != nil {
return time.Time{}, fmt.Errorf("invalid batch.kubernetes.io/cronjob-scheduled-timestamp: %w", err)
}

return jobCreatedAt, nil
}

Expand Down
34 changes: 27 additions & 7 deletions internal/job/createfinbackup/createfinbackup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func TestCreateFinBackup_Perform(t *testing.T) {
makeJob("job-0001", "job-ns", now),
},
input: &input.CreateFinBackup{
FinBackupConfigName: "fbc-ok",
FinBackupConfigNamespace: "ns",
FinBackupConfigName: fbc.Name,
FinBackupConfigNamespace: fbc.Namespace,
JobName: "job-0001",
JobNamespace: "job-ns",
},
Expand All @@ -155,8 +155,8 @@ func TestCreateFinBackup_Perform(t *testing.T) {
},
},
input: &input.CreateFinBackup{
FinBackupConfigName: "fbc-ok",
FinBackupConfigNamespace: "ns",
FinBackupConfigName: fbc.Name,
FinBackupConfigNamespace: fbc.Namespace,
JobName: "job-0002",
JobNamespace: "job-ns",
},
Expand All @@ -169,7 +169,7 @@ func TestCreateFinBackup_Perform(t *testing.T) {
},
input: &input.CreateFinBackup{
FinBackupConfigName: "no-such-fbc",
FinBackupConfigNamespace: "ns",
FinBackupConfigNamespace: fbc.Namespace,
JobName: "job-0003",
JobNamespace: "job-ns",
},
Expand All @@ -182,8 +182,8 @@ func TestCreateFinBackup_Perform(t *testing.T) {
makeJob("job-0004", "job-ns", now),
},
input: &input.CreateFinBackup{
FinBackupConfigName: "fbc-ok",
FinBackupConfigNamespace: "ns",
FinBackupConfigName: fbc.Name,
FinBackupConfigNamespace: fbc.Namespace,
JobName: "job-0004",
JobNamespace: "job-ns",
},
Expand All @@ -194,6 +194,26 @@ func TestCreateFinBackup_Perform(t *testing.T) {
},
wantErr: true,
},
{
name: "fallback-to-creation-timestamp",
existingObjects: []client.Object{
fbc,
&batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job-0005",
Namespace: "job-ns",
CreationTimestamp: metav1.NewTime(now),
},
},
},
input: &input.CreateFinBackup{
FinBackupConfigName: fbc.Name,
FinBackupConfigNamespace: fbc.Namespace,
JobName: "job-0005",
JobNamespace: "job-ns",
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down