io.fabric8.kubernetes.client.ConfigBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.client.ConfigBuilder. 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: ResolveServicesByLabelsConfigAndPortOKOfflineTest.java    From vxms with Apache License 2.0 7 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace(null)
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #2
Source File: DeleteExamples.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
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: OperatorAutoConfiguration.java    From java-operator-sdk with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public KubernetesClient kubernetesClient(OperatorProperties operatorProperties) {
    ConfigBuilder config = new ConfigBuilder();
    config.withTrustCerts(operatorProperties.isTrustSelfSignedCertificates());
    if (StringUtils.isNotBlank(operatorProperties.getUsername())) {
        config.withUsername(operatorProperties.getUsername());
    }
    if (StringUtils.isNotBlank(operatorProperties.getPassword())) {
        config.withUsername(operatorProperties.getPassword());
    }
    if (StringUtils.isNotBlank(operatorProperties.getMasterUrl())) {
        config.withMasterUrl(operatorProperties.getMasterUrl());
    }
    KubernetesClient k8sClient = operatorProperties.isOpenshift() ? new DefaultOpenShiftClient(config.build()) : new DefaultKubernetesClient(config.build());
    return k8sClient;
}
 
Example #4
Source File: KubernetesHandler.java    From apollo with Apache License 2.0 6 votes vote down vote up
private KubernetesClient createKubernetesClient(Environment environment) {
    try {
        ConfigBuilder configBuilder = new ConfigBuilder()
                .withMasterUrl(environment.getKubernetesMaster())
                .withOauthToken(environment.getKubernetesToken());

        String caCert = environment.getKubernetesCaCert();
        if (StringUtils.isNotBlank(caCert)) {
            configBuilder.withCaCertData(caCert);
        }

        Config config = configBuilder.build();
        return new DefaultKubernetesClient(config);
    } catch (Exception e) {
        logger.error("Could not create kubernetes client for environment {}", environment.getId(), e);
        throw new RuntimeException();
    }
}
 
Example #5
Source File: ResolveServicesByLAbelsWithPortNameOfflineTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {

    ClassLoader classLoader = getClass().getClassLoader();
    File ca = new File(classLoader.getResource("ca.crt").getFile());
    File clientcert = new File(classLoader.getResource("client.crt").getFile());
    File clientkey = new File(classLoader.getResource("client.key").getFile());
    System.out.println("port: " + 0 + "  host:" + 0);
    config =
        new ConfigBuilder()
            .withMasterUrl(0 + ":" + 0)
            .withNamespace(null)
            .withCaCertFile(ca.getAbsolutePath())
            .withClientCertFile(clientcert.getAbsolutePath())
            .withClientKeyFile(clientkey.getAbsolutePath())
            .build();
  }
 
Example #6
Source File: ResolveServicesByNameOfflineTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  // KubernetesMockServer plainServer = new KubernetesMockServer(false);
  // plainServer.init();
  String host = "1.1.1.1";
  Integer port = 8080;
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace(null)
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
}
 
Example #7
Source File: ResolveServicesByNameTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #8
Source File: KubernetesMockTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #9
Source File: ResolveServicesByLAbelsWithPortNameTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #10
Source File: ResolveServicesByLabelsConfigOKOfflineTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {

    ClassLoader classLoader = getClass().getClassLoader();
    File ca = new File(classLoader.getResource("ca.crt").getFile());
    File clientcert = new File(classLoader.getResource("client.crt").getFile());
    File clientkey = new File(classLoader.getResource("client.key").getFile());
    System.out.println("port: " + 0 + "  host:" + 0);
    config =
        new ConfigBuilder()
            .withMasterUrl(0 + ":" + 0)
            .withNamespace(null)
            .withCaCertFile(ca.getAbsolutePath())
            .withClientCertFile(clientcert.getAbsolutePath())
            .withClientKeyFile(clientkey.getAbsolutePath())
            .build();
  }
 
Example #11
Source File: ResolveServicesByLabelsTooManyNOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #12
Source File: ResolveServicesByLabelsOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #13
Source File: IstioExecutor.java    From istio-apim with Apache License 2.0 6 votes vote down vote up
/**
 * Build the config for the client
 */
private Config buildConfig() throws APIManagementException {

    System.setProperty(TRY_KUBE_CONFIG, "false");
    System.setProperty(TRY_SERVICE_ACCOUNT, "true");
    ConfigBuilder configBuilder;

    configBuilder = new ConfigBuilder().withMasterUrl(kubernetesAPIServerUrl);

    if (!StringUtils.isEmpty(saTokenFileName)) {
        String token;
        String tokenFile = saTokenFilePath + "/" + saTokenFileName;
        try {
            token = FileUtil.readFileToString(tokenFile);
        } catch (IOException e) {
            throw new APIManagementException("Error while reading the SA Token FIle " + tokenFile);
        }
        configBuilder.withOauthToken(token);
    }

    return configBuilder.build();
}
 
Example #14
Source File: PortForwardExample.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static void main(String args[]) {
  String master = "https://localhost:8443";

  Config config = new ConfigBuilder().withMasterUrl(master).build();
  try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
    String namespace = "default";
    log("namespace", namespace);
    Pod pod = client.pods().inNamespace(namespace).load(PortForwardExample.class.getResourceAsStream("/portforward-example-pod.yml")).get();
    client.pods().inNamespace(namespace).create(pod);
    log("Pod created");

    int containerPort =  pod.getSpec().getContainers().get(0).getPorts().get(0).getContainerPort();
    client.pods().inNamespace(namespace).withName("testpod").waitUntilReady(10, TimeUnit.SECONDS);

    LocalPortForward portForward = client.pods().inNamespace("default").withName("testpod").portForward(containerPort, 8080);
    log("Port forwarded for 60 seconds at http://127.0.0.1:" + portForward.getLocalPort());

    log("Checking forwarded port:-");
    Response response =  new OkHttpClient().newCall(new Request.Builder().get().url("http://127.0.0.1:" + portForward.getLocalPort()).build()).execute();
    log(response.body().string());
    Thread.sleep(60 * 1000);
  } catch (Exception e) {
    log("Exception occurred: ", e.getMessage());
    e.printStackTrace();
  }
}
 
Example #15
Source File: NewProjectExamples.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
  String master = "https://localhost:8443/";
  if (args.length == 1) {
    master = args[0];
  }

  Config config = new ConfigBuilder().withMasterUrl(master).build();

  try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
    ProjectRequest request = null;
    try {
      request = client.projectrequests().createNew().withNewMetadata().withName("thisisatest").endMetadata().withDescription("Jimmi").withDisplayName("Jimmi").done();
    } finally {
      if (request != null) {
        client.projects().withName(request.getMetadata().getName()).delete();
      }
    }
  }
}
 
Example #16
Source File: OpenShiftVersionExample.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
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();

  try(final OpenShiftClient client = new DefaultOpenShiftClient(config)) {
    VersionInfo versionInfo = client.getVersion();

    log("Version details of this OpenShift cluster :-");
    log("Major        : ", versionInfo.getMajor());
    log("Minor        : ", versionInfo.getMinor());
    log("GitVersion   : ", versionInfo.getGitVersion());
    log("BuildDate    : ", versionInfo.getBuildDate());
    log("GitTreeState : ", versionInfo.getGitTreeState());
    log("Platform     : ", versionInfo.getPlatform());
    log("GitVersion   : ", versionInfo.getGitVersion());
    log("GoVersion    : ", versionInfo.getGoVersion());
    log("GitCommit    : ", versionInfo.getGitCommit());
  }
}
 
Example #17
Source File: RawCustomResourceOperationsImplTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() throws IOException {
  this.mockClient = Mockito.mock(OkHttpClient.class, Mockito.RETURNS_DEEP_STUBS);
  this.config = new ConfigBuilder().withMasterUrl("https://localhost:8443/").build();
  this.customResourceDefinitionContext = new CustomResourceDefinitionContext.Builder()
    .withGroup("test.fabric8.io")
    .withName("hellos.test.fabric8.io")
    .withPlural("hellos")
    .withScope("Namespaced")
    .withVersion("v1alpha1")
    .build();

  Call mockCall = mock(Call.class);
  mockSuccessResponse = mockResponse(HttpURLConnection.HTTP_OK);
  when(mockCall.execute())
    .thenReturn(mockSuccessResponse);
  when(mockClient.newCall(any())).thenReturn(mockCall);
}
 
Example #18
Source File: SSLUtils.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #19
Source File: K8sNetworkingUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains workable kubernetes client.
 *
 * @param config kubernetes API config
 * @return kubernetes client
 */
public static KubernetesClient k8sClient(K8sApiConfig config) {
    if (config == null) {
        log.warn("Kubernetes API server config is empty.");
        return null;
    }

    String endpoint = endpoint(config);

    ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(endpoint);

    if (config.scheme() == K8sApiConfig.Scheme.HTTPS) {
        configBuilder.withTrustCerts(true)
                .withOauthToken(config.token())
                .withCaCertData(config.caCertData())
                .withClientCertData(config.clientCertData())
                .withClientKeyData(config.clientKeyData());
    }

    return new DefaultKubernetesClient(configBuilder.build());
}
 
Example #20
Source File: MasterProtocolTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithSSL() throws IOException {
    KubernetesMockServer sslServer = new KubernetesMockServer();
    sslServer.init();

    String host = sslServer.getHostName();
    Integer port = sslServer.getPort();
    Config config = new ConfigBuilder()
            .withMasterUrl(host + ":" +port)
            .withTlsVersions(TLS_1_0)
            .withTrustCerts(true)
            .build();
    assertTrue(config.getMasterUrl().toLowerCase(Locale.ROOT).startsWith(Config.HTTPS_PROTOCOL_PREFIX));

    sslServer.destroy();
}
 
Example #21
Source File: ResolveServicesByLabelWithConfigOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #22
Source File: ResolveServicesByLabelsConfigOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #23
Source File: ExecPipesExample.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, IOException {
    String master = "https://localhost:8443/";
    String podName = null;

    if (args.length == 2) {
        master = args[0];
        podName = args[1];
    }
    if (args.length == 1) {
        podName = args[0];
    }

    Config config = new ConfigBuilder().withMasterUrl(master).build();
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    try (
            KubernetesClient client = new DefaultKubernetesClient(config);
            ExecWatch watch = client.pods().withName(podName)
                    .redirectingInput()
                    .redirectingOutput()
                    .redirectingError()
                    .redirectingErrorChannel()
                    .exec();
            InputStreamPumper pump = new InputStreamPumper(watch.getOutput(), new SystemOutCallback()))
    {

        executorService.submit(pump);
        watch.getInput().write("ls -al\n".getBytes());
        Thread.sleep(5 * 1000);
    } catch (Exception e) {
        throw KubernetesClientException.launderThrowable(e);
    } finally {
        executorService.shutdownNow();
    }
}
 
Example #24
Source File: MySQLSchemaOperator.java    From java-operator-sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    log.info("MySQL Schema Operator starting");

    Config config = new ConfigBuilder().withNamespace(null).build();
    KubernetesClient client = new DefaultKubernetesClient(config);
    Operator operator = new Operator(client);
    operator.registerControllerForAllNamespaces(new SchemaController(client));

    new FtBasic(
            new TkFork(new FkRegex("/health", "ALL GOOD!")), 8080
    ).start(Exit.NEVER);
}
 
Example #25
Source File: CredentialsExample.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    String master = "https://localhost:8443/";
    if (args.length == 1) {
        master = args[0];
    }

    Config config = new ConfigBuilder().withMasterUrl(master)
      .withTrustCerts(true)
      .withUsername("admin")
      .withPassword("admin")
      .withNamespace("default")
      .build();
    try (final KubernetesClient client = new AutoAdaptableKubernetesClient(config)) {

      log("Received pods", client.pods().list());

    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);


        Throwable[] suppressed = e.getSuppressed();
        if (suppressed != null) {
            for (Throwable t : suppressed) {
                logger.error(t.getMessage(), t);
            }
        }
    }
}
 
Example #26
Source File: ReplaceExamples.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
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();
  try (KubernetesClient client = new DefaultKubernetesClient(config)) {
    try {
      log("Create namespace:", client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName("thisisatest").endMetadata().build()));

      Pod createdPod = client.pods().inNamespace("thisisatest").createNew()
        .withNewMetadata()
        .withName("testpod")
        .addToLabels("server", "nginx")
        .endMetadata()
        .withNewSpec()
        .addNewContainer().withName("nginx").withImage("nginx")
        .addNewPort().withContainerPort(80).endPort()
        .endContainer()
        .endSpec().done();
      log("Created testPod:", createdPod);

      Pod updatedPod = client.pods().inNamespace("thisisatest").withName("testpod").edit()
        .editMetadata()
        .addToLabels("server2", "nginx2")
        .and().done();
      log("Replaced testPod:", updatedPod);

    } catch (KubernetesClientException e) {
      logger.error(e.getMessage(), e);
    } finally {
      client.namespaces().withName("thisisatest").delete();
    }
  }
}
 
Example #27
Source File: HttpServerTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testListWithTrustCerts() {
 server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().build()).once();
  //We override the config to create a client that doesn't trust all certs.
  Config override = new ConfigBuilder(server.getClient().getConfiguration()).build();

  KubernetesClient client = new DefaultKubernetesClient(override);
  client.pods().list();
}
 
Example #28
Source File: MasterProtocolTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithoutSSL() throws IOException {
    KubernetesMockServer plainServer = new KubernetesMockServer(false);
    plainServer.init();
    String host = plainServer.getHostName();
    Integer port = plainServer.getPort();
    Config config = new ConfigBuilder()
            .withMasterUrl(host + ":" +port)
            .build();
    assertTrue(config.getMasterUrl().toLowerCase(Locale.ROOT).startsWith(Config.HTTP_PROTOCOL_PREFIX));
    plainServer.destroy();
}
 
Example #29
Source File: KubernetesMockServer.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public NamespacedKubernetesClient createClient() {
    Config config = new ConfigBuilder()
            .withMasterUrl(url("/"))
            .withTrustCerts(true)
            .withTlsVersions(TLS_1_0)
            .withNamespace("test")
            .build();
    return new DefaultKubernetesClient(createHttpClientForMockServer(config), config);
}
 
Example #30
Source File: KubernetesClientUtils.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static Config createConfig(KubernetesClientBuildConfig buildConfig) {
    Config base = new Config();
    return new ConfigBuilder()
            .withTrustCerts(buildConfig.trustCerts)
            .withWatchReconnectInterval((int) buildConfig.watchReconnectInterval.toMillis())
            .withWatchReconnectLimit(buildConfig.watchReconnectLimit)
            .withConnectionTimeout((int) buildConfig.connectionTimeout.toMillis())
            .withRequestTimeout((int) buildConfig.requestTimeout.toMillis())
            .withRollingTimeout(buildConfig.rollingTimeout.toMillis())
            .withMasterUrl(buildConfig.masterUrl.orElse(base.getMasterUrl()))
            .withNamespace(buildConfig.namespace.orElse(base.getNamespace()))
            .withUsername(buildConfig.username.orElse(base.getUsername()))
            .withPassword(buildConfig.password.orElse(base.getPassword()))
            .withCaCertFile(buildConfig.caCertFile.orElse(base.getCaCertFile()))
            .withCaCertData(buildConfig.caCertData.orElse(base.getCaCertData()))
            .withClientCertFile(buildConfig.clientCertFile.orElse(base.getClientCertFile()))
            .withClientCertData(buildConfig.clientCertData.orElse(base.getClientCertData()))
            .withClientKeyFile(buildConfig.clientKeyFile.orElse(base.getClientKeyFile()))
            .withClientKeyData(buildConfig.clientKeyData.orElse(base.getClientKeyData()))
            .withClientKeyPassphrase(buildConfig.clientKeyPassphrase.orElse(base.getClientKeyPassphrase()))
            .withClientKeyAlgo(buildConfig.clientKeyAlgo.orElse(base.getClientKeyAlgo()))
            .withHttpProxy(buildConfig.httpProxy.orElse(base.getHttpProxy()))
            .withHttpsProxy(buildConfig.httpsProxy.orElse(base.getHttpsProxy()))
            .withProxyUsername(buildConfig.proxyUsername.orElse(base.getProxyUsername()))
            .withProxyPassword(buildConfig.proxyPassword.orElse(base.getProxyPassword()))
            .withNoProxy(buildConfig.noProxy.isPresent() ? buildConfig.noProxy.get() : base.getNoProxy())
            .build();
}