kubernetes-client的使用--java中提交argo工作流(4)
- UID
- 1066743
|
kubernetes-client的使用--java中提交argo工作流(4)
测试通过的完整示例代码
提交普通yaml
ArgoController.java
package com.biologic.api;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.biologic.api.service.LogService;
import io.fabric8.kubernetes.api.model.DoneablePod;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
@Controller
@RequestMapping(path = "/report/generation/argo")
public class ArgoController {
public final static String SECRET_TOKEN_ARGO = "123";
@Value("${env}")
private String env;
@Value("${kubernetes.master}")
private String masterurl;
@Autowired
private LogService logService;
@PostMapping(value = "/{chip}/quality-check")
@ResponseBody
public Object quality_check(@RequestParam(value = "token", required = true) String token,
@PathVariable("chip") String chip) {
if (!token.equals(SECRET_TOKEN_ARGO)) {
return "unauthorized";
}
logService.printK8sNow("当前环境 " + env);
String namespace = "default";
if ("sit".equals(env)) {
namespace = "sit";
}
try {
ClassPathResource yamlresource = new ClassPathResource("hello.yaml");
InputStream input=yamlresource.getInputStream();
StringBuffer sb = new StringBuffer();
readToBuffer(sb, input);
String fileContent = sb.toString();
String fileContent2 = fileContent.replaceFirst("value: hello", "value: " + chip);
System.out.println(fileContent2);
InputStream stream = new ByteArrayInputStream(fileContent2.getBytes());
Config config = new ConfigBuilder().withMasterUrl(masterurl).build();
KubernetesClient client = new DefaultKubernetesClient(config);
namespace = client.getNamespace();
List<HasMetadata> resources = client.load(stream).get();
if (resources.isEmpty()) {
System.err.println("No resources loaded from file: " +yamlresource.getPath());
return "No resources loaded from file: " +yamlresource.getPath();
}
HasMetadata resource = resources.get(0);
if (resource instanceof Pod){
Pod pod = (Pod) resource;
System.out.println("Creating pod in namespace " + pod.getMetadata().getNamespace());
NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods = client.pods().inNamespace(namespace);
Pod result = pods.create(pod);
System.out.println("Created pod " + result.getMetadata().getName());
} else {
System.err.println("Loaded resource is not a Pod! " + resource);
}
return fileContent2;
} catch (IOException e) {
e.printStackTrace();
System.out.println("argo未找到配置文件");
}
return "ok";
}
/**
* 将文本文件中的内容读入到buffer中
*
* @param buffer
* buffer
* @param filePath
* 文件路径
* @throws IOException
* 异常
* @author cn.outofmemory
* @date 2013-1-7
*/
private void readToBuffer(StringBuffer buffer, InputStream input) throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
input.close();
}
} |
|
|
|
|
|