Skip to content

Commit c3d3629

Browse files
authored
Merge pull request #623 from yue9944882/chore/enhance-incluster-example
Chore: Add [in|out-of] cluster examples and enhance docs
2 parents a64e01f + 1cf758d commit c3d3629

File tree

3 files changed

+155
-4
lines changed

3 files changed

+155
-4
lines changed

README.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,48 @@ Then manually install the following JARs:
6161

6262
## Example
6363

64-
list all pods:
64+
We prepared a few examples for common use-cases which are shown below:
65+
- __Configuration__:
66+
- [InClusterClientExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/InClusterClientExample.java):
67+
Configure a client while running inside the Kubernetes cluster.
68+
- [KubeConfigFileClientExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/KubeConfigFileClientExample.java):
69+
Configure a client to access a Kubernetes cluster from outside.
70+
- __Basics__:
71+
- [SimpleExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/Example.java):
72+
Simple minimum example of how to use the client.
73+
- [ProtoExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/ProtoExample.java):
74+
Request/receive payloads in protobuf serialization protocol.
75+
- [PatchExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/PatchExample.java):
76+
Patch resource objects in various supported patch formats, equal to `kubectl patch`.
77+
- [FluentExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/FluentExample.java):
78+
Construct arbitrary resource in a fluent builder style.
79+
- __Streaming__:
80+
- [WatchExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/WatchExample.java):
81+
Subscribe watch events from certain resources, equal to `kubectl get <resource> -w`.
82+
- [LogsExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/LogsExample.java):
83+
Fetch logs from running containers, equal to `kubectl logs`.
84+
- [ExecExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/ExecExample.java):
85+
Establish an "exec" session with running containers, equal to `kubectl exec`.
86+
- [PortForwardExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/PortForwardExample.java):
87+
Maps local port to a port on the pod, equal to `kubectl port-forward`.
88+
- [AttachExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/AttachExample.java):
89+
Attach to a process that is already running inside an existing container, equal to `kubectl attach`.
90+
- [CopyExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/CopyExample.java):
91+
Copy files and directories to and from containers, equal to `kubectl cp`.
92+
- [WebSocketExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/WebSocketExample.java):
93+
Establish an arbitrary web-socket session to certain resources.
94+
- __Advanced__:
95+
- [InformerExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/InformerExample.java):
96+
Build an informer which list-watches resources and reflects the notifications to a local cache.
97+
- [LeaderElectionExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/LeaderElectionExample.java):
98+
Leader election utilities to help implement HA controllers.
99+
- [PagerExample](https://github.com/kubernetes-client/java/blob/master/examples/src/main/java/io/kubernetes/client/examples/PagerExample.java):
100+
Support Pagination (only for the list request) to ease server-side loads/network congestion.
101+
102+
103+
__list all pods__:
65104

66-
```java
105+
```
67106
import io.kubernetes.client.ApiClient;
68107
import io.kubernetes.client.ApiException;
69108
import io.kubernetes.client.Configuration;
@@ -88,9 +127,9 @@ public class Example {
88127
}
89128
```
90129

91-
watch on namespace object:
130+
__watch on namespace object__:
92131

93-
```java
132+
```
94133
import com.google.gson.reflect.TypeToken;
95134
import io.kubernetes.client.ApiClient;
96135
import io.kubernetes.client.ApiException;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.kubernetes.client.examples;
15+
16+
import io.kubernetes.client.ApiClient;
17+
import io.kubernetes.client.ApiException;
18+
import io.kubernetes.client.Configuration;
19+
import io.kubernetes.client.apis.CoreV1Api;
20+
import io.kubernetes.client.models.V1Pod;
21+
import io.kubernetes.client.models.V1PodList;
22+
import io.kubernetes.client.util.ClientBuilder;
23+
import java.io.IOException;
24+
25+
/**
26+
* A simple example of how to use the Java API inside a kubernetes cluster
27+
*
28+
* <p>Easiest way to run this: mvn exec:java
29+
* -Dexec.mainClass="io.kubernetes.client.examples.InClusterClientExample"
30+
*
31+
* <p>From inside $REPO_DIR/examples
32+
*/
33+
public class InClusterClientExample {
34+
public static void main(String[] args) throws IOException, ApiException {
35+
36+
// loading the in-cluster config, including:
37+
// 1. service-account CA
38+
// 2. service-account bearer-token
39+
// 3. service-account namespace
40+
// 4. master endpoints(ip, port) from pre-set environment variables
41+
ApiClient client = ClientBuilder.cluster().build();
42+
43+
// set the global default api-client to the in-cluster one from above
44+
Configuration.setDefaultApiClient(client);
45+
46+
// the CoreV1Api loads default api-client from global configuration.
47+
CoreV1Api api = new CoreV1Api();
48+
49+
// invokes the CoreV1Api client
50+
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null);
51+
for (V1Pod item : list.getItems()) {
52+
System.out.println(item.getMetadata().getName());
53+
}
54+
}
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.kubernetes.client.examples;
15+
16+
import io.kubernetes.client.ApiClient;
17+
import io.kubernetes.client.ApiException;
18+
import io.kubernetes.client.Configuration;
19+
import io.kubernetes.client.apis.CoreV1Api;
20+
import io.kubernetes.client.models.V1Pod;
21+
import io.kubernetes.client.models.V1PodList;
22+
import io.kubernetes.client.util.ClientBuilder;
23+
import io.kubernetes.client.util.KubeConfig;
24+
import java.io.FileReader;
25+
import java.io.IOException;
26+
27+
/**
28+
* A simple example of how to use the Java API from an application outside a kubernetes cluster
29+
*
30+
* <p>Easiest way to run this: mvn exec:java
31+
* -Dexec.mainClass="io.kubernetes.client.examples.KubeConfigFileClientExample"
32+
*
33+
* <p>From inside $REPO_DIR/examples
34+
*/
35+
public class KubeConfigFileClientExample {
36+
public static void main(String[] args) throws IOException, ApiException {
37+
38+
// file path to your KubeConfig
39+
String kubeConfigPath = "~/.kube/config";
40+
41+
// loading the out-of-cluster config, a kubeconfig from file-system
42+
ApiClient client =
43+
ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
44+
45+
// set the global default api-client to the in-cluster one from above
46+
Configuration.setDefaultApiClient(client);
47+
48+
// the CoreV1Api loads default api-client from global configuration.
49+
CoreV1Api api = new CoreV1Api();
50+
51+
// invokes the CoreV1Api client
52+
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null);
53+
for (V1Pod item : list.getItems()) {
54+
System.out.println(item.getMetadata().getName());
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)