反序列化

list时的反序列化

k8s.io/client-go/rest/request.go
Result.Into ( r.decoder.Decode )

k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go
DirectDecoder.Decode

k8s.io/apimachinery/pkg/runtime/serializer/json/json.go
Serializer.Decode :

  1. actual, err := s.meta.Interpret(data)
  2. runtime.UseOrCreateObject
  3. caseSensitiveJsonIterator.Unmarshal (github.com/json-iterator/go/config.go frozenConfig.Unmarshal)

vendor/github.com/json-iterator/go/reflect_struct_decoder.go
fourFieldsStructDecoder.Decode

watch时的反序列化

return r.WatchWithSpecificDecoders( func(body io.ReadCloser) streaming.Decoder { framer := r.serializers.Framer.NewFrameReader(body) return streaming.NewDecoder(framer, r.serializers.StreamingSerializer) }, r.serializers.Decoder, )

这里是两层decode,第一层是decode成metav1.Event,第二层是将metav1.Event内的Object解析成对应的对象

k8s.io/client-go/rest/watch/decoder.go

func (d *Decoder) Decode() (watch.EventType, runtime.Object, error) { var got metav1.WatchEvent res, _, err := d.decoder.Decode(nil, &got) if err != nil { return “”, nil, err }

if res != &got {

  1. return "", nil, fmt.Errorf("unable to decode to metav1.Event")

}

switch got.Type {

case string(watch.Added), string(watch.Modified), string(watch.Deleted), string(watch.Error), string(watch.Bookmark):

default:

  1. return "", nil, fmt.Errorf("got invalid watch event type: %v", got.Type)

}

obj, err := runtime.Decode(d.embeddedDecoder, got.Object.Raw)

if err != nil {

  1. return "", nil, fmt.Errorf("unable to decode watch event: %v", err)

}

return watch.EventType(got.Type), obj, nil}