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

Added rename/delete of PVC #493

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Added

- [PR #476](https://github.com/konpyutaika/nifikop/pull/476) - **[Operator/NifiCluster]** Added logic to include injected containers and init containers in desired pod spec.
- [PR #493](https://github.com/konpyutaika/nifikop/pull/493) - **[Operator/NifiCluster]** Added logic to rename/delete PVC.

### Changed

Expand Down
38 changes: 38 additions & 0 deletions pkg/resources/nifi/nifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (r *Reconciler) Reconcile(log zap.Logger) error {
return errors.WrapIfWithDetails(err, "failed to list PVCs")
}

// copy list of pvcs to detect those to delete
pvcsToDelete := make([]corev1.PersistentVolumeClaim, len(pvcs))
copy(pvcsToDelete, pvcs)

for _, storage := range nodeConfig.StorageConfigs {
var pvc *corev1.PersistentVolumeClaim
pvcExists, existingPvc := r.storageConfigPVCExists(pvcs, storage.Name)
Expand All @@ -175,10 +179,44 @@ func (r *Reconciler) Reconcile(log zap.Logger) error {
if err != nil {
return errors.WrapIfWithDetails(err, "failed to reconcile resource", "resource", pvc.GetObjectKind().GroupVersionKind())
}

// remove pvc from the list of those to deleted
for i, pvc := range pvcsToDelete {
if pvcExists && pvc.Name == existingPvc.Name {
pvcsToDelete = append(pvcsToDelete[:i], pvcsToDelete[i+1:]...)
break
}
}
}

for _, pvc := range pvcsToDelete {
// If this is a nifi data volume AND it has a Delete reclaim policy, then delete it. Otherwise, it is configured to be retained.
if pvc.Labels[nifiutil.NifiDataVolumeMountKey] == "true" && pvc.Labels[nifiutil.NifiVolumeReclaimPolicyKey] == string(corev1.PersistentVolumeReclaimDelete) {
err = r.Client.Delete(context.TODO(), &pvc)
if err != nil {
if apierrors.IsNotFound(err) {
// can happen when node was not fully initialized and now is deleted
log.Info(fmt.Sprintf("PVC for Node %s not found. Continue", node.Labels["nodeId"]))
}

return errors.WrapIfWithDetails(err, "could not delete pvc for node", "id", node.Labels["nodeId"])
}
} else {
log.Debug("Not deleting PVC because it should be retained.", zap.String("nodeId", node.Labels["nodeId"]), zap.String("pvcName", pvc.Name))
}
}

// re-lookup the PVCs after we've created any we need to create.
pvcs, err = getCreatedPVCForNode(r.Client, node.Id, r.NifiCluster.Namespace, r.NifiCluster.Name)
// remove pvcs that were delete
for _, pvcToDelete := range pvcsToDelete {
for i, pvc := range pvcs {
if pvcToDelete.Name == pvc.Name {
pvcs = append(pvcs[:i], pvcs[i+1:]...)
}
}
}

if err != nil {
return errors.WrapIfWithDetails(err, "failed to list PVCs")
}
Expand Down