Skip to content

Commit

Permalink
Merge pull request #1066 from equinor/master
Browse files Browse the repository at this point in the history
Fixed deleting job pvc-s (#1064)
  • Loading branch information
satr authored Mar 18, 2024
2 parents cfc6e67 + 375a947 commit 30aa0fd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.30.3
appVersion: 1.50.2
version: 1.30.4
appVersion: 1.50.3
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
74 changes: 55 additions & 19 deletions pkg/apis/deployment/volumemount.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/equinor/radix-operator/pkg/apis/kube"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/equinor/radix-operator/pkg/apis/utils/slice"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -73,6 +72,10 @@ const (
provisionerFileCsiAzure string = "file.csi.azure.com"
)

var (
csiVolumeProvisioners = map[string]any{provisionerBlobCsiAzure: struct{}{}, provisionerFileCsiAzure: struct{}{}}
)

// getStorageClassProvisionerByVolumeMountType convert volume mount type to Storage Class provisioner
func getStorageClassProvisionerByVolumeMountType(radixVolumeMount *radixv1.RadixVolumeMount) (string, bool) {
if radixVolumeMount.BlobFuse2 != nil {
Expand Down Expand Up @@ -809,20 +812,18 @@ func GetRadixVolumeMountStorage(radixVolumeMount *radixv1.RadixVolumeMount) stri
return radixVolumeMount.Storage
}

func (deploy *Deployment) garbageCollectOrphanedCsiAzurePersistentVolumes(excludePvcNames []string) error {
func (deploy *Deployment) garbageCollectOrphanedCsiAzurePersistentVolumes(excludePvcNames map[string]any) error {
pvList, err := deploy.getPersistentVolumesForPvc()
if err != nil {
return err
}
for _, pv := range pvList.Items {
switch {
case pv.Spec.ClaimRef == nil || pv.Spec.ClaimRef.Kind != persistentVolumeClaimKind:
continue
case pv.Spec.CSI == nil || !slice.ContainsString([]string{provisionerBlobCsiAzure, provisionerFileCsiAzure}, pv.Spec.CSI.Driver):
if pv.Spec.ClaimRef == nil || pv.Spec.ClaimRef.Kind != persistentVolumeClaimKind ||
!knownCSIDriver(pv.Spec.CSI) ||
pv.Status.Phase != corev1.VolumeReleased {
continue
case slice.ContainsString(excludePvcNames, pv.Spec.ClaimRef.Name):
continue
case pv.Status.Phase != corev1.VolumeReleased:
}
if _, ok := excludePvcNames[pv.Spec.ClaimRef.Name]; ok {
continue
}
log.Infof("Delete orphaned Csi Azure PersistantVolume %s of PersistantVolumeClaim %s", pv.Name, pv.Spec.ClaimRef.Name)
Expand All @@ -834,6 +835,14 @@ func (deploy *Deployment) garbageCollectOrphanedCsiAzurePersistentVolumes(exclud
return nil
}

func knownCSIDriver(csiPersistentVolumeSource *corev1.CSIPersistentVolumeSource) bool {
if csiPersistentVolumeSource == nil {
return false
}
_, ok := csiVolumeProvisioners[csiPersistentVolumeSource.Driver]
return ok
}

// createOrUpdateCsiAzureVolumeResources Create or update CSI Azure volume resources - StorageClasses, PersistentVolumeClaims, PersistentVolume
func (deploy *Deployment) createOrUpdateCsiAzureVolumeResources(desiredDeployment *appsv1.Deployment) error {
namespace := deploy.radixDeployment.GetNamespace()
Expand All @@ -852,7 +861,11 @@ func (deploy *Deployment) createOrUpdateCsiAzureVolumeResources(desiredDeploymen
scMap := utils.GetStorageClassMap(&scList.Items)
pvcMap := utils.GetPersistentVolumeClaimMap(&pvcList.Items)
radixVolumeMountMap := deploy.getRadixVolumeMountMapByCsiAzureVolumeMountName(componentName)
var actualStorageClassNames, actualPvcNames []string
var actualStorageClassNames []string
actualPvcNames, err := deploy.getCurrentlyUsedPersistentVolumeClaims(namespace)
if err != nil {
return err
}
for _, volume := range desiredDeployment.Spec.Template.Spec.Volumes {
if volume.PersistentVolumeClaim == nil {
continue
Expand All @@ -871,7 +884,7 @@ func (deploy *Deployment) createOrUpdateCsiAzureVolumeResources(desiredDeploymen
return err
}
volume.PersistentVolumeClaim.ClaimName = pvc.Name
actualPvcNames = append(actualPvcNames, pvc.Name)
actualPvcNames[pvc.Name] = struct{}{}
}
err = deploy.garbageCollectCsiAzureStorageClasses(scList, actualStorageClassNames)
if err != nil {
Expand All @@ -888,9 +901,36 @@ func (deploy *Deployment) createOrUpdateCsiAzureVolumeResources(desiredDeploymen
return nil
}

func (deploy *Deployment) getCurrentlyUsedPersistentVolumeClaims(namespace string) (map[string]any, error) {
pvcNames := make(map[string]any)
deploymentList, err := deploy.kubeclient.AppsV1().Deployments(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, deployment := range deploymentList.Items {
addUsedPersistenceVolumeClaimsFrom(deployment.Spec.Template, pvcNames)
}
jobsList, err := deploy.kubeclient.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, job := range jobsList.Items {
addUsedPersistenceVolumeClaimsFrom(job.Spec.Template, pvcNames)
}
return pvcNames, nil
}

func addUsedPersistenceVolumeClaimsFrom(podTemplate corev1.PodTemplateSpec, pvcMap map[string]any) {
for _, volume := range podTemplate.Spec.Volumes {
if volume.PersistentVolumeClaim != nil && len(volume.PersistentVolumeClaim.ClaimName) > 0 {
pvcMap[volume.PersistentVolumeClaim.ClaimName] = struct{}{}
}
}
}

func (deploy *Deployment) garbageCollectCsiAzureStorageClasses(scList *storagev1.StorageClassList, excludeStorageClassName []string) error {
for _, storageClass := range scList.Items {
if slice.ContainsString(excludeStorageClassName, storageClass.Name) {
if commonUtils.ContainsString(excludeStorageClassName, storageClass.Name) {
continue
}
log.Debugf("Delete Csi Azure StorageClass %s", storageClass.Name)
Expand All @@ -902,9 +942,9 @@ func (deploy *Deployment) garbageCollectCsiAzureStorageClasses(scList *storagev1
return nil
}

func (deploy *Deployment) garbageCollectCsiAzurePersistentVolumeClaimsAndPersistentVolumes(namespace string, pvcList *corev1.PersistentVolumeClaimList, excludePvcNames []string) error {
func (deploy *Deployment) garbageCollectCsiAzurePersistentVolumeClaimsAndPersistentVolumes(namespace string, pvcList *corev1.PersistentVolumeClaimList, excludePvcNames map[string]any) error {
for _, pvc := range pvcList.Items {
if slice.ContainsString(excludePvcNames, pvc.Name) {
if _, ok := excludePvcNames[pvc.Name]; ok {
continue
}
pvName := pvc.Spec.VolumeName
Expand All @@ -931,11 +971,7 @@ func (deploy *Deployment) createCsiAzurePersistentVolumeClaim(storageClass *stor
return pvc, nil
}

log.Debugf("Delete PersistentVolumeClaim %s in namespace %s: changed StorageClass name to %s", pvc.Name, namespace, storageClass.Name)
err := deploy.deletePersistentVolumeClaim(namespace, pvc.Name)
if err != nil {
return nil, err
}
log.Debugf("Delete in garbage-collect an old PersistentVolumeClaim %s in namespace %s: changed StorageClass name to %s", pvc.Name, namespace, storageClass.Name)
}
persistentVolumeClaimName, err := createCsiAzurePersistentVolumeClaimName(componentName, radixVolumeMount)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions pkg/apis/utils/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package utils
import (
"encoding/json"
"fmt"

"github.com/equinor/radix-operator/pkg/apis/kube"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/strategicpatch"
)

//GetPersistentVolumeClaimMap Get map from PersistentVolumeClaim with name as key
// GetPersistentVolumeClaimMap Get map from PersistentVolumeClaim with name as key
func GetPersistentVolumeClaimMap(pvcList *[]corev1.PersistentVolumeClaim) map[string]*corev1.PersistentVolumeClaim {
return getPersistentVolumeClaimMap(pvcList, true)
return getPersistentVolumeClaimMap(pvcList, false)
}

func getPersistentVolumeClaimMap(pvcList *[]corev1.PersistentVolumeClaim, ignoreRandomPostfixInName bool) map[string]*corev1.PersistentVolumeClaim {
Expand All @@ -26,7 +27,7 @@ func getPersistentVolumeClaimMap(pvcList *[]corev1.PersistentVolumeClaim, ignore
return pvcMap
}

//EqualPvcLists Compare two PersistentVolumeClaim lists. When ignoreRandomPostfixInName=true - last 6 chars of the name (e.g.'-abc12') are ignored during comparison
// EqualPvcLists Compare two PersistentVolumeClaim lists. When ignoreRandomPostfixInName=true - last 6 chars of the name (e.g.'-abc12') are ignored during comparison
func EqualPvcLists(pvcList1, pvcList2 *[]corev1.PersistentVolumeClaim, ignoreRandomPostfixInName bool) (bool, error) {
if len(*pvcList1) != len(*pvcList2) {
return false, nil
Expand All @@ -45,7 +46,7 @@ func EqualPvcLists(pvcList1, pvcList2 *[]corev1.PersistentVolumeClaim, ignoreRan
return true, nil
}

//EqualPvcs Compare two PersistentVolumeClaim pointers
// EqualPvcs Compare two PersistentVolumeClaim pointers
func EqualPvcs(pvc1 *corev1.PersistentVolumeClaim, pvc2 *corev1.PersistentVolumeClaim, ignoreRandomPostfixInName bool) (bool, error) {
pvc1Copy, labels1 := getPvcCopyWithLabels(pvc1, ignoreRandomPostfixInName)
pvc2Copy, labels2 := getPvcCopyWithLabels(pvc2, ignoreRandomPostfixInName)
Expand All @@ -64,11 +65,11 @@ func EqualPvcs(pvc1 *corev1.PersistentVolumeClaim, pvc2 *corev1.PersistentVolume

func getPvcCopyWithLabels(pvc *corev1.PersistentVolumeClaim, ignoreRandomPostfixInName bool) (*corev1.PersistentVolumeClaim, map[string]string) {
pvcCopy := pvc.DeepCopy()
pvcCopy.ObjectMeta.ManagedFields = nil //HACK: to avoid ManagedFields comparison
pvcCopy.ObjectMeta.ManagedFields = nil // HACK: to avoid ManagedFields comparison
if ignoreRandomPostfixInName {
pvcCopy.ObjectMeta.Name = ShortenString(pvcCopy.ObjectMeta.Name, 6)
}
//to avoid label order variations
// to avoid label order variations
labels := pvcCopy.ObjectMeta.Labels
pvcCopy.ObjectMeta.Labels = map[string]string{}
return pvcCopy, labels
Expand Down

0 comments on commit 30aa0fd

Please sign in to comment.