Skip to content

Commit

Permalink
Fix kargs.ResourceKind and ResourceKindName
Browse files Browse the repository at this point in the history
  • Loading branch information
jgustie committed Feb 12, 2024
1 parent 9a2f4dd commit f01c4bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
24 changes: 13 additions & 11 deletions pkg/pipes/karg/karg.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ func WithWaitOptions(cmd *exec.Cmd, opts ...WaitOption) {
// For Resource and Selector there is more going on... i.e. this is what `kubectl get` says:
// (TYPE[.VERSION][.GROUP] [NAME | -l label] | TYPE[.VERSION][.GROUP]/NAME ...)

// BUT...you can always Kind.version.group to, so long as the trailing dot is there even if group == ""

// Also, you can't have "/" if the name is empty

// Resource represents an individual named resource or type of resource passed as an argument.
type Resource string

Expand All @@ -83,14 +79,18 @@ func (o Resource) kubectlCmd(cmd *exec.Cmd) {
}

// ResourceType returns a resource argument using the GVR(s).
func ResourceType(resource ...string) Resource { return Resource(strings.Join(resource, ",")) }
func ResourceType(resource ...string) Resource {
return Resource(strings.Join(resource, ","))
}

// ResourceKind returns a resource argument using the GVK.
func ResourceKind(apiVersion, kind string) Resource {
if !strings.ContainsRune(apiVersion, '.') {
return Resource(kind + "." + apiVersion + ".")
// This only works because kubectl will actually accept a kind instead of resource name
group, version, ok := strings.Cut(apiVersion, "/")
if !ok {
group, version = version, group
}
return Resource(kind + "." + apiVersion)
return Resource(kind + "." + version + "." + group)
}

// ResourceName returns a resource argument using a GVR and a name.
Expand All @@ -103,10 +103,12 @@ func ResourceName(resourceType, resourceName string) Resource {

// ResourceKindName returns a resource argument using GVK and a name.
func ResourceKindName(apiVersion, kind, name string) Resource {
if !strings.ContainsRune(apiVersion, '.') {
return Resource(kind + "." + apiVersion + "./" + name)
// This only works because kubectl will actually accept a kind instead of resource name
group, version, ok := strings.Cut(apiVersion, "/")
if !ok {
group, version = version, group
}
return Resource(kind + "." + apiVersion + "/" + name)
return Resource(kind + "." + version + "." + group + "/" + name)
}

// AllNamespaces represents the "--all-namespaces" option.
Expand Down
7 changes: 6 additions & 1 deletion pkg/pipes/karg/karg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ func TestWithGetOptions(t *testing.T) {
expected: []string{"kubectl", "get"},
},
{
desc: "resource kind with selector",
desc: "resource core kind with selector",
opts: []GetOption{ResourceKind("v1", "Namespace"), Selector("foo=bar")},
expected: []string{"kubectl", "get", "Namespace.v1.", "--selector", "foo=bar"},
},
{
desc: "resource apps kind with selector",
opts: []GetOption{ResourceKind("apps/v1", "Deployment"), Selector("foo=bar")},
expected: []string{"kubectl", "get", "Deployment.v1.apps", "--selector", "foo=bar"},
},
{
desc: "resource types with all namespaces",
args: []string{"--namespace", "default"},
Expand Down

0 comments on commit f01c4bc

Please sign in to comment.