Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 503f7fb

Browse files
authoredJan 9, 2020
Merge branch 'master' into dotnet
2 parents 8567033 + f9ca13f commit 503f7fb

File tree

11 files changed

+813
-2
lines changed

11 files changed

+813
-2
lines changed
 

‎.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ npm-debug.log
66
.DS_Store
77
.vscode/
88
bin
9-
obj
9+
obj
10+
.idea/
11+
target/
12+
*.iml

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ This repository is meant to demonstrate how to create definitions for Kubernetes
55
You can find examples in the following languages:
66

77
- [Javascript](javascript/README.md)
8-
- [csharp](csharp/README.md)
8+
- [csharp](csharp/README.md)
9+
- [Java](java/README.md)
10+
- [Go](go/README.md)

‎go/README.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# Templating Kubernetes resources with Go
2+
3+
In this section you will learn how to use Go to:
4+
5+
- Create a JSON resource definition of a Kubernetes Pod
6+
- Submit the resource definition to the cluster to create the Pod
7+
8+
For both tasks you will use the official Kubernetes [Go client library](https://github.com/kubernetes/client-go) (client-go).
9+
10+
Let's get started.
11+
12+
## Prerequisites
13+
14+
Make sure you have the `go` command installed. You can find installation instructions in the [Go documentation](https://golang.org/dl/).
15+
16+
## Generating a Pod resource definition
17+
18+
First, create a new directory for the program that you are going to write:
19+
20+
```bash
21+
mkdir generate
22+
cd generate
23+
```
24+
25+
The code for generating a Pod resource definition in JSON looks as follows:
26+
27+
```go
28+
package main
29+
30+
import (
31+
"fmt"
32+
corev1 "k8s.io/api/core/v1"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/util/json"
35+
)
36+
37+
func main() {
38+
pod := createPod("dev")
39+
bytes, err := json.Marshal(pod)
40+
if err != nil {
41+
panic(err)
42+
}
43+
fmt.Println(string(bytes))
44+
}
45+
46+
func createPod(environment string) *corev1.Pod {
47+
return &corev1.Pod{
48+
TypeMeta: metav1.TypeMeta{
49+
Kind: "Pod",
50+
APIVersion: "v1",
51+
},
52+
ObjectMeta: metav1.ObjectMeta{
53+
Name: "test-pod",
54+
},
55+
Spec: corev1.PodSpec{
56+
Containers: []corev1.Container{
57+
{
58+
Name: "test-container",
59+
Image: "nginx",
60+
Env: []corev1.EnvVar{
61+
{
62+
Name: "ENV",
63+
Value: environment,
64+
},
65+
},
66+
},
67+
},
68+
},
69+
}
70+
}
71+
```
72+
73+
Go on and save the above code in a file named `main.go`.
74+
75+
> The above code uses several packages from the [client-go](https://github.com/kubernetes/client-go) library. You can find the documentation of every Go package by pasting its full import path into the search field on [godoc.org](https://godoc.org/). For example, the documentation of the `k8s.io/api/core/v1` package can be found on [godoc.org/k8s.io/api/core/v1](https://godoc.org/k8s.io/api/core/v1).
76+
77+
You can then execute your program with:
78+
79+
```shell
80+
go run main.go
81+
```
82+
83+
The output is the JSON resource definition of a Pod.
84+
85+
Note that this JSON has no newlines and indentation, which makes it hard to read. If you want, you can pretty-print the JSON with [`jq`](https://stedolan.github.io/jq/):
86+
87+
```shell
88+
$ go run main.go | jq
89+
{
90+
"kind": "Pod",
91+
"apiVersion": "v1",
92+
"metadata": {
93+
"name": "test-pod",
94+
"creationTimestamp": null
95+
},
96+
"spec": {
97+
"containers": [
98+
{
99+
"name": "test-container",
100+
"image": "nginx",
101+
"env": [
102+
{
103+
"name": "ENV",
104+
"value": "dev"
105+
}
106+
],
107+
"resources": {}
108+
}
109+
]
110+
},
111+
"status": {}
112+
}
113+
```
114+
115+
You can save this JSON definition in a file:
116+
117+
```shell
118+
go run main.go >pod.json
119+
```
120+
121+
And then you can submit it to the cluster with kubectl as ususal:
122+
123+
```shell
124+
kubectl apply -f pod.json
125+
```
126+
127+
You can verify that the Pod has been created correctly with:
128+
129+
```shell
130+
kubectl get pods
131+
```
132+
133+
## Submitting a Pod resource definition to the cluster
134+
135+
Instead of saving the JSON resource definition to a file and then using kubectl to submit it to the cluster, you can submit the resource definition to the cluster directly in your code.
136+
137+
First of all, create a new directory for the new program that you are going to write:
138+
139+
```shell
140+
cd ..
141+
mkdir create
142+
cd create
143+
```
144+
145+
The code to generate a Pod resource definition _and_ submitting it to the cluster is as follows:
146+
147+
```go
148+
package main
149+
150+
import (
151+
"fmt"
152+
corev1 "k8s.io/api/core/v1"
153+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
154+
"k8s.io/client-go/kubernetes"
155+
"k8s.io/client-go/tools/clientcmd"
156+
"k8s.io/client-go/util/homedir"
157+
"path/filepath"
158+
)
159+
160+
func main() {
161+
kubeconfig, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), ".kube", "config"))
162+
if err != nil {
163+
panic(err)
164+
}
165+
clientset, err := kubernetes.NewForConfig(kubeconfig)
166+
if err != nil {
167+
panic(err)
168+
}
169+
pod := createPod("dev")
170+
_, err = clientset.CoreV1().Pods("default").Create(pod)
171+
if err != nil {
172+
panic(err)
173+
}
174+
fmt.Printf("pod/%s created\n", pod.Name)
175+
}
176+
177+
func createPod(environment string) *corev1.Pod {
178+
// ...same as in the previous program
179+
}
180+
```
181+
182+
Go on and save the above code in a file named `main.go`.
183+
184+
You can then run the program with:
185+
186+
```shell
187+
go run main.go
188+
```
189+
190+
The ouptut should be `pod/test-pod created`.
191+
192+
You can verify that the Pod has indeed been created with:
193+
194+
```shell
195+
kubectl get pods
196+
```
197+
198+
## What's next
199+
200+
As you can imagine, this was just a short demo. You can compose any type of Kubernetes object with the Go client library and you can create, read, update, and delete these objects in your Kubernetes cluster.
201+
202+
Feel free to check out the [example programs](https://github.com/kubernetes/client-go/tree/master/examples) of the Go client library.

‎go/create/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
corev1 "k8s.io/api/core/v1"
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/client-go/kubernetes"
8+
"k8s.io/client-go/tools/clientcmd"
9+
"k8s.io/client-go/util/homedir"
10+
"path/filepath"
11+
)
12+
13+
func main() {
14+
kubeconfig, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), ".kube", "config"))
15+
if err != nil {
16+
panic(err)
17+
}
18+
clientset, err := kubernetes.NewForConfig(kubeconfig)
19+
if err != nil {
20+
panic(err)
21+
}
22+
pod := createPod("dev")
23+
_, err = clientset.CoreV1().Pods("default").Create(pod)
24+
if err != nil {
25+
panic(err)
26+
}
27+
fmt.Printf("pod/%s created\n", pod.Name)
28+
}
29+
30+
func createPod(environment string) *corev1.Pod {
31+
return &corev1.Pod{
32+
TypeMeta: metav1.TypeMeta{
33+
Kind: "Pod",
34+
APIVersion: "v1",
35+
},
36+
ObjectMeta: metav1.ObjectMeta{
37+
Name: "test-pod",
38+
},
39+
Spec: corev1.PodSpec{
40+
Containers: []corev1.Container{
41+
{
42+
Name: "test-container",
43+
Image: "nginx",
44+
Env: []corev1.EnvVar{
45+
{
46+
Name: "ENV",
47+
Value: environment,
48+
},
49+
},
50+
},
51+
},
52+
},
53+
}
54+
}

‎go/generate/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
corev1 "k8s.io/api/core/v1"
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/util/json"
8+
)
9+
10+
func main() {
11+
pod := createPod("dev")
12+
bytes, err := json.Marshal(pod)
13+
if err != nil {
14+
panic(err)
15+
}
16+
fmt.Println(string(bytes))
17+
}
18+
19+
func createPod(environment string) *corev1.Pod {
20+
return &corev1.Pod{
21+
TypeMeta: metav1.TypeMeta{
22+
Kind: "Pod",
23+
APIVersion: "v1",
24+
},
25+
ObjectMeta: metav1.ObjectMeta{
26+
Name: "test-pod",
27+
},
28+
Spec: corev1.PodSpec{
29+
Containers: []corev1.Container{
30+
{
31+
Name: "test-container",
32+
Image: "nginx",
33+
Env: []corev1.EnvVar{
34+
{
35+
Name: "ENV",
36+
Value: environment,
37+
},
38+
},
39+
},
40+
},
41+
},
42+
}
43+
}

‎java/README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Templating Kubernetes resources with Java
2+
3+
In this section you will learn how to use Java and Maven to:
4+
5+
- create JSON (and YAML) definition for a Kubernetes Pod
6+
- submit a Pod definition to the cluster with code
7+
8+
Let's get started.
9+
10+
## Prerequisites
11+
12+
To your Maven application you need to add the `io.fabric8:kubernetes-client` dependency inside the `pom.xml` file
13+
14+
```xml
15+
<dependency>
16+
<groupId>io.fabric8</groupId>
17+
<artifactId>kubernetes-client</artifactId>
18+
<version>4.6.4</version>
19+
</dependency>
20+
```
21+
22+
## Generating Pod definitions
23+
24+
The code is short:
25+
26+
```java
27+
public static void main(String[] args) throws Exception {
28+
String environment = "production";
29+
Pod pod = createPod(namespace);
30+
31+
if (args[0].equals("yaml")) {
32+
System.out.println(SerializationUtils.dumpAsYaml(pod));
33+
} else {
34+
System.out.println(mapper.writeValueAsString(pod));
35+
}
36+
37+
}
38+
39+
public static Pod createPod( String environment ) {
40+
return new PodBuilder().withNewMetadata()
41+
.withName("test-pod")
42+
.endMetadata()
43+
.withNewSpec()
44+
.addNewContainer()
45+
.withName("test-container")
46+
.withImage("k8s.gcr.io/busybox")
47+
.withEnv(new EnvVarBuilder().withName("ENV").withValue(environment).build())
48+
.endContainer()
49+
.endSpec()
50+
.build();
51+
}
52+
```
53+
54+
You can compile and package the Maven application with:
55+
```shell
56+
mvn clean package
57+
```
58+
59+
You can execute the the application with:
60+
61+
```shell
62+
java -jar target/k8s-client-1.0.jar --dry-run
63+
```
64+
65+
The output is a JSON object for the Pod.
66+
67+
```json
68+
{
69+
"apiVersion" : "v1",
70+
"kind" : "Pod",
71+
"metadata" : {
72+
"annotations" : { },
73+
"labels" : { },
74+
"name" : "test-pod"
75+
},
76+
"spec" : {
77+
"containers" : [ {
78+
"env" : [ {
79+
"name" : "ENV",
80+
"value" : "production"
81+
} ],
82+
"image" : "k8s.gcr.io/busybox",
83+
"name" : "test-container"
84+
} ],
85+
"nodeSelector" : { }
86+
}
87+
}
88+
```
89+
90+
_But isn't Kubernetes accepting only YAML?_
91+
92+
YAML is a superset of JSON and any JSON file is also a valid YAML file.
93+
94+
You can create the Pod in the cluster with the following commands:
95+
96+
```shell
97+
java -jar target/k8s-client-1.0.jar yaml --dry-run
98+
kubectl apply -f pod.yaml
99+
```
100+
101+
## Creating a custom Kubectl
102+
103+
Instead of exporting the JSON and feeding it to kubectl, you can send the payload to the cluster directly.
104+
105+
You can use the [Fabric8 Kubernetes API](https://github.com/fabric8io/kubernetes-client) to send the Pod definition to the cluster.
106+
107+
Here's the code:
108+
109+
```java
110+
Config config = new ConfigBuilder().build();
111+
112+
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
113+
if (namespace == null) {
114+
namespace = client.getNamespace();
115+
}
116+
117+
boolean dryRun = false;
118+
for (String arg : args) {
119+
if (arg.equals("--dry-run")) {
120+
dryRun = true;
121+
}
122+
}
123+
if (!dryRun) {
124+
client.pods().inNamespace(namespace).create(pod);
125+
System.out.println("Pod created!");
126+
}
127+
128+
129+
}
130+
131+
```
132+
133+
Assuming you are connected to a running cluster, you can execute the script with:
134+
135+
```shell
136+
java -jar target/k8s-client-1.0.jar yaml
137+
```
138+
139+
And you can verify that the Pod was created with:
140+
141+
```shell
142+
kubectl get pods
143+
```
144+
145+
## What's next
146+
147+
As you can imagine, this is a short demo and you can build more complex objects and use the power of Java and the Fabric8 Kubernetes API to compose large objects from smaller ones.

‎java/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.learnk8s</groupId>
7+
<artifactId>k8s-client</artifactId>
8+
<version>1.0</version>
9+
10+
<name>k8s-client</name>
11+
12+
13+
<properties>
14+
<java.version>11</java.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.fabric8</groupId>
20+
<artifactId>kubernetes-client</artifactId>
21+
<version>4.6.4</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-api</artifactId>
26+
<version>1.7.25</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>slf4j-jdk14</artifactId>
31+
<version>1.7.25</version>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<configuration>
41+
<source>11</source>
42+
<target>11</target>
43+
</configuration>
44+
</plugin>
45+
<plugin>
46+
<artifactId>maven-assembly-plugin</artifactId>
47+
<executions>
48+
<execution>
49+
<phase>package</phase>
50+
<goals>
51+
<goal>single</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
<configuration>
56+
<archive>
57+
<manifest>
58+
<addClasspath>true</addClasspath>
59+
<mainClass>io.learnk8s.KubernetesClientCLI</mainClass>
60+
</manifest>
61+
</archive>
62+
<descriptorRefs>
63+
<descriptorRef>jar-with-dependencies</descriptorRef>
64+
</descriptorRefs>
65+
<finalName>${project.name}-${project.version}</finalName>
66+
<appendAssemblyId>false</appendAssemblyId>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.learnk8s;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.SerializationFeature;
5+
import io.fabric8.kubernetes.api.model.*;
6+
import io.fabric8.kubernetes.client.*;
7+
import io.fabric8.kubernetes.client.Config;
8+
import io.fabric8.kubernetes.client.ConfigBuilder;
9+
import io.fabric8.kubernetes.client.internal.SerializationUtils;
10+
11+
12+
public class KubernetesClientCLI {
13+
14+
15+
private static final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
16+
private static String namespace = null;
17+
18+
public static void main(String[] args) throws Exception {
19+
String environment = "production";
20+
Pod pod = createPod(environment);
21+
22+
if (args.length > 0 && args[0].equals("yaml")) {
23+
System.out.println(SerializationUtils.dumpAsYaml(pod));
24+
} else {
25+
System.out.println(mapper.writeValueAsString(pod));
26+
}
27+
28+
Config config = new ConfigBuilder().build();
29+
30+
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
31+
if (namespace == null) {
32+
namespace = client.getNamespace();
33+
}
34+
35+
boolean dryRun = false;
36+
for (String arg : args) {
37+
if (arg.equals("--dry-run")) {
38+
dryRun = true;
39+
}
40+
}
41+
if (!dryRun) {
42+
client.pods().inNamespace(namespace).create(pod);
43+
System.out.println("Pod created!");
44+
}
45+
46+
47+
}
48+
}
49+
50+
public static Pod createPod(String environment){
51+
return new PodBuilder().withNewMetadata()
52+
.withName("test-pod")
53+
.endMetadata()
54+
.withNewSpec()
55+
.addNewContainer()
56+
.withName("test-container")
57+
.withImage("k8s.gcr.io/busybox")
58+
.withEnv(new EnvVarBuilder().withName("ENV").withValue(environment).build())
59+
.endContainer()
60+
.endSpec()
61+
.build();
62+
}
63+
64+
}

‎python/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Templating Kubernetes resources with Python
2+
3+
In this section you will learn how to use Python to:
4+
5+
- Create a JSON resource definition of a Kubernetes Pod
6+
- Submit the resource definition to the cluster to create the Pod
7+
8+
For both tasks you will use the official Kubernetes [Python client library](https://github.com/kubernetes-client/python).
9+
10+
Let's get started.
11+
12+
## Prerequisites
13+
14+
First, make sure you have Python and the pip package manager installed. You can find installation instructions in the [Python documentation](https://www.python.org/downloads/).
15+
16+
Then, install the Kubernetes Python client library:
17+
18+
```shell
19+
pip install kubernetes
20+
```
21+
22+
## Generating a Pod resource definition
23+
24+
The following is the code for generating and printing out a Pod resource definition in JSON:
25+
26+
```python
27+
from kubernetes import client, config
28+
import json
29+
30+
def main():
31+
pod = create_pod("dev")
32+
print(json.dumps(client.ApiClient().sanitize_for_serialization(pod)))
33+
34+
def create_pod(environment):
35+
return client.V1Pod(
36+
api_version = "v1",
37+
kind = "Pod",
38+
metadata = client.V1ObjectMeta(
39+
name = "test-pod",
40+
),
41+
spec = client.V1PodSpec(
42+
containers = [
43+
client.V1Container(
44+
name = "test-container",
45+
image = "nginx",
46+
env = [
47+
client.V1EnvVar(
48+
name = "ENV",
49+
value = environment,
50+
)
51+
]
52+
)
53+
]
54+
)
55+
)
56+
57+
if __name__ == '__main__':
58+
main()
59+
```
60+
61+
Save the above code in a file named `generate.py`.
62+
63+
> You can find the documentation of the Kubernetes Python client in the project's [GitHub repository](https://github.com/kubernetes-client/python).
64+
65+
You can then run your Python script with:
66+
67+
```shell
68+
python generate.py
69+
```
70+
71+
The output is the JSON resource definition of a Pod.
72+
73+
Note that this JSON has no newlines and indentation, which makes it hard to read. If you want, you can pretty-print the JSON with [`jq`](https://stedolan.github.io/jq/):
74+
75+
```shell
76+
$ python generate.py | jq
77+
{
78+
"apiVersion": "v1",
79+
"kind": "Pod",
80+
"metadata": {
81+
"name": "test-pod"
82+
},
83+
"spec": {
84+
"containers": [
85+
{
86+
"env": [
87+
{
88+
"name": "ENV",
89+
"value": "dev"
90+
}
91+
],
92+
"image": "nginx",
93+
"name": "test-container"
94+
}
95+
]
96+
}
97+
}
98+
```
99+
100+
You can save this JSON definition in a file:
101+
102+
```shell
103+
python generate.py >pod.json
104+
```
105+
106+
And then you can submit it to the cluster with kubectl as ususal:
107+
108+
```shell
109+
kubectl apply -f pod.json
110+
```
111+
112+
You can verify that the Pod has been created correctly with:
113+
114+
```shell
115+
kubectl get pods
116+
```
117+
118+
## Submitting a Pod resource definition to the cluster
119+
120+
Instead of saving the JSON resource definition to a file and then using kubectl to submit it to the cluster, you can submit the resource definition to the cluster directly in your code.
121+
122+
The code to generate a Pod resource definition _and_ submitting it to the cluster is as follows:
123+
124+
```go
125+
from kubernetes import client, config
126+
127+
def main():
128+
config.load_kube_config()
129+
pod = create_pod("dev")
130+
client.CoreV1Api().create_namespaced_pod("default", pod)
131+
print("pod/%s created" % pod.metadata.name)
132+
133+
def create_pod(environment):
134+
# ...same as above
135+
136+
if __name__ == '__main__':
137+
main()
138+
```
139+
140+
Go on and save the above code in a file named `create.py`.
141+
142+
You can then run the script with:
143+
144+
```shell
145+
python create.py
146+
```
147+
148+
The ouptut should be `pod/test-pod created`.
149+
150+
You can verify that the Pod has indeed been created with:
151+
152+
```shell
153+
kubectl get pods
154+
```
155+
156+
## What's next
157+
158+
As you can imagine, this was just a short demo. You can compose any type of Kubernetes object with the Python client library and you can create, read, update, and delete these objects in your Kubernetes cluster.
159+
160+
Feel free to check out the [example programs](https://github.com/kubernetes-client/python/tree/master/examples) of the Python client library.

‎python/create.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from kubernetes import client, config
2+
3+
def main():
4+
config.load_kube_config()
5+
pod = create_pod("dev")
6+
client.CoreV1Api().create_namespaced_pod("default", pod)
7+
print("pod/%s created" % pod.metadata.name)
8+
9+
def create_pod(environment):
10+
return client.V1Pod(
11+
api_version = "v1",
12+
kind = "Pod",
13+
metadata = client.V1ObjectMeta(
14+
name = "test-pod",
15+
),
16+
spec = client.V1PodSpec(
17+
containers = [
18+
client.V1Container(
19+
name = "test-container",
20+
image = "nginx",
21+
env = [
22+
client.V1EnvVar(
23+
name = "ENV",
24+
value = environment,
25+
)
26+
]
27+
)
28+
]
29+
)
30+
)
31+
32+
if __name__ == '__main__':
33+
main()

‎python/generate.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from kubernetes import client, config
2+
import json
3+
4+
def main():
5+
pod = create_pod("dev")
6+
print(json.dumps(client.ApiClient().sanitize_for_serialization(pod)))
7+
8+
def create_pod(environment):
9+
return client.V1Pod(
10+
api_version = "v1",
11+
kind = "Pod",
12+
metadata = client.V1ObjectMeta(
13+
name = "test-pod",
14+
),
15+
spec = client.V1PodSpec(
16+
containers = [
17+
client.V1Container(
18+
name = "test-container",
19+
image = "nginx",
20+
env = [
21+
client.V1EnvVar(
22+
name = "ENV",
23+
value = environment,
24+
)
25+
]
26+
)
27+
]
28+
)
29+
)
30+
31+
if __name__ == '__main__':
32+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.