0
点赞
收藏
分享

微信扫一扫

k8s源码学习-codec编码器

codec编码器是什么?

codec编码器包含编码器和解码器,表示将数据转换为特定格式的过程,在k8s系统中主要负责将etcd中数据进行编码和解码操作

Serializer是什么?

Serializer是序列化器,包含序列化和反序列化操作,序列化操作是将数据(数组,结构体,对象)转为字符串的操作,反序列化是将字符串转为数据过程

codec编码器和Serializer是什么关系?

Serializer算是codec编码器的一种,代码:vendor/k8s.io/apimachinery/pkg/runtime/interfaces.go

// Encoder writes objects to a serialized form
type Encoder interface {
    // Encode writes an object to a stream. Implementations may return errors if the versions are
    // incompatible, or if no conversion is defined.
    Encode(obj Object, w io.Writer) error
    Identifier() Identifier
}

// Decoder attempts to load an object from data.
type Decoder interface {
    // Decode attempts to deserialize the provided data using either the innate typing of the scheme or the
    // default kind, group, and version provided. It returns a decoded object as well as the kind, group, and
    // version from the serialized data, or an error. If into is non-nil, it will be used as the target type
    // and implementations may choose to use it rather than reallocating an object. However, the object is not
    // guaranteed to be populated. The returned object is not guaranteed to match into. If defaults are
    // provided, they are applied to the data by default. If no defaults or partial defaults are provided, the
    // type of the into may be used to guide conversion decisions.
    Decode(data []byte, defaults *schema.GroupVersionKind, into Object) (Object, *schema.GroupVersionKind, error)
}

// Serializer is the core interface for transforming objects into a serialized format and back.
// Implementations may choose to perform conversion of the object, but no assumptions should be made.
type Serializer interface {
    Encoder
    Decoder
}

// Codec is a Serializer that deals with the details of versioning objects. It offers the same
// interface as Serializer, so this is a marker to consumers that care about the version of the objects
// they receive.
type Codec Serializer

Serializer种类

  • jsonSerializer json格式的序列化和发序列化器,使用application/json的ContentType作为标识符
  • yamlSerializer yaml格式的序列化和发序列化器,使用application/yaml的ContentType作为标识符
  • protobufSerializer profobuf格式的序列化和发序列化器,使用application/vnd.kubernetes.protobuf的ContentType作为标识符
举报

相关推荐

k8s源码

k8s学习

K8S学习

k8s 学习

【K8s学习】

0 条评论