From ac14c6773b285403215812a48881c11188758444 Mon Sep 17 00:00:00 2001 From: Matthias Lechner Date: Wed, 18 Sep 2019 15:28:28 +0000 Subject: [PATCH] Bugfix: when applying a non-namespace-scoped object that contains namespace metadata and the object has been applied before, apply() will delete the object --- pkg/apply/desiredset_process.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/apply/desiredset_process.go b/pkg/apply/desiredset_process.go index 98423341..31080235 100644 --- a/pkg/apply/desiredset_process.go +++ b/pkg/apply/desiredset_process.go @@ -65,6 +65,28 @@ func (o *desiredSet) adjustNamespace(gvk schema.GroupVersionKind, objs map[objec return nil } +func (o *desiredSet) clearNamespace(objs map[objectset.ObjectKey]runtime.Object) error { + for k, v := range objs { + if k.Namespace == "" { + continue + } + + v = v.DeepCopyObject() + meta, err := meta.Accessor(v) + if err != nil { + return err + } + + meta.SetNamespace("") + + delete(objs, k) + k.Namespace = "" + objs[k] = v + } + + return nil +} + func (o *desiredSet) createPatcher(client dynamic.NamespaceableResourceInterface) Patcher { return func(namespace, name string, pt types2.PatchType, data []byte) (object runtime.Object, e error) { if namespace != "" { @@ -88,6 +110,11 @@ func (o *desiredSet) process(debugID string, set labels.Selector, gvk schema.Gro o.err(err) return } + } else { + if err := o.clearNamespace(objs); err != nil { + o.err(err) + return + } } patcher, ok := o.patchers[gvk] @@ -243,4 +270,3 @@ func addObjectToMap(objs map[objectset.ObjectKey]runtime.Object, obj interface{} return nil } -