From 6333c35b4397233c60f46881330893705a3a7714 Mon Sep 17 00:00:00 2001 From: "David J. M. Karlsen" Date: Thu, 28 Sep 2023 09:51:29 +0200 Subject: [PATCH] make the actions into a list --- api/v1alpha1/migration_types.go | 15 ++++++++++--- api/v1alpha1/zz_generated.deepcopy.go | 21 ++++++++++++++++++ .../flyway.davidkarlsen.com_migrations.yaml | 22 ++++++++++++++++--- internal/controller/jobutil.go | 11 ++++++++-- 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/api/v1alpha1/migration_types.go b/api/v1alpha1/migration_types.go index 6c983b4..b4cd8ea 100644 --- a/api/v1alpha1/migration_types.go +++ b/api/v1alpha1/migration_types.go @@ -59,6 +59,9 @@ type MigrationSpec struct { // +kubebuilder:validation:Required Database Database `json:"database"` + // settings for flyway + FlywayConfiguration FlywayConfiguration `json:"flywayConfiguration"` + // settings defining the SQL migrations // +kubebuilder:validation:Required MigrationSource MigrationSource `json:"migrationSource"` @@ -87,13 +90,19 @@ func (r *Migration) GetCredentials() v1.SecretReference { } } -// MigrationSource defines the source for the flyway-migrations. -type MigrationSource struct { - +type FlywayConfiguration struct { // Reference to the flyway image to use. // +kubebuilder:validation:Optional FlywayImage string `json:"flywayImage"` + // The flyway actions to apply, like "info", "migrate" + // +kubebuilder:default={"info", "migrate", "info"} + CommandLines []string `json:"commandLines"` +} + +// MigrationSource defines the source for the flyway-migrations. +type MigrationSource struct { + // Reference to the image holding the SQLs to migrate // +kubebuilder:validation:Required ImageRef string `json:"imageRef"` diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 2802cf0..113ee79 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -42,6 +42,26 @@ func (in *Database) DeepCopy() *Database { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *FlywayConfiguration) DeepCopyInto(out *FlywayConfiguration) { + *out = *in + if in.CommandLines != nil { + in, out := &in.CommandLines, &out.CommandLines + *out = make([]string, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FlywayConfiguration. +func (in *FlywayConfiguration) DeepCopy() *FlywayConfiguration { + if in == nil { + return nil + } + out := new(FlywayConfiguration) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Migration) DeepCopyInto(out *Migration) { *out = *in @@ -132,6 +152,7 @@ func (in *MigrationSource) DeepCopy() *MigrationSource { func (in *MigrationSpec) DeepCopyInto(out *MigrationSpec) { *out = *in in.Database.DeepCopyInto(&out.Database) + in.FlywayConfiguration.DeepCopyInto(&out.FlywayConfiguration) in.MigrationSource.DeepCopyInto(&out.MigrationSource) } diff --git a/config/crd/bases/flyway.davidkarlsen.com_migrations.yaml b/config/crd/bases/flyway.davidkarlsen.com_migrations.yaml index 901d346..40dd28c 100644 --- a/config/crd/bases/flyway.davidkarlsen.com_migrations.yaml +++ b/config/crd/bases/flyway.davidkarlsen.com_migrations.yaml @@ -69,6 +69,24 @@ spec: - jdbcUrl - username type: object + flywayConfiguration: + description: settings for flyway + properties: + commandLines: + default: + - info + - migrate + - info + description: The flyway actions to apply + items: + type: string + type: array + flywayImage: + description: Reference to the flyway image to use. + type: string + required: + - commandLines + type: object migrationSource: description: settings defining the SQL migrations properties: @@ -90,9 +108,6 @@ 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 @@ -113,6 +128,7 @@ spec: type: object required: - database + - flywayConfiguration - migrationSource type: object status: diff --git a/internal/controller/jobutil.go b/internal/controller/jobutil.go index 267bdec..cc7bada 100644 --- a/internal/controller/jobutil.go +++ b/internal/controller/jobutil.go @@ -43,10 +43,17 @@ func hasSucceeded(job *batchv1.Job) bool { } func getFlywayImage(migration *flywayv1alpha1.Migration) string { - image, _ := lo.Coalesce(migration.Spec.MigrationSource.FlywayImage, env.GetDefault(envNameFlywayImage, defaultFlywayImage)) + image, _ := lo.Coalesce(migration.Spec.FlywayConfiguration.FlywayImage, env.GetDefault(envNameFlywayImage, defaultFlywayImage)) return image } +func getFlywayArgs(migration *flywayv1alpha1.Migration) []string { + args := migration.Spec.FlywayConfiguration.CommandLines + args = append(args, "-outputType=json") + + return args +} + func createJobSpec(migration *flywayv1alpha1.Migration) *batchv1.Job { const targetPath = "/mnt/target/" envVars := []corev1.EnvVar{ @@ -109,7 +116,7 @@ func createJobSpec(migration *flywayv1alpha1.Migration) *batchv1.Job { Name: "flyway", Image: getFlywayImage(migration), ImagePullPolicy: corev1.PullAlways, - Args: []string{"info", "migrate", "info", "-outputType=json"}, + Args: getFlywayArgs(migration), Env: envVars, VolumeMounts: []corev1.VolumeMount{ {