@@ -18,11 +18,11 @@ package dynamic
18
18
19
19
import (
20
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21
22
"k8s.io/apimachinery/pkg/runtime"
22
23
"k8s.io/apimachinery/pkg/runtime/schema"
23
24
"k8s.io/apimachinery/pkg/runtime/serializer"
24
25
"k8s.io/apimachinery/pkg/runtime/serializer/json"
25
- "k8s.io/apimachinery/pkg/runtime/serializer/versioning"
26
26
)
27
27
28
28
var watchScheme = runtime .NewScheme ()
@@ -41,37 +41,6 @@ func init() {
41
41
metav1 .AddToGroupVersion (deleteScheme , versionV1 )
42
42
}
43
43
44
- var watchJsonSerializerInfo = runtime.SerializerInfo {
45
- MediaType : "application/json" ,
46
- MediaTypeType : "application" ,
47
- MediaTypeSubType : "json" ,
48
- EncodesAsText : true ,
49
- Serializer : json .NewSerializer (json .DefaultMetaFactory , watchScheme , watchScheme , false ),
50
- PrettySerializer : json .NewSerializer (json .DefaultMetaFactory , watchScheme , watchScheme , true ),
51
- StreamSerializer : & runtime.StreamSerializerInfo {
52
- EncodesAsText : true ,
53
- Serializer : json .NewSerializer (json .DefaultMetaFactory , watchScheme , watchScheme , false ),
54
- Framer : json .Framer ,
55
- },
56
- }
57
-
58
- // watchNegotiatedSerializer is used to read the wrapper of the watch stream
59
- type watchNegotiatedSerializer struct {}
60
-
61
- var watchNegotiatedSerializerInstance = watchNegotiatedSerializer {}
62
-
63
- func (s watchNegotiatedSerializer ) SupportedMediaTypes () []runtime.SerializerInfo {
64
- return []runtime.SerializerInfo {watchJsonSerializerInfo }
65
- }
66
-
67
- func (s watchNegotiatedSerializer ) EncoderForVersion (encoder runtime.Encoder , gv runtime.GroupVersioner ) runtime.Encoder {
68
- return versioning .NewDefaultingCodecForScheme (watchScheme , encoder , nil , gv , nil )
69
- }
70
-
71
- func (s watchNegotiatedSerializer ) DecoderToVersion (decoder runtime.Decoder , gv runtime.GroupVersioner ) runtime.Decoder {
72
- return versioning .NewDefaultingCodecForScheme (watchScheme , nil , decoder , nil , gv )
73
- }
74
-
75
44
// basicNegotiatedSerializer is used to handle discovery and error handling serialization
76
45
type basicNegotiatedSerializer struct {}
77
46
@@ -82,8 +51,8 @@ func (s basicNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInf
82
51
MediaTypeType : "application" ,
83
52
MediaTypeSubType : "json" ,
84
53
EncodesAsText : true ,
85
- Serializer : json .NewSerializer (json .DefaultMetaFactory , basicScheme , basicScheme , false ),
86
- PrettySerializer : json .NewSerializer (json .DefaultMetaFactory , basicScheme , basicScheme , true ),
54
+ Serializer : json .NewSerializer (json .DefaultMetaFactory , unstructuredCreater { basicScheme }, unstructuredTyper { basicScheme } , false ),
55
+ PrettySerializer : json .NewSerializer (json .DefaultMetaFactory , unstructuredCreater { basicScheme }, unstructuredTyper { basicScheme } , true ),
87
56
StreamSerializer : & runtime.StreamSerializerInfo {
88
57
EncodesAsText : true ,
89
58
Serializer : json .NewSerializer (json .DefaultMetaFactory , basicScheme , basicScheme , false ),
@@ -94,9 +63,46 @@ func (s basicNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInf
94
63
}
95
64
96
65
func (s basicNegotiatedSerializer ) EncoderForVersion (encoder runtime.Encoder , gv runtime.GroupVersioner ) runtime.Encoder {
97
- return versioning .NewDefaultingCodecForScheme (watchScheme , encoder , nil , gv , nil )
66
+ return runtime.WithVersionEncoder {
67
+ Version : gv ,
68
+ Encoder : encoder ,
69
+ ObjectTyper : unstructuredTyper {basicScheme },
70
+ }
98
71
}
99
72
100
73
func (s basicNegotiatedSerializer ) DecoderToVersion (decoder runtime.Decoder , gv runtime.GroupVersioner ) runtime.Decoder {
101
- return versioning .NewDefaultingCodecForScheme (watchScheme , nil , decoder , nil , gv )
74
+ return decoder
75
+ }
76
+
77
+ type unstructuredCreater struct {
78
+ nested runtime.ObjectCreater
79
+ }
80
+
81
+ func (c unstructuredCreater ) New (kind schema.GroupVersionKind ) (runtime.Object , error ) {
82
+ out , err := c .nested .New (kind )
83
+ if err == nil {
84
+ return out , nil
85
+ }
86
+ out = & unstructured.Unstructured {}
87
+ out .GetObjectKind ().SetGroupVersionKind (kind )
88
+ return out , nil
89
+ }
90
+
91
+ type unstructuredTyper struct {
92
+ nested runtime.ObjectTyper
93
+ }
94
+
95
+ func (t unstructuredTyper ) ObjectKinds (obj runtime.Object ) ([]schema.GroupVersionKind , bool , error ) {
96
+ kinds , unversioned , err := t .nested .ObjectKinds (obj )
97
+ if err == nil {
98
+ return kinds , unversioned , nil
99
+ }
100
+ if _ , ok := obj .(runtime.Unstructured ); ok && ! obj .GetObjectKind ().GroupVersionKind ().Empty () {
101
+ return []schema.GroupVersionKind {obj .GetObjectKind ().GroupVersionKind ()}, false , nil
102
+ }
103
+ return nil , false , err
104
+ }
105
+
106
+ func (t unstructuredTyper ) Recognizes (gvk schema.GroupVersionKind ) bool {
107
+ return true
102
108
}
0 commit comments