0

I created one mini Java app project for listing all the pods, but I'm getting java.lang.NumberFormatException: null exception. Full exception:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.IllegalStateException: java.lang.NumberFormatException: null
    at io.kubernetes.client.util.ClientBuilder.setBasePath(ClientBuilder.java:261)
    at io.kubernetes.client.util.ClientBuilder.cluster(ClientBuilder.java:247)
    at Example.main(Example.java:16)
Caused by: java.lang.NumberFormatException: null
    at java.base/java.lang.Integer.parseInt(Integer.java:614)
    at java.base/java.lang.Integer.valueOf(Integer.java:983)
    at io.kubernetes.client.util.ClientBuilder.setBasePath(ClientBuilder.java:257)
    ... 2 more

This is my Java code, just one main class:

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import java.io.IOException;

public class Example {
    public static void main(String[] args) throws IOException, ApiException {
        ApiClient client = ClientBuilder.cluster().build();

        Configuration.setDefaultApiClient(client);

        CoreV1Api api = new CoreV1Api();

        V1PodList list =
                api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
        for (V1Pod item : list.getItems()) {
            System.out.println(item.getMetadata().getName());
        }
    }
}

This is my pom.xml, I added just 1 dependecy for client-java:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JavaK8sClient</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>15.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>

Any help why I'm getting this? Thanks!

Update, it's failing here, because the port and the host are null: enter image description here

Any idea?

marjan
  • 3
  • 4
  • To make sure you didn't forget, have you checked the environment variables? Do `KUBERNETES_SERVICE_HOST` & `KUBERNETES_SERVICE_PORT` exist? – w568w Aug 24 '22 at 10:18
  • I don't have them at all. – marjan Aug 24 '22 at 10:27
  • @w568w any hint how to set them, what values should they have? – marjan Aug 24 '22 at 10:37
  • Not so familiar with kubernetes, but I suppose that `ClientBuilder.cluster()` should only be used **within** a Pod, where the set of environment variables are added for you. If testing the client in the dev environment, you should instead load config file or specify remote URL manually, e.g. `ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(_yourKubeConfigPath))).build();` to connect to the cluster. – w568w Aug 24 '22 at 10:53
  • @w568w yeah, I already did what you are suggesting for local env, but I need to this for work. Anyway, thanks a lot! – marjan Aug 24 '22 at 11:11

0 Answers0