Skip to content

Commit

Permalink
fix(executor): Resource template gets incorrect plural for certain ty…
Browse files Browse the repository at this point in the history
…pes (#9396)

* fix(executor): resource template gets incorrect plural for certain types

Signed-off-by: William Reed <willie.b.reed@gmail.com>

* fix(executor): fix lint

Signed-off-by: William Reed <willie.b.reed@gmail.com>

Signed-off-by: William Reed <willie.b.reed@gmail.com>
  • Loading branch information
wreed4 authored Aug 25, 2022
1 parent 3ddbb5e commit de6b5ae
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ require (
k8s.io/api v0.24.3
k8s.io/apimachinery v0.24.3
k8s.io/client-go v0.24.3
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7
k8s.io/klog/v2 v2.60.1
k8s.io/kube-openapi v0.0.0-20220627174259-011e075b9cb8
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2318,6 +2318,8 @@ k8s.io/cri-api v0.20.6/go.mod h1:ew44AjNXwyn1s0U4xCKGodU7J1HzBeZ1MpGrpa5r8Yc=
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7 h1:RGb68G3yotdQggcyenx9y0+lnVJCXXcLa6geXOMlf5o=
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
Expand Down
13 changes: 9 additions & 4 deletions workflow/executor/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
apierr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"k8s.io/gengo/namer"
gengotypes "k8s.io/gengo/types"

"github.com/argoproj/argo-workflows/v3/errors"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
Expand Down Expand Up @@ -77,7 +78,11 @@ func inferObjectSelfLink(obj unstructured.Unstructured) string {
gvk := obj.GroupVersionKind()
// This is the best guess we can do here and is what `kubectl` uses under the hood. Hopefully future versions of the
// REST client would remove the need to infer the plural name.
pluralGVR, _ := meta.UnsafeGuessKindToResource(gvk)
lowercaseNamer := namer.NewAllLowercasePluralNamer(map[string]string{})
pluralName := lowercaseNamer.Name(&gengotypes.Type{Name: gengotypes.Name{
Name: gvk.Kind,
}})

var selfLinkPrefix string
if gvk.Group == "" {
selfLinkPrefix = "api"
Expand All @@ -87,10 +92,10 @@ func inferObjectSelfLink(obj unstructured.Unstructured) string {
// 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
var selfLink string
if obj.GetNamespace() == "" {
selfLink = fmt.Sprintf("%s/%s/%s/%s", selfLinkPrefix, obj.GetAPIVersion(), pluralGVR.Resource, obj.GetName())
selfLink = fmt.Sprintf("%s/%s/%s/%s", selfLinkPrefix, obj.GetAPIVersion(), pluralName, obj.GetName())
} else {
selfLink = fmt.Sprintf("%s/%s/namespaces/%s/%s/%s",
selfLinkPrefix, obj.GetAPIVersion(), obj.GetNamespace(), pluralGVR.Resource, obj.GetName())
selfLinkPrefix, obj.GetAPIVersion(), obj.GetNamespace(), pluralName, obj.GetName())
}
return selfLink
}
Expand Down
7 changes: 7 additions & 0 deletions workflow/executor/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ func TestInferSelfLink(t *testing.T) {
})
assert.Equal(t, "apis/test.group/v1/namespaces/test-namespace/duties/test-name", inferObjectSelfLink(obj))

obj.SetGroupVersionKind(schema.GroupVersionKind{
Group: "test.group",
Version: "v1",
Kind: "IngressGateway",
})
assert.Equal(t, "apis/test.group/v1/namespaces/test-namespace/ingressgateways/test-name", inferObjectSelfLink(obj))

obj.SetNamespace("")
obj.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Expand Down

0 comments on commit de6b5ae

Please sign in to comment.