...

Source file src/k8s.io/cli-runtime/pkg/resource/scheme.go

Documentation: k8s.io/cli-runtime/pkg/resource

     1  /*
     2  Copyright 2018 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 resource
    18  
    19  import (
    20  	"encoding/json"
    21  	"io"
    22  	"strings"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	"k8s.io/apimachinery/pkg/runtime/serializer"
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  	"k8s.io/client-go/rest"
    31  )
    32  
    33  // dynamicCodec is a codec that wraps the standard unstructured codec
    34  // with special handling for Status objects.
    35  // Deprecated only used by test code and its wrong
    36  type dynamicCodec struct{}
    37  
    38  func (dynamicCodec) Decode(data []byte, gvk *schema.GroupVersionKind, obj runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
    39  	obj, gvk, err := unstructured.UnstructuredJSONScheme.Decode(data, gvk, obj)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  
    44  	if strings.EqualFold(gvk.Kind, "status") && gvk.Version == "v1" && (gvk.Group == "" || gvk.Group == "meta.k8s.io") {
    45  		if _, ok := obj.(*metav1.Status); !ok {
    46  			obj = &metav1.Status{}
    47  			err := json.Unmarshal(data, obj)
    48  			if err != nil {
    49  				return nil, nil, err
    50  			}
    51  		}
    52  	}
    53  
    54  	return obj, gvk, nil
    55  }
    56  
    57  func (dynamicCodec) Encode(obj runtime.Object, w io.Writer) error {
    58  	// There is no need to handle runtime.CacheableObject, as we only
    59  	// fallback to other encoders here.
    60  	return unstructured.UnstructuredJSONScheme.Encode(obj, w)
    61  }
    62  
    63  // Identifier implements runtime.Encoder interface.
    64  func (dynamicCodec) Identifier() runtime.Identifier {
    65  	return unstructured.UnstructuredJSONScheme.Identifier()
    66  }
    67  
    68  // UnstructuredPlusDefaultContentConfig returns a rest.ContentConfig for dynamic types.  It includes enough codecs to act as a "normal"
    69  // serializer for the rest.client with options, status and the like.
    70  func UnstructuredPlusDefaultContentConfig() rest.ContentConfig {
    71  	// TODO: scheme.Codecs here should become "pkg/apis/server/scheme" which is the minimal core you need
    72  	// to talk to a kubernetes server
    73  	jsonInfo, _ := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
    74  
    75  	jsonInfo.Serializer = dynamicCodec{}
    76  	jsonInfo.PrettySerializer = nil
    77  	return rest.ContentConfig{
    78  		AcceptContentTypes:   runtime.ContentTypeJSON,
    79  		ContentType:          runtime.ContentTypeJSON,
    80  		NegotiatedSerializer: serializer.NegotiatedSerializerWrapper(jsonInfo),
    81  	}
    82  }
    83  

View as plain text