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

Add ability to disable medusa-purge cron #1292

Closed
wants to merge 8 commits into from
Closed
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
3 changes: 2 additions & 1 deletion CHANGELOG/CHANGELOG-1.17.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ When cutting a new release, update the `unreleased` heading to the tag being gen
* [BUGFIX] [#1322](https://github.com/k8ssandra/k8ssandra-operator/issues/1322) Fix bug where server-system-logger customisations from the Containers field would be overwritten when vector was enabled.
* [FEATURE] Add support for HCD 1.0
* [ENHANCEMENT] [#1329](https://github.com/k8ssandra/k8ssandra-operator/issues/1329) Add config emptyDir volume mount on Reaper deployment to allow read only root FS
* [BUGFIX] Fix HCD jvm options generation
* [BUGFIX] Fix HCD jvm options generation
* [ENHANCEMENT] [#1278](https://github.com/k8ssandra/k8ssandra-operator/issues/1278) Add toggle to allow the disabling of the Medusa purge CronJob creation
6 changes: 6 additions & 0 deletions apis/medusa/v1alpha1/medusa_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,10 @@ type MedusaClusterTemplate struct {
// Define the liveness probe settings to use for the Medusa containers.
// +optional
LivenessProbe *corev1.Probe `json:"livenessProbe,omitempty"`

// PurgeBackups toggles if the medusa backups should be purged nightly or not
// Defaults to true.
// +kubebuilder:default=true
// +optional
PurgeBackups *bool `json:"purgeBackups,omitempty"`
}
5 changes: 5 additions & 0 deletions apis/medusa/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions charts/k8ssandra-operator/crds/k8ssandra-operator-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26109,6 +26109,12 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
purgeBackups:
default: true
description: |-
PurgeBackups toggles if the medusa backups should be purged nightly or not
Defaults to true.
type: boolean
readinessProbe:
description: Define the readiness probe settings to use for the
Medusa containers.
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/k8ssandra.io_k8ssandraclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26047,6 +26047,12 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
purgeBackups:
default: true
description: |-
PurgeBackups toggles if the medusa backups should be purged nightly or not
Defaults to true.
type: boolean
readinessProbe:
description: Define the readiness probe settings to use for the
Medusa containers.
Expand Down
46 changes: 33 additions & 13 deletions controllers/k8ssandra/medusa_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,32 @@ func (r *K8ssandraClusterReconciler) reconcileMedusa(

kcKey := utils.GetKey(kc)
// Create a cron job to purge Medusa backups
operatorNamespace := r.getOperatorNamespace()
purgeCronJob, err := medusa.PurgeCronJob(dcConfig, kc.SanitizedName(), operatorNamespace, logger)
if err != nil {
logger.Info("Failed to create Medusa purge backups cronjob", "error", err)
return result.Error(err)
}
purgeCronJob.SetLabels(labels.CleanedUpByLabels(kcKey))
recRes := reconciliation.ReconcileObject(ctx, remoteClient, r.DefaultDelay, *purgeCronJob)
switch {
case recRes.IsError():
return recRes
case recRes.IsRequeue():
return recRes
logger.Info("Checking if Medusa backups should be purged, PurgeBackups: " + fmt.Sprintf("%t", *medusaSpec.PurgeBackups))
if backupPurgeIsOn(medusaSpec.PurgeBackups) {
operatorNamespace := r.getOperatorNamespace()
purgeCronJob, err := medusa.PurgeCronJob(dcConfig, kc.SanitizedName(), operatorNamespace, logger)
if err != nil {
logger.Info("Failed to create Medusa purge backups cronjob", "error", err)
return result.Error(err)
}
purgeCronJob.SetLabels(labels.CleanedUpByLabels(kcKey))
recRes := reconciliation.ReconcileObject(ctx, remoteClient, r.DefaultDelay, *purgeCronJob)
switch {
case recRes.IsError():
return recRes
case recRes.IsRequeue():
return recRes
}
} else {
// if an existing purge cron job exists, delete it
purgeCronJob, _ := medusa.PurgeCronJob(dcConfig, kc.SanitizedName(), r.getOperatorNamespace(), logger)
if purgeCronJob != nil {
logger.Info("Deleting Medusa purge backups cronjob (may have been created before PurgeBackups was set to false")
if err := remoteClient.Delete(ctx, purgeCronJob); err != nil {
logger.Info("Failed to delete Medusa purge backups cronjob", "error", err)
return result.Error(err)
}
}
}

} else {
Expand All @@ -120,6 +133,13 @@ func (r *K8ssandraClusterReconciler) reconcileMedusa(
return result.Continue()
}

func backupPurgeIsOn(purgeBackups *bool) bool {
if purgeBackups == nil {
return true
}
return *purgeBackups
}

// Generate a secret for Medusa or use the existing one if provided in the spec
func (r *K8ssandraClusterReconciler) reconcileMedusaSecrets(
ctx context.Context,
Expand Down
11 changes: 11 additions & 0 deletions docs/content/en/tasks/backup-restore/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,18 @@ spec:
```

The above `CronJob` will be scheduled to run every day at midnight, and trigger a `MedusaTask` object creation to purge the backups.
### Disabling the purge CronJob
If you want to disable the automatic creation of the above purge `CronJob`, you can do so by setting the optional `purgeBackups` field to `false` in the `K8ssandraCluster` spec:

```yaml
apiVersion: k8ssandra.io/v1alpha1
kind: K8ssandraCluster
spec:
medusa:
purgeBackups: false
```

```yaml
## Deprecation Notice

The `CassandraBackup` and `CassandraRestore` CRDs are removed in v1.6.0.
Expand Down
15 changes: 14 additions & 1 deletion test/e2e/medusa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func createMultiDcSingleMedusaJob(t *testing.T, ctx context.Context, namespace s

checkDatacenterReady(t, ctx, dcKey, f)
checkMedusaContainersExist(t, ctx, namespace, dcKey, f, kc)
checkPurgeCronJobExists(t, ctx, namespace, dcKey, f, kc)
createBackupJob(t, ctx, namespace, f, dcKey)
verifyBackupJobFinished(t, ctx, f, dcKey, backupKey)
checkNoPurgeCronJob(t, ctx, namespace, dcKey, f, kc)
}

func checkBucketKeyPresent(t *testing.T, f *framework.E2eFramework, ctx context.Context, namespace string, k8sContext string, kc *k8ssandraapi.K8ssandraCluster) {
Expand Down Expand Up @@ -198,6 +198,19 @@ func checkPurgeCronJobExists(t *testing.T, ctx context.Context, namespace string
}, polling.medusaBackupDone.timeout, polling.medusaBackupDone.interval, "Medusa purge Job didn't finish within timeout")
}

func checkNoPurgeCronJob(t *testing.T, ctx context.Context, namespace string, dcKey framework.ClusterKey, f *framework.E2eFramework, kc *api.K8ssandraCluster) {
require := require.New(t)
// Get the Cassandra pod
dc1 := &cassdcapi.CassandraDatacenter{}
err := f.Get(ctx, dcKey, dc1)
// check medusa containers exist
require.NoError(err, "Error getting the CassandraDatacenter")
// ensure the cronjob was not created
cronJob := &batchv1.CronJob{}
err = f.Get(ctx, framework.NewClusterKey(dcKey.K8sContext, namespace, medusapkg.MedusaPurgeCronJobName(kc.SanitizedName(), dc1.SanitizedName())), cronJob)
require.Error(err, "Cronjob should not exist")
}

func createBackupJob(t *testing.T, ctx context.Context, namespace string, f *framework.E2eFramework, dcKey framework.ClusterKey) {
require := require.New(t)
t.Log("creating MedusaBackupJob")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ spec:
jvmOptions:
heapSize: 512Mi
medusa:
purgeBackups: false
cassandraUserSecretRef:
name: cluster2-medusa
storageProperties:
Expand Down
Loading