mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Refactor loops over SupportedMediaTypes() where mediaType is used to match a single SerializerInfo{}
We have an existing helper function for this: runtime.SerializerInfoForMediaType() This is common prep-work for encoding runtime.Objects into JSON/YAML for transmission over the wire or writing to ComponentConfigs.
This commit is contained in:
		@@ -47,7 +47,6 @@ go_library(
 | 
				
			|||||||
        "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
 | 
					 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apiserver/pkg/server/healthz:go_default_library",
 | 
					        "//staging/src/k8s.io/apiserver/pkg/server/healthz:go_default_library",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,7 +32,6 @@ import (
 | 
				
			|||||||
	v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime"
 | 
						"k8s.io/apimachinery/pkg/runtime"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime/serializer"
 | 
						"k8s.io/apimachinery/pkg/runtime/serializer"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime/serializer/json"
 | 
					 | 
				
			||||||
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
 | 
						utilruntime "k8s.io/apimachinery/pkg/util/runtime"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/util/wait"
 | 
						"k8s.io/apimachinery/pkg/util/wait"
 | 
				
			||||||
	"k8s.io/apiserver/pkg/server/healthz"
 | 
						"k8s.io/apiserver/pkg/server/healthz"
 | 
				
			||||||
@@ -334,19 +333,13 @@ func (o *Options) runLoop() error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (o *Options) writeConfigFile() error {
 | 
					func (o *Options) writeConfigFile() error {
 | 
				
			||||||
	var encoder runtime.Encoder
 | 
						const mediaType = runtime.ContentTypeYAML
 | 
				
			||||||
	mediaTypes := o.codecs.SupportedMediaTypes()
 | 
						info, ok := runtime.SerializerInfoForMediaType(o.codecs.SupportedMediaTypes(), mediaType)
 | 
				
			||||||
	for _, info := range mediaTypes {
 | 
						if !ok {
 | 
				
			||||||
		if info.MediaType == "application/yaml" {
 | 
							return fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
 | 
				
			||||||
			encoder = info.Serializer
 | 
					 | 
				
			||||||
			break
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if encoder == nil {
 | 
					
 | 
				
			||||||
		return errors.New("unable to locate yaml encoder")
 | 
						encoder := o.codecs.EncoderForVersion(info.Serializer, v1alpha1.SchemeGroupVersion)
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	encoder = json.NewYAMLSerializer(json.DefaultMetaFactory, o.scheme, o.scheme)
 | 
					 | 
				
			||||||
	encoder = o.codecs.EncoderForVersion(encoder, v1alpha1.SchemeGroupVersion)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	configFile, err := os.Create(o.WriteConfigTo)
 | 
						configFile, err := os.Create(o.WriteConfigTo)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,7 +23,6 @@ go_library(
 | 
				
			|||||||
        "//staging/src/k8s.io/api/core/v1:go_default_library",
 | 
					        "//staging/src/k8s.io/api/core/v1:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
 | 
					 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
 | 
					        "//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library",
 | 
					        "//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,12 +17,11 @@ limitations under the License.
 | 
				
			|||||||
package options
 | 
					package options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime"
 | 
						"k8s.io/apimachinery/pkg/runtime"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime/serializer/json"
 | 
					 | 
				
			||||||
	kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
 | 
						kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
 | 
				
			||||||
	kubeschedulerscheme "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
 | 
						kubeschedulerscheme "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
 | 
				
			||||||
	kubeschedulerconfigv1alpha1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1alpha1"
 | 
						kubeschedulerconfigv1alpha1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1alpha1"
 | 
				
			||||||
@@ -48,19 +47,13 @@ func loadConfig(data []byte) (*kubeschedulerconfig.KubeSchedulerConfiguration, e
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// WriteConfigFile writes the config into the given file name as YAML.
 | 
					// WriteConfigFile writes the config into the given file name as YAML.
 | 
				
			||||||
func WriteConfigFile(fileName string, cfg *kubeschedulerconfig.KubeSchedulerConfiguration) error {
 | 
					func WriteConfigFile(fileName string, cfg *kubeschedulerconfig.KubeSchedulerConfiguration) error {
 | 
				
			||||||
	var encoder runtime.Encoder
 | 
						const mediaType = runtime.ContentTypeYAML
 | 
				
			||||||
	mediaTypes := kubeschedulerscheme.Codecs.SupportedMediaTypes()
 | 
						info, ok := runtime.SerializerInfoForMediaType(kubeschedulerscheme.Codecs.SupportedMediaTypes(), mediaType)
 | 
				
			||||||
	for _, info := range mediaTypes {
 | 
						if !ok {
 | 
				
			||||||
		if info.MediaType == "application/yaml" {
 | 
							return fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
 | 
				
			||||||
			encoder = info.Serializer
 | 
					 | 
				
			||||||
			break
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if encoder == nil {
 | 
					
 | 
				
			||||||
		return errors.New("unable to locate yaml encoder")
 | 
						encoder := kubeschedulerscheme.Codecs.EncoderForVersion(info.Serializer, kubeschedulerconfigv1alpha1.SchemeGroupVersion)
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	encoder = json.NewYAMLSerializer(json.DefaultMetaFactory, kubeschedulerscheme.Scheme, kubeschedulerscheme.Scheme)
 | 
					 | 
				
			||||||
	encoder = kubeschedulerscheme.Codecs.EncoderForVersion(encoder, kubeschedulerconfigv1alpha1.SchemeGroupVersion)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	configFile, err := os.Create(fileName)
 | 
						configFile, err := os.Create(fileName)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,7 +42,7 @@ func MarshalToYaml(obj runtime.Object, gv schema.GroupVersion) ([]byte, error) {
 | 
				
			|||||||
// TODO: Is specifying the gv really needed here?
 | 
					// TODO: Is specifying the gv really needed here?
 | 
				
			||||||
// TODO: Can we support json out of the box easily here?
 | 
					// TODO: Can we support json out of the box easily here?
 | 
				
			||||||
func MarshalToYamlForCodecs(obj runtime.Object, gv schema.GroupVersion, codecs serializer.CodecFactory) ([]byte, error) {
 | 
					func MarshalToYamlForCodecs(obj runtime.Object, gv schema.GroupVersion, codecs serializer.CodecFactory) ([]byte, error) {
 | 
				
			||||||
	mediaType := "application/yaml"
 | 
						const mediaType = runtime.ContentTypeYAML
 | 
				
			||||||
	info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
 | 
						info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
 | 
				
			||||||
	if !ok {
 | 
						if !ok {
 | 
				
			||||||
		return []byte{}, errors.Errorf("unsupported media type %q", mediaType)
 | 
							return []byte{}, errors.Errorf("unsupported media type %q", mediaType)
 | 
				
			||||||
@@ -61,7 +61,7 @@ func UnmarshalFromYaml(buffer []byte, gv schema.GroupVersion) (runtime.Object, e
 | 
				
			|||||||
// TODO: Is specifying the gv really needed here?
 | 
					// TODO: Is specifying the gv really needed here?
 | 
				
			||||||
// TODO: Can we support json out of the box easily here?
 | 
					// TODO: Can we support json out of the box easily here?
 | 
				
			||||||
func UnmarshalFromYamlForCodecs(buffer []byte, gv schema.GroupVersion, codecs serializer.CodecFactory) (runtime.Object, error) {
 | 
					func UnmarshalFromYamlForCodecs(buffer []byte, gv schema.GroupVersion, codecs serializer.CodecFactory) (runtime.Object, error) {
 | 
				
			||||||
	mediaType := "application/yaml"
 | 
						const mediaType = runtime.ContentTypeYAML
 | 
				
			||||||
	info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
 | 
						info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
 | 
				
			||||||
	if !ok {
 | 
						if !ok {
 | 
				
			||||||
		return nil, errors.Errorf("unsupported media type %q", mediaType)
 | 
							return nil, errors.Errorf("unsupported media type %q", mediaType)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -197,22 +197,22 @@ func LogResponseObject(ae *auditinternal.Event, obj runtime.Object, gv schema.Gr
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func encodeObject(obj runtime.Object, gv schema.GroupVersion, serializer runtime.NegotiatedSerializer) (*runtime.Unknown, error) {
 | 
					func encodeObject(obj runtime.Object, gv schema.GroupVersion, serializer runtime.NegotiatedSerializer) (*runtime.Unknown, error) {
 | 
				
			||||||
	supported := serializer.SupportedMediaTypes()
 | 
						const mediaType = runtime.ContentTypeJSON
 | 
				
			||||||
	for i := range supported {
 | 
						info, ok := runtime.SerializerInfoForMediaType(serializer.SupportedMediaTypes(), mediaType)
 | 
				
			||||||
		if supported[i].MediaType == "application/json" {
 | 
						if !ok {
 | 
				
			||||||
			enc := serializer.EncoderForVersion(supported[i].Serializer, gv)
 | 
							return nil, fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
 | 
				
			||||||
			var buf bytes.Buffer
 | 
					 | 
				
			||||||
			if err := enc.Encode(obj, &buf); err != nil {
 | 
					 | 
				
			||||||
				return nil, fmt.Errorf("encoding failed: %v", err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return &runtime.Unknown{
 | 
					 | 
				
			||||||
				Raw:         buf.Bytes(),
 | 
					 | 
				
			||||||
				ContentType: runtime.ContentTypeJSON,
 | 
					 | 
				
			||||||
			}, nil
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil, fmt.Errorf("no json encoder found")
 | 
					
 | 
				
			||||||
 | 
						enc := serializer.EncoderForVersion(info.Serializer, gv)
 | 
				
			||||||
 | 
						var buf bytes.Buffer
 | 
				
			||||||
 | 
						if err := enc.Encode(obj, &buf); err != nil {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("encoding failed: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &runtime.Unknown{
 | 
				
			||||||
 | 
							Raw:         buf.Bytes(),
 | 
				
			||||||
 | 
							ContentType: runtime.ContentTypeJSON,
 | 
				
			||||||
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// LogAnnotation fills in the Annotations according to the key value pair.
 | 
					// LogAnnotation fills in the Annotations according to the key value pair.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -79,10 +79,7 @@ func NegotiateInputSerializerForMediaType(mediaType string, streaming bool, ns r
 | 
				
			|||||||
		mediaType = mediaTypes[0].MediaType
 | 
							mediaType = mediaTypes[0].MediaType
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if mediaType, _, err := mime.ParseMediaType(mediaType); err == nil {
 | 
						if mediaType, _, err := mime.ParseMediaType(mediaType); err == nil {
 | 
				
			||||||
		for _, info := range mediaTypes {
 | 
							if info, ok := runtime.SerializerInfoForMediaType(mediaTypes, mediaType); ok {
 | 
				
			||||||
			if info.MediaType != mediaType {
 | 
					 | 
				
			||||||
				continue
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			return info, nil
 | 
								return info, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -94,17 +94,12 @@ func serveWatch(watcher watch.Interface, scope *RequestScope, mediaTypeOptions n
 | 
				
			|||||||
	var embeddedEncoder runtime.Encoder
 | 
						var embeddedEncoder runtime.Encoder
 | 
				
			||||||
	contentKind, contentSerializer, transform := targetEncodingForTransform(scope, mediaTypeOptions, req)
 | 
						contentKind, contentSerializer, transform := targetEncodingForTransform(scope, mediaTypeOptions, req)
 | 
				
			||||||
	if transform {
 | 
						if transform {
 | 
				
			||||||
		var embedded runtime.Serializer
 | 
							info, ok := runtime.SerializerInfoForMediaType(contentSerializer.SupportedMediaTypes(), serializer.MediaType)
 | 
				
			||||||
		for _, supported := range contentSerializer.SupportedMediaTypes() {
 | 
							if !ok {
 | 
				
			||||||
			if supported.MediaType == serializer.MediaType {
 | 
					 | 
				
			||||||
				embedded = supported.Serializer
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if embedded == nil {
 | 
					 | 
				
			||||||
			scope.err(fmt.Errorf("no encoder for %q exists in the requested target %#v", serializer.MediaType, contentSerializer), w, req)
 | 
								scope.err(fmt.Errorf("no encoder for %q exists in the requested target %#v", serializer.MediaType, contentSerializer), w, req)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		embeddedEncoder = contentSerializer.EncoderForVersion(embedded, contentKind.GroupVersion())
 | 
							embeddedEncoder = contentSerializer.EncoderForVersion(info.Serializer, contentKind.GroupVersion())
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		embeddedEncoder = scope.Serializer.EncoderForVersion(serializer.Serializer, contentKind.GroupVersion())
 | 
							embeddedEncoder = scope.Serializer.EncoderForVersion(serializer.Serializer, contentKind.GroupVersion())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -56,18 +56,12 @@ func (dynamicCodec) Encode(obj runtime.Object, w io.Writer) error {
 | 
				
			|||||||
	return unstructured.UnstructuredJSONScheme.Encode(obj, w)
 | 
						return unstructured.UnstructuredJSONScheme.Encode(obj, w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ContentConfig returns a rest.ContentConfig for dynamic types.  It includes enough codecs to act as a "normal"
 | 
					// UnstructuredPlusDefaultContentConfig returns a rest.ContentConfig for dynamic types.  It includes enough codecs to act as a "normal"
 | 
				
			||||||
// serializer for the rest.client with options, status and the like.
 | 
					// serializer for the rest.client with options, status and the like.
 | 
				
			||||||
func UnstructuredPlusDefaultContentConfig() rest.ContentConfig {
 | 
					func UnstructuredPlusDefaultContentConfig() rest.ContentConfig {
 | 
				
			||||||
	var jsonInfo runtime.SerializerInfo
 | 
					 | 
				
			||||||
	// TODO: scheme.Codecs here should become "pkg/apis/server/scheme" which is the minimal core you need
 | 
						// TODO: scheme.Codecs here should become "pkg/apis/server/scheme" which is the minimal core you need
 | 
				
			||||||
	// to talk to a kubernetes server
 | 
						// to talk to a kubernetes server
 | 
				
			||||||
	for _, info := range scheme.Codecs.SupportedMediaTypes() {
 | 
						jsonInfo, _ := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
 | 
				
			||||||
		if info.MediaType == runtime.ContentTypeJSON {
 | 
					 | 
				
			||||||
			jsonInfo = info
 | 
					 | 
				
			||||||
			break
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	jsonInfo.Serializer = dynamicCodec{}
 | 
						jsonInfo.Serializer = dynamicCodec{}
 | 
				
			||||||
	jsonInfo.PrettySerializer = nil
 | 
						jsonInfo.PrettySerializer = nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -59,15 +59,9 @@ func (dynamicCodec) Encode(obj runtime.Object, w io.Writer) error {
 | 
				
			|||||||
// ContentConfig returns a rest.ContentConfig for dynamic types.
 | 
					// ContentConfig returns a rest.ContentConfig for dynamic types.
 | 
				
			||||||
// Deprecated only used by test code and its wrong
 | 
					// Deprecated only used by test code and its wrong
 | 
				
			||||||
func ContentConfig() rest.ContentConfig {
 | 
					func ContentConfig() rest.ContentConfig {
 | 
				
			||||||
	var jsonInfo runtime.SerializerInfo
 | 
					 | 
				
			||||||
	// TODO: scheme.Codecs here should become "pkg/apis/server/scheme" which is the minimal core you need
 | 
						// TODO: scheme.Codecs here should become "pkg/apis/server/scheme" which is the minimal core you need
 | 
				
			||||||
	// to talk to a kubernetes server
 | 
						// to talk to a kubernetes server
 | 
				
			||||||
	for _, info := range scheme.Codecs.SupportedMediaTypes() {
 | 
						jsonInfo, _ := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
 | 
				
			||||||
		if info.MediaType == runtime.ContentTypeJSON {
 | 
					 | 
				
			||||||
			jsonInfo = info
 | 
					 | 
				
			||||||
			break
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	jsonInfo.Serializer = dynamicCodec{}
 | 
						jsonInfo.Serializer = dynamicCodec{}
 | 
				
			||||||
	jsonInfo.PrettySerializer = nil
 | 
						jsonInfo.PrettySerializer = nil
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user