io.kubernetes.client.util.KubeConfig Java Examples

The following examples show how to use io.kubernetes.client.util.KubeConfig. 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: KubeConfigFileClientExample.java    From java with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) throws IOException, ApiException {

    // file path to your KubeConfig
    String kubeConfigPath = "~/.kube/config";

    // loading the out-of-cluster config, a kubeconfig from file-system
    ApiClient client =
        ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();

    // set the global default api-client to the in-cluster one from above
    Configuration.setDefaultApiClient(client);

    // the CoreV1Api loads default api-client from global configuration.
    CoreV1Api api = new CoreV1Api();

    // invokes the CoreV1Api client
    V1PodList list =
        api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
      System.out.println(item.getMetadata().getName());
    }
  }
 
Example #2
Source File: KubeOpt.java    From dew with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates a new Kube opt.
 *
 * @param log              the log
 * @param base64KubeConfig the base64 kube config
 */
protected KubeOpt(Logger log, String base64KubeConfig) {
    this.log = log;
    YamlHelper.init(log);
    try {
        client = Config.fromConfig(
                KubeConfig.loadKubeConfig(
                        new StringReader(
                                $.security.decodeBase64ToString(base64KubeConfig, "UTF-8")
                        )
                )
        );
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    client.setReadTimeout(0);
    Configuration.setDefaultApiClient(client);
    coreApi = new CoreV1Api(client);
    appsApi = new AppsV1Api(client);
    extensionsApi = new ExtensionsV1beta1Api(client);
    rbacAuthorizationApi = new RbacAuthorizationV1Api(client);
    autoscalingApi = new AutoscalingV2beta2Api(client);
    podLogs = new PodLogs(client);
}
 
Example #3
Source File: ExperimentRestApiIT.java    From submarine with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startUp() throws IOException {
  Assert.assertTrue(checkIfServerIsRunning());

  // The kube config path defined by kind-cluster-build.sh
  String confPath = System.getProperty("user.home") + "/.kube/kind-config-kind";
  KubeConfig config = KubeConfig.loadKubeConfig(new FileReader(confPath));
  ApiClient client = ClientBuilder.kubeconfig(config).build();
  Configuration.setDefaultApiClient(client);
  k8sApi = new CustomObjectsApi();

  kfOperatorMap = new HashMap<>();
  kfOperatorMap.put("tensorflow", new KfOperator("v1", "tfjobs"));
  kfOperatorMap.put("pytorch", new KfOperator("v1", "pytorchjobs"));
}
 
Example #4
Source File: KubernetesNatManager.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
protected void doStart() throws NatInitializationException {
  LOG.info("Starting kubernetes NAT manager.");
  try {

    KubeConfig.registerAuthenticator(new GCPAuthenticator());

    LOG.debug("Trying to update information using Kubernetes client SDK.");
    final ApiClient client = ClientBuilder.cluster().build();

    // set the global default api-client to the in-cluster one from above
    Configuration.setDefaultApiClient(client);

    // the CoreV1Api loads default api-client from global configuration.
    final CoreV1Api api = new CoreV1Api();
    // invokes the CoreV1Api client
    final V1Service service =
        api.listServiceForAllNamespaces(null, null, null, null, null, null, null, null, null)
            .getItems().stream()
            .filter(v1Service -> v1Service.getMetadata().getName().contains(besuPodNameFilter))
            .findFirst()
            .orElseThrow(() -> new NatInitializationException("Service not found"));
    updateUsingBesuService(service);
  } catch (Exception e) {
    throw new NatInitializationException(e.getMessage(), e);
  }
}
 
Example #5
Source File: KubeconfigAuthentication.java    From java with Apache License 2.0 5 votes vote down vote up
public KubeconfigAuthentication(final KubeConfig config) throws IOException {
  this.clientCert =
      config.getDataOrFileRelative(
          config.getClientCertificateData(), config.getClientCertificateFile());
  this.clientKey =
      config.getDataOrFileRelative(config.getClientKeyData(), config.getClientKeyFile());
  this.username = config.getUsername();
  this.password = config.getPassword();
  this.token = config.getAccessToken();
}