|
| 1 | +/* |
| 2 | +Copyright 2017 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> |
| 38 | + * Easiest way to run this: mvn exec:java |
| 39 | + * -Dexec.mainClass="io.kubernetes.client.examples.ExpandedExample" |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * From inside $REPO_DIR/examples |
| 43 | + */ |
| 44 | +public class ExpandedExample { |
| 45 | + |
| 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 static final Logger LOGGER = LoggerFactory.getLogger(ExpandedExample.class); |
| 62 | + |
| 63 | + /** |
| 64 | + * Constructor |
| 65 | + * |
| 66 | + * @throws java.io.IOException |
| 67 | + */ |
| 68 | + public ExpandedExample() throws IOException { |
| 69 | + //ApiClient client = Config.defaultClient(); |
| 70 | + //If you want to use specific k8s cluster and access token, please use following? |
| 71 | + ApiClient client = Config.fromToken(API_SERVER_NAME, ACCESS_TOKEN, false); |
| 72 | + Configuration.setDefaultApiClient(client); |
| 73 | + corev1Api = new CoreV1Api(client); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Main method |
| 78 | + * |
| 79 | + * @param args |
| 80 | + */ |
| 81 | + public static void main(String[] args) { |
| 82 | + try { |
| 83 | + ExpandedExample example = new ExpandedExample(); |
| 84 | + |
| 85 | + //ScaleUp/ScaleDown the Deployment pod |
| 86 | + //Please change the name of Deployment? |
| 87 | + System.out.println("----- Scale Deployment Start -----"); |
| 88 | + example.scaleDeployment("account-service", 5); |
| 89 | + |
| 90 | + //List all of the namaspaces and pods |
| 91 | + List<String> nameSpaces = example.getAllNameSpaces(); |
| 92 | + nameSpaces.stream().forEach(namespace -> { |
| 93 | + try { |
| 94 | + System.out.println("----- " + namespace + " -----"); |
| 95 | + example.getNamespacedPod(namespace).stream().forEach(System.out::println); |
| 96 | + } catch (ApiException ex) { |
| 97 | + LOGGER.warn("Couldn't get the pods in namespace:" + namespace , ex); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + //Print all of the Services |
| 102 | + System.out.println("----- Print list all Services Start -----"); |
| 103 | + List<String> services = example.getServices(); |
| 104 | + services.stream().forEach(System.out::println); |
| 105 | + System.out.println("----- Print list all Services End -----"); |
| 106 | + |
| 107 | + //Print log of specific pod. In this example show the first pod logs. |
| 108 | + System.out.println("----- Print Log of Specific Pod Start -----"); |
| 109 | + String firstPodName = example.getPods().get(0); |
| 110 | + example.printLog(DEFAULT_NAME_SPACE, firstPodName); |
| 111 | + System.out.println("----- Print Log of Specific Pod End -----"); |
| 112 | + |
| 113 | + } catch (ApiException | IOException ex) { |
| 114 | + LOGGER.warn("Exception had occured " , ex); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get all namespaces in k8s cluster |
| 120 | + * |
| 121 | + * @return |
| 122 | + * @throws ApiException |
| 123 | + */ |
| 124 | + public List<String> getAllNameSpaces() throws ApiException { |
| 125 | + V1NamespaceList listNamespace = corev1Api.listNamespace("true", null, null, Boolean.FALSE, null, Integer.SIZE, null, Integer.BYTES, Boolean.FALSE); |
| 126 | + List<String> list = listNamespace.getItems() |
| 127 | + .stream() |
| 128 | + .map(v1Namespace -> v1Namespace.getMetadata().getName()) |
| 129 | + .collect(Collectors.toList()); |
| 130 | + return list; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * List all pod names in all namespaces in k8s cluster |
| 135 | + * |
| 136 | + * @return |
| 137 | + * @throws ApiException |
| 138 | + */ |
| 139 | + public List<String> getPods() throws ApiException { |
| 140 | + V1PodList v1podList = corev1Api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); |
| 141 | + List<String> podList = v1podList.getItems() |
| 142 | + .stream() |
| 143 | + .map(v1Pod -> v1Pod.getMetadata().getName()) |
| 144 | + .collect(Collectors.toList()); |
| 145 | + return podList; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * List all pod in the default namespace |
| 150 | + * |
| 151 | + * @return |
| 152 | + * @throws ApiException |
| 153 | + */ |
| 154 | + public List<String> getNamespacedPod() throws ApiException { |
| 155 | + return getNamespacedPod(DEFAULT_NAME_SPACE, null); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * List pod in specific namespace |
| 160 | + * |
| 161 | + * @param namespace |
| 162 | + * @return |
| 163 | + * @throws ApiException |
| 164 | + */ |
| 165 | + public List<String> getNamespacedPod(String namespace) throws ApiException { |
| 166 | + return getNamespacedPod(namespace, null); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * List pod in specific namespace with label |
| 171 | + * |
| 172 | + * @param namespace |
| 173 | + * @param label |
| 174 | + * @return |
| 175 | + * @throws ApiException |
| 176 | + */ |
| 177 | + public List<String> getNamespacedPod(String namespace, String label) throws ApiException { |
| 178 | + V1PodList listNamespacedPod = corev1Api.listNamespacedPod(namespace, null, null, null, Boolean.FALSE, label, Integer.SIZE, null, Integer.BYTES, Boolean.FALSE); |
| 179 | + List<String> listPods = listNamespacedPod.getItems() |
| 180 | + .stream() |
| 181 | + .map(v1pod -> v1pod.getMetadata().getName()) |
| 182 | + .collect(Collectors.toList()); |
| 183 | + return listPods; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * List all Services in default namespace |
| 188 | + * |
| 189 | + * @return |
| 190 | + * @throws ApiException |
| 191 | + */ |
| 192 | + public List<String> getServices() throws ApiException { |
| 193 | + V1ServiceList listNamespacedService = corev1Api.listNamespacedService(DEFAULT_NAME_SPACE, null, null, null, Boolean.FALSE, null, Integer.SIZE, null, Integer.BYTES, Boolean.FALSE); |
| 194 | + return listNamespacedService.getItems().stream().map(v1service -> v1service.getMetadata().getName()).collect(Collectors.toList()); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Scale up/down the number of pod in Deployment |
| 199 | + * |
| 200 | + * @param deploymentName |
| 201 | + * @param numberOfReplicas |
| 202 | + * @throws ApiException |
| 203 | + */ |
| 204 | + public void scaleDeployment(String deploymentName, int numberOfReplicas) throws ApiException { |
| 205 | + ExtensionsV1beta1Api extensionV1Api = new ExtensionsV1beta1Api(); |
| 206 | + extensionV1Api.setApiClient(corev1Api.getApiClient()); |
| 207 | + ExtensionsV1beta1DeploymentList listNamespacedDeployment = extensionV1Api.listNamespacedDeployment(DEFAULT_NAME_SPACE, null, null, null, Boolean.FALSE, null, null, null, null, Boolean.FALSE); |
| 208 | + |
| 209 | + List<ExtensionsV1beta1Deployment> extensionsV1beta1DeploymentItems = listNamespacedDeployment.getItems(); |
| 210 | + Optional<ExtensionsV1beta1Deployment> findedDeployment = extensionsV1beta1DeploymentItems.stream() |
| 211 | + .filter((ExtensionsV1beta1Deployment deployment) -> deployment.getMetadata().getName().equals(deploymentName)) |
| 212 | + .findFirst(); |
| 213 | + findedDeployment.ifPresent((ExtensionsV1beta1Deployment deploy) -> { |
| 214 | + try { |
| 215 | + ExtensionsV1beta1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas); |
| 216 | + ExtensionsV1beta1Deployment newDeploy = deploy.spec(newSpec); |
| 217 | + extensionV1Api.replaceNamespacedDeployment(deploymentName, DEFAULT_NAME_SPACE, newDeploy, null); |
| 218 | + } catch (ApiException ex) { |
| 219 | + LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex); |
| 220 | + } |
| 221 | + }); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Print out the Log for specific Pods |
| 226 | + * |
| 227 | + * @param namespace |
| 228 | + * @param podName |
| 229 | + * @throws ApiException |
| 230 | + */ |
| 231 | + public void printLog(String namespace, String podName) throws ApiException { |
| 232 | + //https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CoreV1Api.md#readNamespacedPodLog |
| 233 | + String readNamespacedPodLog = corev1Api.readNamespacedPodLog(podName, namespace, null, Boolean.FALSE, Integer.MAX_VALUE, null, Boolean.FALSE, Integer.MAX_VALUE, 40, Boolean.FALSE); |
| 234 | + System.out.println(readNamespacedPodLog); |
| 235 | + } |
| 236 | +} |
0 commit comments