Skip to content

Commit

Permalink
have better job-generation check (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkarlsen authored Nov 14, 2023
1 parent 562e922 commit 22e7492
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
9 changes: 7 additions & 2 deletions api/v1alpha1/migration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
)

const (
prefix = "flyway-operator.davidkarlsen.com"
paused = prefix + "/" + "paused"
Prefix = "flyway-operator.davidkarlsen.com"
Generation = Prefix + "/" + "generation"
paused = Prefix + "/" + "paused"
)

// MigrationStatus defines the observed state of Migration
Expand All @@ -54,6 +55,10 @@ func (m *Migration) IsPaused() bool {
return len(filtered) > 0
}

func (m *Migration) GenerationAsString() string {
return strconv.Itoa(int(m.Generation))
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

Expand Down
9 changes: 5 additions & 4 deletions internal/controller/jobutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/samber/lo"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
eq "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)
Expand All @@ -19,9 +18,8 @@ const (
envNameFlywayImage = "FLYWAY_IMAGE"
)

func jobsAreEqual(first *batchv1.Job, second *batchv1.Job) bool {
return first != nil && second != nil &&
eq.Semantic.DeepEqual(first.Spec.Template.Spec.InitContainers[0].Image, second.Spec.Template.Spec.InitContainers[0].Image)
func jobIsCurrent(job *batchv1.Job, migration *flywayv1alpha1.Migration) bool {
return job.Annotations[flywayv1alpha1.Generation] == migration.GenerationAsString()
}

// from https://github.com/kubernetes/kubernetes/blob/v1.28.1/pkg/controller/job/utils.go
Expand Down Expand Up @@ -108,6 +106,9 @@ func createJobSpec(migration *flywayv1alpha1.Migration) *batchv1.Job {
"app.kubernetes.io/name": "flyway",
"app.kubernetes.io/instance": migration.Name,
},
Annotations: map[string]string{
flywayv1alpha1.Generation: migration.GenerationAsString(),
},
},
Spec: batchv1.JobSpec{
BackoffLimit: pointer.Int32(2),
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/migration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ func (r *MigrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return r.ManageSuccess(ctx, migration)
}

jobsAreEqual := jobsAreEqual(existingJob, newJob)
jobIsCurrent := jobIsCurrent(existingJob, migration)

if hasFailed(existingJob) || !jobsAreEqual {
if hasFailed(existingJob) || !jobIsCurrent {
return r.submitMigrationJob(ctx, migration, newJob)
}

if hasSucceeded(existingJob) {
if jobsAreEqual {
if jobIsCurrent {
logger.Info("Migration succeeded")
r.GetRecorder().Event(migration, corev1.EventTypeNormal, "Succeeded",
fmt.Sprintf("Migration Succeeded: %s, source: %s", req.NamespacedName, migration.Spec.MigrationSource.ImageRef))
Expand Down

0 comments on commit 22e7492

Please sign in to comment.