2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/apimachinery/pkg/runtime"
24 "k8s.io/apimachinery/pkg/runtime/schema"
27 var _ runtime.Unstructured = &UnstructuredList{}
28 var _ metav1.ListInterface = &UnstructuredList{}
30 // UnstructuredList allows lists that do not have Golang structs
31 // registered to be manipulated generically. This can be used to deal
32 // with the API lists from a plug-in.
33 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
34 // +k8s:deepcopy-gen=true
35 type UnstructuredList struct {
36 Object map[string]interface{}
38 // Items is a list of unstructured objects.
39 Items []Unstructured `json:"items"`
42 func (u *UnstructuredList) GetObjectKind() schema.ObjectKind { return u }
44 func (u *UnstructuredList) IsList() bool { return true }
46 func (u *UnstructuredList) EachListItem(fn func(runtime.Object) error) error {
47 for i := range u.Items {
48 if err := fn(&u.Items[i]); err != nil {
55 // UnstructuredContent returns a map contain an overlay of the Items field onto
56 // the Object field. Items always overwrites overlay.
57 func (u *UnstructuredList) UnstructuredContent() map[string]interface{} {
58 out := make(map[string]interface{}, len(u.Object)+1)
60 // shallow copy every property
61 for k, v := range u.Object {
65 items := make([]interface{}, len(u.Items))
66 for i, item := range u.Items {
67 items[i] = item.UnstructuredContent()
73 // SetUnstructuredContent obeys the conventions of List and keeps Items and the items
74 // array in sync. If items is not an array of objects in the incoming map, then any
75 // mismatched item will be removed.
76 func (obj *UnstructuredList) SetUnstructuredContent(content map[string]interface{}) {
82 items, ok := obj.Object["items"].([]interface{})
83 if !ok || items == nil {
84 items = []interface{}{}
86 unstructuredItems := make([]Unstructured, 0, len(items))
87 newItems := make([]interface{}, 0, len(items))
88 for _, item := range items {
89 o, ok := item.(map[string]interface{})
93 unstructuredItems = append(unstructuredItems, Unstructured{Object: o})
94 newItems = append(newItems, o)
96 obj.Items = unstructuredItems
97 obj.Object["items"] = newItems
100 func (u *UnstructuredList) DeepCopy() *UnstructuredList {
104 out := new(UnstructuredList)
106 out.Object = runtime.DeepCopyJSON(u.Object)
107 out.Items = make([]Unstructured, len(u.Items))
108 for i := range u.Items {
109 u.Items[i].DeepCopyInto(&out.Items[i])
114 // MarshalJSON ensures that the unstructured list object produces proper
115 // JSON when passed to Go's standard JSON library.
116 func (u *UnstructuredList) MarshalJSON() ([]byte, error) {
118 err := UnstructuredJSONScheme.Encode(u, &buf)
119 return buf.Bytes(), err
122 // UnmarshalJSON ensures that the unstructured list object properly
123 // decodes JSON when passed to Go's standard JSON library.
124 func (u *UnstructuredList) UnmarshalJSON(b []byte) error {
125 _, _, err := UnstructuredJSONScheme.Decode(b, nil, u)
129 func (u *UnstructuredList) GetAPIVersion() string {
130 return getNestedString(u.Object, "apiVersion")
133 func (u *UnstructuredList) SetAPIVersion(version string) {
134 u.setNestedField(version, "apiVersion")
137 func (u *UnstructuredList) GetKind() string {
138 return getNestedString(u.Object, "kind")
141 func (u *UnstructuredList) SetKind(kind string) {
142 u.setNestedField(kind, "kind")
145 func (u *UnstructuredList) GetResourceVersion() string {
146 return getNestedString(u.Object, "metadata", "resourceVersion")
149 func (u *UnstructuredList) SetResourceVersion(version string) {
150 u.setNestedField(version, "metadata", "resourceVersion")
153 func (u *UnstructuredList) GetSelfLink() string {
154 return getNestedString(u.Object, "metadata", "selfLink")
157 func (u *UnstructuredList) SetSelfLink(selfLink string) {
158 u.setNestedField(selfLink, "metadata", "selfLink")
161 func (u *UnstructuredList) GetContinue() string {
162 return getNestedString(u.Object, "metadata", "continue")
165 func (u *UnstructuredList) SetContinue(c string) {
166 u.setNestedField(c, "metadata", "continue")
169 func (u *UnstructuredList) SetGroupVersionKind(gvk schema.GroupVersionKind) {
170 u.SetAPIVersion(gvk.GroupVersion().String())
174 func (u *UnstructuredList) GroupVersionKind() schema.GroupVersionKind {
175 gv, err := schema.ParseGroupVersion(u.GetAPIVersion())
177 return schema.GroupVersionKind{}
179 gvk := gv.WithKind(u.GetKind())
183 func (u *UnstructuredList) setNestedField(value interface{}, fields ...string) {
185 u.Object = make(map[string]interface{})
187 SetNestedField(u.Object, value, fields...)