Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2479 from StrongMonkey/fix-volume-delete
Browse files Browse the repository at this point in the history
Fix: add logic to update volume reclaim policy to delete
  • Loading branch information
StrongMonkey authored Feb 15, 2024
2 parents 01ccd48 + df03e6e commit b5a6c7f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
49 changes: 49 additions & 0 deletions pkg/server/registry/apigroups/acorn/volumes/policyswitcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package volumes

import (
"context"

"github.com/acorn-io/mink/pkg/strategy"
"github.com/acorn-io/mink/pkg/strategy/remote"
"github.com/acorn-io/mink/pkg/types"
v1 "k8s.io/api/core/v1"
)

type PolicySwitcherStrategy struct {
strategy strategy.CompleteStrategy
translator *Translator
remote *remote.Remote
}

// NewPolicySwitcherStrategy returns a new policy switcher strategy that switch persistvolume policy from retain to delete when the volume is being deleted.
// This makes sure the actual storage resource is deleted when the volume is deleted.
func NewPolicySwitcherStrategy(s strategy.CompleteStrategy, t *Translator, r *remote.Remote) strategy.Deleter {
return &PolicySwitcherStrategy{
translator: t,
strategy: s,
remote: r,
}
}

func (p *PolicySwitcherStrategy) Delete(ctx context.Context, obj types.Object) (types.Object, error) {
pvName, err := p.translator.FromVolumeToPVName(ctx, obj.GetNamespace(), obj.GetName())
if err != nil {
return nil, err
}
pvObj, err := p.remote.Get(ctx, "", pvName)
if err != nil {
return nil, err
}
pv := pvObj.(*v1.PersistentVolume)
pv.Spec.PersistentVolumeReclaimPolicy = v1.PersistentVolumeReclaimDelete
_, err = p.remote.Update(ctx, pv)
if err != nil {
return nil, err
}

return p.strategy.Delete(ctx, obj)
}

func (p *PolicySwitcherStrategy) Get(ctx context.Context, namespace, name string) (types.Object, error) {
return p.strategy.Get(ctx, namespace, name)
}
9 changes: 6 additions & 3 deletions pkg/server/registry/apigroups/acorn/volumes/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import (
)

func NewStorage(c kclient.WithWatch) rest.Storage {
translated := translation.NewTranslationStrategy(&Translator{
r := remote.NewRemote(&corev1.PersistentVolume{}, c)
t := &Translator{
c: c,
}, remote.NewRemote(&corev1.PersistentVolume{}, c))
}
translated := translation.NewTranslationStrategy(t, r)
remoteResource := publicname.NewStrategy(translated)
policySwitcher := NewPolicySwitcherStrategy(remoteResource, t, r)

return stores.NewBuilder(c.Scheme(), &apiv1.Volume{}).
WithGet(remoteResource).
WithList(remoteResource).
WithDelete(remoteResource).
WithDelete(policySwitcher).
WithWatch(remoteResource).
WithTableConverter(tables.VolumeConverter).
Build()
Expand Down
30 changes: 30 additions & 0 deletions pkg/server/registry/apigroups/acorn/volumes/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ type Translator struct {
c kclient.Client
}

func (t *Translator) FromVolumeToPVName(ctx context.Context, namespace, name string) (string, error) {
i := strings.LastIndex(name, ".")
// If there is not a period, or string ends with period, parse it not as an alias
if i == -1 || i+1 >= len(name) {
return name, nil
}

// parse it of the form <appName>.<shortVolName>
prefix := name[:i]
volumeName := name[i+1:]

pvs := &corev1.PersistentVolumeList{}
err := t.c.List(ctx, pvs, &kclient.ListOptions{
LabelSelector: klabels.SelectorFromSet(map[string]string{
labels.AcornAppName: prefix,
labels.AcornAppNamespace: namespace,
labels.AcornVolumeName: volumeName,
}),
})
if err != nil {
return "", err
}

if len(pvs.Items) == 1 {
return pvs.Items[0].Name, nil
}

return name, nil
}

func (t *Translator) FromPublicName(ctx context.Context, namespace, name string) (string, string, error) {
i := strings.LastIndex(name, ".")
// If there is not a period, or string ends with period, parse it not as an alias
Expand Down

0 comments on commit b5a6c7f

Please sign in to comment.