Skip to content

#5945: Gradle plugin for generating the CRDs from generated classes #6910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 234 additions & 0 deletions crd-generator/gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) 2015 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<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>

<parent>
<artifactId>crd-generator-parent</artifactId>
<groupId>io.fabric8</groupId>
<version>7.2-SNAPSHOT</version>
</parent>

<!--
GAV has to be compliant with plugin id
https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_markers
${plugin.id}:${plugin.id}.gradle.plugin:${version}
-->
<groupId>io.fabric8.crd-generator</groupId>
<artifactId>io.fabric8.crd-generator.gradle.plugin</artifactId>
<name>Fabric8 :: CRD generator :: Gradle Plugin</name>

<packaging>jar</packaging>

<repositories>
<repository>
<id>maven-central</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>repo.gradle.org</id>
<url>https://repo.gradle.org/gradle/libs-releases-local/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>ext.repo.gradle.org</id>
<url>https://repo.gradle.org/gradle/ext-releases-local/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-api.version}</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-all</artifactId>
<version>${gradle-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-api-v2</artifactId>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-collector</artifactId>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
<version>33.2.0-jre</version>
</dependency>
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>${gradle.kotlin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<properties>
<gradle.kotlin.version>1.9.10</gradle.kotlin.version>
</properties>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${gradle.kotlin.version}</version>
<executions>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/kotlin</source>
<source>src/it/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/test/kotlin</source>
<source>${project.basedir}/src/it/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.marcnuri.plugins</groupId>
<artifactId>gradle-api-maven-plugin</artifactId>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<testSourceDirectory>${project.basedir}/src/it/kotlin</testSourceDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemProperties>
<property>
<name>fabric8-client.version</name>
<value>${project.version}</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.fabric8.crd.generator.gradle.plugin

import io.kotest.matchers.collections.containAll
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.Test

class CrdGeneratorPluginIntegrationTest : GradlePluginIntegrationTest() {
override val projectFiles =
mapOf(
"settings.gradle.kts" to "/",
"gradle.properties" to "/",
"src/integrationTest/kotlin/io/fabric8/crd/generator/example/GreetingResource.kt" to
"/src/main/kotlin/io/fabric8/crd/generator/example",
"src/functionalTest/resources/base/build.gradle.kts" to
"/",
)

@Test
fun `it should compile and generate from sources`() {
// WHEN: running task
val result =
GradleRunner
.create()
.withProjectDir(projectDir)
.withArguments("generateCRDs")
.withPluginClasspath()
.build()
// THEN: kotlin sources have been build
result.tasks.find { it.path == ":compileKotlin" }?.outcome shouldBe TaskOutcome.SUCCESS
// AND: main crds have been generated
result.tasks.find { it.path == ":generateCRDsMain" }?.outcome shouldBe TaskOutcome.SUCCESS
// AND: generated file exists in the correct directory
buildDirOf("crds/main").list()!!.toList() should containAll("greetings.example.com-v1.yml")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.fabric8.crd.generator.gradle.plugin

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.io.TempDir
import java.io.File

abstract class GradlePluginIntegrationTest {
@TempDir
lateinit var projectDir: File

protected abstract val projectFiles: Map<String, String>

protected val buildDir by lazy { File(projectDir, "build") }

fun buildDirOf(subDir: String) = File(buildDir, subDir)

private fun File.copyToProjectDir(targetDir: String) {
File(projectDir, targetDir.trimStart('/') + name).let { targetFile ->
if (!exists()) {
throw IllegalArgumentException("source '$absolutePath' does not exist")
}
targetFile.parentFile.mkdirs()
if (isDirectory) {
copyRecursively(targetFile)
} else {
copyTo(targetFile)
}
}
}

@BeforeEach
fun prepareProjectDir() {
projectFiles.forEach { File(it.key).copyToProjectDir(it.value) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.fabric8.crd.generator.gradle.plugin

import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional

/**
* Configuration used by the [CrdGeneratorTask].
*/
abstract class CrdGeneratorConfig {
/**
* Directive configuring the collection of the classes
*/
@get:Nested
abstract val collect: CollectDirective

/**
* Directive configuring the emission of the CRDs.
*/
@get:Nested
abstract val emit: EmitDirective

abstract class CollectDirective {
/**
* enforce the creation of a jandex index, even if one is found.
*/
@get:Input
@get:Optional
abstract val forceIndex: Property<Boolean>

/**
* names of the classes to be scanned
*/
@get:Input
@get:Optional
val classes = mutableSetOf<String>()

/**
* package filter for the classes
*/
@get:Nested
@get:Optional
abstract val packages: Packages

abstract class Packages {
/**
* include the provided packages by name.
*/
@get:Input
val include = mutableSetOf<String>()

/**
* exclude the provided packages by name.
*/
@get:Input
val exclude = mutableSetOf<String>()
}
}

abstract class EmitDirective {
/**
* if to parallelize the generation of the CRDs
*/
@get:Input
@get:Optional
abstract val parallel: Property<Boolean>

/**
* apiVersions of the CRDs to be generated
*/
@get:Input
@get:Optional
val crdVersions = mutableSetOf<String>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.fabric8.crd.generator.gradle.plugin

import org.gradle.api.tasks.Internal

/**
* Extension object decorating [CrdGeneratorConfig].
*/
abstract class CrdGeneratorExtension(
@Internal val generatorTask: CrdGeneratorTask,
) : CrdGeneratorConfig()
Loading