123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- /*
- Copyright 2015 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package unstructured
- import (
- "bytes"
- "errors"
- "fmt"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/runtime/schema"
- "k8s.io/apimachinery/pkg/types"
- utilruntime "k8s.io/apimachinery/pkg/util/runtime"
- )
- // Unstructured allows objects that do not have Golang structs registered to be manipulated
- // generically. This can be used to deal with the API objects from a plug-in. Unstructured
- // objects still have functioning TypeMeta features-- kind, version, etc.
- //
- // WARNING: This object has accessors for the v1 standard metadata. You *MUST NOT* use this
- // type if you are dealing with objects that are not in the server meta v1 schema.
- //
- // TODO: make the serialization part of this type distinct from the field accessors.
- // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
- // +k8s:deepcopy-gen=true
- type Unstructured struct {
- // Object is a JSON compatible map with string, float, int, bool, []interface{}, or
- // map[string]interface{}
- // children.
- Object map[string]interface{}
- }
- var _ metav1.Object = &Unstructured{}
- var _ runtime.Unstructured = &Unstructured{}
- func (obj *Unstructured) GetObjectKind() schema.ObjectKind { return obj }
- func (obj *Unstructured) IsList() bool {
- field, ok := obj.Object["items"]
- if !ok {
- return false
- }
- _, ok = field.([]interface{})
- return ok
- }
- func (obj *Unstructured) ToList() (*UnstructuredList, error) {
- if !obj.IsList() {
- // return an empty list back
- return &UnstructuredList{Object: obj.Object}, nil
- }
- ret := &UnstructuredList{}
- ret.Object = obj.Object
- err := obj.EachListItem(func(item runtime.Object) error {
- castItem := item.(*Unstructured)
- ret.Items = append(ret.Items, *castItem)
- return nil
- })
- if err != nil {
- return nil, err
- }
- return ret, nil
- }
- func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
- field, ok := obj.Object["items"]
- if !ok {
- return errors.New("content is not a list")
- }
- items, ok := field.([]interface{})
- if !ok {
- return fmt.Errorf("content is not a list: %T", field)
- }
- for _, item := range items {
- child, ok := item.(map[string]interface{})
- if !ok {
- return fmt.Errorf("items member is not an object: %T", child)
- }
- if err := fn(&Unstructured{Object: child}); err != nil {
- return err
- }
- }
- return nil
- }
- func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
- if obj.Object == nil {
- return make(map[string]interface{})
- }
- return obj.Object
- }
- func (obj *Unstructured) SetUnstructuredContent(content map[string]interface{}) {
- obj.Object = content
- }
- // MarshalJSON ensures that the unstructured object produces proper
- // JSON when passed to Go's standard JSON library.
- func (u *Unstructured) MarshalJSON() ([]byte, error) {
- var buf bytes.Buffer
- err := UnstructuredJSONScheme.Encode(u, &buf)
- return buf.Bytes(), err
- }
- // UnmarshalJSON ensures that the unstructured object properly decodes
- // JSON when passed to Go's standard JSON library.
- func (u *Unstructured) UnmarshalJSON(b []byte) error {
- _, _, err := UnstructuredJSONScheme.Decode(b, nil, u)
- return err
- }
- func (in *Unstructured) DeepCopy() *Unstructured {
- if in == nil {
- return nil
- }
- out := new(Unstructured)
- *out = *in
- out.Object = runtime.DeepCopyJSON(in.Object)
- return out
- }
- func (u *Unstructured) setNestedField(value interface{}, fields ...string) {
- if u.Object == nil {
- u.Object = make(map[string]interface{})
- }
- SetNestedField(u.Object, value, fields...)
- }
- func (u *Unstructured) setNestedSlice(value []string, fields ...string) {
- if u.Object == nil {
- u.Object = make(map[string]interface{})
- }
- SetNestedStringSlice(u.Object, value, fields...)
- }
- func (u *Unstructured) setNestedMap(value map[string]string, fields ...string) {
- if u.Object == nil {
- u.Object = make(map[string]interface{})
- }
- SetNestedStringMap(u.Object, value, fields...)
- }
- func (u *Unstructured) GetOwnerReferences() []metav1.OwnerReference {
- field, found, err := NestedFieldNoCopy(u.Object, "metadata", "ownerReferences")
- if !found || err != nil {
- return nil
- }
- original, ok := field.([]interface{})
- if !ok {
- return nil
- }
- ret := make([]metav1.OwnerReference, 0, len(original))
- for _, obj := range original {
- o, ok := obj.(map[string]interface{})
- if !ok {
- // expected map[string]interface{}, got something else
- return nil
- }
- ret = append(ret, extractOwnerReference(o))
- }
- return ret
- }
- func (u *Unstructured) SetOwnerReferences(references []metav1.OwnerReference) {
- if references == nil {
- RemoveNestedField(u.Object, "metadata", "ownerReferences")
- return
- }
- newReferences := make([]interface{}, 0, len(references))
- for _, reference := range references {
- out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&reference)
- if err != nil {
- utilruntime.HandleError(fmt.Errorf("unable to convert Owner Reference: %v", err))
- continue
- }
- newReferences = append(newReferences, out)
- }
- u.setNestedField(newReferences, "metadata", "ownerReferences")
- }
- func (u *Unstructured) GetAPIVersion() string {
- return getNestedString(u.Object, "apiVersion")
- }
- func (u *Unstructured) SetAPIVersion(version string) {
- u.setNestedField(version, "apiVersion")
- }
- func (u *Unstructured) GetKind() string {
- return getNestedString(u.Object, "kind")
- }
- func (u *Unstructured) SetKind(kind string) {
- u.setNestedField(kind, "kind")
- }
- func (u *Unstructured) GetNamespace() string {
- return getNestedString(u.Object, "metadata", "namespace")
- }
- func (u *Unstructured) SetNamespace(namespace string) {
- if len(namespace) == 0 {
- RemoveNestedField(u.Object, "metadata", "namespace")
- return
- }
- u.setNestedField(namespace, "metadata", "namespace")
- }
- func (u *Unstructured) GetName() string {
- return getNestedString(u.Object, "metadata", "name")
- }
- func (u *Unstructured) SetName(name string) {
- if len(name) == 0 {
- RemoveNestedField(u.Object, "metadata", "name")
- return
- }
- u.setNestedField(name, "metadata", "name")
- }
- func (u *Unstructured) GetGenerateName() string {
- return getNestedString(u.Object, "metadata", "generateName")
- }
- func (u *Unstructured) SetGenerateName(generateName string) {
- if len(generateName) == 0 {
- RemoveNestedField(u.Object, "metadata", "generateName")
- return
- }
- u.setNestedField(generateName, "metadata", "generateName")
- }
- func (u *Unstructured) GetUID() types.UID {
- return types.UID(getNestedString(u.Object, "metadata", "uid"))
- }
- func (u *Unstructured) SetUID(uid types.UID) {
- if len(string(uid)) == 0 {
- RemoveNestedField(u.Object, "metadata", "uid")
- return
- }
- u.setNestedField(string(uid), "metadata", "uid")
- }
- func (u *Unstructured) GetResourceVersion() string {
- return getNestedString(u.Object, "metadata", "resourceVersion")
- }
- func (u *Unstructured) SetResourceVersion(resourceVersion string) {
- if len(resourceVersion) == 0 {
- RemoveNestedField(u.Object, "metadata", "resourceVersion")
- return
- }
- u.setNestedField(resourceVersion, "metadata", "resourceVersion")
- }
- func (u *Unstructured) GetGeneration() int64 {
- val, found, err := NestedInt64(u.Object, "metadata", "generation")
- if !found || err != nil {
- return 0
- }
- return val
- }
- func (u *Unstructured) SetGeneration(generation int64) {
- if generation == 0 {
- RemoveNestedField(u.Object, "metadata", "generation")
- return
- }
- u.setNestedField(generation, "metadata", "generation")
- }
- func (u *Unstructured) GetSelfLink() string {
- return getNestedString(u.Object, "metadata", "selfLink")
- }
- func (u *Unstructured) SetSelfLink(selfLink string) {
- if len(selfLink) == 0 {
- RemoveNestedField(u.Object, "metadata", "selfLink")
- return
- }
- u.setNestedField(selfLink, "metadata", "selfLink")
- }
- func (u *Unstructured) GetContinue() string {
- return getNestedString(u.Object, "metadata", "continue")
- }
- func (u *Unstructured) SetContinue(c string) {
- if len(c) == 0 {
- RemoveNestedField(u.Object, "metadata", "continue")
- return
- }
- u.setNestedField(c, "metadata", "continue")
- }
- func (u *Unstructured) GetCreationTimestamp() metav1.Time {
- var timestamp metav1.Time
- timestamp.UnmarshalQueryParameter(getNestedString(u.Object, "metadata", "creationTimestamp"))
- return timestamp
- }
- func (u *Unstructured) SetCreationTimestamp(timestamp metav1.Time) {
- ts, _ := timestamp.MarshalQueryParameter()
- if len(ts) == 0 || timestamp.Time.IsZero() {
- RemoveNestedField(u.Object, "metadata", "creationTimestamp")
- return
- }
- u.setNestedField(ts, "metadata", "creationTimestamp")
- }
- func (u *Unstructured) GetDeletionTimestamp() *metav1.Time {
- var timestamp metav1.Time
- timestamp.UnmarshalQueryParameter(getNestedString(u.Object, "metadata", "deletionTimestamp"))
- if timestamp.IsZero() {
- return nil
- }
- return ×tamp
- }
- func (u *Unstructured) SetDeletionTimestamp(timestamp *metav1.Time) {
- if timestamp == nil {
- RemoveNestedField(u.Object, "metadata", "deletionTimestamp")
- return
- }
- ts, _ := timestamp.MarshalQueryParameter()
- u.setNestedField(ts, "metadata", "deletionTimestamp")
- }
- func (u *Unstructured) GetDeletionGracePeriodSeconds() *int64 {
- val, found, err := NestedInt64(u.Object, "metadata", "deletionGracePeriodSeconds")
- if !found || err != nil {
- return nil
- }
- return &val
- }
- func (u *Unstructured) SetDeletionGracePeriodSeconds(deletionGracePeriodSeconds *int64) {
- if deletionGracePeriodSeconds == nil {
- RemoveNestedField(u.Object, "metadata", "deletionGracePeriodSeconds")
- return
- }
- u.setNestedField(*deletionGracePeriodSeconds, "metadata", "deletionGracePeriodSeconds")
- }
- func (u *Unstructured) GetLabels() map[string]string {
- m, _, _ := NestedStringMap(u.Object, "metadata", "labels")
- return m
- }
- func (u *Unstructured) SetLabels(labels map[string]string) {
- if labels == nil {
- RemoveNestedField(u.Object, "metadata", "labels")
- return
- }
- u.setNestedMap(labels, "metadata", "labels")
- }
- func (u *Unstructured) GetAnnotations() map[string]string {
- m, _, _ := NestedStringMap(u.Object, "metadata", "annotations")
- return m
- }
- func (u *Unstructured) SetAnnotations(annotations map[string]string) {
- if annotations == nil {
- RemoveNestedField(u.Object, "metadata", "annotations")
- return
- }
- u.setNestedMap(annotations, "metadata", "annotations")
- }
- func (u *Unstructured) SetGroupVersionKind(gvk schema.GroupVersionKind) {
- u.SetAPIVersion(gvk.GroupVersion().String())
- u.SetKind(gvk.Kind)
- }
- func (u *Unstructured) GroupVersionKind() schema.GroupVersionKind {
- gv, err := schema.ParseGroupVersion(u.GetAPIVersion())
- if err != nil {
- return schema.GroupVersionKind{}
- }
- gvk := gv.WithKind(u.GetKind())
- return gvk
- }
- func (u *Unstructured) GetInitializers() *metav1.Initializers {
- m, found, err := nestedMapNoCopy(u.Object, "metadata", "initializers")
- if !found || err != nil {
- return nil
- }
- out := &metav1.Initializers{}
- if err := runtime.DefaultUnstructuredConverter.FromUnstructured(m, out); err != nil {
- utilruntime.HandleError(fmt.Errorf("unable to retrieve initializers for object: %v", err))
- return nil
- }
- return out
- }
- func (u *Unstructured) SetInitializers(initializers *metav1.Initializers) {
- if initializers == nil {
- RemoveNestedField(u.Object, "metadata", "initializers")
- return
- }
- out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(initializers)
- if err != nil {
- utilruntime.HandleError(fmt.Errorf("unable to retrieve initializers for object: %v", err))
- }
- u.setNestedField(out, "metadata", "initializers")
- }
- func (u *Unstructured) GetFinalizers() []string {
- val, _, _ := NestedStringSlice(u.Object, "metadata", "finalizers")
- return val
- }
- func (u *Unstructured) SetFinalizers(finalizers []string) {
- if finalizers == nil {
- RemoveNestedField(u.Object, "metadata", "finalizers")
- return
- }
- u.setNestedSlice(finalizers, "metadata", "finalizers")
- }
- func (u *Unstructured) GetClusterName() string {
- return getNestedString(u.Object, "metadata", "clusterName")
- }
- func (u *Unstructured) SetClusterName(clusterName string) {
- if len(clusterName) == 0 {
- RemoveNestedField(u.Object, "metadata", "clusterName")
- return
- }
- u.setNestedField(clusterName, "metadata", "clusterName")
- }
|