public URL getRootUrl() { try { if (!Utils.isNullOrEmpty(apiGroupName)) { return new URL(URLUtils.join(config.getMasterUrl().toString(), "apis", apiGroupName, apiGroupVersion)); } return new URL(URLUtils.join(config.getMasterUrl().toString(), "api", apiGroupVersion)); } catch (MalformedURLException e) { throw KubernetesClientException.launderThrowable(e); } }
public VersionInfo fetchVersion() { try { Request.Builder requestBuilder = new Request.Builder() .get() .url(URLUtils.join(config.getMasterUrl(), versionEndpoint)); Response response = client.newCall(requestBuilder.build()).execute(); ObjectMapper objectMapper = new ObjectMapper(); Map<String, String> myMap = objectMapper.readValue(response.body().string(), HashMap.class); return new VersionInfo(myMap); } catch(Exception e) { KubernetesClientException.launderThrowable(e); } return null; } }
public RootPaths getRootPaths() { try { URL requestUrl = new URL(config.getMasterUrl()); Request.Builder req = new Request.Builder().get().url(requestUrl); return handleResponse(req, RootPaths.class); } catch (KubernetesClientException e) { if (e.getCode() != 404) { throw e; } return null; } catch (InterruptedException | ExecutionException | IOException e) { throw KubernetesClientException.launderThrowable(e); } }
public BaseClient(final OkHttpClient httpClient, Config config) throws KubernetesClientException { try { this.httpClient = httpClient; this.namespace = config.getNamespace(); this.configuration = config; this.apiVersion = config.getApiVersion(); if (config.getMasterUrl() == null) { throw new KubernetesClientException("Unknown Kubernetes master URL - " + "please set with the builder, or set with either system property \"" + Config.KUBERNETES_MASTER_SYSTEM_PROPERTY + "\"" + " or environment variable \"" + Utils.convertSystemPropertyNameToEnvVar(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY) + "\""); } this.masterUrl = new URL(config.getMasterUrl()); } catch (Exception e) { throw KubernetesClientException.launderThrowable(e); } }
public static boolean isHttpsAvailable(Config config) { Config sslConfig = new ConfigBuilder(config) .withMasterUrl(Config.HTTPS_PROTOCOL_PREFIX + config.getMasterUrl()) .withRequestTimeout(1000) .withConnectionTimeout(1000) .build(); OkHttpClient client = HttpClientUtils.createHttpClient(config); try { Request request = new Request.Builder().get().url(sslConfig.getMasterUrl()) .build(); Response response = client.newCall(request).execute(); try (ResponseBody body = response.body()) { return response.isSuccessful(); } } catch (Throwable t) { LOG.warn("SSL handshake failed. Falling back to insecure connection."); } finally { if (client != null && client.connectionPool() != null) { client.connectionPool().evictAll(); } } return false; }
base.getMasterUrl())) .withApiVersion(or(kubernetesClientProperties.getApiVersion(), base.getApiVersion()))
@Test public void testMasterUrlWithServiceAccountIPv6() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "2001:db8:1f70::999:de8:7648:6e8"); System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443"); Config config = Config.autoConfigure(null); assertNotNull(config); assertEquals("https://[2001:db8:1f70::999:de8:7648:6e8]:443/", config.getMasterUrl()); }
@Test public void testMasterUrlWithServiceAccount() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "10.0.0.1"); System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443"); Config config = Config.autoConfigure(null); assertNotNull(config); assertEquals("https://10.0.0.1:443/", config.getMasterUrl()); }
private static String getDefaultOpenShiftUrl(Config config) { String openshiftUrl = Utils.getSystemPropertyOrEnvVar(OPENSHIFT_URL_SYTEM_PROPERTY); if (openshiftUrl != null) { // The OPENSHIFT_URL environment variable may be set to the root url (i.e. without the '/oapi/version' path) in some configurations if (isRootURL(openshiftUrl)) { openshiftUrl = URLUtils.join(openshiftUrl, "oapi", getDefaultOapiVersion(config)); } return openshiftUrl; } else { return URLUtils.join(config.getMasterUrl(), "oapi", getDefaultOapiVersion(config)); } }
@Test public void testWithNonExistingNamespacePath() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "nokubeconfigfile"); System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, "nonamespace"); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); Config config = new Config(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); assertEquals(null, config.getNamespace()); }
@Test public void testWithNamespacePathAndSystemProperties() { System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, TEST_NAMESPACE_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "testns"); Config config = new Config(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); assertEquals("testns", config.getNamespace()); }
@Test public void testWithNamespacePath() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "nokubeconfigfile"); System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, TEST_NAMESPACE_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); Config config = new Config(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); assertEquals("testnsfrompath", config.getNamespace()); }
@Test public void testWithNamespacePathAndSytemPropertiesAndBuilder() { System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, TEST_NAMESPACE_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "tobeoverriden"); Config config = new ConfigBuilder() .withNamespace("testns2") .build(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); assertEquals("testns2", config.getNamespace()); }
@Test public void testWithKubeConfig() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); Config config = new Config(); assertNotNull(config); assertEquals("https://172.28.128.4:8443/", config.getMasterUrl()); assertEquals("testns", config.getNamespace()); assertEquals("token", config.getOauthToken()); assertTrue(config.getCaCertFile().endsWith("testns/ca.pem".replace("/", File.separator))); assertTrue(new File(config.getCaCertFile()).isAbsolute()); }
@Test public void testWithMultipleKubeConfigAndOverrideContext() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE + File.pathSeparator + "some-other-file"); Config config = Config.autoConfigure("production/172-28-128-4:8443/root"); assertNotNull(config); assertEquals("https://172.28.128.4:8443/", config.getMasterUrl()); assertEquals("production", config.getNamespace()); assertEquals("supertoken", config.getOauthToken()); assertTrue(config.getCaCertFile().endsWith("testns/ca.pem".replace("/", File.separator))); assertTrue(new File(config.getCaCertFile()).isAbsolute()); }
@Test public void testWithKubeConfigAndOverrideContext() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); Config config = Config.autoConfigure("production/172-28-128-4:8443/root"); assertNotNull(config); assertEquals("https://172.28.128.4:8443/", config.getMasterUrl()); assertEquals("production", config.getNamespace()); assertEquals("supertoken", config.getOauthToken()); assertTrue(config.getCaCertFile().endsWith("testns/ca.pem".replace("/", File.separator))); assertTrue(new File(config.getCaCertFile()).isAbsolute()); }
@Test public void testWithKubeConfigAndSystemProperties() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); Config config = new Config(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); assertEquals("testns", config.getNamespace()); assertEquals("token", config.getOauthToken()); }
@Test public void testWithKubeConfigAndSytemPropertiesAndBuilder() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); Config config = new ConfigBuilder() .withNamespace("testns2") .build(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); assertEquals("token", config.getOauthToken()); assertEquals("testns2", config.getNamespace()); }
@Test public void shouldInstantiateClientUsingSerializeDeserialize() throws MalformedURLException { DefaultKubernetesClient original = new DefaultKubernetesClient(); String json = Serialization.asJson(original.getConfiguration()); DefaultKubernetesClient copy = DefaultKubernetesClient.fromConfig(json); Assert.assertEquals(original.getConfiguration().getMasterUrl(), copy.getConfiguration().getMasterUrl()); Assert.assertEquals(original.getConfiguration().getOauthToken(), copy.getConfiguration().getOauthToken()); Assert.assertEquals(original.getConfiguration().getNamespace(), copy.getConfiguration().getNamespace()); Assert.assertEquals(original.getConfiguration().getUsername(), copy.getConfiguration().getUsername()); Assert.assertEquals(original.getConfiguration().getPassword(), copy.getConfiguration().getPassword()); }
@Test public void shouldInstantiateClientUsingSerializeDeserialize() throws MalformedURLException { DefaultOpenShiftClient original = new DefaultOpenShiftClient(); String json = Serialization.asJson(original.getConfiguration()); DefaultOpenShiftClient copy = DefaultOpenShiftClient.fromConfig(json); Assert.assertEquals(original.getConfiguration().getMasterUrl(), copy.getConfiguration().getMasterUrl()); Assert.assertEquals(original.getConfiguration().getOauthToken(), copy.getConfiguration().getOauthToken()); Assert.assertEquals(original.getConfiguration().getNamespace(), copy.getConfiguration().getNamespace()); Assert.assertEquals(original.getConfiguration().getUsername(), copy.getConfiguration().getUsername()); Assert.assertEquals(original.getConfiguration().getPassword(), copy.getConfiguration().getPassword()); } }