Skip to content

Commit 8823622

Browse files
committed
Added more example with Java SE 8 Lambda expression
1 parent 91d4e44 commit 8823622

File tree

2 files changed

+204
-28
lines changed

2 files changed

+204
-28
lines changed

examples/pom.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,10 @@
7777
</execution>
7878
</executions>
7979
</plugin>
80-
<plugin>
81-
<groupId>com.coveo</groupId>
82-
<artifactId>fmt-maven-plugin</artifactId>
83-
<version>2.2.0</version>
84-
<executions>
85-
<execution>
86-
<phase>test</phase>
87-
<goals>
88-
<goal>check</goal>
89-
</goals>
90-
</execution>
91-
</executions>
92-
</plugin>
9380
</plugins>
9481
</build>
9582
<properties>
96-
<java.version>1.7</java.version>
83+
<java.version>1.8</java.version>
9784
<maven.compiler.source>${java.version}</maven.compiler.source>
9885
<maven.compiler.target>${java.version}</maven.compiler.target>
9986
<swagger-core-version>1.5.12</swagger-core-version>

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

Lines changed: 203 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,225 @@
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
*/
1313
package io.kubernetes.client.examples;
1414

1515
import io.kubernetes.client.ApiClient;
1616
import io.kubernetes.client.ApiException;
1717
import io.kubernetes.client.Configuration;
1818
import io.kubernetes.client.apis.CoreV1Api;
19-
import io.kubernetes.client.models.V1Pod;
19+
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
20+
import io.kubernetes.client.models.ExtensionsV1beta1Deployment;
21+
import io.kubernetes.client.models.ExtensionsV1beta1DeploymentList;
22+
import io.kubernetes.client.models.ExtensionsV1beta1DeploymentSpec;
23+
import io.kubernetes.client.models.V1NamespaceList;
2024
import io.kubernetes.client.models.V1PodList;
25+
import io.kubernetes.client.models.V1ServiceList;
2126
import io.kubernetes.client.util.Config;
22-
import java.io.IOException;
27+
import java.util.List;
28+
import java.util.Optional;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
31+
import java.util.stream.Collectors;
2332

2433
/**
2534
* A simple example of how to use the Java API
2635
*
27-
* <p>Easiest way to run this: mvn exec:java
36+
* <p>
37+
* Easiest way to run this: mvn exec:java
2838
* -Dexec.mainClass="io.kubernetes.client.examples.Example"
2939
*
30-
* <p>From inside $REPO_DIR/examples
40+
* <p>
41+
* From inside $REPO_DIR/examples
3142
*/
3243
public class Example {
33-
public static void main(String[] args) throws IOException, ApiException {
34-
ApiClient client = Config.defaultClient();
35-
Configuration.setDefaultApiClient(client);
3644

37-
CoreV1Api api = new CoreV1Api();
38-
V1PodList list =
39-
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
40-
for (V1Pod item : list.getItems()) {
41-
System.out.println(item.getMetadata().getName());
45+
private final CoreV1Api corev1Api;
46+
private final static String DEFAULT_NAME_SPACE = "default";
47+
48+
/*
49+
For API_SERVER_NAME, you can get the server name as follows.
50+
$ kubectl cluster-info|grep master
51+
Kubernetes master is running at https://*****************.hcp.japaneast.azmk8s.io:443
52+
*/
53+
private final static String API_SERVER_NAME = "https://*****************.hcp.japaneast.azmk8s.io";
54+
/*
55+
For ACCESS_TOKEN , you can get the token as follows
56+
$ kubectl describe secret $(kubectl get secrets | grep default | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d '\t'
57+
*/
58+
private static final String ACCESS_TOKEN = "********************************";
59+
60+
private final static Logger LOGGER = Logger.getLogger(Example.class.getName());
61+
62+
/**
63+
* Constructor
64+
*/
65+
public Example() {
66+
ApiClient client = Config.fromToken(API_SERVER_NAME, ACCESS_TOKEN, false);
67+
Configuration.setDefaultApiClient(client);
68+
corev1Api = new CoreV1Api(client);
69+
}
70+
71+
/**
72+
* Main method
73+
*
74+
* @param args
75+
*/
76+
public static void main(String[] args) {
77+
try {
78+
Example operation = new Example();
79+
operation.executeCommand();
80+
} catch (ApiException ex) {
81+
LOGGER.log(Level.SEVERE, null, ex);
82+
}
83+
}
84+
85+
public void executeCommand() throws ApiException {
86+
87+
//ScaleUp/ScaleDown the Deployment pod
88+
System.out.println("----- Scale Deployment Start -----");
89+
scaleDeployment("account-service", 5);
90+
System.out.println("----- Scale Deployment End -----");
91+
92+
//List all of the namaspaces and pods
93+
List<String> nameSpaces = getAllNameSapces();
94+
nameSpaces.stream().forEach(namespace -> {
95+
try {
96+
System.out.println("----- " + namespace + " -----");
97+
getNamespacedPod(namespace).stream().forEach(System.out::println);
98+
} catch (ApiException ex) {
99+
LOGGER.log(Level.SEVERE, null, ex);
100+
}
101+
});
102+
103+
//Print all of the Services
104+
System.out.println("----- Print list all Services Start -----");
105+
List<String> services = getServices();
106+
services.stream().forEach(System.out::println);
107+
System.out.println("----- Print list all Services End -----");
108+
109+
//Print log of specific pod
110+
System.out.println("----- Print Log of Specific Pod Start -----");
111+
printLog(DEFAULT_NAME_SPACE, "account-service-cbd44cc8-tq5hb");
112+
System.out.println("----- Print Log of Specific Pod End -----");
113+
}
114+
115+
/**
116+
* Get all namespaces in k8s cluster
117+
*
118+
* @return
119+
* @throws ApiException
120+
*/
121+
public List<String> getAllNameSapces() throws ApiException {
122+
V1NamespaceList listNamespace = corev1Api.listNamespace("true", null, null, Boolean.FALSE, null, Integer.SIZE, null, Integer.BYTES, Boolean.FALSE);
123+
List<String> list = listNamespace.getItems()
124+
.stream()
125+
.map(v1Namespace -> v1Namespace.getMetadata().getName())
126+
.collect(Collectors.toList());
127+
return list;
128+
}
129+
130+
/**
131+
* List all pod names in all namespaces in k8s cluster
132+
*
133+
* @return
134+
* @throws ApiException
135+
*/
136+
public List<String> getPods() throws ApiException {
137+
V1PodList v1podList = corev1Api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
138+
List<String> podList = v1podList.getItems()
139+
.stream()
140+
.map(v1Pod -> v1Pod.getMetadata().getName())
141+
.collect(Collectors.toList());
142+
return podList;
143+
}
144+
145+
/**
146+
* List all pod in the default namespace
147+
*
148+
* @return
149+
* @throws ApiException
150+
*/
151+
public List<String> getNamespacedPod() throws ApiException {
152+
return getNamespacedPod(DEFAULT_NAME_SPACE, null);
153+
}
154+
155+
/**
156+
* List pod in specific namespace
157+
*
158+
* @param namespace
159+
* @return
160+
* @throws ApiException
161+
*/
162+
public List<String> getNamespacedPod(String namespace) throws ApiException {
163+
return getNamespacedPod(namespace, null);
164+
}
165+
166+
/**
167+
* List pod in specific namespace with label
168+
*
169+
* @param namespace
170+
* @param label
171+
* @return
172+
* @throws ApiException
173+
*/
174+
public List<String> getNamespacedPod(String namespace, String label) throws ApiException {
175+
V1PodList listNamespacedPod = corev1Api.listNamespacedPod(namespace, null, null, null, Boolean.FALSE, label, Integer.SIZE, null, Integer.BYTES, Boolean.FALSE);
176+
List<String> listPods = listNamespacedPod.getItems()
177+
.stream()
178+
.map(v1pod -> v1pod.getMetadata().getName())
179+
.collect(Collectors.toList());
180+
return listPods;
181+
}
182+
183+
/**
184+
* List all Services in default namespace
185+
*
186+
* @return
187+
* @throws ApiException
188+
*/
189+
public List<String> getServices() throws ApiException {
190+
V1ServiceList listNamespacedService = corev1Api.listNamespacedService(DEFAULT_NAME_SPACE, null, null, null, Boolean.FALSE, null, Integer.SIZE, null, Integer.BYTES, Boolean.FALSE);
191+
return listNamespacedService.getItems().stream().map(v1service -> v1service.getMetadata().getName()).collect(Collectors.toList());
192+
}
193+
194+
/**
195+
* Scale up/down the number of pod in Deployment
196+
*
197+
* @param deploymentName
198+
* @param numberOfReplicas
199+
* @throws ApiException
200+
*/
201+
public void scaleDeployment(String deploymentName, int numberOfReplicas) throws ApiException {
202+
ExtensionsV1beta1Api extensionV1Api = new ExtensionsV1beta1Api();
203+
extensionV1Api.setApiClient(corev1Api.getApiClient());
204+
ExtensionsV1beta1DeploymentList listNamespacedDeployment = extensionV1Api.listNamespacedDeployment(DEFAULT_NAME_SPACE,null,null,null,Boolean.FALSE,null,null,null,null,Boolean.FALSE);
205+
206+
List<ExtensionsV1beta1Deployment> extensionsV1beta1DeploymentItems = listNamespacedDeployment.getItems();
207+
Optional<ExtensionsV1beta1Deployment> findedDeployment = extensionsV1beta1DeploymentItems.stream()
208+
.filter((ExtensionsV1beta1Deployment deployment) -> deployment.getMetadata().getName().equals(deploymentName))
209+
.findFirst();
210+
findedDeployment.ifPresent((ExtensionsV1beta1Deployment deploy) -> {
211+
try {
212+
ExtensionsV1beta1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas);
213+
ExtensionsV1beta1Deployment newDeploy = deploy.spec(newSpec);
214+
extensionV1Api.replaceNamespacedDeployment(deploymentName, DEFAULT_NAME_SPACE, newDeploy, null);
215+
} catch (ApiException ex) {
216+
LOGGER.log(Level.SEVERE, null, ex);
217+
}
218+
});
219+
}
220+
221+
/**
222+
* Print out the Log for specific Pods
223+
*
224+
* @param namespace
225+
* @param podName
226+
* @throws ApiException
227+
*/
228+
public void printLog(String namespace, String podName) throws ApiException {
229+
//https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CoreV1Api.md#readNamespacedPodLog
230+
String readNamespacedPodLog = corev1Api.readNamespacedPodLog(podName, namespace, null, Boolean.FALSE, Integer.MAX_VALUE, null, Boolean.FALSE, Integer.MAX_VALUE, 40, Boolean.FALSE);
231+
System.out.println(readNamespacedPodLog);
42232
}
43-
}
44233
}

0 commit comments

Comments
 (0)