Skip to content

Commit

Permalink
feat: Support YAML format in the legacy NegotiatedSerializer (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Feb 23, 2024
1 parent ec08a20 commit e8b7297
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 33 deletions.
2 changes: 1 addition & 1 deletion compose/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ NACOS_PASSWORD=''
NACOS_DATA_ENC_KEY='0123456789abcdef0123456789abcdef'
NACOS_SERVER_TAG='v2.2.3'
HIGRESS_RUNNER_TAG='0.0.3'
HIGRESS_API_SERVER_TAG='0.0.12'
HIGRESS_API_SERVER_TAG='0.0.13'
HIGRESS_CONTROLLER_TAG='1.3.4'
HIGRESS_PILOT_TAG='1.3.4'
HIGRESS_GATEWAY_TAG='1.3.4'
Expand Down
33 changes: 1 addition & 32 deletions src/apiserver/pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/version"
"k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"
Expand Down Expand Up @@ -73,39 +72,9 @@ var (
// Codecs provides methods for retrieving codecs and serializers for specific
// versions and content types.
Codecs = serializer.NewCodecFactory(Scheme)
LegacyNegotiatedSerializer = createLegacyNegotiatedSerializer(Scheme)
LegacyNegotiatedSerializer = codec.CreateLegacyNegotiatedSerializer(Scheme)
)

func createLegacyNegotiatedSerializer(scheme *runtime.Scheme) runtime.NegotiatedSerializer {
mf := json.DefaultMetaFactory
jsonSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: false, Pretty: false, Strict: false},
)
prettyJsonSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: false, Pretty: true, Strict: false},
)
strictJsonSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: false, Pretty: false, Strict: true},
)
return runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{
MediaType: "application/json",
MediaTypeType: "application",
MediaTypeSubType: "json",
EncodesAsText: true,
Serializer: jsonSerializer,
PrettySerializer: prettyJsonSerializer,
StrictSerializer: strictJsonSerializer,
StreamSerializer: &runtime.StreamSerializerInfo{
Serializer: jsonSerializer,
EncodesAsText: true,
Framer: json.Framer,
},
})
}

func init() {
_ = corev1.AddToScheme(Scheme)
metav1.AddToGroupVersion(Scheme, corev1.SchemeGroupVersion)
Expand Down
75 changes: 75 additions & 0 deletions src/apiserver/pkg/codec/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package codec

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
)

type legacyNegotiatedSerializer struct {
scheme *runtime.Scheme
accepts []runtime.SerializerInfo
}

func CreateLegacyNegotiatedSerializer(scheme *runtime.Scheme) runtime.NegotiatedSerializer {
mf := json.DefaultMetaFactory
jsonSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: false, Pretty: false, Strict: false},
)
prettyJsonSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: false, Pretty: true, Strict: false},
)
strictJsonSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: false, Pretty: false, Strict: true},
)
yamlSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: true, Pretty: false, Strict: false},
)
strictYamlSerializer := json.NewSerializerWithOptions(
mf, scheme, scheme,
json.SerializerOptions{Yaml: true, Pretty: false, Strict: true},
)
return &legacyNegotiatedSerializer{
scheme: scheme,
accepts: []runtime.SerializerInfo{
{
MediaType: "application/json",
MediaTypeType: "application",
MediaTypeSubType: "json",
EncodesAsText: true,
Serializer: jsonSerializer,
PrettySerializer: prettyJsonSerializer,
StrictSerializer: strictJsonSerializer,
StreamSerializer: &runtime.StreamSerializerInfo{
Serializer: jsonSerializer,
EncodesAsText: true,
Framer: json.Framer,
},
},
{
MediaType: "application/yaml",
MediaTypeType: "application",
MediaTypeSubType: "yaml",
EncodesAsText: true,
Serializer: yamlSerializer,
StrictSerializer: strictYamlSerializer,
},
},
}
}

func (l *legacyNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo {
return l.accepts
}

func (l *legacyNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
return versioning.NewDefaultingCodecForScheme(l.scheme, encoder, nil, gv, nil)
}

func (l *legacyNegotiatedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder {
return versioning.NewDefaultingCodecForScheme(l.scheme, nil, decoder, nil, gv)
}

0 comments on commit e8b7297

Please sign in to comment.