|
|
@@ -1,9 +1,16 @@
|
|
|
package nju.seec.SEECdemo.service.api;
|
|
|
|
|
|
+import com.google.common.reflect.TypeToken;
|
|
|
+import io.kubernetes.client.ApiClient;
|
|
|
+import io.kubernetes.client.ApiException;
|
|
|
+import io.kubernetes.client.apis.AppsV1Api;
|
|
|
+import io.kubernetes.client.apis.CoreV1Api;
|
|
|
+import io.kubernetes.client.models.V1Deployment;
|
|
|
+import io.kubernetes.client.models.V1Namespace;
|
|
|
+import io.kubernetes.client.models.V1Pod;
|
|
|
+import io.kubernetes.client.util.Watch;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.DeploymentApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.model.Container;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.model.ContainerPort;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.model.Deployment;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.model.*;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
@@ -11,9 +18,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.Set;
|
|
|
+import java.net.SocketTimeoutException;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import static nju.seec.SEECdemo.util.Constants.APPLICATION_LABEL;
|
|
|
|
|
|
@@ -25,6 +31,17 @@ public class DeploymentApiImplTest {
|
|
|
private DeploymentApi deploymentApi;
|
|
|
|
|
|
private Deployment deployment;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApiClient watchApiClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CoreV1Api coreV1Api;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AppsV1Api appsV1Api;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
@Before
|
|
|
public void before() {
|
|
|
deployment = new Deployment();
|
|
|
@@ -34,14 +51,35 @@ public class DeploymentApiImplTest {
|
|
|
|
|
|
Container container = new Container();
|
|
|
container.setName("container");
|
|
|
- container.setImage("10.1.1.243:18082/container_demo:1.0");
|
|
|
- ContainerPort containerPort = new ContainerPort();
|
|
|
- containerPort.setPort(8081);
|
|
|
- containerPort.setProtocol("TCP");
|
|
|
+ container.setImage("10.1.1.243:18082/container_demo:3.0");
|
|
|
+ ContainerPort service = new ContainerPort();
|
|
|
+ service.setName("service");
|
|
|
+ service.setPort(8081);
|
|
|
+ service.setProtocol("TCP");
|
|
|
Set<ContainerPort> ports = new HashSet<>();
|
|
|
- ports.add(containerPort);
|
|
|
+ ports.add(service);
|
|
|
+
|
|
|
+ ContainerPort health = new ContainerPort();
|
|
|
+ health.setName("health");
|
|
|
+ health.setPort(9090);
|
|
|
+ health.setProtocol("TCP");
|
|
|
+ ports.add(health);
|
|
|
container.setPorts(ports);
|
|
|
|
|
|
+ Map<String, String> env = new HashMap<>();
|
|
|
+ env.put("DB_USERNAME", "root");
|
|
|
+ env.put("DB_URL", "mysql.group25.svc.cluster.local:3306");
|
|
|
+ env.put("DB_SCHEMA", "container_demo");
|
|
|
+ env.put("DB_PASSWORD", "password");
|
|
|
+ container.setEnv(env);
|
|
|
+
|
|
|
+ Probe probe = new Probe();
|
|
|
+ HttpGetAction httpGetAction = new HttpGetAction();
|
|
|
+ httpGetAction.setPath("/actuator/health");
|
|
|
+ httpGetAction.setPort(9090);
|
|
|
+ probe.setHttpGetAction(httpGetAction);
|
|
|
+ container.setReadinessProbe(probe);
|
|
|
+
|
|
|
deployment.setLabels(Collections.singletonMap(APPLICATION_LABEL, "test"));
|
|
|
deployment.setImagePullSecrets(Collections.singletonList("docker-registry-secret"));
|
|
|
|
|
|
@@ -54,12 +92,32 @@ public class DeploymentApiImplTest {
|
|
|
deploymentApi.deleteIfExist("demo2", "test");
|
|
|
|
|
|
deploymentApi.createSync(deployment);
|
|
|
+
|
|
|
+ try {
|
|
|
+ Watch<V1Pod> watch = Watch.createWatch(watchApiClient,
|
|
|
+ new CoreV1Api(watchApiClient).listNamespacedPodCall("demo2", null, null, null, null, "demo.seec.nju.cn/app=test", null, null, null, true, null, null ),
|
|
|
+ new TypeToken<Watch.Response<V1Pod>>(){}.getType());
|
|
|
+ watch.forEach(response -> {
|
|
|
+ System.out.printf("%s pod : %s %s%n", response.type, response.object.getMetadata().getName(), response.object.getStatus());
|
|
|
+ });
|
|
|
+ } catch (ApiException e) {
|
|
|
+ System.out.println(e.getResponseBody());
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testCreateAsync() {
|
|
|
deploymentApi.deleteIfExist("demo2", "test");
|
|
|
deploymentApi.createAsync(deployment);
|
|
|
+ try {
|
|
|
+ Thread.sleep(60000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
@@ -75,4 +133,5 @@ public class DeploymentApiImplTest {
|
|
|
public void testDeleteIfExist() {
|
|
|
deploymentApi.deleteIfExist("demo", "test");
|
|
|
}
|
|
|
+
|
|
|
}
|