Skip to content

Commit 279884a

Browse files
committed
Modified Original One
Based on the review, I modified original one.
1 parent 8f2652d commit 279884a

File tree

1 file changed

+13
-206
lines changed
  • examples/src/main/java/io/kubernetes/client/examples

1 file changed

+13
-206
lines changed

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

Lines changed: 13 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -9,229 +9,36 @@
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.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;
19+
import io.kubernetes.client.models.V1Pod;
2420
import io.kubernetes.client.models.V1PodList;
25-
import io.kubernetes.client.models.V1ServiceList;
2621
import io.kubernetes.client.util.Config;
2722
import java.io.IOException;
28-
import java.util.List;
29-
import java.util.Optional;
30-
import java.util.logging.Level;
31-
import java.util.logging.Logger;
32-
import java.util.stream.Collectors;
3323

3424
/**
3525
* A simple example of how to use the Java API
3626
*
37-
* <p>
38-
* Easiest way to run this: mvn exec:java
27+
* <p>Easiest way to run this: mvn exec:java
3928
* -Dexec.mainClass="io.kubernetes.client.examples.Example"
4029
*
41-
* <p>
42-
* From inside $REPO_DIR/examples
30+
* <p>From inside $REPO_DIR/examples
4331
*/
4432
public class Example {
33+
public static void main(String[] args) throws IOException, ApiException {
34+
ApiClient client = Config.defaultClient();
35+
Configuration.setDefaultApiClient(client);
4536

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

0 commit comments

Comments
 (0)