Skip to content

Commit 8a2ae59

Browse files
authored
Merge pull request #130 from fanminshi/impl_list
*: implement List and add necessary pieces
2 parents 2b6c0ff + 4d8146a commit 8a2ae59

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

pkg/sdk/query/op.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ type ListOp struct {
5656
metaListOptions *metav1.ListOptions
5757
}
5858

59+
func NewListOp() *ListOp {
60+
op := &ListOp{}
61+
op.setDefaults()
62+
return op
63+
}
64+
5965
// ListOption configures ListOp.
6066
type ListOption func(*ListOp)
6167

pkg/sdk/query/query.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,28 @@ func Get(into sdkTypes.Object, opts ...GetOption) error {
5050
}
5151
return nil
5252
}
53+
54+
// List retrieves the specified object list and unmarshals the retrieved data into the "into" object.
55+
// "namespace" indicates which kubernetes namespace to look for the list of kubernetes objects.
56+
// "into" is a sdkType.Object that must have
57+
// "Kind" and "APIVersion" specified in its "TypeMeta" field
58+
// Those are used to construct the underlying resource client.
59+
// "opts" configures the List operation.
60+
func List(namespace string, into sdkTypes.Object, opts ...ListOption) error {
61+
gvk := into.GetObjectKind().GroupVersionKind()
62+
apiVersion, kind := gvk.ToAPIVersionAndKind()
63+
resourceClient, _, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
64+
if err != nil {
65+
return fmt.Errorf("failed to get resource client for (apiVersion:%s, kind:%s, ns:%s): %v", apiVersion, kind, namespace, err)
66+
}
67+
o := NewListOp()
68+
o.applyOpts(opts)
69+
l, err := resourceClient.List(*o.metaListOptions)
70+
if err != nil {
71+
return err
72+
}
73+
if err := k8sutil.RuntimeObjectIntoRuntimeObject(l, into); err != nil {
74+
return fmt.Errorf("failed to unmarshal the retrieved data: %v", err)
75+
}
76+
return nil
77+
}

pkg/util/k8sutil/k8sutil.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ func UnstructuredIntoRuntimeObject(u *unstructured.Unstructured, into runtime.Ob
101101
return nil
102102
}
103103

104+
// RuntimeObjectIntoRuntimeObject unmarshalls an runtime.Object into a given runtime object
105+
func RuntimeObjectIntoRuntimeObject(from runtime.Object, into runtime.Object) error {
106+
b, err := json.Marshal(from)
107+
if err != nil {
108+
return err
109+
}
110+
gvk := from.GetObjectKind().GroupVersionKind()
111+
decoder := decoder(gvk.GroupVersion())
112+
_, _, err = decoder.Decode(b, &gvk, into)
113+
if err != nil {
114+
return fmt.Errorf("failed to decode json data with gvk(%v): %v", gvk.String(), err)
115+
}
116+
return nil
117+
}
118+
104119
// GetNameAndNamespace extracts the name and namespace from the given runtime.Object
105120
// and returns a error if any of those is missing.
106121
func GetNameAndNamespace(object runtime.Object) (string, string, error) {

0 commit comments

Comments
 (0)