Debian Codegraph @

pkg/apis/meta/v1/unstructured/unstructuredscheme/scheme.go

raw
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 unstructuredscheme
18
19 import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23 "k8s.io/apimachinery/pkg/runtime"
24 "k8s.io/apimachinery/pkg/runtime/schema"
25 "k8s.io/apimachinery/pkg/runtime/serializer/json"
26 "k8s.io/apimachinery/pkg/runtime/serializer/versioning"
27 )
28
29 var scheme = runtime.NewScheme()
30
31 // NewUnstructuredNegotiatedSerializer returns a simple, negotiated serializer
32 func NewUnstructuredNegotiatedSerializer() runtime.NegotiatedSerializer {
33 return unstructuredNegotiatedSerializer{
34 scheme: scheme,
35 typer: NewUnstructuredObjectTyper(),
36 creator: NewUnstructuredCreator(),
37 }
38 }
39
40 type unstructuredNegotiatedSerializer struct {
41 scheme *runtime.Scheme
42 typer runtime.ObjectTyper
43 creator runtime.ObjectCreater
44 }
45
46 func (s unstructuredNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo {
47 return []runtime.SerializerInfo{
48 {
49 MediaType: "application/json",
50 MediaTypeType: "application",
51 MediaTypeSubType: "json",
52 EncodesAsText: true,
53 Serializer: json.NewSerializerWithOptions(json.DefaultMetaFactory, s.creator, s.typer, json.SerializerOptions{}),
54 PrettySerializer: json.NewSerializerWithOptions(json.DefaultMetaFactory, s.creator, s.typer, json.SerializerOptions{Pretty: true}),
55 StreamSerializer: &runtime.StreamSerializerInfo{
56 EncodesAsText: true,
57 Serializer: json.NewSerializerWithOptions(json.DefaultMetaFactory, s.creator, s.typer, json.SerializerOptions{}),
58 Framer: json.Framer,
59 },
60 },
61 {
62 MediaType: "application/yaml",
63 MediaTypeType: "application",
64 MediaTypeSubType: "yaml",
65 EncodesAsText: true,
66 Serializer: json.NewSerializerWithOptions(json.DefaultMetaFactory, s.creator, s.typer, json.SerializerOptions{Yaml: true}),
67 },
68 }
69 }
70
71 func (s unstructuredNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
72 return versioning.NewDefaultingCodecForScheme(s.scheme, encoder, nil, gv, nil)
73 }
74
75 func (s unstructuredNegotiatedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder {
76 return versioning.NewDefaultingCodecForScheme(s.scheme, nil, decoder, nil, gv)
77 }
78
79 type unstructuredObjectTyper struct {
80 }
81
82 // NewUnstructuredObjectTyper returns an object typer that can deal with unstructured things
83 func NewUnstructuredObjectTyper() runtime.ObjectTyper {
84 return unstructuredObjectTyper{}
85 }
86
87 func (t unstructuredObjectTyper) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind, bool, error) {
88 // Delegate for things other than Unstructured.
89 if _, ok := obj.(runtime.Unstructured); !ok {
90 return nil, false, fmt.Errorf("cannot type %T", obj)
91 }
92 gvk := obj.GetObjectKind().GroupVersionKind()
93 if len(gvk.Kind) == 0 {
94 return nil, false, runtime.NewMissingKindErr("object has no kind field ")
95 }
96 if len(gvk.Version) == 0 {
97 return nil, false, runtime.NewMissingVersionErr("object has no apiVersion field")
98 }
99
100 return []schema.GroupVersionKind{obj.GetObjectKind().GroupVersionKind()}, false, nil
101 }
102
103 func (t unstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool {
104 return true
105 }
106
107 type unstructuredCreator struct{}
108
109 // NewUnstructuredCreator returns a simple object creator that always returns an unstructured
110 func NewUnstructuredCreator() runtime.ObjectCreater {
111 return unstructuredCreator{}
112 }
113
114 func (c unstructuredCreator) New(kind schema.GroupVersionKind) (runtime.Object, error) {
115 ret := &unstructured.Unstructured{}
116 ret.SetGroupVersionKind(kind)
117 return ret, nil
118 }
119
120 type unstructuredDefaulter struct {
121 }
122
123 // NewUnstructuredDefaulter returns defaulter suitable for unstructured types that doesn't default anything
124 func NewUnstructuredDefaulter() runtime.ObjectDefaulter {
125 return unstructuredDefaulter{}
126 }
127
128 func (d unstructuredDefaulter) Default(in runtime.Object) {
129 }