Skip to content

Commit

Permalink
No prune for namespaces that contain current objects
Browse files Browse the repository at this point in the history
  • Loading branch information
seans3 committed Nov 9, 2020
1 parent fcb33da commit d090658
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
31 changes: 27 additions & 4 deletions pkg/apply/prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package prune

import (
"sort"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -82,8 +83,15 @@ type Options struct {
// objects. Returns an error if there was a problem.
func (po *PruneOptions) Prune(localInv *unstructured.Unstructured, localObjs []*unstructured.Unstructured, currentUIDs sets.String,
eventChannel chan<- event.Event, o Options) error {
invNamespace := localInv.GetNamespace()
invNamespace := strings.TrimSpace(strings.ToLower(localInv.GetNamespace()))
klog.V(4).Infof("prune local inventory object: %s/%s", invNamespace, localInv.GetName())
// Create the set of namespaces for currently (locally) applied objects, including
// the namespace the inventory object lives in (if it's not cluster-scoped). When
// pruning, check this set of namespaces to ensure these namespaces are not deleted.
localNamespaces := mergeObjNamespaces(localObjs)
if invNamespace != "" {
localNamespaces.Insert(invNamespace)
}
clusterObjs, err := po.InvClient.GetClusterObjs(localInv)
if err != nil {
return err
Expand Down Expand Up @@ -126,11 +134,12 @@ func (po *PruneOptions) Prune(localInv *unstructured.Unstructured, localObjs []*
eventChannel <- createPruneEvent(obj, event.PruneSkipped)
continue
}
// If regular pruning (not destroying), skip deleting inventory namespace.
// If regular pruning (not destroying), skip deleting namespace containing
// currently applied objects.
if !po.Destroy {
if clusterObj.GroupKind == object.CoreV1Namespace.GroupKind() &&
clusterObj.Name == invNamespace {
klog.V(7).Infof("skip pruning inventory namespace: %s", obj)
localNamespaces.Has(strings.ToLower(clusterObj.Name)) {
klog.V(7).Infof("skip pruning namespace: %s", clusterObj.Name)
eventChannel <- createPruneEvent(obj, event.PruneSkipped)
continue
}
Expand All @@ -148,6 +157,20 @@ func (po *PruneOptions) Prune(localInv *unstructured.Unstructured, localObjs []*
return po.InvClient.Replace(localInv, localIds)
}

// mergeObjNamespaces returns a set of strings of all the namespaces
// for non cluster-scoped objects. These namespaces are forced to
// lower-case.
func mergeObjNamespaces(objs []*unstructured.Unstructured) sets.String {
namespaces := sets.NewString()
for _, obj := range objs {
namespace := strings.TrimSpace(strings.ToLower(obj.GetNamespace()))
if namespace != "" {
namespaces.Insert(namespace)
}
}
return namespaces
}

// preventDeleteAnnotation returns true if the "onRemove:keep"
// annotation exists within the annotation map; false otherwise.
func preventDeleteAnnotation(annotations map[string]string) bool {
Expand Down
21 changes: 19 additions & 2 deletions pkg/apply/prune/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

var testNamespace = "test-inventory-namespace"
var inventoryObjName = "test-inventory-obj"
var namespaceName = "namespace"
var podName = "pod-1"
var pdbName = "pdb"
var roleName = "role"

Expand All @@ -45,7 +45,18 @@ var namespace = &unstructured.Unstructured{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": namespaceName,
"name": testNamespace,
"uid": "uid1",
},
},
}

var pod = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": podName,
"namespace": testNamespace,
"uid": "uid1",
},
Expand Down Expand Up @@ -150,6 +161,12 @@ func TestPrune(t *testing.T) {
prunedObjs: []*unstructured.Unstructured{},
isError: false,
},
"Namespace not pruned if objects are still in it": {
pastObjs: []*unstructured.Unstructured{namespace, pdb, pod},
currentObjs: []*unstructured.Unstructured{pod},
prunedObjs: []*unstructured.Unstructured{pdb},
isError: false,
},
}
for name, tc := range tests {
for i := range common.Strategies {
Expand Down

0 comments on commit d090658

Please sign in to comment.