Skip to content

Commit

Permalink
fix: inject DeserializedKubernetesResourcesBuildItem optionally
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Laprun <claprun@redhat.com>
  • Loading branch information
metacosm committed Aug 12, 2024
1 parent 01fe99c commit 2095958
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import io.quarkus.deployment.builditem.GeneratedFileSystemResourceBuildItem;
import io.quarkus.deployment.pkg.builditem.JarBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.kubernetes.deployment.KubernetesCommonHelper;
import io.quarkus.kubernetes.deployment.KubernetesConfig;
import io.quarkus.kubernetes.deployment.ResourceNameUtil;

Expand Down Expand Up @@ -136,37 +135,6 @@ CSVMetadataBuildItem gatherCSVMetadata(KubernetesConfig kubernetesConfig,
return new CSVMetadataBuildItem(csvGroups);
}

private static String getDefaultProviderURLFromSCMInfo(ApplicationInfoBuildItem appConfiguration,
JarBuildItem jarBuildItem) {
final var maybeProject = KubernetesCommonHelper.createProject(appConfiguration, Optional.empty(),
jarBuildItem.getPath());
return maybeProject.map(project -> {
final var scmInfo = project.getScmInfo();
if (scmInfo != null) {
var origin = scmInfo.getRemote().get("origin");
if (origin != null) {
try {
int atSign = origin.indexOf('@');
if (atSign > 0) {
origin = origin.substring(atSign + 1);
origin = origin.replaceFirst(":", "/");
origin = "https://" + origin;
}

int dotGit = origin.indexOf(".git");
if (dotGit > 0 && dotGit < origin.length() - 1) {
origin = origin.substring(0, dotGit);
}
return origin;
} catch (Exception e) {
log.warnv("Couldn't parse SCM information: {0}", origin);
}
}
}
return null;
}).orElse(null);
}

private static ReconcilerAugmentedClassInfo augmentReconcilerInfo(
ReconcilerAugmentedClassInfo reconcilerInfo) {
// if primary resource is a CR, check if it is annotated with CSVMetadata and augment it if it is
Expand Down Expand Up @@ -224,7 +192,7 @@ void generateBundle(ApplicationInfoBuildItem configuration,
VersionBuildItem versionBuildItem,
BuildProducer<GeneratedBundleBuildItem> doneGeneratingCSV,
GeneratedCRDInfoBuildItem generatedCustomResourcesDefinitions,
DeserializedKubernetesResourcesBuildItem generatedKubernetesResources,
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<DeserializedKubernetesResourcesBuildItem> maybeGeneratedKubeResources,
BuildProducer<GeneratedFileSystemResourceBuildItem> generatedCSVs) {
final var crds = generatedCustomResourcesDefinitions.getCRDGenerationInfo().getCrds()
.values().stream()
Expand All @@ -238,36 +206,38 @@ void generateBundle(ApplicationInfoBuildItem configuration,
final var roles = new LinkedList<Role>();
final var deployments = new LinkedList<Deployment>();

final var resources = generatedKubernetesResources.getResources();
resources.forEach(r -> {
if (r instanceof ServiceAccount) {
serviceAccounts.add((ServiceAccount) r);
return;
}
maybeGeneratedKubeResources.ifPresent(generatedKubeResources -> {
final var resources = generatedKubeResources.getResources();
resources.forEach(r -> {
if (r instanceof ServiceAccount) {
serviceAccounts.add((ServiceAccount) r);
return;
}

if (r instanceof ClusterRoleBinding) {
clusterRoleBindings.add((ClusterRoleBinding) r);
return;
}
if (r instanceof ClusterRoleBinding) {
clusterRoleBindings.add((ClusterRoleBinding) r);
return;
}

if (r instanceof ClusterRole) {
clusterRoles.add((ClusterRole) r);
return;
}
if (r instanceof ClusterRole) {
clusterRoles.add((ClusterRole) r);
return;
}

if (r instanceof RoleBinding) {
roleBindings.add((RoleBinding) r);
return;
}
if (r instanceof RoleBinding) {
roleBindings.add((RoleBinding) r);
return;
}

if (r instanceof Role) {
roles.add((Role) r);
return;
}
if (r instanceof Role) {
roles.add((Role) r);
return;
}

if (r instanceof Deployment) {
deployments.add((Deployment) r);
}
if (r instanceof Deployment) {
deployments.add((Deployment) r);
}
});
});

final var deploymentName = ResourceNameUtil.getResourceName(kubernetesConfig, configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;
Expand Down Expand Up @@ -156,21 +157,24 @@ void addClusterRolesForReconcilers(HelmTargetDirectoryBuildItem helmTargetDirect

@BuildStep
@Produce(ArtifactResultBuildItem.class)
void addExplicitlyAddedKubernetesResources(DeserializedKubernetesResourcesBuildItem generatedKubernetesResources,
void addExplicitlyAddedKubernetesResources(
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<DeserializedKubernetesResourcesBuildItem> maybedGeneratedKubeRes,
HelmTargetDirectoryBuildItem helmDirBI,
ApplicationInfoBuildItem appInfo, KubernetesConfig kubernetesConfig) {
var resources = generatedKubernetesResources.getResources();
resources = filterOutStandardResources(resources, ResourceNameUtil.getResourceName(kubernetesConfig, appInfo));
if (!resources.isEmpty()) {
final var kubernetesManifest = helmDirBI.getPathToTemplatesDir().resolve("kubernetes.yml");
// Generate a possibly multi-document YAML
String yaml = resources.stream().map(FileUtils::asYaml).collect(Collectors.joining());
try {
Files.writeString(kubernetesManifest, yaml);
} catch (IOException e) {
throw new IllegalStateException(e);
maybedGeneratedKubeRes.ifPresent(generatedKubernetesResources -> {
var resources = generatedKubernetesResources.getResources();
resources = filterOutStandardResources(resources, ResourceNameUtil.getResourceName(kubernetesConfig, appInfo));
if (!resources.isEmpty()) {
final var kubernetesManifest = helmDirBI.getPathToTemplatesDir().resolve("kubernetes.yml");
// Generate a possibly multi-document YAML
String yaml = resources.stream().map(FileUtils::asYaml).collect(Collectors.joining());
try {
Files.writeString(kubernetesManifest, yaml);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
});
}

private List<HasMetadata> filterOutStandardResources(List<HasMetadata> resources, String operatorName) {
Expand Down Expand Up @@ -202,29 +206,33 @@ private void addTemplateFiles(HelmTargetDirectoryBuildItem helmDirBI) {
@BuildStep
@Produce(ArtifactResultBuildItem.class)
void addGeneratedDeployment(HelmTargetDirectoryBuildItem helmDirBI,
DeserializedKubernetesResourcesBuildItem deserializedKubernetesResources,
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<DeserializedKubernetesResourcesBuildItem> maybeDeserializedKubeResources,
ControllerConfigurationsBuildItem controllerConfigurations,
ApplicationInfoBuildItem appInfo) {
// add an env var for each reconciler's watch namespace in the operator's deployment
var deployment = (Deployment) deserializedKubernetesResources.getResources().stream()
.filter(Deployment.class::isInstance).findFirst()
.orElseThrow();
// copy the deployment so that changes are not propagated outside of this method
final var firstContainer = deployment.edit().editSpec().editTemplate().editSpec().editFirstContainer();
controllerConfigurations.getControllerConfigs()
.forEach((name, unused) -> firstContainer.addNewEnv()
.withName(ConfigurationUtils.getNamespacesPropertyName(name, true))
.withValue("{watchNamespaces}").endEnv());
deployment = firstContainer.endContainer().endSpec().endTemplate().endSpec().build();
if (maybeDeserializedKubeResources.isEmpty()) {
log.warn("No Kubernetes manifests were found, no Helm chart will be generated");
} else {
// add an env var for each reconciler's watch namespace in the operator's deployment
var deployment = (Deployment) maybeDeserializedKubeResources.get().getResources().stream()
.filter(Deployment.class::isInstance).findFirst()
.orElseThrow();
// copy the deployment so that changes are not propagated outside of this method
final var firstContainer = deployment.edit().editSpec().editTemplate().editSpec().editFirstContainer();
controllerConfigurations.getControllerConfigs()
.forEach((name, unused) -> firstContainer.addNewEnv()
.withName(ConfigurationUtils.getNamespacesPropertyName(name, true))
.withValue("{watchNamespaces}").endEnv());
deployment = firstContainer.endContainer().endSpec().endTemplate().endSpec().build();

// a bit hacky solution to get the exact placeholder without brackets
final var template = FileUtils.asYaml(deployment);
var res = template.replace("\"{watchNamespaces}\"", "{{ .Values.watchNamespaces }}");
res = res.replaceAll(appInfo.getVersion(), "{{ .Chart.AppVersion }}");
try {
Files.writeString(helmDirBI.getPathToTemplatesDir().resolve("deployment.yaml"), res);
} catch (IOException e) {
throw new IllegalStateException(e);
// a bit hacky solution to get the exact placeholder without brackets
final var template = FileUtils.asYaml(deployment);
var res = template.replace("\"{watchNamespaces}\"", "{{ .Values.watchNamespaces }}");
res = res.replaceAll(appInfo.getVersion(), "{{ .Chart.AppVersion }}");
try {
Files.writeString(helmDirBI.getPathToTemplatesDir().resolve("deployment.yaml"), res);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

Expand Down

0 comments on commit 2095958

Please sign in to comment.