Skip to content

Commit

Permalink
solve instaclustr#407: label each pvc with the pod UID
Browse files Browse the repository at this point in the history
  • Loading branch information
srteam2020 committed Mar 24, 2021
1 parent 74130de commit 34e1e04
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func (r *ReconcileCassandraDataCenter) Reconcile(request reconcile.Request) (rec
return reconcile.Result{}, fmt.Errorf("could not fetch CassandraDataCenter instance: %s", err)
}

if err := r.updatePVCsWithPodUid(reqLogger, instance); err != nil {
return reconcile.Result{}, err
}

if finalized, err := r.finalizeIfNecessary(reqLogger, instance); err != nil {
return reconcile.Result{}, err
} else if finalized {
Expand Down
46 changes: 46 additions & 0 deletions pkg/controller/cassandradatacenter/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,45 @@ func (r *ReconcileCassandraDataCenter) getPVCs(
}
}

func (r *ReconcileCassandraDataCenter) updatePVCsWithPodUid(reqLogger logr.Logger, instance *cassandraoperatorv1alpha1.CassandraDataCenter) error {
if pods, err := AllPodsInCDC(r.client, instance); err != nil {
return err
} else {
if len(pods) == 0 {
return nil
}
if existingPVCs, err := r.getPVCs(instance, nil); err != nil {
return err
} else {
if len(pods) != len(existingPVCs) {
return nil
}
for _, pod := range pods {
for _, volume := range pod.Spec.Volumes {
podsPVC := volume.VolumeSource.PersistentVolumeClaim
if podsPVC != nil {
for _, c := range existingPVCs {
if c.Name == podsPVC.ClaimName {
if _, ok := c.Labels[CassandraPodUID]; ok {
break
}
c.Labels[CassandraPodUID] = string(pod.UID)
if err := r.client.Update(context.TODO(), &c); err != nil {
reqLogger.Info(fmt.Sprintf("Fail to label PVC %s with pod %s's uid.", c.Name, pod.Name))
return err
}
reqLogger.Info(fmt.Sprintf("Successfully label PVC %s with pod %s's uid.", c.Name, pod.Name))
break
}
}
}
}
}
}
}
return nil
}

func (r *ReconcileCassandraDataCenter) finalizePVCs(reqLogger logr.Logger, instance *cassandraoperatorv1alpha1.CassandraDataCenter) error {

pvcList := corev1.PersistentVolumeClaimList{}
Expand Down Expand Up @@ -197,6 +236,13 @@ func (r *ReconcileCassandraDataCenter) finalizeDeletedPods(reqLogger logr.Logger
if podsPVC != nil {
for _, c := range existingPVCs {
if c.Name == podsPVC.ClaimName {
if _, ok := c.Labels[CassandraPodUID]; !ok {
reqLogger.Info(fmt.Sprintf("Cannot delete PVC %s: PVC is not labled with the pod UID yet.", c.Name))
return nil
} else if c.Labels[CassandraPodUID] != string(pod.UID) {
reqLogger.Info(fmt.Sprintf("Cannot delete PVC %s: PVC's label does not match pod UID'.", c.Name))
return nil
}
if err := r.deletePersistenceVolumeClaim(reqLogger, c); err != nil {

r.recorder.Event(
Expand Down
13 changes: 7 additions & 6 deletions pkg/controller/cassandradatacenter/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
)

const (
ManagedByKey = "app.kubernetes.io/managed-by"
ManagedByValue = "com.instaclustr.cassandra-operator"
RackKey = "cassandra-operator.instaclustr.com/rack"
DataCenterKey = "cassandra-operator.instaclustr.com/datacenter"
DataCenterUID = "cassandra-operator.instaclustr.com/datacenterUID"
ClusterKey = "cassandra-operator.instaclustr.com/cluster"
ManagedByKey = "app.kubernetes.io/managed-by"
ManagedByValue = "com.instaclustr.cassandra-operator"
RackKey = "cassandra-operator.instaclustr.com/rack"
DataCenterKey = "cassandra-operator.instaclustr.com/datacenter"
DataCenterUID = "cassandra-operator.instaclustr.com/datacenterUID"
CassandraPodUID = "cassandra-operator.instaclustr.com/cassandraPodUID"
ClusterKey = "cassandra-operator.instaclustr.com/cluster"
)

// ANNOTATIONS
Expand Down

0 comments on commit 34e1e04

Please sign in to comment.