Skip to content

Commit

Permalink
fix plural name bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wreed4 committed Aug 19, 2022
1 parent 6e8d162 commit 268834f
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 @@ -218,6 +218,7 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7 // indirect
moul.io/http2curl v1.0.1-0.20190925090545-5cd742060b0e // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
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 268834f

Please sign in to comment.