Kubernetes 官方 java 客户端之六:OpenAPI 基本操作
<name>openapi</name>
<description>Demo project for openapi client</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
新增 OpenAPIDemoApplication.java,这是新工程的引导类,也有两个 web 接口,一个创建 namespace,另一个按照 namespace 查询 pod 列表,关键位置已添加了注释,就不多赘述了:
package com.bolingcavalry.openapi;
import com.google.gson.GsonBuilder;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1NamespaceBuilder;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.io.FileReader;
@SpringBootApplication
@RestController
@Slf4j
public class OpenAPIDemoApplication {
public static void main(String[] args) {
SpringApplication.run(OpenAPIDemoApplication.class, args);
}
/**
默认的全局设置
@return
@throws Exception
*/
@PostConstruct
private void setDefaultApiClient() throws Exception {
// 存放 K8S 的 config 文件的全路径
String kubeConfigPath = "/Users/zhaoqin/temp/202007/05/config";
// 以 config 作为入参创建的 client 对象,可以访问到 K8S 的 API Server
ApiClient client = ClientBuilder
.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath)))
.build();
// 创建操作类
Configuration.setDefaultApiClient(client);
}
@RequestMapping(value = "/openapi/createnamespace/{namespace}", method = RequestMethod.GET)
public V1Namespace createnamespace(@PathVariable("namespace") String namespace) throws Exception {
CoreV1Api coreV1Api = new CoreV1Api();
V1Namespace v1Namespace = new V1NamespaceBuilder()
.withNewMetadata()
.withName(namespace)
.endMetadata()
.build();
V1Namespace ns = coreV1Api.createNamespace(v1Namespace, null, null, null);
// 使用 Gson 将集合对象序列化成 JSON,在日志中打印出来
log.info("ns info \n{}", new GsonBuilder().setPrettyPrinting().create().toJson(ns));
return ns;
}
@RequestMapping(value = "/openapi/pods/{namespace}", method = RequestMethod.GET)
public V1PodList pods(@PathVariable("namespace") String namespace) throws ApiException {
CoreV1Api apiInstance = new CoreV1Api();
// String | If 'true', then the output is pretty printed.
String pretty = null;
// 订阅事件相关的参数,这里用不上
Boolean allowWatchBookmarks = false;
// 连续查找的标志,类似于翻页
String _continue = null;
// 字段选择器
String fieldSelect
or = "status.phase=Running";
// 根据标签过滤
// String labelSelector = "component=kube-apiserver";
String labelSelector = null;
Integer limit = null;
String resourceVersion = null;
Integer timeoutSeconds = null;
Boolean watch = false;
V1PodList v1PodList = apiInstance.listNamespacedPod(namespace,
pretty,
allowWatchBookmarks,
_continue,
fieldSelector,
labelSelector,
limit,
resourceVersion,
timeoutSeconds,
watch);
评论