| @@ -41,6 +41,8 @@ public class K8sClientUtil { | |||||
| private String hostPath; | private String hostPath; | ||||
| @Value("${dockerpush.proxyUrl}") | @Value("${dockerpush.proxyUrl}") | ||||
| private String proxyUrl; | private String proxyUrl; | ||||
| @Value("${proxy.useProxy:false}") | |||||
| private boolean useProxy; | |||||
| @Value("${git.localPath}") | @Value("${git.localPath}") | ||||
| String localPathlocal; | String localPathlocal; | ||||
| @@ -322,7 +324,6 @@ public class K8sClientUtil { | |||||
| * @param port port | * @param port port | ||||
| * @param mountPath 映射路径 | * @param mountPath 映射路径 | ||||
| * @param subPath pvc子路径 | * @param subPath pvc子路径 | ||||
| * @param pvcName 存储名 | |||||
| * @param image 镜像 | * @param image 镜像 | ||||
| * @return 创建成功的pod,的nodePort端口 | * @return 创建成功的pod,的nodePort端口 | ||||
| */ | */ | ||||
| @@ -353,40 +354,68 @@ public class K8sClientUtil { | |||||
| int lastIndex = hostPath.lastIndexOf('/'); | int lastIndex = hostPath.lastIndexOf('/'); | ||||
| String newPath = hostPath.substring(0, lastIndex); | String newPath = hostPath.substring(0, lastIndex); | ||||
| V1Pod pod = new V1PodBuilder() | |||||
| .withNewMetadata() | |||||
| .withName(podName) | |||||
| .withLabels(selector) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(podName) | |||||
| .withImage(image) | |||||
| .withPorts(new V1ContainerPort().containerPort(port).protocol("TCP")) | |||||
| .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath).subPath(subPath)) | |||||
| .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() | |||||
| .addNewEnv() | |||||
| .withName("HTTP_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("HTTPS_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("NO_PROXY") | |||||
| .withValue("localhost,kubernetes.default.svc") | |||||
| .endEnv() | |||||
| .endContainer() | |||||
| .addNewVolume() | |||||
| .withName("workspace") | |||||
| .withHostPath(new V1HostPathVolumeSource().path(newPath).type("DirectoryOrCreate")) | |||||
| V1Pod pod; | |||||
| if (useProxy) { | |||||
| pod = new V1PodBuilder() | |||||
| .withNewMetadata() | |||||
| .withName(podName) | |||||
| .withLabels(selector) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(podName) | |||||
| .withImage(image) | |||||
| .withPorts(new V1ContainerPort().containerPort(port).protocol("TCP")) | |||||
| .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath).subPath(subPath)) | |||||
| .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() | |||||
| .addNewEnv() | |||||
| .withName("HTTP_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("HTTPS_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("NO_PROXY") | |||||
| .withValue("localhost,kubernetes.default.svc") | |||||
| .endEnv() | |||||
| .endContainer() | |||||
| .addNewVolume() | |||||
| .withName("workspace") | |||||
| .withHostPath(new V1HostPathVolumeSource().path(newPath).type("DirectoryOrCreate")) | |||||
| // .withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | // .withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | ||||
| .endVolume() | |||||
| .withTerminationGracePeriodSeconds(14400L) | |||||
| .endSpec() | |||||
| .build(); | |||||
| .endVolume() | |||||
| .withTerminationGracePeriodSeconds(14400L) | |||||
| .endSpec() | |||||
| .build(); | |||||
| } else { | |||||
| pod = new V1PodBuilder() | |||||
| .withNewMetadata() | |||||
| .withName(podName) | |||||
| .withLabels(selector) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(podName) | |||||
| .withImage(image) | |||||
| .withPorts(new V1ContainerPort().containerPort(port).protocol("TCP")) | |||||
| .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath).subPath(subPath)) | |||||
| .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() | |||||
| .addNewEnv() | |||||
| .withName("NO_PROXY") | |||||
| .withValue("localhost,kubernetes.default.svc") | |||||
| .endEnv() | |||||
| .endContainer() | |||||
| .addNewVolume() | |||||
| .withName("workspace") | |||||
| .withHostPath(new V1HostPathVolumeSource().path(newPath).type("DirectoryOrCreate")) | |||||
| // .withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | |||||
| .endVolume() | |||||
| .withTerminationGracePeriodSeconds(14400L) | |||||
| .endSpec() | |||||
| .build(); | |||||
| } | |||||
| try { | try { | ||||
| pod = api.createNamespacedPod(namespace, pod, null, null, null); | pod = api.createNamespacedPod(namespace, pod, null, null, null); | ||||
| @@ -495,7 +524,7 @@ public class K8sClientUtil { | |||||
| //配置资源 | //配置资源 | ||||
| V1ResourceRequirements v1ResourceRequirements = setPodResource(devEnvironment.getComputingResourceId()); | V1ResourceRequirements v1ResourceRequirements = setPodResource(devEnvironment.getComputingResourceId()); | ||||
| String image = (String)JsonUtils.jsonToMap(devEnvironment.getImage()).get("value"); | |||||
| String image = (String) JsonUtils.jsonToMap(devEnvironment.getImage()).get("value"); | |||||
| V1Pod pod = new V1PodBuilder() | V1Pod pod = new V1PodBuilder() | ||||
| .withNewMetadata() | .withNewMetadata() | ||||
| @@ -644,7 +673,7 @@ public class K8sClientUtil { | |||||
| } | } | ||||
| public V1Pod createPodWithEnv(String podName, String namespace, String proxyUrl, String mountPath, String image) { | |||||
| public V1Pod createPodWithEnv(String podName, String namespace, String mountPath, String image) { | |||||
| CoreV1Api api = new CoreV1Api(apiClient); | CoreV1Api api = new CoreV1Api(apiClient); | ||||
| V1SecurityContext v1SecurityContext = new V1SecurityContext(); | V1SecurityContext v1SecurityContext = new V1SecurityContext(); | ||||
| @@ -656,38 +685,65 @@ public class K8sClientUtil { | |||||
| List<V1Volume> volumes = new ArrayList<>(); | List<V1Volume> volumes = new ArrayList<>(); | ||||
| volumes.add(new V1Volume().name("workspace").hostPath(new V1HostPathVolumeSource().path(hostPath + "/images").type("DirectoryOrCreate"))); | volumes.add(new V1Volume().name("workspace").hostPath(new V1HostPathVolumeSource().path(hostPath + "/images").type("DirectoryOrCreate"))); | ||||
| V1Pod pod = new V1PodBuilder() | |||||
| .withNewMetadata() | |||||
| .withName(podName) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(podName) | |||||
| .withImage(image) // 替换为您实际要使用的镜像名称 | |||||
| .withSecurityContext(v1SecurityContext) | |||||
| V1Pod pod; | |||||
| if (useProxy) { | |||||
| pod = new V1PodBuilder() | |||||
| .withNewMetadata() | |||||
| .withName(podName) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(podName) | |||||
| .withImage(image) // 替换为您实际要使用的镜像名称 | |||||
| .withSecurityContext(v1SecurityContext) | |||||
| // .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath)) | // .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath)) | ||||
| .withVolumeMounts(volumeMounts) | |||||
| .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() | |||||
| .addNewEnv() | |||||
| .withName("HTTP_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("HTTPS_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("NO_PROXY") | |||||
| .withValue("localhost,kubernetes.default.svc") | |||||
| .endEnv() | |||||
| .endContainer() | |||||
| .withVolumeMounts(volumeMounts) | |||||
| .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() | |||||
| .addNewEnv() | |||||
| .withName("HTTP_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("HTTPS_PROXY") | |||||
| .withValue(proxyUrl) | |||||
| .endEnv() | |||||
| .addNewEnv() | |||||
| .withName("NO_PROXY") | |||||
| .withValue("localhost,kubernetes.default.svc") | |||||
| .endEnv() | |||||
| .endContainer() | |||||
| // .addNewVolume() | // .addNewVolume() | ||||
| // .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | // .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | ||||
| // .endVolume() | // .endVolume() | ||||
| .withVolumes(volumes) | |||||
| .endSpec() | |||||
| .build(); | |||||
| .withVolumes(volumes) | |||||
| .endSpec() | |||||
| .build(); | |||||
| } else { | |||||
| pod = new V1PodBuilder() | |||||
| .withNewMetadata() | |||||
| .withName(podName) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(podName) | |||||
| .withImage(image) // 替换为您实际要使用的镜像名称 | |||||
| .withSecurityContext(v1SecurityContext) | |||||
| // .withVolumeMounts(new V1VolumeMount().name("workspace").mountPath(mountPath)) | |||||
| .withVolumeMounts(volumeMounts) | |||||
| .withNewSecurityContext().withNewPrivileged(true).endSecurityContext() | |||||
| .addNewEnv() | |||||
| .withName("NO_PROXY") | |||||
| .withValue("localhost,kubernetes.default.svc") | |||||
| .endEnv() | |||||
| .endContainer() | |||||
| // .addNewVolume() | |||||
| // .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | |||||
| // .endVolume() | |||||
| .withVolumes(volumes) | |||||
| .endSpec() | |||||
| .build(); | |||||
| } | |||||
| try { | try { | ||||
| pod = api.createNamespacedPod(namespace, pod, null, null, null); | pod = api.createNamespacedPod(namespace, pod, null, null, null); | ||||
| } catch (ApiException e) { | } catch (ApiException e) { | ||||