|
10 | 10 | import java.nio.file.Path; |
11 | 11 | import java.util.List; |
12 | 12 | import java.util.Map; |
| 13 | +import java.util.Optional; |
13 | 14 | import java.util.stream.Collectors; |
14 | 15 |
|
15 | 16 | import org.jboss.logging.Logger; |
@@ -156,21 +157,24 @@ void addClusterRolesForReconcilers(HelmTargetDirectoryBuildItem helmTargetDirect |
156 | 157 |
|
157 | 158 | @BuildStep |
158 | 159 | @Produce(ArtifactResultBuildItem.class) |
159 | | - void addExplicitlyAddedKubernetesResources(DeserializedKubernetesResourcesBuildItem generatedKubernetesResources, |
| 160 | + void addExplicitlyAddedKubernetesResources( |
| 161 | + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<DeserializedKubernetesResourcesBuildItem> maybedGeneratedKubeRes, |
160 | 162 | HelmTargetDirectoryBuildItem helmDirBI, |
161 | 163 | ApplicationInfoBuildItem appInfo, KubernetesConfig kubernetesConfig) { |
162 | | - var resources = generatedKubernetesResources.getResources(); |
163 | | - resources = filterOutStandardResources(resources, ResourceNameUtil.getResourceName(kubernetesConfig, appInfo)); |
164 | | - if (!resources.isEmpty()) { |
165 | | - final var kubernetesManifest = helmDirBI.getPathToTemplatesDir().resolve("kubernetes.yml"); |
166 | | - // Generate a possibly multi-document YAML |
167 | | - String yaml = resources.stream().map(FileUtils::asYaml).collect(Collectors.joining()); |
168 | | - try { |
169 | | - Files.writeString(kubernetesManifest, yaml); |
170 | | - } catch (IOException e) { |
171 | | - throw new IllegalStateException(e); |
| 164 | + maybedGeneratedKubeRes.ifPresent(generatedKubernetesResources -> { |
| 165 | + var resources = generatedKubernetesResources.getResources(); |
| 166 | + resources = filterOutStandardResources(resources, ResourceNameUtil.getResourceName(kubernetesConfig, appInfo)); |
| 167 | + if (!resources.isEmpty()) { |
| 168 | + final var kubernetesManifest = helmDirBI.getPathToTemplatesDir().resolve("kubernetes.yml"); |
| 169 | + // Generate a possibly multi-document YAML |
| 170 | + String yaml = resources.stream().map(FileUtils::asYaml).collect(Collectors.joining()); |
| 171 | + try { |
| 172 | + Files.writeString(kubernetesManifest, yaml); |
| 173 | + } catch (IOException e) { |
| 174 | + throw new IllegalStateException(e); |
| 175 | + } |
172 | 176 | } |
173 | | - } |
| 177 | + }); |
174 | 178 | } |
175 | 179 |
|
176 | 180 | private List<HasMetadata> filterOutStandardResources(List<HasMetadata> resources, String operatorName) { |
@@ -202,29 +206,33 @@ private void addTemplateFiles(HelmTargetDirectoryBuildItem helmDirBI) { |
202 | 206 | @BuildStep |
203 | 207 | @Produce(ArtifactResultBuildItem.class) |
204 | 208 | void addGeneratedDeployment(HelmTargetDirectoryBuildItem helmDirBI, |
205 | | - DeserializedKubernetesResourcesBuildItem deserializedKubernetesResources, |
| 209 | + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<DeserializedKubernetesResourcesBuildItem> maybeDeserializedKubeResources, |
206 | 210 | ControllerConfigurationsBuildItem controllerConfigurations, |
207 | 211 | ApplicationInfoBuildItem appInfo) { |
208 | | - // add an env var for each reconciler's watch namespace in the operator's deployment |
209 | | - var deployment = (Deployment) deserializedKubernetesResources.getResources().stream() |
210 | | - .filter(Deployment.class::isInstance).findFirst() |
211 | | - .orElseThrow(); |
212 | | - // copy the deployment so that changes are not propagated outside of this method |
213 | | - final var firstContainer = deployment.edit().editSpec().editTemplate().editSpec().editFirstContainer(); |
214 | | - controllerConfigurations.getControllerConfigs() |
215 | | - .forEach((name, unused) -> firstContainer.addNewEnv() |
216 | | - .withName(ConfigurationUtils.getNamespacesPropertyName(name, true)) |
217 | | - .withValue("{watchNamespaces}").endEnv()); |
218 | | - deployment = firstContainer.endContainer().endSpec().endTemplate().endSpec().build(); |
| 212 | + if (maybeDeserializedKubeResources.isEmpty()) { |
| 213 | + log.warn("No Kubernetes manifests were found, no Helm chart will be generated"); |
| 214 | + } else { |
| 215 | + // add an env var for each reconciler's watch namespace in the operator's deployment |
| 216 | + var deployment = (Deployment) maybeDeserializedKubeResources.get().getResources().stream() |
| 217 | + .filter(Deployment.class::isInstance).findFirst() |
| 218 | + .orElseThrow(); |
| 219 | + // copy the deployment so that changes are not propagated outside of this method |
| 220 | + final var firstContainer = deployment.edit().editSpec().editTemplate().editSpec().editFirstContainer(); |
| 221 | + controllerConfigurations.getControllerConfigs() |
| 222 | + .forEach((name, unused) -> firstContainer.addNewEnv() |
| 223 | + .withName(ConfigurationUtils.getNamespacesPropertyName(name, true)) |
| 224 | + .withValue("{watchNamespaces}").endEnv()); |
| 225 | + deployment = firstContainer.endContainer().endSpec().endTemplate().endSpec().build(); |
219 | 226 |
|
220 | | - // a bit hacky solution to get the exact placeholder without brackets |
221 | | - final var template = FileUtils.asYaml(deployment); |
222 | | - var res = template.replace("\"{watchNamespaces}\"", "{{ .Values.watchNamespaces }}"); |
223 | | - res = res.replaceAll(appInfo.getVersion(), "{{ .Chart.AppVersion }}"); |
224 | | - try { |
225 | | - Files.writeString(helmDirBI.getPathToTemplatesDir().resolve("deployment.yaml"), res); |
226 | | - } catch (IOException e) { |
227 | | - throw new IllegalStateException(e); |
| 227 | + // a bit hacky solution to get the exact placeholder without brackets |
| 228 | + final var template = FileUtils.asYaml(deployment); |
| 229 | + var res = template.replace("\"{watchNamespaces}\"", "{{ .Values.watchNamespaces }}"); |
| 230 | + res = res.replaceAll(appInfo.getVersion(), "{{ .Chart.AppVersion }}"); |
| 231 | + try { |
| 232 | + Files.writeString(helmDirBI.getPathToTemplatesDir().resolve("deployment.yaml"), res); |
| 233 | + } catch (IOException e) { |
| 234 | + throw new IllegalStateException(e); |
| 235 | + } |
228 | 236 | } |
229 | 237 | } |
230 | 238 |
|
|
0 commit comments