From 6bb8bf87c50d04001e1ca92f4617cfd9407cc9e1 Mon Sep 17 00:00:00 2001 From: kwong6 Date: Tue, 16 Aug 2022 11:02:23 -0700 Subject: [PATCH] fix: improper selfLinks for cluster-scoped resources Signed-off-by: kwong6 --- workflow/executor/resource.go | 10 ++++++++-- workflow/executor/resource_test.go | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/workflow/executor/resource.go b/workflow/executor/resource.go index 59afca4d5d40..969934b78d5b 100644 --- a/workflow/executor/resource.go +++ b/workflow/executor/resource.go @@ -85,8 +85,14 @@ func inferObjectSelfLink(obj unstructured.Unstructured) string { selfLinkPrefix = "apis" } // We cannot use `obj.GetSelfLink()` directly since it is deprecated and will be removed after Kubernetes 1.21: https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/1164-remove-selflink - return fmt.Sprintf("%s/%s/namespaces/%s/%s/%s", - selfLinkPrefix, obj.GetAPIVersion(), obj.GetNamespace(), pluralGVR.Resource, obj.GetName()) + var selfLink string + if obj.GetNamespace() == "" { + selfLink = fmt.Sprintf("%s/%s/%s/%s", selfLinkPrefix, obj.GetAPIVersion(), pluralGVR.Resource, obj.GetName()) + } else { + selfLink = fmt.Sprintf("%s/%s/namespaces/%s/%s/%s", + selfLinkPrefix, obj.GetAPIVersion(), obj.GetNamespace(), pluralGVR.Resource, obj.GetName()) + } + return selfLink } func (we *WorkflowExecutor) getKubectlArguments(action string, manifestPath string, flags []string) ([]string, error) { diff --git a/workflow/executor/resource_test.go b/workflow/executor/resource_test.go index e350928ea000..963c591b4c1e 100644 --- a/workflow/executor/resource_test.go +++ b/workflow/executor/resource_test.go @@ -174,6 +174,15 @@ func TestInferSelfLink(t *testing.T) { Kind: "Duty", }) assert.Equal(t, "apis/test.group/v1/namespaces/test-namespace/duties/test-name", inferObjectSelfLink(obj)) + + obj.SetNamespace("") + obj.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "Namespace", + }) + assert.Equal(t, "api/v1/namespaces/test-name", inferObjectSelfLink(obj)) + } // TestResourceExecRetry tests whether Exec retries transitive errors