Illustration of the implementation of the api multi-version mechanism in kubernetes

In web development, with the update and iteration of the version, it is usually necessary to maintain multiple versions of the api in the system. The data structure of the multiple versions of the api is often different. Today, let's learn the Scheme mechanism in kubernetes. How to solve this problem, how to use the data in the HTTP request to deserialize

1. The processing flow of web requests

1.1 HTTP request processing flow

image.pngUsually, the webServer first processes the Http protocol, and then parses it into an Http request object inside the basic webServer, which usually holds the request header of the corresponding request and the corresponding byte sequence of the underlying layer (read from the socket stream) and then First, deserialization is usually performed according to the corresponding encoding format to complete the mapping from the byte sequence to the business model of the current interface, and then it is handed over to the business logic for processing, so as to finally perform persistent storage. The focus of this article is on the reverse Serialization part

2. Implementation of Model Mapping

2.1 Describe resource version information

/api/{version}/{resource}/{action}

The above is a basic web url. Usually, we will register a corresponding URL for each version, which will contain two key information, namely version and resource. Through these two information, we can usually know that this may be a certain The version of the resource, if we wrap the subsequent action, we can usually know the specific operation of the corresponding resource

2.2 Group information

image.pngToday, when microservices are popular, we usually divide microservices according to business functions. In essence, a microservice may be a set of functions that implement a specific business scenario. For example, a user system usually contains all relevant operations of the user. There is a similar concept in kubernetes called Group

POST /apis/batch/v1beta1/namespaces/{namespace}/cronjobs
POST /apis/apps/v1/namespaces/{namespace}/daemonsets

Let's take a look at an example. This is a url to create daemonsets and cronjobs. If it is split according to Group, resource, and version, it can be divided into the following: batch, v1beta1, cronjobs and apps, v1, daemonsets, which is the GroupVersionKind that everyone tries, Where kind corresponds to resource

2.3 Implementation of Model Mapping

image.pngWe get the GroupVersionKind information of the resource through the url. How to map it to a specific type? This seems to be very simple. It can be done by combining reflection and map. We get the corresponding GVK information through the url, and then through our mapping table, we know which the corresponding model is, and then we only need to convert. just fine

gvkToType map[schema.GroupVersionKind]reflect.Type

3. Deserialization implementation

3.1 Decoding mechanism

How to deserialize the data stream in the corresponding Http into an internal object? Don't forget that it is the Http protocol, which must be the information in the header header. We can know the corresponding encoding format through the serialization in the header header. , you only need to call the decoding of the corresponding format to complete

Content-Type: "application/json"

3.2 Default Object

image.pngIf you want to decode a byte array in json format, you usually need to do the following operations. We need to pass in a pointer to a target object, and then json will parse the corresponding byte data into the target object. We also need such an object, use to store the deserialized result

func Unmarshal(data []byte, v interface{}) error {}

So as long as I provide another object constructor corresponding to the current version, is that all right? the answer is yes

func() Object{ return 目标对象 },

4. Design Summary

image.pngFirst, when url registration is performed, we construct the version information of the resource corresponding to the url mapping, namely GroupVersionKind. For many subsequent operations, we can obtain the corresponding target operation or object through the corresponding version mapping, and then obtain the corresponding target operation or object through the fields in the Header. The decoder, and the byte sequence in the Body is decoded to the target object, the mapping and deserialization of multi-version resources can be realized.

kubernetes study notes address: https://www.yuque.com/baxiaoshi/tyado3

> WeChat ID: baxiaoshi2020 > Follow the bulletin number to read more source code analysis articles 21 days greenhouse> For more articles, follow www.sreguide.com > This article is published by OpenWrite , a blog post multiple platform

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324123228&siteId=291194637