codec.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package codec
  14. import (
  15. "fmt"
  16. // ensure the core apis are installed
  17. _ "k8s.io/kubernetes/pkg/apis/core/install"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. "k8s.io/apimachinery/pkg/runtime/serializer"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  23. "k8s.io/kubernetes/pkg/kubelet/apis/config/scheme"
  24. )
  25. // EncodeKubeletConfig encodes an internal KubeletConfiguration to an external YAML representation
  26. func EncodeKubeletConfig(internal *kubeletconfig.KubeletConfiguration, targetVersion schema.GroupVersion) ([]byte, error) {
  27. encoder, err := NewKubeletconfigYAMLEncoder(targetVersion)
  28. if err != nil {
  29. return nil, err
  30. }
  31. // encoder will convert to external version
  32. data, err := runtime.Encode(encoder, internal)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return data, nil
  37. }
  38. // NewKubeletconfigYAMLEncoder returns an encoder that can write objects in the kubeletconfig API group to YAML
  39. func NewKubeletconfigYAMLEncoder(targetVersion schema.GroupVersion) (runtime.Encoder, error) {
  40. _, codecs, err := scheme.NewSchemeAndCodecs()
  41. if err != nil {
  42. return nil, err
  43. }
  44. mediaType := "application/yaml"
  45. info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
  46. if !ok {
  47. return nil, fmt.Errorf("unsupported media type %q", mediaType)
  48. }
  49. return codecs.EncoderForVersion(info.Serializer, targetVersion), nil
  50. }
  51. // NewYAMLEncoder generates a new runtime.Encoder that encodes objects to YAML
  52. func NewYAMLEncoder(groupName string) (runtime.Encoder, error) {
  53. // encode to YAML
  54. mediaType := "application/yaml"
  55. info, ok := runtime.SerializerInfoForMediaType(legacyscheme.Codecs.SupportedMediaTypes(), mediaType)
  56. if !ok {
  57. return nil, fmt.Errorf("unsupported media type %q", mediaType)
  58. }
  59. versions := legacyscheme.Scheme.PrioritizedVersionsForGroup(groupName)
  60. if len(versions) == 0 {
  61. return nil, fmt.Errorf("no enabled versions for group %q", groupName)
  62. }
  63. // the "best" version supposedly comes first in the list returned from legacyscheme.Registry.EnabledVersionsForGroup
  64. return legacyscheme.Codecs.EncoderForVersion(info.Serializer, versions[0]), nil
  65. }
  66. // DecodeKubeletConfiguration decodes a serialized KubeletConfiguration to the internal type
  67. func DecodeKubeletConfiguration(kubeletCodecs *serializer.CodecFactory, data []byte) (*kubeletconfig.KubeletConfiguration, error) {
  68. // the UniversalDecoder runs defaulting and returns the internal type by default
  69. obj, gvk, err := kubeletCodecs.UniversalDecoder().Decode(data, nil, nil)
  70. if err != nil {
  71. return nil, fmt.Errorf("failed to decode, error: %v", err)
  72. }
  73. internalKC, ok := obj.(*kubeletconfig.KubeletConfiguration)
  74. if !ok {
  75. return nil, fmt.Errorf("failed to cast object to KubeletConfiguration, unexpected type: %v", gvk)
  76. }
  77. return internalKC, nil
  78. }