<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>openapi | 伪架构师</title>
    <link>/tags/openapi/</link>
      <atom:link href="/tags/openapi/index.xml" rel="self" type="application/rss+xml" />
    <description>openapi</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>zh</language><lastBuildDate>Sun, 25 Dec 2016 04:22:42 +0800</lastBuildDate>
    <image>
      <url>/img/logo-wide.png</url>
      <title>openapi</title>
      <link>/tags/openapi/</link>
    </image>
    
    <item>
      <title>Kubernetes 支持 OpenAPI</title>
      <link>/post/openapi-support-in-kubernetes/</link>
      <pubDate>Sun, 25 Dec 2016 04:22:42 +0800</pubDate>
      <guid>/post/openapi-support-in-kubernetes/</guid>
      <description>&lt;p&gt;原文：&lt;a href=&#34;https://kubernetes.io/blog/2016/12/kubernetes-supports-openapi&#34; target=&#34;_blank&#34;&gt;Kubernetes supports OpenAPI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&#34;https://www.openapis.org/&#34; target=&#34;_blank&#34;&gt;Open API&lt;/a&gt; 让 API 提供者可以定义自己的操作和模型，并让开发者可以自动化的生成喜欢语言的客户端，用以和 API 服务器通信。Kubernetes 已经支持 Swagger 1.2（OpenAPI 规范的前身）有一段时间了，但是这一标准不够完整和有效，凭借这一支持，非常难生成工具或客户端。&lt;/p&gt;

&lt;p&gt;在 Kubernetes 1.4，我们对目前的模型和操作进行了升级，引入了 Open API 规范（在没被捐献给 &lt;a href=&#34;https://www.openapis.org/about&#34; target=&#34;_blank&#34;&gt;Open API&lt;/a&gt; 之前被称作 Swagger 2.0）支持的 Alpha 版本。从 &lt;a href=&#34;http://blog.kubernetes.io/2016/12/kubernetes-1.5-supporting-production-workloads.html&#34; target=&#34;_blank&#34;&gt;Kubernetes 1.5&lt;/a&gt; 开始，OpenAPI 规范的支持已经完备，能够直接从 Kubernetes 源码生成规范，对于模型和方法的任何变更，都会保障文档和规范的完全同步。&lt;/p&gt;

&lt;p&gt;新规范让我们有了更好的 API 文档，甚至还有了一个 &lt;a href=&#34;https://github.com/kubernetes-incubator/client-python&#34; target=&#34;_blank&#34;&gt;Python 客户端&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;这一模块化的规范用 GroupVersion 进行分隔，这一做法属于未雨绸缪，我们想要让不同的 API Server 使用不同的 GroupVersion。&lt;/p&gt;

&lt;p&gt;规范的结构在 &lt;a href=&#34;https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md&#34; target=&#34;_blank&#34;&gt;Open API spec definition&lt;/a&gt; 中有解释。我们用 &lt;a href=&#34;https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#tag-object&#34; target=&#34;_blank&#34;&gt;operation 标记&lt;/a&gt; 来拆分每个 GroupVersion 并尽可能的丰富其中的模型、路径、操作等信息。操作的参数、调用方法以及响应都有文档描述。&lt;/p&gt;

&lt;p&gt;例如，获取 Pod 信息的 OpenAPI 规范&lt;/p&gt;

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

&lt;p&gt;有了这些信息，以及 &lt;code&gt;kube-apiserver&lt;/code&gt; 的 URL，就可以据此来调用接口了（&lt;code&gt;/api/v1/namespaces/{namespace}/pods/{name}&lt;/code&gt;），参数包括 &lt;code&gt;name&lt;/code&gt;、&lt;code&gt;exact&lt;/code&gt; 以及 &lt;code&gt;export&lt;/code&gt; 等，调用结果会返回 Pod 信息。客户库生成器也会使用这些信息来创建一个 API 函数调用来读取 Pod 信息。例如 &lt;a href=&#34;https://github.com/kubernetes-incubator/client-python&#34; target=&#34;_blank&#34;&gt;Python 客户端&lt;/a&gt; 能够很简单的进行如下调用：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from kubernetes import client
ret = client.CoreV1Api().read_namespaced_pod(name=&amp;quot;pods_name&amp;quot;, namespace=&amp;quot;default&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href=&#34;https://gist.github.com/mbohlool/d5ec1dace27ef90cf742555c05480146&#34; target=&#34;_blank&#34;&gt;https://gist.github.com/mbohlool/d5ec1dace27ef90cf742555c05480146&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;一个简化版的 &lt;a href=&#34;https://gist.github.com/mbohlool/d5ec1dace27ef90cf742555c05480146&#34; target=&#34;_blank&#34;&gt;&lt;code&gt;read_namespaced_pod&lt;/code&gt;&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;Python Client：&lt;a href=&#34;https://github.com/kubernetes-incubator/client-python&#34; target=&#34;_blank&#34;&gt;https://github.com/kubernetes-incubator/client-python&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;还可以使用 Swagger-codegen 文档生成器来根据这些信息生成文档：&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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 &#39;true&#39;, then the output is pretty printed.
exact (optional)
Query Parameter — Should the export be exact. Exact export maintains cluster-specific fields like &#39;Namespace&#39;.
export (optional)
Query Parameter — Should this value be exported. Export strips fields that a user can not specify.
Return type
v1.Pod

Produces
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.
application/json
application/yaml
application/vnd.kubernetes.protobuf
Responses
200
OK v1.Pod
401
Unauthorized
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;有两种方式访问 OpenAPI ：&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;从 &lt;code&gt;kube-apiserver&lt;/code&gt;/swagger.json。这个文件会包含所有启用的 GroupVersion 方法和模型，其中的内容会是该 API Server 所对应的最新版本。&lt;/li&gt;
&lt;li&gt;Kubernetes 的 Github 仓库，可以访问 master 或者其他指定的 Release。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;有很多&lt;a href=&#34;http://swagger.io/tools/&#34; target=&#34;_blank&#34;&gt;工具&lt;/a&gt; 能和这些 API 协同工作，例如可以用 &lt;a href=&#34;http://swagger.io/swagger-editor/&#34; target=&#34;_blank&#34;&gt;swagger editor&lt;/a&gt; 来打开规范文件并渲染文档，或者生成客户端；还可以直接利用 &lt;a href=&#34;http://swagger.io/swagger-codegen/&#34; target=&#34;_blank&#34;&gt;swagger codegen&lt;/a&gt; 来生成文档和客户端。自动生成的客户端多数时候是开箱即用的。不过可能需要做一些登录和 Kubernetes 相关的设置。可以使用 &lt;a href=&#34;https://github.com/kubernetes-incubator/client-python&#34; target=&#34;_blank&#34;&gt;Python 客户端&lt;/a&gt; 作为模板来开发自己的客户端。&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
