kubernetes-client / java

Official Java client library for kubernetes
http://kubernetes.io/
Apache License 2.0
3.54k stars 1.88k forks source link

Unknown apiVersionKind with empty known kinds in Yaml #365

Closed danielfree closed 6 years ago

danielfree commented 6 years ago

pom.xml :

  <dependency>
       <groupId>io.kubernetes</groupId>
       <artifactId>client-java</artifactId>
       <version>2.0.0</version>
   </dependency>

I load a yml via

     ClassPathResource deployYml = new ClassPathResource("deploy.yml");
      byte[] bdata = FileCopyUtils.copyToByteArray(deployYml.getInputStream());
      String data = new String(bdata, StandardCharsets.UTF_8);
      AppsV1beta1Deployment myDeployment = (AppsV1beta1Deployment) Yaml.load(data);

which runs fine in IntelliJ, but after package as spring boot single jar and run with java -jar, it gives the following error:

java.io.IOException: Unknown apiVersionKind: apps/v1beta1/Deployment known kinds are: {}

and it seems like the Yaml doesn't load anything into Map<String, Class<?>> classes

danielfree commented 6 years ago

seems like something related with getTopLevelClasses() see https://github.com/google/guava/issues/2152 almost the same scenario as the next to last comment.

brendandburns commented 6 years ago

This was fixed by this PR:

https://github.com/kubernetes-client/java/pull/314

You can manually call the static method on the Yaml object to do the right class-loading, or you can try updating to the 3.0.0-beta1 library...

danielfree commented 6 years ago

@brendandburns The issue is solved after upgrading to 3.0.0-beta2, however it's only working when packaged as war and deployed in tomcat, but still not working when packaged as single jar and run as java -jar. For now I would just stick to war/tomcat. Thanks.

dennis-benzinger-hybris commented 5 years ago

@danielfree, in your Spring Boot project you probably package your dependency JARs into the main JAR of your application (JAR in JAR) and use the Spring LaunchedURLClassLoader. The problem is that Guava doesn't understand JARs in JARs.

See https://github.com/spring-projects/spring-boot/issues/5338#issuecomment-192327258 for an alternative.

dennis-benzinger-hybris commented 5 years ago

@danielfree, the easiest solution I've found is to tell Spring Boot that the io.kubernetes:client-java-api JAR has to be unpacked from the main JAR at runtime (only the JAR, not the individual .class files).

Here's an excerpt from my pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- Enable io.kubernetes:client-java to find its model classes. -->
                    <requiresUnpack>
                        <dependency>
                            <groupId>io.kubernetes</groupId>
                            <artifactId>client-java-api</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>
wangyushuai commented 4 years ago

I encountered this problem in 6.0.1, and running strpingboot app in local IDE is normal ,but run in docker envrioment is found error.

my code:

    obj = Yaml.load(readJarFile("templates/k8s/application/deployment.yaml"));

  public String readJarFile(String classPath) throws IOException {
         ClassPathResource resource = new ClassPathResource(classPath);
         byte[] binaryData = FileCopyUtils.copyToByteArray(resource.getInputStream());
         String yamlStr = new String(binaryData, StandardCharsets.UTF_8);
         log.debug("[loadTemplate]:{},requestId -> {}",yamlStr,ServiceThreadLocal.getRequestId());
         return yamlStr;
         //return resource.getInputStream();
     }

error :

java.io.IOException: Unknown apiVersionKind: apps/v1/Deployment known kinds are: {}
    at io.kubernetes.client.util.Yaml.modelMapper(Yaml.java:491)
    at io.kubernetes.client.util.Yaml.load(Yaml.java:185)
    at io.kubernetes.client.util.Yaml.load(Yaml.java:162)
    at )

@brendandburns

wangyushuai commented 4 years ago

@brendandburns The issue is solved after upgrading to 3.0.0-beta2, however it's only working when packaged as war and deployed in tomcat, but still not working when packaged as single jar and run as java -jar. For now I would just stick to war/tomcat. Thanks.

@brendandburns The issue is solved after upgrading to 3.0.0-beta2, however it's only working when packaged as war and deployed in tomcat, but still not working when packaged as single jar and run as java -jar. For now I would just stick to war/tomcat. Thanks.

@danielfree

I found this error in 6.0.1

wangyushuai commented 4 years ago

@danielfree, the easiest solution I've found is to tell Spring Boot that the io.kubernetes:client-java-api JAR has to be unpacked from the main JAR at runtime (only the JAR, not the individual .class files).

Here's an excerpt from my pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- Enable io.kubernetes:client-java to find its model classes. -->
                    <requiresUnpack>
                        <dependency>
                            <groupId>io.kubernetes</groupId>
                            <artifactId>client-java-api</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>

The solution is effective !!