From 3ce1a7c00c8e83b8fe6a11b53852d89973bb77f7 Mon Sep 17 00:00:00 2001 From: John Sanda Date: Mon, 24 May 2021 12:49:37 -0400 Subject: [PATCH] fix JSON patch and add logging (#45) I have changed the CassdcTemplateSpec to a pointer. I think that makes more sense going forward for situations where we need to determine whether the property has been set. I manually tested a backup/restore operation to make sure that the change does not break anything. --- CHANGELOG.md | 3 +++ api/v1alpha1/cassandrabackup_types.go | 2 +- api/v1alpha1/zz_generated.deepcopy.go | 6 +++++- .../cassandra.k8ssandra.io_cassandrabackups.yaml | 2 -- config/crd/kustomization.yaml | 9 +-------- config/crd/patches/backup_validation_patch.json | 6 ------ config/crd/patches/cassdc_config_patch.json | 8 ++++---- controllers/cassandrabackup_controller.go | 14 +++++++++++++- 8 files changed, 27 insertions(+), 23 deletions(-) delete mode 100644 config/crd/patches/backup_validation_patch.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e49962..36401bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ Changelog for medusa-operator. New PRs should update the `master / unreleased` s * [BUGFIX] ``` +## v0.3.2 +* [BUGFIX] #44 Fix JSON patch script for CRD + ## v0.3.1 * [BUGFIX] #42 Fix CRD version upgrade diff --git a/api/v1alpha1/cassandrabackup_types.go b/api/v1alpha1/cassandrabackup_types.go index 0f5b439..32b757d 100644 --- a/api/v1alpha1/cassandrabackup_types.go +++ b/api/v1alpha1/cassandrabackup_types.go @@ -44,7 +44,7 @@ type CassandraDatacenterTemplateSpec struct { // CassandraBackupStatus defines the observed state of CassandraBackup type CassandraBackupStatus struct { - CassdcTemplateSpec CassandraDatacenterTemplateSpec `json:"cassdcTemplateSpec"` + CassdcTemplateSpec *CassandraDatacenterTemplateSpec `json:"cassdcTemplateSpec,omitempty"` StartTime metav1.Time `json:"startTime,omitempty"` diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 310ab62..86e89b2 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -101,7 +101,11 @@ func (in *CassandraBackupSpec) DeepCopy() *CassandraBackupSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *CassandraBackupStatus) DeepCopyInto(out *CassandraBackupStatus) { *out = *in - in.CassdcTemplateSpec.DeepCopyInto(&out.CassdcTemplateSpec) + if in.CassdcTemplateSpec != nil { + in, out := &in.CassdcTemplateSpec, &out.CassdcTemplateSpec + *out = new(CassandraDatacenterTemplateSpec) + (*in).DeepCopyInto(*out) + } in.StartTime.DeepCopyInto(&out.StartTime) in.FinishTime.DeepCopyInto(&out.FinishTime) if in.InProgress != nil { diff --git a/config/crd/bases/cassandra.k8ssandra.io_cassandrabackups.yaml b/config/crd/bases/cassandra.k8ssandra.io_cassandrabackups.yaml index 28380b8..08eba76 100644 --- a/config/crd/bases/cassandra.k8ssandra.io_cassandrabackups.yaml +++ b/config/crd/bases/cassandra.k8ssandra.io_cassandrabackups.yaml @@ -4213,8 +4213,6 @@ spec: startTime: format: date-time type: string - required: - - cassdcTemplateSpec type: object type: object served: true diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 1553b76..cff0080 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -23,16 +23,9 @@ patches: - path: patches/cassdc_config_patch.json target: group: apiextensions.k8s.io - version: v1beta1 + version: v1 kind: CustomResourceDefinition name: cassandrabackups.cassandra.k8ssandra.io -- path: patches/backup_validation_patch.json - target: - group: apiextensions.k8s.io - version: v1beta1 - kind: CustomResourceDefinition - name: cassandrabackups.cassandra.k8ssandra.io - # the following config is for teaching kustomize how to do kustomization for CRDs. configurations: diff --git a/config/crd/patches/backup_validation_patch.json b/config/crd/patches/backup_validation_patch.json deleted file mode 100644 index c4403d4..0000000 --- a/config/crd/patches/backup_validation_patch.json +++ /dev/null @@ -1,6 +0,0 @@ -[ - { - "op": "remove", - "path": "/spec/validation/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/volumes/items/properties/configMap" - } -] diff --git a/config/crd/patches/cassdc_config_patch.json b/config/crd/patches/cassdc_config_patch.json index 5d87729..7fa7f59 100644 --- a/config/crd/patches/cassdc_config_patch.json +++ b/config/crd/patches/cassdc_config_patch.json @@ -1,18 +1,18 @@ [ { "op": "remove", - "path": "/spec/validation/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/config" + "path": "/spec/versions/0/schema/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/config" }, { "op": "remove", - "path": "/spec/validation/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/containers/items/properties/ports" + "path": "/spec/versions/0/schema/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/containers/items/properties/ports" }, { "op": "remove", - "path": "/spec/validation/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/initContainers/items/properties/ports" + "path": "/spec/versions/0/schema/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/initContainers/items/properties/ports" }, { "op": "remove", - "path": "/spec/validation/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/topologySpreadConstraints" + "path": "/spec/versions/0/schema/openAPIV3Schema/properties/status/properties/cassdcTemplateSpec/properties/spec/properties/podTemplateSpec/properties/spec/properties/topologySpreadConstraints" } ] \ No newline at end of file diff --git a/controllers/cassandrabackup_controller.go b/controllers/cassandrabackup_controller.go index 4465671..72491bb 100644 --- a/controllers/cassandrabackup_controller.go +++ b/controllers/cassandrabackup_controller.go @@ -59,9 +59,12 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er ctx := context.Background() _ = r.Log.WithValues("cassandrabackup", req.NamespacedName) + r.Log.Info("Starting reconciliation") + instance := &api.CassandraBackup{} err := r.Get(ctx, req.NamespacedName, instance) if err != nil { + r.Log.Error(err, "Failed to get CassandraBackup") if errors.IsNotFound(err) { return ctrl.Result{}, nil } @@ -78,6 +81,7 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er // If the backup is already finished, there is nothing to do. if backupFinished(backup) { + r.Log.Info("Backup operation is already finished") return ctrl.Result{Requeue: false}, nil } @@ -85,6 +89,7 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er if !backup.Status.StartTime.IsZero() { // If there is anything in progress, simply requeue the request if len(backup.Status.InProgress) > 0 { + r.Log.Info("Backups already in progress") return ctrl.Result{RequeueAfter: 10 * time.Second}, nil } @@ -103,6 +108,8 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er return ctrl.Result{Requeue: false}, nil } + r.Log.Info("Backups have not been started yet") + cassdcKey := types.NamespacedName{Namespace: backup.Namespace, Name: backup.Spec.CassandraDatacenter} cassdc := &cassdcapi.CassandraDatacenter{} err = r.Get(ctx, cassdcKey, cassdc) @@ -113,6 +120,7 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er pods, err := r.getCassandraDatacenterPods(ctx, cassdc) if err != nil { + r.Log.Error(err, "Failed to get datacenter pods") return ctrl.Result{RequeueAfter: 10 * time.Second}, err } @@ -133,11 +141,14 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er for _, pod := range pods { backup.Status.InProgress = append(backup.Status.InProgress, pod.Name) } + r.Log.Info("checking status", "CassandraDatacenterTemplateSpec", backup.Status.CassdcTemplateSpec) if err := r.Status().Patch(context.Background(), backup, patch); err != nil { + r.Log.Error(err, "Failed to patch status") // We received a stale object, requeue for next processing return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil } + r.Log.Info("Starting backups") // Do the actual backup in the background go func() { wg := sync.WaitGroup{} @@ -170,6 +181,7 @@ func (r *CassandraBackupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er }() } wg.Wait() + r.Log.Info("finished backup operations") if err := r.Status().Patch(context.Background(), backup, patch); err != nil { r.Log.Error(err, "failed to patch status", "Backup", fmt.Sprintf("%s/%s", backup.Name, backup.Namespace)) } @@ -221,7 +233,7 @@ func (r *CassandraBackupReconciler) addCassdcSpecToStatus(ctx context.Context, b }, } - backup.Status.CassdcTemplateSpec = templateSpec + backup.Status.CassdcTemplateSpec = &templateSpec return nil }