Java Code Examples for io.fabric8.kubernetes.client.KubernetesClient#close()
The following examples show how to use
io.fabric8.kubernetes.client.KubernetesClient#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KubernetesClientProvider.java From kubernetes-plugin with Apache License 2.0 | 6 votes |
@Restricted(NoExternalUse.class) // testing only public static boolean gracefulClose(KubernetesClient client, OkHttpClient httpClient) { Dispatcher dispatcher = httpClient.dispatcher(); // Remove the client if there are no more users int runningCallsCount = dispatcher.runningCallsCount(); int queuedCallsCount = dispatcher.queuedCallsCount(); if (runningCallsCount == 0 && queuedCallsCount == 0) { LOGGER.log(Level.FINE, "Closing {0}", client.toString()); client.close(); return true; } else { LOGGER.log(Level.INFO, "Not closing {0}: there are still running ({1}) or queued ({2}) calls", new Object[] { client.toString(), runningCallsCount, queuedCallsCount }); return false; } }
Example 2
Source File: DeleteExamples.java From kubernetes-client with Apache License 2.0 | 6 votes |
public static void main(String[] args) { String master = "https://localhost:8443/"; if (args.length == 1) { master = args[0]; } Config config = new ConfigBuilder().withMasterUrl(master).build(); KubernetesClient client = new DefaultKubernetesClient(config); try { log("Create namespace:", client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName("thisisatest").endMetadata().build())); log("Deleted namespace:", client.namespaces().withName("test").delete()); log("Deleted testPod:", client.pods().inNamespace("thisisatest").withName("testpod").delete()); log("Deleted pod by label:", client.pods().withLabel("this", "works").delete()); } catch (KubernetesClientException e) { logger.error(e.getMessage(), e); } finally { client.namespaces().withName("thisisatest").delete(); client.close(); } }
Example 3
Source File: KafkaComputerLauncher.java From remoting-kafka-plugin with MIT License | 5 votes |
private void launchKubernetesPod(KafkaCloudComputer computer) { KubernetesClient client = null; try { KafkaCloudSlave agent = computer.getNode(); if (agent == null) { throw new IllegalStateException("Node has been removed, cannot launch " + computer.getName()); } KafkaKubernetesCloud cloud = agent.getCloud(); client = cloud.connect(); // Build Pod Pod pod = new PodBuilder() .withNewMetadata() .withName(agent.getName()) .endMetadata() .withNewSpec() .addNewContainer() .withName(agent.getName()) .withImage(cloud.getContainerImage()) .withArgs(getLaunchArguments(computer).split(" ")) .endContainer() .endSpec() .build(); // Start Pod String podId = pod.getMetadata().getName(); String namespace = agent.getNamespace(); LOGGER.fine(String.format("Creating Pod: %s/%s", namespace, podId)); client.pods().inNamespace(namespace).create(pod); LOGGER.info(String.format("Created Pod: %s/%s", namespace, podId)); } catch (Exception e) { LOGGER.log(Level.SEVERE, "Failed to launch Kubernetes pod for computer " + computer.getDisplayName(), e); } finally { if (client != null) { client.close(); } } }
Example 4
Source File: Main.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public static void main(String[] args) { log.info("Init-kafka {} is starting", Main.class.getPackage().getImplementationVersion()); InitWriterConfig config = InitWriterConfig.fromMap(System.getenv()); // Workaround for https://github.com/fabric8io/kubernetes-client/issues/2212 // Can be removed after upgrade to Fabric8 4.10.2 or higher or to Java 11 if (Util.shouldDisableHttp2()) { System.setProperty("http2.disable", "true"); } KubernetesClient client = new DefaultKubernetesClient(); log.info("Init-kafka started with config: {}", config); InitWriter writer = new InitWriter(client, config); if (config.getRackTopologyKey() != null) { if (!writer.writeRack()) { System.exit(1); } } if (config.isExternalAddress()) { if (!writer.writeExternalAddress()) { System.exit(1); } } client.close(); }
Example 5
Source File: KubernetesClientProvider.java From kubernetes-plugin with Apache License 2.0 | 5 votes |
/** * Gracefully close expired clients * * @return whether some clients have been closed or not */ @Restricted(NoExternalUse.class) // testing only public static boolean closeExpiredClients() { boolean b = false; if (expiredClients.isEmpty()) { return b; } LOGGER.log(Level.FINE, "Closing {0} expired clients", new Object[] { expiredClients.size() }); for (Iterator<Client> it = expiredClients.iterator(); it.hasNext();) { Client expiredClient = it.next(); // only purge it if the EXPIRED_CLIENTS_PURGE time has elapsed if (Instant.now().minus(EXPIRED_CLIENTS_PURGE_TIME, ChronoUnit.SECONDS) .isBefore(expiredClient.getExpired())) { break; } KubernetesClient client = expiredClient.client; if (client instanceof HttpClientAware) { if (gracefulClose(client, ((HttpClientAware) client).getHttpClient())) { it.remove(); b = true; } } else { LOGGER.log(Level.WARNING, "{0} is not {1}, forcing close", new Object[] { client.toString(), HttpClientAware.class.getSimpleName() }); client.close(); it.remove(); b = true; } } return b; }
Example 6
Source File: ConfigMapExample.java From kubernetes-client with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws InterruptedException { Config config = new ConfigBuilder().build(); KubernetesClient client = new DefaultKubernetesClient(config); String namespace = null; if (args.length > 0) { namespace = args[0]; } if (namespace == null) { namespace = client.getNamespace(); } if (namespace == null) { namespace = "default"; } String name = "cheese"; try { Resource<ConfigMap, DoneableConfigMap> configMapResource = client.configMaps().inNamespace(namespace).withName(name); ConfigMap configMap = configMapResource.createOrReplace(new ConfigMapBuilder(). withNewMetadata().withName(name).endMetadata(). addToData("foo", "" + new Date()). addToData("bar", "beer"). build()); log("Upserted ConfigMap at " + configMap.getMetadata().getSelfLink() + " data " + configMap.getData()); } finally { client.close(); } }
Example 7
Source File: KubernetesDeploy.java From quarkus with Apache License 2.0 | 4 votes |
private Result doCheck() { Config config = ConfigProvider.getConfig(); if (!config.getOptionalValue(DEPLOY, Boolean.class).orElse(false)) { return Result.notConfigured(); } // No need to perform the check multiple times. if (serverFound) { return Result.enabled(); } KubernetesClient client = KubernetesClientUtils.createClient(); String masterURL = client.getConfiguration().getMasterUrl(); try { //Let's check if we can connect. VersionInfo version = client.getVersion(); if (version == null) { return Result.exceptional(new RuntimeException( "Although a Kubernetes deployment was requested, it however cannot take place because the version of the API Server at '" + masterURL + "' could not be determined. Please ensure that a valid token is being used.")); } log.info("Kubernetes API Server at '" + masterURL + "' successfully contacted."); log.debugf("Kubernetes Version: %s.%s", version.getMajor(), version.getMinor()); serverFound = true; return Result.enabled(); } catch (Exception e) { if (e.getCause() instanceof SSLHandshakeException) { return Result.exceptional(new RuntimeException( "Although a Kubernetes deployment was requested, it however cannot take place because the API Server (at '" + masterURL + "') certificates are not trusted. The certificates can be configured using the relevant configuration propertiers under the 'quarkus.kubernetes-client' config root, or \"quarkus.kubernetes-client.trust-certs=true\" can be set to explicitly trust the certificates (not recommended)", e)); } else { return Result.exceptional(new RuntimeException( "Although a Kubernetes deployment was requested, it however cannot take place because there was an error during communication with the API Server at '" + masterURL + "'", e)); } } finally { client.close(); } }
Example 8
Source File: DeploymentExamples.java From kubernetes-client with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws InterruptedException { Config config = new ConfigBuilder().build(); KubernetesClient client = new DefaultKubernetesClient(config); try { // Create a namespace for all our stuff Namespace ns = new NamespaceBuilder().withNewMetadata().withName("thisisatest").addToLabels("this", "rocks").endMetadata().build(); log("Created namespace", client.namespaces().createOrReplace(ns)); ServiceAccount fabric8 = new ServiceAccountBuilder().withNewMetadata().withName("fabric8").endMetadata().build(); client.serviceAccounts().inNamespace("thisisatest").createOrReplace(fabric8); for (int i = 0; i < 2; i++) { System.err.println("Iteration:" + (i+1)); Deployment deployment = new DeploymentBuilder() .withNewMetadata() .withName("nginx") .endMetadata() .withNewSpec() .withReplicas(1) .withNewTemplate() .withNewMetadata() .addToLabels("app", "nginx") .endMetadata() .withNewSpec() .addNewContainer() .withName("nginx") .withImage("nginx") .addNewPort() .withContainerPort(80) .endPort() .endContainer() .endSpec() .endTemplate() .withNewSelector() .addToMatchLabels("app", "nginx") .endSelector() .endSpec() .build(); deployment = client.apps().deployments().inNamespace("thisisatest").create(deployment); log("Created deployment", deployment); System.err.println("Scaling up:" + deployment.getMetadata().getName()); client.apps().deployments().inNamespace("thisisatest").withName("nginx").scale(2, true); log("Created replica sets:", client.apps().replicaSets().inNamespace("thisisatest").list().getItems()); System.err.println("Deleting:" + deployment.getMetadata().getName()); client.resource(deployment).delete(); } log("Done."); }finally { client.namespaces().withName("thisisatest").delete(); client.close(); } }