From 26356305d9c3d9c2fe4452c89117e96e05197332 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Thu, 18 Jan 2024 14:30:36 +0200 Subject: [PATCH] ssa: Restore `namespace not specified` error handing Signed-off-by: Stefan Prodan --- ssa/errors/errors.go | 2 +- ssa/manager_apply_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ssa/errors/errors.go b/ssa/errors/errors.go index 18c8de74..dfbb528d 100644 --- a/ssa/errors/errors.go +++ b/ssa/errors/errors.go @@ -52,7 +52,7 @@ func (e *DryRunErr) Error() string { } if apierrors.IsNotFound(e.Unwrap()) { - if e.involvedObject.GetNamespace() != "" { + if e.involvedObject.GetNamespace() == "" { return fmt.Sprintf("%s namespace not specified: %s", utils.FmtUnstructured(e.involvedObject), e.Unwrap().Error()) } return fmt.Sprintf("%s not found: %s", utils.FmtUnstructured(e.involvedObject), e.Unwrap().Error()) diff --git a/ssa/manager_apply_test.go b/ssa/manager_apply_test.go index ffa95dec..b7c49ed4 100644 --- a/ssa/manager_apply_test.go +++ b/ssa/manager_apply_test.go @@ -949,6 +949,26 @@ func TestApply_Cleanup_Exclusions(t *testing.T) { }) } +func TestApply_MissingNamespaceErr(t *testing.T) { + timeout := 10 * time.Second + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + id := generateName("err") + objects, err := readManifest("testdata/test1.yaml", id) + if err != nil { + t.Fatal(err) + } + + _, configMap := getFirstObject(objects, "ConfigMap", id) + unstructured.RemoveNestedField(configMap.Object, "metadata", "namespace") + + _, err = manager.ApplyAllStaged(ctx, []*unstructured.Unstructured{configMap}, DefaultApplyOptions()) + if !strings.Contains(err.Error(), "namespace not specified") { + t.Fatal("Expected namespace not specified error") + } +} + func containsItemString(s []string, e string) bool { for _, a := range s { if a == e {