1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

[Fix] GT-11 Orphan PVC are not removed (#962)

This commit is contained in:
jwierzbo 2022-04-27 12:32:23 +02:00 committed by GitHub
parent b37219f9b0
commit 9193424779
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 6 deletions

View file

@ -1,6 +1,7 @@
# Change Log # Change Log
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A) ## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
- (Bugfix) Orphan PVC are not removed
## [1.2.10](https://github.com/arangodb/kube-arangodb/tree/1.2.10) (2022-04-27) ## [1.2.10](https://github.com/arangodb/kube-arangodb/tree/1.2.10) (2022-04-27)
- (Feature) Allow configuration for securityContext.runAsUser value - (Feature) Allow configuration for securityContext.runAsUser value

View file

@ -155,6 +155,7 @@ func (d *Deployment) inspectDeployment(lastInterval util.Interval) util.Interval
return nextInterval.ReduceTo(maxInspectionInterval) return nextInterval.ReduceTo(maxInspectionInterval)
} }
// inspectDeploymentWithError ensures that the deployment is in a valid state
func (d *Deployment) inspectDeploymentWithError(ctx context.Context, lastInterval util.Interval) (nextInterval util.Interval, inspectError error) { func (d *Deployment) inspectDeploymentWithError(ctx context.Context, lastInterval util.Interval) (nextInterval util.Interval, inspectError error) {
t := time.Now() t := time.Now()
@ -434,6 +435,7 @@ func (d *Deployment) refreshMaintenanceTTL(ctx context.Context) {
} }
} }
// ensureResources creates all required resources for the deployment
func (d *Deployment) ensureResources(ctx context.Context, lastInterval util.Interval, cachedStatus inspectorInterface.Inspector) (util.Interval, error) { func (d *Deployment) ensureResources(ctx context.Context, lastInterval util.Interval, cachedStatus inspectorInterface.Inspector) (util.Interval, error) {
// Ensure all resources are created // Ensure all resources are created
if d.haveServiceMonitorCRD { if d.haveServiceMonitorCRD {

View file

@ -209,7 +209,8 @@ func (r *Resources) EnsurePodsLabels(ctx context.Context, cachedStatus inspector
func (r *Resources) EnsurePersistentVolumeClaimsLabels(ctx context.Context, cachedStatus inspectorInterface.Inspector) error { func (r *Resources) EnsurePersistentVolumeClaimsLabels(ctx context.Context, cachedStatus inspectorInterface.Inspector) error {
changed := false changed := false
if err := cachedStatus.PersistentVolumeClaim().V1().Iterate(func(persistentVolumeClaim *core.PersistentVolumeClaim) error {
actionFn := func(persistentVolumeClaim *core.PersistentVolumeClaim) error {
if ensureGroupLabelsMap(persistentVolumeClaim.Kind, persistentVolumeClaim, r.context.GetSpec(), func(name string, d []byte) error { if ensureGroupLabelsMap(persistentVolumeClaim.Kind, persistentVolumeClaim, r.context.GetSpec(), func(name string, d []byte) error {
return globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error { return globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
_, err := r.context.PersistentVolumeClaimsModInterface().Patch(ctxChild, name, types.JSONPatchType, d, meta.PatchOptions{}) _, err := r.context.PersistentVolumeClaimsModInterface().Patch(ctxChild, name, types.JSONPatchType, d, meta.PatchOptions{})
@ -220,9 +221,13 @@ func (r *Resources) EnsurePersistentVolumeClaimsLabels(ctx context.Context, cach
} }
return nil return nil
}, func(persistentVolumeClaim *core.PersistentVolumeClaim) bool { }
ownerFilterFn := func(persistentVolumeClaim *core.PersistentVolumeClaim) bool {
return r.isChildResource(persistentVolumeClaim) return r.isChildResource(persistentVolumeClaim)
}); err != nil { }
if err := cachedStatus.PersistentVolumeClaim().V1().Iterate(actionFn, ownerFilterFn); err != nil {
return err return err
} }

View file

@ -24,14 +24,17 @@ import (
"context" "context"
"time" "time"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/arangodb/kube-arangodb/pkg/deployment/patch"
"github.com/arangodb/kube-arangodb/pkg/metrics" "github.com/arangodb/kube-arangodb/pkg/metrics"
"github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
pvcv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim/v1" pvcv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim/v1"
) )
@ -76,6 +79,26 @@ func (r *Resources) InspectPVCs(ctx context.Context, cachedStatus inspectorInter
return nil return nil
} }
owner := r.context.GetAPIObject().AsOwner()
if k8sutil.UpdateOwnerRefToObjectIfNeeded(pvc.GetObjectMeta(), &owner) {
q := patch.NewPatch(patch.ItemReplace(patch.NewPath("metadata", "ownerReferences"), pvc.ObjectMeta.OwnerReferences))
d, err := q.Marshal()
if err != nil {
log.Debug().Err(err).Msg("Failed to prepare PVC patch (ownerReferences)")
return errors.WithStack(err)
}
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
_, err := r.context.PersistentVolumeClaimsModInterface().Patch(ctxChild, pvc.GetName(), types.JSONPatchType, d, meta.PatchOptions{})
return err
})
if err != nil {
log.Debug().Err(err).Msg("Failed to update PVC (ownerReferences)")
return errors.WithStack(err)
}
}
if k8sutil.IsPersistentVolumeClaimMarkedForDeletion(pvc) { if k8sutil.IsPersistentVolumeClaimMarkedForDeletion(pvc) {
// Process finalizers // Process finalizers
if x, err := r.runPVCFinalizers(ctx, pvc, group, memberStatus); err != nil { if x, err := r.runPVCFinalizers(ctx, pvc, group, memberStatus); err != nil {

View file

@ -56,6 +56,21 @@ func AddOwnerRefToObject(obj metav1.Object, ownerRef *metav1.OwnerReference) {
} }
} }
// UpdateOwnerRefToObjectIfNeeded add given owner reference to given object if it does not exist yet
func UpdateOwnerRefToObjectIfNeeded(obj metav1.Object, ownerRef *metav1.OwnerReference) bool {
if ownerRef != nil {
for _, existingOwnerRef := range obj.GetOwnerReferences() {
if existingOwnerRef.UID == ownerRef.UID {
return false
}
}
AddOwnerRefToObject(obj, ownerRef)
return true
}
return false
}
// LabelsForExporterServiceSelector returns a map of labels, used to select the all arangodb-exporter containers // LabelsForExporterServiceSelector returns a map of labels, used to select the all arangodb-exporter containers
func LabelsForExporterServiceSelector(deploymentName string) map[string]string { func LabelsForExporterServiceSelector(deploymentName string) map[string]string {
return map[string]string{ return map[string]string{

View file

@ -38,6 +38,22 @@ func TestAddOwnerRefToObject(t *testing.T) {
assert.Len(t, p.GetOwnerReferences(), 1) assert.Len(t, p.GetOwnerReferences(), 1)
} }
// UpdateOwnerRefToObjectIfNeeded tests UpdateOwnerRefToObjectIfNeeded.
func TestUpdateOwnerRefToObjectIfNeeded(t *testing.T) {
p := &v1.Pod{}
result := UpdateOwnerRefToObjectIfNeeded(p, nil)
assert.Len(t, p.GetOwnerReferences(), 0)
assert.False(t, result)
result = UpdateOwnerRefToObjectIfNeeded(p, &metav1.OwnerReference{})
assert.Len(t, p.GetOwnerReferences(), 1)
assert.True(t, result)
result = UpdateOwnerRefToObjectIfNeeded(p, &metav1.OwnerReference{})
assert.Len(t, p.GetOwnerReferences(), 1)
assert.False(t, result)
}
// TestLabelsForDeployment tests LabelsForDeployment. // TestLabelsForDeployment tests LabelsForDeployment.
func TestLabelsForDeployment(t *testing.T) { func TestLabelsForDeployment(t *testing.T) {
l := LabelsForDeployment("test", "role") l := LabelsForDeployment("test", "role")