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

let user control flyway image through api #22

Merged
merged 7 commits into from
Sep 17, 2023
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
5 changes: 5 additions & 0 deletions api/v1alpha1/migration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (r *Migration) GetCredentials() v1.SecretReference {

// MigrationSource defines the source for the flyway-migrations.
type MigrationSource struct {

// Reference to the flyway image to use.
// +kubebuilder:validation:Optional
FlywayImage string `json:"flywayImage"`

// Reference to the image holding the SQLs to migrate
// +kubebuilder:validation:Required
ImageRef string `json:"imageRef"`
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/flyway.davidkarlsen.com_migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ spec:
default: UTF-8
description: The encoding of the SQL-files.
type: string
flywayImage:
description: Reference to the flyway image to use.
type: string
imageRef:
description: Reference to the image holding the SQLs to migrate
type: string
Expand Down
13 changes: 12 additions & 1 deletion internal/controller/jobutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import (
"fmt"
"github.com/caitlinelfring/go-env-default"
flywayv1alpha1 "github.com/davidkarlsen/flyway-operator/api/v1alpha1"
"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"
)

const (
defaultFlywayImage = "docker.io/flyway/flyway:9"
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)
Expand All @@ -36,6 +42,11 @@ func hasSucceeded(job *batchv1.Job) bool {
return job.Status.Succeeded > 0
}

func getFlywayImage(migration *flywayv1alpha1.Migration) string {
image, _ := lo.Coalesce(migration.Spec.MigrationSource.FlywayImage, env.GetDefault(envNameFlywayImage, defaultFlywayImage))
return image
}

func createJobSpec(migration *flywayv1alpha1.Migration) *batchv1.Job {
const targetPath = "/mnt/target/"
envVars := []corev1.EnvVar{
Expand Down Expand Up @@ -96,7 +107,7 @@ func createJobSpec(migration *flywayv1alpha1.Migration) *batchv1.Job {
Containers: []corev1.Container{
{
Name: "flyway",
Image: env.GetDefault(envNameFlywayImage, defaultFlywayImage),
Image: getFlywayImage(migration),
ImagePullPolicy: corev1.PullAlways,
Args: []string{"info", "migrate", "info", "-outputType=json"},
Env: envVars,
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/migration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ import (
)

const (
sqlVolumeName = "sql"
defaultFlywayImage = "ghcr.io/davidkarlsen/flyway-db2:9.22"
envNameFlywayImage = "FLYWAY_IMAGE"
clientContextKey = "client"
sqlVolumeName = "sql"
clientContextKey = "client"
)

// MigrationReconciler reconciles a Migration object
Expand Down