Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set client custom GVK for source list for machine readable output #980

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
| 🐛
| Fix client side volume name generation
| https://github.com/knative/client/pull/975[#975]

| ✨
| `kn source list` output now has client custom GVK set as `{Group: client.knative.dev, Version: v1alpha1, Kind: SourceList}`
| https://github.com/knative/client/pull/980[#980]
|===

## v0.16.0 (2020-07-14)
Expand Down
31 changes: 11 additions & 20 deletions pkg/dynamic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
crdKinds = "customresourcedefinitions"
sourcesLabelKey = "duck.knative.dev/source"
sourcesLabelValue = "true"
sourceListGroup = "client.knative.dev"
sourceListVersion = "v1alpha1"
sourceListKind = "SourceList"
)

// KnDynamicClient to client-go Dynamic client. All methods are relative to the
Expand Down Expand Up @@ -111,9 +114,8 @@ func (c knDynamicClient) RawClient() dynamic.Interface {
// only given types of source objects
func (c *knDynamicClient) ListSources(types ...WithType) (*unstructured.UnstructuredList, error) {
var (
sourceList unstructured.UnstructuredList
options metav1.ListOptions
numberOfSourceTypesFound int
sourceList unstructured.UnstructuredList
options metav1.ListOptions
)
sourceTypes, err := c.ListSourcesTypes()
if err != nil {
Expand Down Expand Up @@ -151,16 +153,11 @@ func (c *knDynamicClient) ListSources(types ...WithType) (*unstructured.Unstruct
}

if len(sList.Items) > 0 {
// keep a track if we found source objects of different types
numberOfSourceTypesFound++
sourceList.Items = append(sourceList.Items, sList.Items...)
sourceList.SetGroupVersionKind(sList.GetObjectKind().GroupVersionKind())
}
}
// Clear the Group and Version for list if there are multiple types of source objects found
// Keep the source's GVK if there is only one type of source objects found or requested via --type filter
if numberOfSourceTypesFound > 1 {
sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "", Kind: "List"})
if len(sourceList.Items) > 0 {
sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: sourceListGroup, Version: sourceListVersion, Kind: sourceListKind})
}
return &sourceList, nil
}
Expand All @@ -172,9 +169,8 @@ func (c *knDynamicClient) ListSourcesUsingGVKs(gvks *[]schema.GroupVersionKind,
}

var (
sourceList unstructured.UnstructuredList
options metav1.ListOptions
numberOfSourceTypesFound int
sourceList unstructured.UnstructuredList
options metav1.ListOptions
)
namespace := c.Namespace()
filters := WithTypes(types).List()
Expand All @@ -193,16 +189,11 @@ func (c *knDynamicClient) ListSourcesUsingGVKs(gvks *[]schema.GroupVersionKind,
}

if len(sList.Items) > 0 {
// keep a track if we found source objects of different types
numberOfSourceTypesFound++
sourceList.Items = append(sourceList.Items, sList.Items...)
sourceList.SetGroupVersionKind(sList.GetObjectKind().GroupVersionKind())
}
}
// Clear the Group and Version for list if there are multiple types of source objects found
// Keep the source's GVK if there is only one type of source objects found or requested via --type filter
if numberOfSourceTypesFound > 1 {
sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "", Kind: "List"})
if len(sourceList.Items) > 0 {
sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: sourceListGroup, Version: sourceListVersion, Kind: sourceListKind})
}
return &sourceList, nil
}
3 changes: 3 additions & 0 deletions pkg/dynamic/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestListSources(t *testing.T) {
sources, err := client.ListSources(WithTypeFilter("pingsource"), WithTypeFilter("ApiServerSource"))
assert.NilError(t, err)
assert.Equal(t, len(sources.Items), 2)
assert.DeepEqual(t, sources.GroupVersionKind(), schema.GroupVersionKind{sourceListGroup, sourceListVersion, sourceListKind})
})
}

Expand Down Expand Up @@ -149,12 +150,14 @@ func TestListSourcesUsingGVKs(t *testing.T) {
assert.NilError(t, err)
assert.Check(t, s != nil)
assert.Equal(t, len(s.Items), 2)
assert.DeepEqual(t, s.GroupVersionKind(), schema.GroupVersionKind{sourceListGroup, sourceListVersion, sourceListKind})

// withType
s, err = client.ListSourcesUsingGVKs(&gvks, WithTypeFilter("PingSource"))
assert.NilError(t, err)
assert.Check(t, s != nil)
assert.Equal(t, len(s.Items), 1)
assert.DeepEqual(t, s.GroupVersionKind(), schema.GroupVersionKind{sourceListGroup, sourceListVersion, sourceListKind})
})

}
Expand Down