Skip to content

Commit f1d6bff

Browse files
committed
Added static modifiers for all methods
I changed all method modifiers from instance method to class method based on the reviewer's proposal.
1 parent c380a8a commit f1d6bff

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.kubernetes.client.models.V1PodList;
2525
import io.kubernetes.client.models.V1ServiceList;
2626
import io.kubernetes.client.util.Config;
27-
import java.io.IOException;
2827
import java.util.List;
2928
import java.util.Optional;
3029
import java.util.stream.Collectors;
@@ -41,7 +40,7 @@
4140
*/
4241
public class ExpandedExample {
4342

44-
private final CoreV1Api corev1Api;
43+
private static final CoreV1Api COREV1_API;
4544
private static final String DEFAULT_NAME_SPACE = "default";
4645
private static final Integer TIME_OUT_VALUE = 180;
4746
/*
@@ -63,12 +62,12 @@ public class ExpandedExample {
6362
*
6463
* @throws java.io.IOException
6564
*/
66-
public ExpandedExample() throws IOException {
65+
static {
6766
// ApiClient client = Config.defaultClient();
6867
// If you want to use specific k8s cluster and access token, please use following?
6968
ApiClient client = Config.fromToken(API_SERVER_NAME, ACCESS_TOKEN, false);
7069
Configuration.setDefaultApiClient(client);
71-
corev1Api = new CoreV1Api(client);
70+
COREV1_API = new CoreV1Api(client);
7271
}
7372

7473
/**
@@ -78,40 +77,39 @@ public ExpandedExample() throws IOException {
7877
*/
7978
public static void main(String[] args) {
8079
try {
81-
ExpandedExample example = new ExpandedExample();
8280

8381
// ScaleUp/ScaleDown the Deployment pod
8482
// Please change the name of Deployment?
8583
System.out.println("----- Scale Deployment Start -----");
86-
example.scaleDeployment("account-service", 5);
84+
scaleDeployment("account-service", 5);
8785

8886
// List all of the namaspaces and pods
89-
List<String> nameSpaces = example.getAllNameSpaces();
87+
List<String> nameSpaces = getAllNameSpaces();
9088
nameSpaces
9189
.stream()
9290
.forEach(
9391
namespace -> {
9492
try {
9593
System.out.println("----- " + namespace + " -----");
96-
example.getNamespacedPod(namespace).stream().forEach(System.out::println);
94+
getNamespacedPod(namespace).stream().forEach(System.out::println);
9795
} catch (ApiException ex) {
9896
LOGGER.warn("Couldn't get the pods in namespace:" + namespace, ex);
9997
}
10098
});
10199

102100
// Print all of the Services
103101
System.out.println("----- Print list all Services Start -----");
104-
List<String> services = example.getServices();
102+
List<String> services = getServices();
105103
services.stream().forEach(System.out::println);
106104
System.out.println("----- Print list all Services End -----");
107105

108106
// Print log of specific pod. In this example show the first pod logs.
109107
System.out.println("----- Print Log of Specific Pod Start -----");
110-
String firstPodName = example.getPods().get(0);
111-
example.printLog(DEFAULT_NAME_SPACE, firstPodName);
108+
String firstPodName = getPods().get(0);
109+
printLog(DEFAULT_NAME_SPACE, firstPodName);
112110
System.out.println("----- Print Log of Specific Pod End -----");
113111

114-
} catch (ApiException | IOException ex) {
112+
} catch (ApiException ex) {
115113
LOGGER.warn("Exception had occured ", ex);
116114
}
117115
}
@@ -122,9 +120,9 @@ public static void main(String[] args) {
122120
* @return
123121
* @throws ApiException
124122
*/
125-
public List<String> getAllNameSpaces() throws ApiException {
123+
public static List<String> getAllNameSpaces() throws ApiException {
126124
V1NamespaceList listNamespace =
127-
corev1Api.listNamespace(
125+
COREV1_API.listNamespace(
128126
"true", null, null, Boolean.FALSE, null, 0, null, Integer.MAX_VALUE, Boolean.FALSE);
129127
List<String> list =
130128
listNamespace
@@ -141,9 +139,9 @@ public List<String> getAllNameSpaces() throws ApiException {
141139
* @return
142140
* @throws ApiException
143141
*/
144-
public List<String> getPods() throws ApiException {
142+
public static List<String> getPods() throws ApiException {
145143
V1PodList v1podList =
146-
corev1Api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
144+
COREV1_API.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
147145
List<String> podList =
148146
v1podList
149147
.getItems()
@@ -159,7 +157,7 @@ public List<String> getPods() throws ApiException {
159157
* @return
160158
* @throws ApiException
161159
*/
162-
public List<String> getNamespacedPod() throws ApiException {
160+
public static List<String> getNamespacedPod() throws ApiException {
163161
return getNamespacedPod(DEFAULT_NAME_SPACE, null);
164162
}
165163

@@ -170,7 +168,7 @@ public List<String> getNamespacedPod() throws ApiException {
170168
* @return
171169
* @throws ApiException
172170
*/
173-
public List<String> getNamespacedPod(String namespace) throws ApiException {
171+
public static List<String> getNamespacedPod(String namespace) throws ApiException {
174172
return getNamespacedPod(namespace, null);
175173
}
176174

@@ -182,9 +180,9 @@ public List<String> getNamespacedPod(String namespace) throws ApiException {
182180
* @return
183181
* @throws ApiException
184182
*/
185-
public List<String> getNamespacedPod(String namespace, String label) throws ApiException {
183+
public static List<String> getNamespacedPod(String namespace, String label) throws ApiException {
186184
V1PodList listNamespacedPod =
187-
corev1Api.listNamespacedPod(
185+
COREV1_API.listNamespacedPod(
188186
namespace,
189187
null,
190188
null,
@@ -210,9 +208,9 @@ public List<String> getNamespacedPod(String namespace, String label) throws ApiE
210208
* @return
211209
* @throws ApiException
212210
*/
213-
public List<String> getServices() throws ApiException {
211+
public static List<String> getServices() throws ApiException {
214212
V1ServiceList listNamespacedService =
215-
corev1Api.listNamespacedService(
213+
COREV1_API.listNamespacedService(
216214
DEFAULT_NAME_SPACE,
217215
null,
218216
null,
@@ -237,9 +235,10 @@ public List<String> getServices() throws ApiException {
237235
* @param numberOfReplicas
238236
* @throws ApiException
239237
*/
240-
public void scaleDeployment(String deploymentName, int numberOfReplicas) throws ApiException {
238+
public static void scaleDeployment(String deploymentName, int numberOfReplicas)
239+
throws ApiException {
241240
ExtensionsV1beta1Api extensionV1Api = new ExtensionsV1beta1Api();
242-
extensionV1Api.setApiClient(corev1Api.getApiClient());
241+
extensionV1Api.setApiClient(COREV1_API.getApiClient());
243242
ExtensionsV1beta1DeploymentList listNamespacedDeployment =
244243
extensionV1Api.listNamespacedDeployment(
245244
DEFAULT_NAME_SPACE,
@@ -282,10 +281,10 @@ public void scaleDeployment(String deploymentName, int numberOfReplicas) throws
282281
* @param podName
283282
* @throws ApiException
284283
*/
285-
public void printLog(String namespace, String podName) throws ApiException {
284+
public static void printLog(String namespace, String podName) throws ApiException {
286285
// https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CoreV1Api.md#readNamespacedPodLog
287286
String readNamespacedPodLog =
288-
corev1Api.readNamespacedPodLog(
287+
COREV1_API.readNamespacedPodLog(
289288
podName,
290289
namespace,
291290
null,

0 commit comments

Comments
 (0)