Skip to content

Commit

Permalink
fix: properly wait for kubernetes extension to generate manifests
Browse files Browse the repository at this point in the history
This relies on newly introduced GeneratedKubernetesResourceBuildItem
from quarkusio/quarkus#20113 which would need
to be backported.
  • Loading branch information
metacosm committed Sep 14, 2021
1 parent 3493b77 commit 69255a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<GeneratedKubernetesResourceBuildItem> 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<String, ClusterServiceVersionBuilder>(7);
info.getCrds().forEach((crdName, crdVersionToInfo) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -106,10 +106,8 @@ void updateControllerConfigurations(
@BuildStep
void generateCSV(OutputTargetBuildItem outputTarget, GeneratedCRDInfoBuildItem generatedCRDs,
BuildProducer<GeneratedCSVBuildItem> ignored,
DeploymentResultBuildItem sync
// needed to ensure that this step runs after the container image has been built
/* @SuppressWarnings("unused") List<ArtifactResultBuildItem> artifactResults */) {
CSVGenerator.generate(outputTarget, generatedCRDs.getCRDGenerationInfo());
List<GeneratedKubernetesResourceBuildItem> generatedKubernetesManifests) {
CSVGenerator.generate(outputTarget, generatedCRDs.getCRDGenerationInfo(), generatedKubernetesManifests);
}

@BuildStep
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.version>2.2.2.Final</quarkus.version>
<quarkus.version>999-SNAPSHOT</quarkus.version>
<java-operator-sdk.version>1.9.6</java-operator-sdk.version>
<kubernetes-client.version>5.7.2</kubernetes-client.version>
<kubernetes-client.version>5.7.3</kubernetes-client.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down

0 comments on commit 69255a1

Please sign in to comment.