eclipse-jkube / jkube

Build and Deploy java applications on Kubernetes
https://www.eclipse.dev/jkube/
Eclipse Public License 2.0
775 stars 523 forks source link

How can I use the org.eclipse.jkube.kubernetes plugin in Gradle Kotlin DSL? #3174

Open wjmwss opened 5 months ago

wjmwss commented 5 months ago

How can I use the org.eclipse.jkube.kubernetes plugin in Gradle Kotlin DSL?

rohanKanojia commented 5 months ago

@wjmwss : Hello, Could you please share more details on what you're trying to do? What configuration option do you want to use?

arman-yekkehkhani commented 5 months ago

@wjmwss Here is an article by Rohan to get started using Jkube with Gradle Get started with Gradle plugins for Eclipse JKube. You might need to make slight adjustments to build script to compensate for the difference between Groovy and Koltin DSL.

wjmwss commented 5 months ago

@wjmwss : Hello, Could you please share more details on what you're trying to do? What configuration option do you want to use?

Thank you for your reply!

I'm a beginner in gradle kotlin dsl and now I hope to use the 'org.eclipse.jkube.kubernetes' plugin to quickly deploy my spring boot project to kubernetes.

I have referred to the official documentation of this plugin, the example in the documentation are all using gradle groovy dsl, I encountered difficulties in converting it to gradle kotlin dsl: 581718955956_ pic

No matter what I try, I cannot convert the example gradle groovy dsl in the documentation to gradle kotlin dsl: 591718956264_ pic

Can you please provide a complete example of using the 'org.eclipse.jkube.kubernetes' plugin about gradle kotlin dsl?

Thank you!

wjmwss commented 5 months ago

@wjmwss Here is an article by Rohan to get started using Jkube with Gradle Get started with Gradle plugins for Eclipse JKube. You might need to make slight adjustments to build script to compensate for the difference between Groovy and Koltin DSL.

Thank you for your reply!

I'm a beginner in gradle kotlin dsl, my biggest problem currently is not knowing how to convert gradle groovy dsl to gradle kotlin dsl.

In the source code of 'KubernetesExtension', I see that many of the input parameters to the f unction are 'Closure'. 'Closure' in my understanding belongs to groovy syntax, and it seems that kotlin cannot be called? 601718956576_ pic

What can be called by kotlin is 'Action', and I see that all the functions of the 'KubernetesExtension', only the 'addImage' function use Action. WX20240621-35926 PM@2x

Therefore, I cannot determine whether the 'org.eclipse.jkube.kubernetes' plugin supports kotlin DSL, and I am not sure if my understanding is correct.

arman-yekkehkhani commented 5 months ago

@wjmwss Here is a minimal build script with similar configurations. I assume the Kotlin API may not be fully covered yet, so you can follow a general approach using builders to build your config object incrementally.

import org.eclipse.jkube.kit.common.Assembly
import org.eclipse.jkube.kit.common.AssemblyConfiguration
import org.eclipse.jkube.kit.common.AssemblyFileSet
import org.eclipse.jkube.kit.config.image.ImageConfiguration
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration

plugins {
    id("java")
    id("org.eclipse.jkube.kubernetes") version "1.16.2"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}

kubernetes {
    val assemblyConfig = AssemblyConfiguration.builder()
            .targetDir("/deployments")
            .layers(listOf(Assembly.builder()
                    .fileSets(listOf(AssemblyFileSet.builder()
                            .directory(file("${project.rootDir}/build/dependencies"))
                            .build())
                    ).build()))
            .build()

    val buildConfig = BuildConfiguration.builder()
            .from("quay.io/jkube/jkube-java:0.0.13")
            .assembly(assemblyConfig)
            .env(mapOf(
                    "JAVA_LIB_DIR" to "/deployments/dependencies/*",
                    "JAVA_MAIN_CLASS" to "org.apache.camel.cdi.Main",
            ))
            .labels(mapOf(
                    "labelWithValue" to "foo",
                    "version" to "${project.version}",
                    "artifactId" to project.name,
            ))
            .ports(listOf("8787"))
            .build()

    val image = ImageConfiguration.builder()
            .name("jkube/${project.name}:${project.version}")
            .alias("camel-service")
            .build(buildConfig)
            .build()

    images = listOf(image)
}
wjmwss commented 5 months ago

@wjmwss Here is a minimal build script with similar configurations. I assume the Kotlin API may not be fully covered yet, so you can follow a general approach using builders to build your config object incrementally.

import org.eclipse.jkube.kit.common.Assembly
import org.eclipse.jkube.kit.common.AssemblyConfiguration
import org.eclipse.jkube.kit.common.AssemblyFileSet
import org.eclipse.jkube.kit.config.image.ImageConfiguration
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration

plugins {
    id("java")
    id("org.eclipse.jkube.kubernetes") version "1.16.2"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}

kubernetes {
    val assemblyConfig = AssemblyConfiguration.builder()
            .targetDir("/deployments")
            .layers(listOf(Assembly.builder()
                    .fileSets(listOf(AssemblyFileSet.builder()
                            .directory(file("${project.rootDir}/build/dependencies"))
                            .build())
                    ).build()))
            .build()

    val buildConfig = BuildConfiguration.builder()
            .from("quay.io/jkube/jkube-java:0.0.13")
            .assembly(assemblyConfig)
            .env(mapOf(
                    "JAVA_LIB_DIR" to "/deployments/dependencies/*",
                    "JAVA_MAIN_CLASS" to "org.apache.camel.cdi.Main",
            ))
            .labels(mapOf(
                    "labelWithValue" to "foo",
                    "version" to "${project.version}",
                    "artifactId" to project.name,
            ))
            .ports(listOf("8787"))
            .build()

    val image = ImageConfiguration.builder()
            .name("jkube/${project.name}:${project.version}")
            .alias("camel-service")
            .build(buildConfig)
            .build()

    images = listOf(image)
}

Your code example is very helpful to me, thank you so much!