kubernetes-client的使用--java中提交argo工作流(8)
- UID
- 1066743
|
kubernetes-client的使用--java中提交argo工作流(8)
完整代码
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 com.biologic.entity.ArgoCluster;
import com.biologic.entity.ArgoList;
import com.biologic.entity.DoneableArgo;
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.api.model.apiextensions.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinitionList;
import io.fabric8.kubernetes.client.AppsAPIGroupClient;
import io.fabric8.kubernetes.client.AutoAdaptableKubernetesClient;
import io.fabric8.kubernetes.client.AutoscalingAPIGroupClient;
import io.fabric8.kubernetes.client.BatchAPIGroupClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.CustomResourceList;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.ExtensionsAPIGroupClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.Resource;
@Controller
@RequestMapping(path = "/report/generation/argo")
public class ArgoController {
public final static String SECRET_TOKEN_ARGO = "uiuda";
private static final String ARGO_CRD_NAME = "workflows.argoproj.io";
@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 +"'");
String fileContent3 = fileContent2.replaceFirst("-random", "-" + chip);
String fileContent4 = fileContent3.replaceFirst("namespace: default", "namespace: " + namespace);
System.out.println(fileContent4);
InputStream stream = new ByteArrayInputStream(fileContent4.getBytes());
Config config = new ConfigBuilder().withMasterUrl(masterurl).build();
KubernetesClient client = new DefaultKubernetesClient(config);
CustomResourceDefinition argoCRD = null;
try {
if (!client.supportsApiPath("/apis/apiextensions.k8s.io/v1beta1") && !client.supportsApiPath("/apis/apiextensions.k8s.io/v1")) {
System.out.println("WARNING this cluster does not support the API Group apiextensions.k8s.io");
return "fail";
}
CustomResourceDefinitionList list = client.customResourceDefinitions().list();
if (list == null) {
System.out.println("ERROR no list returned!");
return "fail";
}
List<CustomResourceDefinition> items = list.getItems();
for (CustomResourceDefinition item : items) {
System.out.println("CustomResourceDefinition " + item.getMetadata().getName() + " has version: " + item.getApiVersion());
if (ARGO_CRD_NAME.equals(item.getMetadata().getName())) {
argoCRD = item;
}
}
} catch (KubernetesClientException e) {
System.out.println("Failed: " + e);
e.printStackTrace();
}
if (argoCRD != null) {
System.out.println("Found CRD: " + argoCRD.getMetadata().getSelfLink());
} else {
return "fail";
}
MixedOperation<ArgoCluster, ArgoList, DoneableArgo, Resource<ArgoCluster, DoneableArgo>> argoClient = client.customResources(argoCRD, ArgoCluster.class, ArgoList.class, DoneableArgo.class);
CustomResourceList<ArgoCluster> argoList =argoClient.list();
List<ArgoCluster> items = argoList.getItems();
System.out.println(" found " + items.size() + " argo");
for (ArgoCluster item : items) {
System.out.println(" " + item);
}
ArgoCluster createArgo= argoClient.load(stream).get();
ArgoCluster finishpod=argoClient.create(createArgo);
String podname=finishpod.getMetadata().getName();
Pod pod = client.pods().inNamespace(namespace).withName(podname).get();
if (pod !=null){
System.out.println("Creating pod in namespace " + pod.getMetadata().getNamespace());
System.out.println("Created pod " + podname);
} else {
System.err.println("not found a Pod! " + podname);
}
return fileContent2;
} catch (IOException e) {
e.printStackTrace();
System.out.println("argo未找到配置文件");
}
return "ok";
} |
|
|
|
|
|