24
24
import io .kubernetes .client .models .V1PodList ;
25
25
import io .kubernetes .client .models .V1ServiceList ;
26
26
import io .kubernetes .client .util .Config ;
27
- import java .io .IOException ;
28
27
import java .util .List ;
29
28
import java .util .Optional ;
30
29
import java .util .stream .Collectors ;
41
40
*/
42
41
public class ExpandedExample {
43
42
44
- private final CoreV1Api corev1Api ;
43
+ private static final CoreV1Api COREV1_API ;
45
44
private static final String DEFAULT_NAME_SPACE = "default" ;
46
45
private static final Integer TIME_OUT_VALUE = 180 ;
47
46
/*
@@ -63,12 +62,12 @@ public class ExpandedExample {
63
62
*
64
63
* @throws java.io.IOException
65
64
*/
66
- public ExpandedExample () throws IOException {
65
+ static {
67
66
// ApiClient client = Config.defaultClient();
68
67
// If you want to use specific k8s cluster and access token, please use following?
69
68
ApiClient client = Config .fromToken (API_SERVER_NAME , ACCESS_TOKEN , false );
70
69
Configuration .setDefaultApiClient (client );
71
- corev1Api = new CoreV1Api (client );
70
+ COREV1_API = new CoreV1Api (client );
72
71
}
73
72
74
73
/**
@@ -78,40 +77,39 @@ public ExpandedExample() throws IOException {
78
77
*/
79
78
public static void main (String [] args ) {
80
79
try {
81
- ExpandedExample example = new ExpandedExample ();
82
80
83
81
// ScaleUp/ScaleDown the Deployment pod
84
82
// Please change the name of Deployment?
85
83
System .out .println ("----- Scale Deployment Start -----" );
86
- example . scaleDeployment ("account-service" , 5 );
84
+ scaleDeployment ("account-service" , 5 );
87
85
88
86
// List all of the namaspaces and pods
89
- List <String > nameSpaces = example . getAllNameSpaces ();
87
+ List <String > nameSpaces = getAllNameSpaces ();
90
88
nameSpaces
91
89
.stream ()
92
90
.forEach (
93
91
namespace -> {
94
92
try {
95
93
System .out .println ("----- " + namespace + " -----" );
96
- example . getNamespacedPod (namespace ).stream ().forEach (System .out ::println );
94
+ getNamespacedPod (namespace ).stream ().forEach (System .out ::println );
97
95
} catch (ApiException ex ) {
98
96
LOGGER .warn ("Couldn't get the pods in namespace:" + namespace , ex );
99
97
}
100
98
});
101
99
102
100
// Print all of the Services
103
101
System .out .println ("----- Print list all Services Start -----" );
104
- List <String > services = example . getServices ();
102
+ List <String > services = getServices ();
105
103
services .stream ().forEach (System .out ::println );
106
104
System .out .println ("----- Print list all Services End -----" );
107
105
108
106
// Print log of specific pod. In this example show the first pod logs.
109
107
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 );
112
110
System .out .println ("----- Print Log of Specific Pod End -----" );
113
111
114
- } catch (ApiException | IOException ex ) {
112
+ } catch (ApiException ex ) {
115
113
LOGGER .warn ("Exception had occured " , ex );
116
114
}
117
115
}
@@ -122,9 +120,9 @@ public static void main(String[] args) {
122
120
* @return
123
121
* @throws ApiException
124
122
*/
125
- public List <String > getAllNameSpaces () throws ApiException {
123
+ public static List <String > getAllNameSpaces () throws ApiException {
126
124
V1NamespaceList listNamespace =
127
- corev1Api .listNamespace (
125
+ COREV1_API .listNamespace (
128
126
"true" , null , null , Boolean .FALSE , null , 0 , null , Integer .MAX_VALUE , Boolean .FALSE );
129
127
List <String > list =
130
128
listNamespace
@@ -141,9 +139,9 @@ public List<String> getAllNameSpaces() throws ApiException {
141
139
* @return
142
140
* @throws ApiException
143
141
*/
144
- public List <String > getPods () throws ApiException {
142
+ public static List <String > getPods () throws ApiException {
145
143
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 );
147
145
List <String > podList =
148
146
v1podList
149
147
.getItems ()
@@ -159,7 +157,7 @@ public List<String> getPods() throws ApiException {
159
157
* @return
160
158
* @throws ApiException
161
159
*/
162
- public List <String > getNamespacedPod () throws ApiException {
160
+ public static List <String > getNamespacedPod () throws ApiException {
163
161
return getNamespacedPod (DEFAULT_NAME_SPACE , null );
164
162
}
165
163
@@ -170,7 +168,7 @@ public List<String> getNamespacedPod() throws ApiException {
170
168
* @return
171
169
* @throws ApiException
172
170
*/
173
- public List <String > getNamespacedPod (String namespace ) throws ApiException {
171
+ public static List <String > getNamespacedPod (String namespace ) throws ApiException {
174
172
return getNamespacedPod (namespace , null );
175
173
}
176
174
@@ -182,9 +180,9 @@ public List<String> getNamespacedPod(String namespace) throws ApiException {
182
180
* @return
183
181
* @throws ApiException
184
182
*/
185
- public List <String > getNamespacedPod (String namespace , String label ) throws ApiException {
183
+ public static List <String > getNamespacedPod (String namespace , String label ) throws ApiException {
186
184
V1PodList listNamespacedPod =
187
- corev1Api .listNamespacedPod (
185
+ COREV1_API .listNamespacedPod (
188
186
namespace ,
189
187
null ,
190
188
null ,
@@ -210,9 +208,9 @@ public List<String> getNamespacedPod(String namespace, String label) throws ApiE
210
208
* @return
211
209
* @throws ApiException
212
210
*/
213
- public List <String > getServices () throws ApiException {
211
+ public static List <String > getServices () throws ApiException {
214
212
V1ServiceList listNamespacedService =
215
- corev1Api .listNamespacedService (
213
+ COREV1_API .listNamespacedService (
216
214
DEFAULT_NAME_SPACE ,
217
215
null ,
218
216
null ,
@@ -237,9 +235,10 @@ public List<String> getServices() throws ApiException {
237
235
* @param numberOfReplicas
238
236
* @throws ApiException
239
237
*/
240
- public void scaleDeployment (String deploymentName , int numberOfReplicas ) throws ApiException {
238
+ public static void scaleDeployment (String deploymentName , int numberOfReplicas )
239
+ throws ApiException {
241
240
ExtensionsV1beta1Api extensionV1Api = new ExtensionsV1beta1Api ();
242
- extensionV1Api .setApiClient (corev1Api .getApiClient ());
241
+ extensionV1Api .setApiClient (COREV1_API .getApiClient ());
243
242
ExtensionsV1beta1DeploymentList listNamespacedDeployment =
244
243
extensionV1Api .listNamespacedDeployment (
245
244
DEFAULT_NAME_SPACE ,
@@ -282,10 +281,10 @@ public void scaleDeployment(String deploymentName, int numberOfReplicas) throws
282
281
* @param podName
283
282
* @throws ApiException
284
283
*/
285
- public void printLog (String namespace , String podName ) throws ApiException {
284
+ public static void printLog (String namespace , String podName ) throws ApiException {
286
285
// https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CoreV1Api.md#readNamespacedPodLog
287
286
String readNamespacedPodLog =
288
- corev1Api .readNamespacedPodLog (
287
+ COREV1_API .readNamespacedPodLog (
289
288
podName ,
290
289
namespace ,
291
290
null ,
0 commit comments