Skip to content

Commit

Permalink
fix: do not modify deployment as it is live and reused elsewhere
Browse files Browse the repository at this point in the history
Fixes #855

Signed-off-by: Chris Laprun <claprun@redhat.com>
  • Loading branch information
metacosm committed Mar 21, 2024
1 parent 91a3e6b commit 341f162
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public DeserializedKubernetesResourcesBuildItem(List<HasMetadata> resources) {
this.resources = resources;
}

/**
* Note that these resources are "live" so any modification made to them will be propagated anywhere this method is called
* and a reference on the result is kept so if you don't want local changes to be propagated, make a copy of the resources
* you interact with, first.
*/
public List<HasMetadata> getResources() {
return resources;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.quarkus.kubernetes.deployment.ResourceNameUtil;
import io.quarkus.qute.Qute;

// Note that steps of this processor won't be run in dev mode because ArtifactResultBuildItems are only considered in NORMAL mode
@BuildSteps(onlyIf = HelmGenerationEnabled.class)
public class HelmChartProcessor {

Expand Down Expand Up @@ -154,7 +155,7 @@ private List<HasMetadata> filterOutStandardResources(List<HasMetadata> resources
return !r.getMetadata().getName().equals(operatorName);
}
return true;
}).collect(Collectors.toList());
}).toList();
}

@BuildStep
Expand All @@ -170,13 +171,16 @@ void addGeneratedDeployment(HelmTargetDirectoryBuildItem helmDirBI,
ControllerConfigurationsBuildItem controllerConfigurations,
ApplicationInfoBuildItem appInfo) {
// add an env var for each reconciler's watch namespace in the operator's deployment
final var deployment = (Deployment) deserializedKubernetesResources.getResources().stream()
var deployment = (Deployment) deserializedKubernetesResources.getResources().stream()
.filter(Deployment.class::isInstance).findFirst()
.orElseThrow();
final var envs = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv();
// 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) -> envs.add(new EnvVar(ConfigurationUtils.getNamespacesPropertyName(name, true),
"{watchNamespaces}", null)));
.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);
Expand All @@ -194,7 +198,7 @@ void addGeneratedDeployment(HelmTargetDirectoryBuildItem helmDirBI,
private void addCRDs(HelmTargetDirectoryBuildItem helmDirBI, GeneratedCRDInfoBuildItem generatedCRDInfoBuildItem) {
var crdInfos = generatedCRDInfoBuildItem.getCRDGenerationInfo().getCrds().values().stream()
.flatMap(m -> m.values().stream())
.collect(Collectors.toList());
.toList();

final var crdDir = helmDirBI.getPathToHelmDir().resolve(CRD_DIR);
crdInfos.forEach(crdInfo -> {
Expand Down

0 comments on commit 341f162

Please sign in to comment.