Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Kubernetes supports new features of OpenAPI

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Open API allows API providers to define their own operations and models, and allows developers to automatically generate language-like clients to communicate with API servers. Kubernetes has supported Swagger 1.2 (the predecessor of the OpenAPI specification) for some time, but this standard is not complete and effective, and with this support, it is very difficult to generate tools or clients.

In Kubernetes 1.4, we upgraded the current model and operation to introduce a version of Alpha supported by the Open API specification (called Swagger 2.0 before it was donated to Open API). Starting with Kubernetes 1.5, the OpenAPI specification has been fully supported and can be generated directly from the Kubernetes source code, ensuring full synchronization of documents and specifications for any changes to the model and method.

The new specification gives us better API documentation and even a Python client.

This modular specification is separated by GroupVersion, which is prepared in advance, and we want different API Server to use different GroupVersion.

The structure of the specification is explained in Open API spec definition. We use operation tags to split each GroupVersion and enrich the model, path, operation and other information as much as possible. The parameters of the operation, the calling method, and the response are all documented.

For example, the OpenAPI specification for obtaining Pod information

{... "paths": {"/ api/v1/namespaces/ {namespace} / pods/ {name}": {"get": {"description": "read the specified Pod", "consumes": ["* / *"], "produces": ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"] "schemes": ["https"], "tags": ["core_v1"], "operationId": "readCoreV1NamespacedPod", "parameters": [{"uniqueItems": true, "type": "boolean", "description": "Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'. "," name ":" exact "," in ":" query "}, {" uniqueItems ": true," type ":" boolean "," description ":" Should this value be exported. Export strips fields that a user can not specify. "," name ":" export "," in ":" query "}]," responses ": {" description ":" OK "," schema ": {" $ref ":" # / definitions/v1.Pod "}} {"description": "Unauthorized"}}, … }...

With this information, as well as the URL of kube-apiserver, you can call the interface (/ api/v1/namespaces/ {namespace} / pods/ {name}). The parameters include name, exact, export, and so on. The result of the call will return Pod information. The client library generator also uses this information to create an API function call to read the Pod information. For example, the Python client can simply make the following calls:

From kubernetes import client ret = client.CoreV1Api () .read_namespaced_pod (name= "pods_name", namespace= "default") https://gist.github.com/mbohlool/d5ec1dace27ef90cf742555c05480146

A simplified version of read_namespaced_pod;Python Client: https://github.com/kubernetes-incubator/client-python can also use the Swagger-codegen document generator to generate documents based on this information:

GET / api/v1/namespaces/ {namespace} / pods/ {name} (readCoreV1NamespacedPod) read the specified Pod Path parameters name (required) Path Parameter-name of the Pod namespace (required) Path Parameter-object name and auth scope, such as for teams and projects Consumes This API call consumes the following media types via the Content-Type request header: * / * Query parameters pretty (optional) Query Parameter-If 'true', then the output is pretty printed. Exact (optional) Query Parameter-Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'. Export (optional) Query Parameter-Should this value be exported. Export strips fields that a user can not specify. Return type v1.PodProduces This API call produces the following media types according to the Accept request header; the media type will be conveyed by the Content-Type response header. There are two ways for application/json application/yaml application/vnd.kubernetes.protobuf Responses 200OK v1.Pod 401 Unauthorized to access OpenAPI

From kube-apiserver/swagger.json. This file will contain all enabled GroupVersion methods and models, and the contents will be the latest version of the API Server.

Kubernetes's Github repository, which can access master or other specified Release.

There are many tools that can work with these API, such as using swagger editor to open specification files and render documents, or to generate clients; you can also directly use swagger codegen to generate documents and clients. Automatically generated clients are used out of the box most of the time. However, you may need to make some login and Kubernetes-related settings.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report