日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Kubernetes 支持 OpenAPI

發布時間:2025/3/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Kubernetes 支持 OpenAPI 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Open API 讓 API 提供者可以定義自己的操作和模型,并讓開發者可以自動化的生成喜歡語言 的客戶端,用以和 API 服務器通信。Kubernetes 已經支持 Swagger 1.2(OpenAPI 規范的前身)有一段時間了,但是這一標準不夠完整和有效,憑借這一支持,非常難生成工具或客戶端。

在 Kubernetes 1.4,我們對目前的模型和操作進行了升級,引入了 Open API 規范(在沒被捐獻給 Open API 之前被稱作 Swagger 2.0)支持的 Alpha 版本。從 Kubernetes 1.5 開始,OpenAPI 規范的支持已經完備,能夠直接從 Kubernetes 源碼生成規范,對于模型和方法的任何變更,都會保障文檔和規范的完全同步。

新規范讓我們有了更好的 API 文檔,甚至還有了一個 Python 客戶端。

這一模塊化的規范用 GroupVersion 進行分隔,這一做法屬于未雨綢繆,我們想要讓不同的 API Server 使用不同的 GroupVersion。

規范的結構在 Open API spec definition 中有解釋。我們用 operation 標記 來拆分每個 GroupVersion 并盡可能的豐富其中的模型、路徑、操作等信息。操作的參數、調用方法以及響應都有文檔描述。

例如,獲取 Pod 信息的 OpenAPI 規范

{ ... "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": { "200": { "description": "OK", "schema": { "$ref": "#/definitions/v1.Pod" } }, "401": { "description": "Unauthorized" } } }, }

有了這些信息,以及 kube-apiserver 的 URL,就可以據此來調用接口了(/api/v1/namespaces/{namespace}/pods/{name}),參數包括 name、exact 以及 export 等,調用結果會返回 Pod 信息。客戶庫生成器也會使用這些信息來創建一個 API 函數調用來讀取 Pod 信息。例如 Python 客戶端 能夠很簡單的進行如下調用:

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

一個簡化版的 read_namespaced_pod。

Python Clienthttps://github.com/kubernetes-incubator/client-python

還可以使用 Swagger-codegen 文檔生成器來根據這些信息生成文檔:

GET /api/v1/namespaces/{namespace}/pods/{name} (readCoreV1NamespacedPod)read the specified Pod Path parametersname (required) Path Parameter name of the Pod namespace (required) Path Parameter object name and auth scope, such as for teams and projectsConsumes This API call consumes the following media types via the Content-Type request header: */*Query parameterspretty (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 typev1.PodProducesThis 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.application/jsonapplication/yamlapplication/vnd.kubernetes.protobufResponses200OK v1.Pod401Unauthorized

有兩種方式訪問 OpenAPI :

  • 從 kube-apiserver/swagger.json。這個文件會包含所有啟用的 GroupVersion 方法和模型,其中的內容會是該 API Server 所對應的最新版本。
  • Kubernetes 的 Github 倉庫,可以訪問 master 或者其他指定的 Release。

有很多工具 能和這些 API 協同工作,例如可以用 swagger editor 來打開規范文件并渲染文檔,或者生成客戶端;還可以直接利用 swagger codegen 來生成文檔和客戶端。自動生成的客戶端多數時候是開箱即用的。不過可能需要做一些登錄和 Kubernetes 相關的設置。可以使用 Python 客戶端 作為模板來開發自己的客戶端

本文轉自中文社區-Kubernetes 支持 OpenAPI

總結

以上是生活随笔為你收集整理的Kubernetes 支持 OpenAPI的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。