76
vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go
generated
vendored
76
vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go
generated
vendored
@ -27,11 +27,15 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// NestedFieldCopy returns a deep copy of the value of a nested field.
|
||||
// Returns false if the value is missing.
|
||||
// No error is returned for a nil field.
|
||||
//
|
||||
// Note: fields passed to this function are treated as keys within the passed
|
||||
// object; no array/slice syntax is supported.
|
||||
func NestedFieldCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
|
||||
val, found, err := NestedFieldNoCopy(obj, fields...)
|
||||
if !found || err != nil {
|
||||
@ -43,6 +47,9 @@ func NestedFieldCopy(obj map[string]interface{}, fields ...string) (interface{},
|
||||
// NestedFieldNoCopy returns a reference to a nested field.
|
||||
// Returns false if value is not found and an error if unable
|
||||
// to traverse obj.
|
||||
//
|
||||
// Note: fields passed to this function are treated as keys within the passed
|
||||
// object; no array/slice syntax is supported.
|
||||
func NestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
|
||||
var val interface{} = obj
|
||||
|
||||
@ -275,6 +282,22 @@ func getNestedString(obj map[string]interface{}, fields ...string) string {
|
||||
return val
|
||||
}
|
||||
|
||||
func getNestedInt64(obj map[string]interface{}, fields ...string) int64 {
|
||||
val, found, err := NestedInt64(obj, fields...)
|
||||
if !found || err != nil {
|
||||
return 0
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func getNestedInt64Pointer(obj map[string]interface{}, fields ...string) *int64 {
|
||||
val, found, err := NestedInt64(obj, fields...)
|
||||
if !found || err != nil {
|
||||
return nil
|
||||
}
|
||||
return &val
|
||||
}
|
||||
|
||||
func jsonPath(fields []string) string {
|
||||
return "." + strings.Join(fields, ".")
|
||||
}
|
||||
@ -307,6 +330,8 @@ var UnstructuredJSONScheme runtime.Codec = unstructuredJSONScheme{}
|
||||
|
||||
type unstructuredJSONScheme struct{}
|
||||
|
||||
const unstructuredJSONSchemeIdentifier runtime.Identifier = "unstructuredJSON"
|
||||
|
||||
func (s unstructuredJSONScheme) Decode(data []byte, _ *schema.GroupVersionKind, obj runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
|
||||
var err error
|
||||
if obj != nil {
|
||||
@ -327,7 +352,14 @@ func (s unstructuredJSONScheme) Decode(data []byte, _ *schema.GroupVersionKind,
|
||||
return obj, &gvk, nil
|
||||
}
|
||||
|
||||
func (unstructuredJSONScheme) Encode(obj runtime.Object, w io.Writer) error {
|
||||
func (s unstructuredJSONScheme) Encode(obj runtime.Object, w io.Writer) error {
|
||||
if co, ok := obj.(runtime.CacheableObject); ok {
|
||||
return co.CacheEncode(s.Identifier(), s.doEncode, w)
|
||||
}
|
||||
return s.doEncode(obj, w)
|
||||
}
|
||||
|
||||
func (unstructuredJSONScheme) doEncode(obj runtime.Object, w io.Writer) error {
|
||||
switch t := obj.(type) {
|
||||
case *Unstructured:
|
||||
return json.NewEncoder(w).Encode(t.Object)
|
||||
@ -351,6 +383,11 @@ func (unstructuredJSONScheme) Encode(obj runtime.Object, w io.Writer) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Identifier implements runtime.Encoder interface.
|
||||
func (unstructuredJSONScheme) Identifier() runtime.Identifier {
|
||||
return unstructuredJSONSchemeIdentifier
|
||||
}
|
||||
|
||||
func (s unstructuredJSONScheme) decode(data []byte) (runtime.Object, error) {
|
||||
type detector struct {
|
||||
Items gojson.RawMessage
|
||||
@ -378,12 +415,6 @@ func (s unstructuredJSONScheme) decodeInto(data []byte, obj runtime.Object) erro
|
||||
return s.decodeToUnstructured(data, x)
|
||||
case *UnstructuredList:
|
||||
return s.decodeToList(data, x)
|
||||
case *runtime.VersionedObjects:
|
||||
o, err := s.decode(data)
|
||||
if err == nil {
|
||||
x.Objects = []runtime.Object{o}
|
||||
}
|
||||
return err
|
||||
default:
|
||||
return json.Unmarshal(data, x)
|
||||
}
|
||||
@ -438,12 +469,30 @@ func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList
|
||||
return nil
|
||||
}
|
||||
|
||||
type JSONFallbackEncoder struct {
|
||||
runtime.Encoder
|
||||
type jsonFallbackEncoder struct {
|
||||
encoder runtime.Encoder
|
||||
identifier runtime.Identifier
|
||||
}
|
||||
|
||||
func (c JSONFallbackEncoder) Encode(obj runtime.Object, w io.Writer) error {
|
||||
err := c.Encoder.Encode(obj, w)
|
||||
func NewJSONFallbackEncoder(encoder runtime.Encoder) runtime.Encoder {
|
||||
result := map[string]string{
|
||||
"name": "fallback",
|
||||
"base": string(encoder.Identifier()),
|
||||
}
|
||||
identifier, err := gojson.Marshal(result)
|
||||
if err != nil {
|
||||
klog.Fatalf("Failed marshaling identifier for jsonFallbackEncoder: %v", err)
|
||||
}
|
||||
return &jsonFallbackEncoder{
|
||||
encoder: encoder,
|
||||
identifier: runtime.Identifier(identifier),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *jsonFallbackEncoder) Encode(obj runtime.Object, w io.Writer) error {
|
||||
// There is no need to handle runtime.CacheableObject, as we only
|
||||
// fallback to other encoders here.
|
||||
err := c.encoder.Encode(obj, w)
|
||||
if runtime.IsNotRegisteredError(err) {
|
||||
switch obj.(type) {
|
||||
case *Unstructured, *UnstructuredList:
|
||||
@ -452,3 +501,8 @@ func (c JSONFallbackEncoder) Encode(obj runtime.Object, w io.Writer) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Identifier implements runtime.Encoder interface.
|
||||
func (c *jsonFallbackEncoder) Identifier() runtime.Identifier {
|
||||
return c.identifier
|
||||
}
|
||||
|
48
vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured.go
generated
vendored
48
vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured.go
generated
vendored
@ -47,6 +47,7 @@ type Unstructured struct {
|
||||
|
||||
var _ metav1.Object = &Unstructured{}
|
||||
var _ runtime.Unstructured = &Unstructured{}
|
||||
var _ metav1.ListInterface = &Unstructured{}
|
||||
|
||||
func (obj *Unstructured) GetObjectKind() schema.ObjectKind { return obj }
|
||||
|
||||
@ -126,6 +127,16 @@ func (u *Unstructured) UnmarshalJSON(b []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// NewEmptyInstance returns a new instance of the concrete type containing only kind/apiVersion and no other data.
|
||||
// This should be called instead of reflect.New() for unstructured types because the go type alone does not preserve kind/apiVersion info.
|
||||
func (in *Unstructured) NewEmptyInstance() runtime.Unstructured {
|
||||
out := new(Unstructured)
|
||||
if in != nil {
|
||||
out.GetObjectKind().SetGroupVersionKind(in.GetObjectKind().GroupVersionKind())
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (in *Unstructured) DeepCopy() *Unstructured {
|
||||
if in == nil {
|
||||
return nil
|
||||
@ -319,6 +330,18 @@ func (u *Unstructured) SetContinue(c string) {
|
||||
u.setNestedField(c, "metadata", "continue")
|
||||
}
|
||||
|
||||
func (u *Unstructured) GetRemainingItemCount() *int64 {
|
||||
return getNestedInt64Pointer(u.Object, "metadata", "remainingItemCount")
|
||||
}
|
||||
|
||||
func (u *Unstructured) SetRemainingItemCount(c *int64) {
|
||||
if c == nil {
|
||||
RemoveNestedField(u.Object, "metadata", "remainingItemCount")
|
||||
} else {
|
||||
u.setNestedField(*c, "metadata", "remainingItemCount")
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Unstructured) GetCreationTimestamp() metav1.Time {
|
||||
var timestamp metav1.Time
|
||||
timestamp.UnmarshalQueryParameter(getNestedString(u.Object, "metadata", "creationTimestamp"))
|
||||
@ -408,31 +431,6 @@ func (u *Unstructured) GroupVersionKind() schema.GroupVersionKind {
|
||||
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
|
||||
|
22
vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured_list.go
generated
vendored
22
vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured_list.go
generated
vendored
@ -52,6 +52,16 @@ func (u *UnstructuredList) EachListItem(fn func(runtime.Object) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewEmptyInstance returns a new instance of the concrete type containing only kind/apiVersion and no other data.
|
||||
// This should be called instead of reflect.New() for unstructured types because the go type alone does not preserve kind/apiVersion info.
|
||||
func (u *UnstructuredList) NewEmptyInstance() runtime.Unstructured {
|
||||
out := new(UnstructuredList)
|
||||
if u != nil {
|
||||
out.SetGroupVersionKind(u.GroupVersionKind())
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// UnstructuredContent returns a map contain an overlay of the Items field onto
|
||||
// the Object field. Items always overwrites overlay.
|
||||
func (u *UnstructuredList) UnstructuredContent() map[string]interface{} {
|
||||
@ -166,6 +176,18 @@ func (u *UnstructuredList) SetContinue(c string) {
|
||||
u.setNestedField(c, "metadata", "continue")
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) GetRemainingItemCount() *int64 {
|
||||
return getNestedInt64Pointer(u.Object, "metadata", "remainingItemCount")
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) SetRemainingItemCount(c *int64) {
|
||||
if c == nil {
|
||||
RemoveNestedField(u.Object, "metadata", "remainingItemCount")
|
||||
} else {
|
||||
u.setNestedField(*c, "metadata", "remainingItemCount")
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) SetGroupVersionKind(gvk schema.GroupVersionKind) {
|
||||
u.SetAPIVersion(gvk.GroupVersion().String())
|
||||
u.SetKind(gvk.Kind)
|
||||
|
Reference in New Issue
Block a user