From 69255a15469791c69642f9145913eab52fdd6d8a Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Tue, 14 Sep 2021 21:14:54 +0200 Subject: [PATCH] fix: properly wait for kubernetes extension to generate manifests This relies on newly introduced GeneratedKubernetesResourceBuildItem from https://github.com/quarkusio/quarkus/pull/20113 which would need to be backported. --- .../operatorsdk/deployment/CSVGenerator.java | 67 +++++++++---------- .../deployment/OperatorSDKProcessor.java | 8 +-- pom.xml | 4 +- 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CSVGenerator.java b/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CSVGenerator.java index 925c9151..446779d9 100644 --- a/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CSVGenerator.java +++ b/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CSVGenerator.java @@ -2,12 +2,13 @@ import static io.quarkus.kubernetes.deployment.Constants.KUBERNETES; +import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Base64; import java.util.HashMap; +import java.util.List; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.ObjectMapper; @@ -16,7 +17,6 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; import io.dekorate.utils.Serialization; -import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.ServiceAccount; import io.fabric8.kubernetes.api.model.apps.Deployment; import io.fabric8.openshift.api.model.ClusterRole; @@ -25,6 +25,7 @@ import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersionBuilder; import io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo; import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem; +import io.quarkus.kubernetes.spi.GeneratedKubernetesResourceBuildItem; public class CSVGenerator { private static final ObjectMapper YAML_MAPPER; @@ -38,45 +39,43 @@ public class CSVGenerator { YAML_MAPPER.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false); } - public static void generate(OutputTargetBuildItem outputTarget, CRDGenerationInfo info) { + public static void generate(OutputTargetBuildItem outputTarget, CRDGenerationInfo info, + List generatedKubernetesManifests) { // load generated manifests final var outputDir = outputTarget.getOutputDirectory().resolve(KUBERNETES); - File manifest = outputDir.resolve("kubernetes.yml").toFile(); - final var serviceAccountName = new String[1]; final var clusterRole = new ClusterRole[1]; final var role = new Role[1]; final var deployment = new Deployment[1]; - if (manifest.exists()) { - KubernetesList manifests; - try (FileInputStream fis = new FileInputStream(manifest)) { - manifests = Serialization.unmarshalAsList(fis); - } catch (IOException e) { - throw new RuntimeException(e); - } - - manifests.getItems().forEach(i -> { - if (i instanceof ServiceAccount) { - serviceAccountName[0] = i.getMetadata().getName(); - return; - } - if (i instanceof ClusterRole) { - clusterRole[0] = (ClusterRole) i; - return; - } - - if (i instanceof Role) { - role[0] = (Role) i; - return; - } - - if (i instanceof Deployment) { - deployment[0] = (Deployment) i; - return; - } - }); - } + generatedKubernetesManifests.stream() + .filter(bi -> bi.getName().equals("kubernetes.yml")) + .findAny() + .ifPresent( + bi -> { + final var resources = Serialization.unmarshalAsList(new ByteArrayInputStream(bi.getContent())); + resources.getItems().forEach(r -> { + if (r instanceof ServiceAccount) { + serviceAccountName[0] = r.getMetadata().getName(); + return; + } + + if (r instanceof ClusterRole) { + clusterRole[0] = (ClusterRole) r; + return; + } + + if (r instanceof Role) { + role[0] = (Role) r; + return; + } + + if (r instanceof Deployment) { + deployment[0] = (Deployment) r; + return; + } + }); + }); final var controllerToCSVBuilders = new HashMap(7); info.getCrds().forEach((crdName, crdVersionToInfo) -> { diff --git a/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java b/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java index 0dd2cbc8..19677694 100644 --- a/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/OperatorSDKProcessor.java @@ -46,13 +46,13 @@ import io.quarkus.deployment.builditem.LiveReloadBuildItem; import io.quarkus.deployment.builditem.nativeimage.ForceNonWeakReflectiveClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; -import io.quarkus.deployment.pkg.builditem.DeploymentResultBuildItem; import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem; import io.quarkus.deployment.util.JandexUtil; import io.quarkus.gizmo.AssignableResultHandle; import io.quarkus.gizmo.MethodCreator; import io.quarkus.gizmo.MethodDescriptor; import io.quarkus.gizmo.ResultHandle; +import io.quarkus.kubernetes.spi.GeneratedKubernetesResourceBuildItem; import io.quarkus.runtime.QuarkusApplication; class OperatorSDKProcessor { @@ -106,10 +106,8 @@ void updateControllerConfigurations( @BuildStep void generateCSV(OutputTargetBuildItem outputTarget, GeneratedCRDInfoBuildItem generatedCRDs, BuildProducer ignored, - DeploymentResultBuildItem sync - // needed to ensure that this step runs after the container image has been built - /* @SuppressWarnings("unused") List artifactResults */) { - CSVGenerator.generate(outputTarget, generatedCRDs.getCRDGenerationInfo()); + List generatedKubernetesManifests) { + CSVGenerator.generate(outputTarget, generatedCRDs.getCRDGenerationInfo(), generatedKubernetesManifests); } @BuildStep diff --git a/pom.xml b/pom.xml index d47f7581..d2482070 100644 --- a/pom.xml +++ b/pom.xml @@ -24,9 +24,9 @@ 11 UTF-8 UTF-8 - 2.2.2.Final + 999-SNAPSHOT 1.9.6 - 5.7.2 + 5.7.3