use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class KubectlScale method execute.
@Override
public ApiType execute() throws KubectlException {
validate();
String jsonPatchStr = String.format("[{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":%d}]", replicas);
AppsV1Api api = new AppsV1Api(this.apiClient);
try {
if (apiTypeClass.equals(V1Deployment.class)) {
return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedDeploymentCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, // field-manager is optional
null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
} else if (apiTypeClass.equals(V1ReplicaSet.class)) {
return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedReplicaSetCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
} else if (apiTypeClass.equals(V1StatefulSet.class)) {
return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedStatefulSetCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
} else {
throw new KubectlException("Unsupported class for scale: " + apiTypeClass);
}
} catch (ApiException ex) {
throw new KubectlException(ex);
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class ExpandedExample method main.
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
try {
ApiClient client = Config.defaultClient();
// To change the context of k8s cluster, you can use
// io.kubernetes.client.util.KubeConfig
Configuration.setDefaultApiClient(client);
COREV1_API = new CoreV1Api(client);
// ScaleUp/ScaleDown the Deployment pod
// Please change the name of Deployment?
System.out.println("----- Scale Deployment Start -----");
scaleDeployment("account-service", 5);
// List all of the namaspaces and pods
List<String> nameSpaces = getAllNameSpaces();
nameSpaces.stream().forEach(namespace -> {
try {
System.out.println("----- " + namespace + " -----");
getNamespacedPod(namespace).stream().forEach(System.out::println);
} catch (ApiException ex) {
LOGGER.warn("Couldn't get the pods in namespace:" + namespace, ex);
}
});
// Print all of the Services
System.out.println("----- Print list all Services Start -----");
List<String> services = getServices();
services.stream().forEach(System.out::println);
System.out.println("----- Print list all Services End -----");
// Print log of specific pod. In this example show the first pod logs.
System.out.println("----- Print Log of Specific Pod Start -----");
String firstPodName = getPods().get(0);
printLog(DEFAULT_NAME_SPACE, firstPodName);
System.out.println("----- Print Log of Specific Pod End -----");
} catch (ApiException | IOException ex) {
LOGGER.warn("Exception had occured ", ex);
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class ExpandedExample method getAllNameSpaces.
/**
* Get all namespaces in k8s cluster
*
* @return
* @throws ApiException
*/
public static List<String> getAllNameSpaces() throws ApiException {
V1NamespaceList listNamespace = COREV1_API.listNamespace("true", null, null, null, null, 0, null, null, Integer.MAX_VALUE, Boolean.FALSE);
List<String> list = listNamespace.getItems().stream().map(v1Namespace -> v1Namespace.getMetadata().getName()).collect(Collectors.toList());
return list;
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class ExpandedExample method getPods.
/**
* List all pod names in all namespaces in k8s cluster
*
* @return
* @throws ApiException
*/
public static List<String> getPods() throws ApiException {
V1PodList v1podList = COREV1_API.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
List<String> podList = v1podList.getItems().stream().map(v1Pod -> v1Pod.getMetadata().getName()).collect(Collectors.toList());
return podList;
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class LeaderElectorTest method deleteLeaseLockResource.
private void deleteLeaseLockResource() throws Exception {
try {
CoordinationV1Api coordinationV1Api = new CoordinationV1Api(apiClient);
coordinationV1Api.deleteNamespacedLease(LOCK_RESOURCE_NAME, NAMESPACE, null, null, null, null, null, null);
} catch (ApiException ex) {
if (ex.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
throw ex;
}
}
}
Aggregations