Skip to content

Commit

Permalink
fix JSON patch and add logging (#45)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsanda authored May 24, 2021
1 parent ad9a45c commit 3ce1a7c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/cassandrabackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
6 changes: 5 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 0 additions & 2 deletions config/crd/bases/cassandra.k8ssandra.io_cassandrabackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4213,8 +4213,6 @@ spec:
startTime:
format: date-time
type: string
required:
- cassdcTemplateSpec
type: object
type: object
served: true
Expand Down
9 changes: 1 addition & 8 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions config/crd/patches/backup_validation_patch.json

This file was deleted.

8 changes: 4 additions & 4 deletions config/crd/patches/cassdc_config_patch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
14 changes: 13 additions & 1 deletion controllers/cassandrabackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -78,13 +81,15 @@ 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
}

// First check to see if the backup is already in progress
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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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{}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -221,7 +233,7 @@ func (r *CassandraBackupReconciler) addCassdcSpecToStatus(ctx context.Context, b
},
}

backup.Status.CassdcTemplateSpec = templateSpec
backup.Status.CassdcTemplateSpec = &templateSpec
return nil
}

Expand Down

0 comments on commit 3ce1a7c

Please sign in to comment.