Skip to content

Commit 9f429de

Browse files
Added example for create StatefulSet from file
Update README.md Co-Authored-By: Min Kim <[email protected]>
1 parent 3879f27 commit 9f429de

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ We prepared a few examples for common use-cases which are shown below:
7676
Patch resource objects in various supported patch formats, equal to `kubectl patch`.
7777
- [FluentExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/FluentExample.java):
7878
Construct arbitrary resource in a fluent builder style.
79+
- [YamlExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/YamlExample.java):
80+
Suggested way to load or dump resource in Yaml.
7981
- __Streaming__:
8082
- [WatchExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/WatchExample.java):
8183
Subscribe watch events from certain resources, equal to `kubectl get <resource> -w`.

examples/src/main/java/io/kubernetes/client/examples/YamlExample.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@
1212
*/
1313
package io.kubernetes.client.examples;
1414

15+
import io.kubernetes.client.ApiClient;
1516
import io.kubernetes.client.ApiException;
17+
import io.kubernetes.client.Configuration;
18+
import io.kubernetes.client.apis.CoreV1Api;
1619
import io.kubernetes.client.custom.IntOrString;
20+
import io.kubernetes.client.models.V1DeleteOptions;
1721
import io.kubernetes.client.models.V1Pod;
1822
import io.kubernetes.client.models.V1PodBuilder;
1923
import io.kubernetes.client.models.V1Service;
2024
import io.kubernetes.client.models.V1ServiceBuilder;
25+
import io.kubernetes.client.models.V1Status;
26+
import io.kubernetes.client.util.Config;
2127
import io.kubernetes.client.util.Yaml;
28+
import java.io.File;
2229
import java.io.IOException;
2330
import java.util.HashMap;
2431

@@ -67,5 +74,36 @@ public static void main(String[] args) throws IOException, ApiException, ClassNo
6774
.endSpec()
6875
.build();
6976
System.out.println(Yaml.dump(svc));
77+
78+
// Read yaml configuration file, and deploy it
79+
ApiClient client = Config.defaultClient();
80+
Configuration.setDefaultApiClient(client);
81+
82+
// See issue #474. Not needed at most cases, but it is needed if you are using war
83+
// packging or running this on JUnit.
84+
Yaml.addModelMap("v1", "Service", V1Service.class);
85+
86+
// Example yaml file can be found in $REPO_DIR/test-svc.yaml
87+
File file = new File("test-svc.yaml");
88+
V1Service yamlSvc = (V1Service) Yaml.load(file);
89+
90+
// Deployment and StatefulSet is defined in apps/v1, so you should use AppsV1Api instead of
91+
// CoreV1API
92+
CoreV1Api api = new CoreV1Api();
93+
V1Service createResult = api.createNamespacedService("default", yamlSvc, null, null, null);
94+
95+
System.out.println(createResult);
96+
97+
V1Status deleteResult =
98+
api.deleteNamespacedService(
99+
yamlSvc.getMetadata().getName(),
100+
"default",
101+
null,
102+
new V1DeleteOptions(),
103+
null,
104+
null,
105+
null,
106+
null);
107+
System.out.println(deleteResult);
70108
}
71109
}

examples/test-svc.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: test-service
5+
spec:
6+
type: ClusterIP
7+
selector:
8+
app: test-service
9+
ports:
10+
- name: port-of-container
11+
port: 8080

0 commit comments

Comments
 (0)