Feat: support custom cascading gc policy (#6059)

This commit is contained in:
Somefive
2023-06-01 10:12:10 +08:00
committed by GitHub
parent e215b18a3e
commit 1a6dafafde
8 changed files with 115 additions and 9 deletions

View File

@@ -17,7 +17,9 @@ limitations under the License.
package v1alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
@@ -56,8 +58,9 @@ const (
// GarbageCollectPolicyRule defines a single garbage-collect policy rule
type GarbageCollectPolicyRule struct {
Selector ResourcePolicyRuleSelector `json:"selector"`
Strategy GarbageCollectStrategy `json:"strategy"`
Selector ResourcePolicyRuleSelector `json:"selector"`
Strategy GarbageCollectStrategy `json:"strategy"`
Propagation *GarbageCollectPropagation `json:"propagation"`
}
// GarbageCollectStrategy the strategy for target resource to recycle
@@ -73,6 +76,16 @@ const (
GarbageCollectStrategyOnAppUpdate GarbageCollectStrategy = "onAppUpdate"
)
// GarbageCollectPropagation the deletion propagation setting similar to metav1.DeletionPropagation
type GarbageCollectPropagation string
const (
// GarbageCollectPropagationOrphan orphan child resources while deleting target resources
GarbageCollectPropagationOrphan = "orphan"
// GarbageCollectPropagationCascading delete child resources in background while deleting target resources
GarbageCollectPropagationCascading = "cascading"
)
// Type the type name of the policy
func (in *GarbageCollectPolicySpec) Type() string {
return GarbageCollectPolicyType
@@ -87,3 +100,18 @@ func (in *GarbageCollectPolicySpec) FindStrategy(manifest *unstructured.Unstruct
}
return nil
}
// FindDeleteOption find delete option for target resource
func (in *GarbageCollectPolicySpec) FindDeleteOption(manifest *unstructured.Unstructured) []client.DeleteOption {
for _, rule := range in.Rules {
if rule.Selector.Match(manifest) && rule.Propagation != nil {
switch *rule.Propagation {
case GarbageCollectPropagationOrphan:
return []client.DeleteOption{client.PropagationPolicy(metav1.DeletePropagationOrphan)}
case GarbageCollectPropagationCascading:
return []client.DeleteOption{client.PropagationPolicy(metav1.DeletePropagationBackground)}
}
}
}
return nil
}

View File

@@ -314,6 +314,11 @@ func (in *EnvTraitPatch) DeepCopy() *EnvTraitPatch {
func (in *GarbageCollectPolicyRule) DeepCopyInto(out *GarbageCollectPolicyRule) {
*out = *in
in.Selector.DeepCopyInto(&out.Selector)
if in.Propagation != nil {
in, out := &in.Propagation, &out.Propagation
*out = new(GarbageCollectPropagation)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GarbageCollectPolicyRule.