|
| 1 | +/* |
| 2 | +Copyright 2015 The Kubernetes Authors. |
| 3 | +
|
| 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 |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 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. |
| 15 | +*/ |
| 16 | + |
| 17 | +package unstructured |
| 18 | + |
| 19 | +import ( |
| 20 | + gojson "encoding/json" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "strings" |
| 25 | + |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/conversion/unstructured" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 30 | + "k8s.io/apimachinery/pkg/types" |
| 31 | + "k8s.io/apimachinery/pkg/util/json" |
| 32 | +) |
| 33 | + |
| 34 | +func getNestedField(obj map[string]interface{}, fields ...string) interface{} { |
| 35 | + var val interface{} = obj |
| 36 | + for _, field := range fields { |
| 37 | + if _, ok := val.(map[string]interface{}); !ok { |
| 38 | + return nil |
| 39 | + } |
| 40 | + val = val.(map[string]interface{})[field] |
| 41 | + } |
| 42 | + return val |
| 43 | +} |
| 44 | + |
| 45 | +func getNestedString(obj map[string]interface{}, fields ...string) string { |
| 46 | + if str, ok := getNestedField(obj, fields...).(string); ok { |
| 47 | + return str |
| 48 | + } |
| 49 | + return "" |
| 50 | +} |
| 51 | + |
| 52 | +func getNestedInt64(obj map[string]interface{}, fields ...string) int64 { |
| 53 | + if str, ok := getNestedField(obj, fields...).(int64); ok { |
| 54 | + return str |
| 55 | + } |
| 56 | + return 0 |
| 57 | +} |
| 58 | + |
| 59 | +func getNestedInt64Pointer(obj map[string]interface{}, fields ...string) *int64 { |
| 60 | + nested := getNestedField(obj, fields...) |
| 61 | + switch n := nested.(type) { |
| 62 | + case int64: |
| 63 | + return &n |
| 64 | + case *int64: |
| 65 | + return n |
| 66 | + default: |
| 67 | + return nil |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func getNestedSlice(obj map[string]interface{}, fields ...string) []string { |
| 72 | + if m, ok := getNestedField(obj, fields...).([]interface{}); ok { |
| 73 | + strSlice := make([]string, 0, len(m)) |
| 74 | + for _, v := range m { |
| 75 | + if str, ok := v.(string); ok { |
| 76 | + strSlice = append(strSlice, str) |
| 77 | + } |
| 78 | + } |
| 79 | + return strSlice |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func getNestedMap(obj map[string]interface{}, fields ...string) map[string]string { |
| 85 | + if m, ok := getNestedField(obj, fields...).(map[string]interface{}); ok { |
| 86 | + strMap := make(map[string]string, len(m)) |
| 87 | + for k, v := range m { |
| 88 | + if str, ok := v.(string); ok { |
| 89 | + strMap[k] = str |
| 90 | + } |
| 91 | + } |
| 92 | + return strMap |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func setNestedField(obj map[string]interface{}, value interface{}, fields ...string) { |
| 98 | + m := obj |
| 99 | + if len(fields) > 1 { |
| 100 | + for _, field := range fields[0 : len(fields)-1] { |
| 101 | + if _, ok := m[field].(map[string]interface{}); !ok { |
| 102 | + m[field] = make(map[string]interface{}) |
| 103 | + } |
| 104 | + m = m[field].(map[string]interface{}) |
| 105 | + } |
| 106 | + } |
| 107 | + m[fields[len(fields)-1]] = value |
| 108 | +} |
| 109 | + |
| 110 | +func setNestedSlice(obj map[string]interface{}, value []string, fields ...string) { |
| 111 | + m := make([]interface{}, 0, len(value)) |
| 112 | + for _, v := range value { |
| 113 | + m = append(m, v) |
| 114 | + } |
| 115 | + setNestedField(obj, m, fields...) |
| 116 | +} |
| 117 | + |
| 118 | +func setNestedMap(obj map[string]interface{}, value map[string]string, fields ...string) { |
| 119 | + m := make(map[string]interface{}, len(value)) |
| 120 | + for k, v := range value { |
| 121 | + m[k] = v |
| 122 | + } |
| 123 | + setNestedField(obj, m, fields...) |
| 124 | +} |
| 125 | + |
| 126 | +func extractOwnerReference(src interface{}) metav1.OwnerReference { |
| 127 | + v := src.(map[string]interface{}) |
| 128 | + // though this field is a *bool, but when decoded from JSON, it's |
| 129 | + // unmarshalled as bool. |
| 130 | + var controllerPtr *bool |
| 131 | + controller, ok := (getNestedField(v, "controller")).(bool) |
| 132 | + if !ok { |
| 133 | + controllerPtr = nil |
| 134 | + } else { |
| 135 | + controllerCopy := controller |
| 136 | + controllerPtr = &controllerCopy |
| 137 | + } |
| 138 | + var blockOwnerDeletionPtr *bool |
| 139 | + blockOwnerDeletion, ok := (getNestedField(v, "blockOwnerDeletion")).(bool) |
| 140 | + if !ok { |
| 141 | + blockOwnerDeletionPtr = nil |
| 142 | + } else { |
| 143 | + blockOwnerDeletionCopy := blockOwnerDeletion |
| 144 | + blockOwnerDeletionPtr = &blockOwnerDeletionCopy |
| 145 | + } |
| 146 | + return metav1.OwnerReference{ |
| 147 | + Kind: getNestedString(v, "kind"), |
| 148 | + Name: getNestedString(v, "name"), |
| 149 | + APIVersion: getNestedString(v, "apiVersion"), |
| 150 | + UID: (types.UID)(getNestedString(v, "uid")), |
| 151 | + Controller: controllerPtr, |
| 152 | + BlockOwnerDeletion: blockOwnerDeletionPtr, |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func setOwnerReference(src metav1.OwnerReference) map[string]interface{} { |
| 157 | + ret := make(map[string]interface{}) |
| 158 | + setNestedField(ret, src.Kind, "kind") |
| 159 | + setNestedField(ret, src.Name, "name") |
| 160 | + setNestedField(ret, src.APIVersion, "apiVersion") |
| 161 | + setNestedField(ret, string(src.UID), "uid") |
| 162 | + // json.Unmarshal() extracts boolean json fields as bool, not as *bool and hence extractOwnerReference() |
| 163 | + // expects bool or a missing field, not *bool. So if pointer is nil, fields are omitted from the ret object. |
| 164 | + // If pointer is non-nil, they are set to the referenced value. |
| 165 | + if src.Controller != nil { |
| 166 | + setNestedField(ret, *src.Controller, "controller") |
| 167 | + } |
| 168 | + if src.BlockOwnerDeletion != nil { |
| 169 | + setNestedField(ret, *src.BlockOwnerDeletion, "blockOwnerDeletion") |
| 170 | + } |
| 171 | + return ret |
| 172 | +} |
| 173 | + |
| 174 | +func getOwnerReferences(object map[string]interface{}) ([]map[string]interface{}, error) { |
| 175 | + field := getNestedField(object, "metadata", "ownerReferences") |
| 176 | + if field == nil { |
| 177 | + return nil, fmt.Errorf("cannot find field metadata.ownerReferences in %v", object) |
| 178 | + } |
| 179 | + ownerReferences, ok := field.([]map[string]interface{}) |
| 180 | + if ok { |
| 181 | + return ownerReferences, nil |
| 182 | + } |
| 183 | + // TODO: This is hacky... |
| 184 | + interfaces, ok := field.([]interface{}) |
| 185 | + if !ok { |
| 186 | + return nil, fmt.Errorf("expect metadata.ownerReferences to be a slice in %#v", object) |
| 187 | + } |
| 188 | + ownerReferences = make([]map[string]interface{}, 0, len(interfaces)) |
| 189 | + for i := 0; i < len(interfaces); i++ { |
| 190 | + r, ok := interfaces[i].(map[string]interface{}) |
| 191 | + if !ok { |
| 192 | + return nil, fmt.Errorf("expect element metadata.ownerReferences to be a map[string]interface{} in %#v", object) |
| 193 | + } |
| 194 | + ownerReferences = append(ownerReferences, r) |
| 195 | + } |
| 196 | + return ownerReferences, nil |
| 197 | +} |
| 198 | + |
| 199 | +var converter = unstructured.NewConverter(false) |
| 200 | + |
| 201 | +// UnstructuredJSONScheme is capable of converting JSON data into the Unstructured |
| 202 | +// type, which can be used for generic access to objects without a predefined scheme. |
| 203 | +// TODO: move into serializer/json. |
| 204 | +var UnstructuredJSONScheme runtime.Codec = unstructuredJSONScheme{} |
| 205 | + |
| 206 | +type unstructuredJSONScheme struct{} |
| 207 | + |
| 208 | +func (s unstructuredJSONScheme) Decode(data []byte, _ *schema.GroupVersionKind, obj runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) { |
| 209 | + var err error |
| 210 | + if obj != nil { |
| 211 | + err = s.decodeInto(data, obj) |
| 212 | + } else { |
| 213 | + obj, err = s.decode(data) |
| 214 | + } |
| 215 | + |
| 216 | + if err != nil { |
| 217 | + return nil, nil, err |
| 218 | + } |
| 219 | + |
| 220 | + gvk := obj.GetObjectKind().GroupVersionKind() |
| 221 | + if len(gvk.Kind) == 0 { |
| 222 | + return nil, &gvk, runtime.NewMissingKindErr(string(data)) |
| 223 | + } |
| 224 | + |
| 225 | + return obj, &gvk, nil |
| 226 | +} |
| 227 | + |
| 228 | +func (unstructuredJSONScheme) Encode(obj runtime.Object, w io.Writer) error { |
| 229 | + switch t := obj.(type) { |
| 230 | + case *Unstructured: |
| 231 | + return json.NewEncoder(w).Encode(t.Object) |
| 232 | + case *UnstructuredList: |
| 233 | + items := make([]map[string]interface{}, 0, len(t.Items)) |
| 234 | + for _, i := range t.Items { |
| 235 | + items = append(items, i.Object) |
| 236 | + } |
| 237 | + listObj := make(map[string]interface{}, len(t.Object)+1) |
| 238 | + for k, v := range t.Object { // Make a shallow copy |
| 239 | + listObj[k] = v |
| 240 | + } |
| 241 | + listObj["items"] = items |
| 242 | + return json.NewEncoder(w).Encode(listObj) |
| 243 | + case *runtime.Unknown: |
| 244 | + // TODO: Unstructured needs to deal with ContentType. |
| 245 | + _, err := w.Write(t.Raw) |
| 246 | + return err |
| 247 | + default: |
| 248 | + return json.NewEncoder(w).Encode(t) |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +func (s unstructuredJSONScheme) decode(data []byte) (runtime.Object, error) { |
| 253 | + type detector struct { |
| 254 | + Items gojson.RawMessage |
| 255 | + } |
| 256 | + var det detector |
| 257 | + if err := json.Unmarshal(data, &det); err != nil { |
| 258 | + return nil, err |
| 259 | + } |
| 260 | + |
| 261 | + if det.Items != nil { |
| 262 | + list := &UnstructuredList{} |
| 263 | + err := s.decodeToList(data, list) |
| 264 | + return list, err |
| 265 | + } |
| 266 | + |
| 267 | + // No Items field, so it wasn't a list. |
| 268 | + unstruct := &Unstructured{} |
| 269 | + err := s.decodeToUnstructured(data, unstruct) |
| 270 | + return unstruct, err |
| 271 | +} |
| 272 | + |
| 273 | +func (s unstructuredJSONScheme) decodeInto(data []byte, obj runtime.Object) error { |
| 274 | + switch x := obj.(type) { |
| 275 | + case *Unstructured: |
| 276 | + return s.decodeToUnstructured(data, x) |
| 277 | + case *UnstructuredList: |
| 278 | + return s.decodeToList(data, x) |
| 279 | + case *runtime.VersionedObjects: |
| 280 | + o, err := s.decode(data) |
| 281 | + if err == nil { |
| 282 | + x.Objects = []runtime.Object{o} |
| 283 | + } |
| 284 | + return err |
| 285 | + default: |
| 286 | + return json.Unmarshal(data, x) |
| 287 | + } |
| 288 | +} |
| 289 | + |
| 290 | +func (unstructuredJSONScheme) decodeToUnstructured(data []byte, unstruct *Unstructured) error { |
| 291 | + m := make(map[string]interface{}) |
| 292 | + if err := json.Unmarshal(data, &m); err != nil { |
| 293 | + return err |
| 294 | + } |
| 295 | + |
| 296 | + unstruct.Object = m |
| 297 | + |
| 298 | + return nil |
| 299 | +} |
| 300 | + |
| 301 | +func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList) error { |
| 302 | + type decodeList struct { |
| 303 | + Items []gojson.RawMessage |
| 304 | + } |
| 305 | + |
| 306 | + var dList decodeList |
| 307 | + if err := json.Unmarshal(data, &dList); err != nil { |
| 308 | + return err |
| 309 | + } |
| 310 | + |
| 311 | + if err := json.Unmarshal(data, &list.Object); err != nil { |
| 312 | + return err |
| 313 | + } |
| 314 | + |
| 315 | + // For typed lists, e.g., a PodList, API server doesn't set each item's |
| 316 | + // APIVersion and Kind. We need to set it. |
| 317 | + listAPIVersion := list.GetAPIVersion() |
| 318 | + listKind := list.GetKind() |
| 319 | + itemKind := strings.TrimSuffix(listKind, "List") |
| 320 | + |
| 321 | + delete(list.Object, "items") |
| 322 | + list.Items = nil |
| 323 | + for _, i := range dList.Items { |
| 324 | + unstruct := &Unstructured{} |
| 325 | + if err := s.decodeToUnstructured([]byte(i), unstruct); err != nil { |
| 326 | + return err |
| 327 | + } |
| 328 | + // This is hacky. Set the item's Kind and APIVersion to those inferred |
| 329 | + // from the List. |
| 330 | + if len(unstruct.GetKind()) == 0 && len(unstruct.GetAPIVersion()) == 0 { |
| 331 | + unstruct.SetKind(itemKind) |
| 332 | + unstruct.SetAPIVersion(listAPIVersion) |
| 333 | + } |
| 334 | + list.Items = append(list.Items, *unstruct) |
| 335 | + } |
| 336 | + return nil |
| 337 | +} |
| 338 | + |
| 339 | +// UnstructuredObjectConverter is an ObjectConverter for use with |
| 340 | +// Unstructured objects. Since it has no schema or type information, |
| 341 | +// it will only succeed for no-op conversions. This is provided as a |
| 342 | +// sane implementation for APIs that require an object converter. |
| 343 | +type UnstructuredObjectConverter struct{} |
| 344 | + |
| 345 | +func (UnstructuredObjectConverter) Convert(in, out, context interface{}) error { |
| 346 | + unstructIn, ok := in.(*Unstructured) |
| 347 | + if !ok { |
| 348 | + return fmt.Errorf("input type %T in not valid for unstructured conversion", in) |
| 349 | + } |
| 350 | + |
| 351 | + unstructOut, ok := out.(*Unstructured) |
| 352 | + if !ok { |
| 353 | + return fmt.Errorf("output type %T in not valid for unstructured conversion", out) |
| 354 | + } |
| 355 | + |
| 356 | + // maybe deep copy the map? It is documented in the |
| 357 | + // ObjectConverter interface that this function is not |
| 358 | + // guaranteeed to not mutate the input. Or maybe set the input |
| 359 | + // object to nil. |
| 360 | + unstructOut.Object = unstructIn.Object |
| 361 | + return nil |
| 362 | +} |
| 363 | + |
| 364 | +func (UnstructuredObjectConverter) ConvertToVersion(in runtime.Object, target runtime.GroupVersioner) (runtime.Object, error) { |
| 365 | + if kind := in.GetObjectKind().GroupVersionKind(); !kind.Empty() { |
| 366 | + gvk, ok := target.KindForGroupVersionKinds([]schema.GroupVersionKind{kind}) |
| 367 | + if !ok { |
| 368 | + // TODO: should this be a typed error? |
| 369 | + return nil, fmt.Errorf("%v is unstructured and is not suitable for converting to %q", kind, target) |
| 370 | + } |
| 371 | + in.GetObjectKind().SetGroupVersionKind(gvk) |
| 372 | + } |
| 373 | + return in, nil |
| 374 | +} |
| 375 | + |
| 376 | +func (UnstructuredObjectConverter) ConvertFieldLabel(version, kind, label, value string) (string, string, error) { |
| 377 | + return "", "", errors.New("unstructured cannot convert field labels") |
| 378 | +} |
0 commit comments