...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/objectmeta/coerce.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/schema/objectmeta

     1  /*
     2  Copyright 2019 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 objectmeta
    18  
    19  import (
    20  	"fmt"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	utiljson "k8s.io/apimachinery/pkg/util/json"
    26  	"k8s.io/apimachinery/pkg/util/validation/field"
    27  	kjson "sigs.k8s.io/json"
    28  )
    29  
    30  // GetObjectMeta calls GetObjectMetaWithOptions without returning unknown field paths.
    31  func GetObjectMeta(obj map[string]interface{}, dropMalformedFields bool) (*metav1.ObjectMeta, bool, error) {
    32  	meta, found, _, err := GetObjectMetaWithOptions(obj, ObjectMetaOptions{
    33  		DropMalformedFields: dropMalformedFields,
    34  	})
    35  	return meta, found, err
    36  }
    37  
    38  // ObjectMetaOptions provides the options for how GetObjectMeta should retrieve the object meta.
    39  type ObjectMetaOptions struct {
    40  	// DropMalformedFields discards malformed serialized metadata fields that
    41  	// cannot be successfully decoded to the corresponding ObjectMeta field.
    42  	// This only applies to fields that are recognized as part of the schema,
    43  	// but of an invalid type (i.e. cause an error when unmarshaling, rather
    44  	// than being dropped or causing a strictErr).
    45  	DropMalformedFields bool
    46  	// ReturnUnknownFieldPaths will return the paths to fields that are not
    47  	// recognized as part of the schema.
    48  	ReturnUnknownFieldPaths bool
    49  	// ParentPath provides the current path up to the given ObjectMeta.
    50  	// If nil, the metadata is assumed to be at the root of the object.
    51  	ParentPath *field.Path
    52  }
    53  
    54  // GetObjectMetaWithOptions  does conversion of JSON to ObjectMeta.
    55  // It first tries json.Unmarshal into a metav1.ObjectMeta
    56  // type. If that does not work and opts.DropMalformedFields is true, it does field-by-field best-effort conversion
    57  // throwing away fields which lead to errors.
    58  // If opts.ReturnedUnknownFields is true, it will UnmarshalStrict instead, returning the paths of any unknown fields
    59  // it encounters (i.e. paths returned as strict errs from UnmarshalStrict)
    60  func GetObjectMetaWithOptions(obj map[string]interface{}, opts ObjectMetaOptions) (*metav1.ObjectMeta, bool, []string, error) {
    61  	metadata, found := obj["metadata"]
    62  	if !found {
    63  		return nil, false, nil, nil
    64  	}
    65  
    66  	// round-trip through JSON first, hoping that unmarshalling just works
    67  	objectMeta := &metav1.ObjectMeta{}
    68  	metadataBytes, err := utiljson.Marshal(metadata)
    69  	if err != nil {
    70  		return nil, false, nil, err
    71  	}
    72  	var unmarshalErr error
    73  	if opts.ReturnUnknownFieldPaths {
    74  		var strictErrs []error
    75  		strictErrs, unmarshalErr = kjson.UnmarshalStrict(metadataBytes, objectMeta)
    76  		if unmarshalErr == nil {
    77  			if len(strictErrs) > 0 {
    78  				unknownPaths := []string{}
    79  				prefix := opts.ParentPath.Child("metadata").String()
    80  				for _, err := range strictErrs {
    81  					if fieldPathErr, ok := err.(kjson.FieldError); ok {
    82  						unknownPaths = append(unknownPaths, prefix+"."+fieldPathErr.FieldPath())
    83  					}
    84  				}
    85  				return objectMeta, true, unknownPaths, nil
    86  			}
    87  			return objectMeta, true, nil, nil
    88  		}
    89  	} else {
    90  		if unmarshalErr = utiljson.Unmarshal(metadataBytes, objectMeta); unmarshalErr == nil {
    91  			// if successful, return
    92  			return objectMeta, true, nil, nil
    93  		}
    94  	}
    95  	if !opts.DropMalformedFields {
    96  		// if we're not trying to drop malformed fields, return the error
    97  		return nil, true, nil, unmarshalErr
    98  	}
    99  
   100  	metadataMap, ok := metadata.(map[string]interface{})
   101  	if !ok {
   102  		return nil, false, nil, fmt.Errorf("invalid metadata: expected object, got %T", metadata)
   103  	}
   104  
   105  	// Go field by field accumulating into the metadata object.
   106  	// This takes advantage of the fact that you can repeatedly unmarshal individual fields into a single struct,
   107  	// each iteration preserving the old key-values.
   108  	accumulatedObjectMeta := &metav1.ObjectMeta{}
   109  	testObjectMeta := &metav1.ObjectMeta{}
   110  	var unknownFields []string
   111  	for k, v := range metadataMap {
   112  		// serialize a single field
   113  		if singleFieldBytes, err := utiljson.Marshal(map[string]interface{}{k: v}); err == nil {
   114  			// do a test unmarshal
   115  			if utiljson.Unmarshal(singleFieldBytes, testObjectMeta) == nil {
   116  				// if that succeeds, unmarshal for real
   117  				if opts.ReturnUnknownFieldPaths {
   118  					strictErrs, _ := kjson.UnmarshalStrict(singleFieldBytes, accumulatedObjectMeta)
   119  					if len(strictErrs) > 0 {
   120  						prefix := opts.ParentPath.Child("metadata").String()
   121  						for _, err := range strictErrs {
   122  							if fieldPathErr, ok := err.(kjson.FieldError); ok {
   123  								unknownFields = append(unknownFields, prefix+"."+fieldPathErr.FieldPath())
   124  							}
   125  						}
   126  					}
   127  				} else {
   128  					utiljson.Unmarshal(singleFieldBytes, accumulatedObjectMeta)
   129  				}
   130  			}
   131  		}
   132  	}
   133  
   134  	return accumulatedObjectMeta, true, unknownFields, nil
   135  }
   136  
   137  // SetObjectMeta writes back ObjectMeta into a JSON data structure.
   138  func SetObjectMeta(obj map[string]interface{}, objectMeta *metav1.ObjectMeta) error {
   139  	if objectMeta == nil {
   140  		unstructured.RemoveNestedField(obj, "metadata")
   141  		return nil
   142  	}
   143  
   144  	metadata, err := runtime.DefaultUnstructuredConverter.ToUnstructured(objectMeta)
   145  	if err != nil {
   146  		return err
   147  	}
   148  
   149  	obj["metadata"] = metadata
   150  	return nil
   151  }
   152  

View as plain text