Skip to content

Commit

Permalink
Set client custom GVK for source list for machine readable output (kn…
Browse files Browse the repository at this point in the history
…ative#980)

- Use custom GVK {Group: client.knative.dev, Version: v1alpha1, Kind: SourceList}
 - Source list may contain different source types CO and machine readable output (using -o)
   requires List object to have GVK set, since the list contains different types of source COs,
   we set a custom client GVK on it.

 Fixes knative#953
  • Loading branch information
navidshaikh authored and rhuss committed Sep 9, 2020
1 parent d8681c4 commit fc2dcd6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
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

0 comments on commit fc2dcd6

Please sign in to comment.