|
| 1 | +/* |
| 2 | +Copyright 2018 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 | +package io.kubernetes.client.examples; |
| 14 | + |
| 15 | +import io.kubernetes.client.ApiClient; |
| 16 | +import io.kubernetes.client.ApiException; |
| 17 | +import io.kubernetes.client.Configuration; |
| 18 | +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; |
| 24 | +import io.kubernetes.client.models.V1PodList; |
| 25 | +import io.kubernetes.client.models.V1ServiceList; |
| 26 | +import io.kubernetes.client.util.Config; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +/** |
| 35 | + * A simple example of how to use the Java API |
| 36 | + * |
| 37 | + * <p>Easiest way to run this: mvn exec:java |
| 38 | + * -Dexec.mainClass="io.kubernetes.client.examples.ExpandedExample" |
| 39 | + * |
| 40 | + * <p>From inside $REPO_DIR/examples |
| 41 | + */ |
| 42 | +public class ExpandedExample { |
| 43 | + |
| 44 | + private static CoreV1Api COREV1_API; |
| 45 | + private static final String DEFAULT_NAME_SPACE = "default"; |
| 46 | + private static final Integer TIME_OUT_VALUE = 180; |
| 47 | + private static final Logger LOGGER = LoggerFactory.getLogger(ExpandedExample.class); |
| 48 | + |
| 49 | + /** |
| 50 | + * Main method |
| 51 | + * |
| 52 | + * @param args |
| 53 | + */ |
| 54 | + public static void main(String[] args) { |
| 55 | + try { |
| 56 | + ApiClient client = Config.defaultClient(); |
| 57 | + // To change the context of k8s cluster, you can use |
| 58 | + // io.kubernetes.client.util.KubeConfig |
| 59 | + Configuration.setDefaultApiClient(client); |
| 60 | + COREV1_API = new CoreV1Api(client); |
| 61 | + |
| 62 | + // ScaleUp/ScaleDown the Deployment pod |
| 63 | + // Please change the name of Deployment? |
| 64 | + System.out.println("----- Scale Deployment Start -----"); |
| 65 | + scaleDeployment("account-service", 5); |
| 66 | + |
| 67 | + // List all of the namaspaces and pods |
| 68 | + List<String> nameSpaces = getAllNameSpaces(); |
| 69 | + nameSpaces |
| 70 | + .stream() |
| 71 | + .forEach( |
| 72 | + namespace -> { |
| 73 | + try { |
| 74 | + System.out.println("----- " + namespace + " -----"); |
| 75 | + getNamespacedPod(namespace).stream().forEach(System.out::println); |
| 76 | + } catch (ApiException ex) { |
| 77 | + LOGGER.warn("Couldn't get the pods in namespace:" + namespace, ex); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // Print all of the Services |
| 82 | + System.out.println("----- Print list all Services Start -----"); |
| 83 | + List<String> services = getServices(); |
| 84 | + services.stream().forEach(System.out::println); |
| 85 | + System.out.println("----- Print list all Services End -----"); |
| 86 | + |
| 87 | + // Print log of specific pod. In this example show the first pod logs. |
| 88 | + System.out.println("----- Print Log of Specific Pod Start -----"); |
| 89 | + String firstPodName = getPods().get(0); |
| 90 | + printLog(DEFAULT_NAME_SPACE, firstPodName); |
| 91 | + System.out.println("----- Print Log of Specific Pod End -----"); |
| 92 | + } catch (ApiException | IOException ex) { |
| 93 | + LOGGER.warn("Exception had occured ", ex); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get all namespaces in k8s cluster |
| 99 | + * |
| 100 | + * @return |
| 101 | + * @throws ApiException |
| 102 | + */ |
| 103 | + public static List<String> getAllNameSpaces() throws ApiException { |
| 104 | + V1NamespaceList listNamespace = |
| 105 | + COREV1_API.listNamespace( |
| 106 | + "true", null, null, Boolean.FALSE, null, 0, null, Integer.MAX_VALUE, Boolean.FALSE); |
| 107 | + List<String> list = |
| 108 | + listNamespace |
| 109 | + .getItems() |
| 110 | + .stream() |
| 111 | + .map(v1Namespace -> v1Namespace.getMetadata().getName()) |
| 112 | + .collect(Collectors.toList()); |
| 113 | + return list; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * List all pod names in all namespaces in k8s cluster |
| 118 | + * |
| 119 | + * @return |
| 120 | + * @throws ApiException |
| 121 | + */ |
| 122 | + public static List<String> getPods() throws ApiException { |
| 123 | + V1PodList v1podList = |
| 124 | + COREV1_API.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); |
| 125 | + List<String> podList = |
| 126 | + v1podList |
| 127 | + .getItems() |
| 128 | + .stream() |
| 129 | + .map(v1Pod -> v1Pod.getMetadata().getName()) |
| 130 | + .collect(Collectors.toList()); |
| 131 | + return podList; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * List all pod in the default namespace |
| 136 | + * |
| 137 | + * @return |
| 138 | + * @throws ApiException |
| 139 | + */ |
| 140 | + public static List<String> getNamespacedPod() throws ApiException { |
| 141 | + return getNamespacedPod(DEFAULT_NAME_SPACE, null); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * List pod in specific namespace |
| 146 | + * |
| 147 | + * @param namespace |
| 148 | + * @return |
| 149 | + * @throws ApiException |
| 150 | + */ |
| 151 | + public static List<String> getNamespacedPod(String namespace) throws ApiException { |
| 152 | + return getNamespacedPod(namespace, null); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * List pod in specific namespace with label |
| 157 | + * |
| 158 | + * @param namespace |
| 159 | + * @param label |
| 160 | + * @return |
| 161 | + * @throws ApiException |
| 162 | + */ |
| 163 | + public static List<String> getNamespacedPod(String namespace, String label) throws ApiException { |
| 164 | + V1PodList listNamespacedPod = |
| 165 | + COREV1_API.listNamespacedPod( |
| 166 | + namespace, |
| 167 | + null, |
| 168 | + null, |
| 169 | + null, |
| 170 | + Boolean.FALSE, |
| 171 | + label, |
| 172 | + Integer.MAX_VALUE, |
| 173 | + null, |
| 174 | + TIME_OUT_VALUE, |
| 175 | + Boolean.FALSE); |
| 176 | + List<String> listPods = |
| 177 | + listNamespacedPod |
| 178 | + .getItems() |
| 179 | + .stream() |
| 180 | + .map(v1pod -> v1pod.getMetadata().getName()) |
| 181 | + .collect(Collectors.toList()); |
| 182 | + return listPods; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * List all Services in default namespace |
| 187 | + * |
| 188 | + * @return |
| 189 | + * @throws ApiException |
| 190 | + */ |
| 191 | + public static List<String> getServices() throws ApiException { |
| 192 | + V1ServiceList listNamespacedService = |
| 193 | + COREV1_API.listNamespacedService( |
| 194 | + DEFAULT_NAME_SPACE, |
| 195 | + null, |
| 196 | + null, |
| 197 | + null, |
| 198 | + Boolean.FALSE, |
| 199 | + null, |
| 200 | + Integer.MAX_VALUE, |
| 201 | + null, |
| 202 | + TIME_OUT_VALUE, |
| 203 | + Boolean.FALSE); |
| 204 | + return listNamespacedService |
| 205 | + .getItems() |
| 206 | + .stream() |
| 207 | + .map(v1service -> v1service.getMetadata().getName()) |
| 208 | + .collect(Collectors.toList()); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Scale up/down the number of pod in Deployment |
| 213 | + * |
| 214 | + * @param deploymentName |
| 215 | + * @param numberOfReplicas |
| 216 | + * @throws ApiException |
| 217 | + */ |
| 218 | + public static void scaleDeployment(String deploymentName, int numberOfReplicas) |
| 219 | + throws ApiException { |
| 220 | + ExtensionsV1beta1Api extensionV1Api = new ExtensionsV1beta1Api(); |
| 221 | + extensionV1Api.setApiClient(COREV1_API.getApiClient()); |
| 222 | + ExtensionsV1beta1DeploymentList listNamespacedDeployment = |
| 223 | + extensionV1Api.listNamespacedDeployment( |
| 224 | + DEFAULT_NAME_SPACE, |
| 225 | + null, |
| 226 | + null, |
| 227 | + null, |
| 228 | + Boolean.FALSE, |
| 229 | + null, |
| 230 | + null, |
| 231 | + null, |
| 232 | + null, |
| 233 | + Boolean.FALSE); |
| 234 | + |
| 235 | + List<ExtensionsV1beta1Deployment> extensionsV1beta1DeploymentItems = |
| 236 | + listNamespacedDeployment.getItems(); |
| 237 | + Optional<ExtensionsV1beta1Deployment> findedDeployment = |
| 238 | + extensionsV1beta1DeploymentItems |
| 239 | + .stream() |
| 240 | + .filter( |
| 241 | + (ExtensionsV1beta1Deployment deployment) -> |
| 242 | + deployment.getMetadata().getName().equals(deploymentName)) |
| 243 | + .findFirst(); |
| 244 | + findedDeployment.ifPresent( |
| 245 | + (ExtensionsV1beta1Deployment deploy) -> { |
| 246 | + try { |
| 247 | + ExtensionsV1beta1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas); |
| 248 | + ExtensionsV1beta1Deployment newDeploy = deploy.spec(newSpec); |
| 249 | + extensionV1Api.replaceNamespacedDeployment( |
| 250 | + deploymentName, DEFAULT_NAME_SPACE, newDeploy, null); |
| 251 | + } catch (ApiException ex) { |
| 252 | + LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex); |
| 253 | + } |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Print out the Log for specific Pods |
| 259 | + * |
| 260 | + * @param namespace |
| 261 | + * @param podName |
| 262 | + * @throws ApiException |
| 263 | + */ |
| 264 | + public static void printLog(String namespace, String podName) throws ApiException { |
| 265 | + // https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CoreV1Api.md#readNamespacedPodLog |
| 266 | + String readNamespacedPodLog = |
| 267 | + COREV1_API.readNamespacedPodLog( |
| 268 | + podName, |
| 269 | + namespace, |
| 270 | + null, |
| 271 | + Boolean.FALSE, |
| 272 | + Integer.MAX_VALUE, |
| 273 | + null, |
| 274 | + Boolean.FALSE, |
| 275 | + Integer.MAX_VALUE, |
| 276 | + 40, |
| 277 | + Boolean.FALSE); |
| 278 | + System.out.println(readNamespacedPodLog); |
| 279 | + } |
| 280 | +} |
0 commit comments