Skip to content

Instantly share code, notes, and snippets.

@tamanugi
Created August 10, 2021 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamanugi/3641af9a39ed59bbd87b718d06fa8fc7 to your computer and use it in GitHub Desktop.
Save tamanugi/3641af9a39ed59bbd87b718d06fa8fc7 to your computer and use it in GitHub Desktop.
kubernetes-client/java Trial code. create yaml & create deployment via API.
# generated yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: app-nginx-test
name: app-nginx-test
spec:
replicas: 1
selector:
matchLabels:
app: app-nginx-test
strategy: {}
template:
metadata:
labels:
app: app-nginx-test
spec:
containers:
- image: nginx
name: app-nginx-test
resources:
limits: {}
package jp.co.monocrea.monolib4iac.domain.eks;
import com.google.common.io.Files;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentBuilder;
import io.kubernetes.client.openapi.models.V1LabelSelectorBuilder;
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
import io.kubernetes.client.openapi.models.V1PodTemplateSpecBuilder;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Yaml;
import java.io.File;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class K8sService {
public V1Deployment buildDeployment(String appName) throws Exception {
Map<String, String> appLabel = Map.of("app", appName);
V1PodTemplateSpec podSpec =
new V1PodTemplateSpecBuilder()
.withNewMetadata()
.withLabels(appLabel)
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName(appName)
.withImage("nginx")
.withNewResources()
.withLimits(new HashMap<>())
.endResources()
.endContainer()
.endSpec()
.build();
return new V1DeploymentBuilder()
.withApiVersion("apps/v1")
.withKind("Deployment")
.withNewMetadata()
.withLabels(appLabel)
.withName(appName)
.endMetadata()
.withNewSpec()
.withReplicas(1)
.withSelector(new V1LabelSelectorBuilder().withMatchLabels(appLabel).build())
.withNewStrategy()
.endStrategy()
.withTemplate(podSpec)
.endSpec()
.build();
}
public void createYaml(String appName) throws Exception {
V1Deployment deployment = buildDeployment(appName);
try (Writer w = Files.newWriter(new File("hoge-deployment.yaml"), StandardCharsets.UTF_8)) {
Yaml.dump(deployment, w);
}
}
public void createPod(String appName) throws Exception {
ApiClient client = Config.fromConfig("/Users/xxx/.kube/config");
AppsV1Api appsApi = new AppsV1Api(client);
try {
appsApi.createNamespacedDeployment("default", buildDeployment(appName), null, null, null);
} catch (ApiException e) {
log.error("failed", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment