This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2479 from StrongMonkey/fix-volume-delete
Fix: add logic to update volume reclaim policy to delete
- Loading branch information
Showing
3 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
pkg/server/registry/apigroups/acorn/volumes/policyswitcher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters