Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch BundleGenerationConfiguration to ConfigMapping #920

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ private static HashSet<String> addCRDManifestBuilder(Map<String, CRDInfo> crds,

private static SortedMap<String, String> generateBundleLabels(CSVMetadataHolder csvMetadata,
BundleGenerationConfiguration bundleConfiguration, Version version) {
var packageName = bundleConfiguration.packageName.orElse(csvMetadata.bundleName);
var packageName = bundleConfiguration.packageName().orElse(csvMetadata.bundleName);

SortedMap<String, String> values = new TreeMap<>();
values.put(join(BUNDLE_PREFIX, CHANNEL, DEFAULT, ANNOTATIONS_VERSION),
bundleConfiguration.defaultChannel.orElse(bundleConfiguration.channels.get(0)));
values.put(join(BUNDLE_PREFIX, CHANNELS, ANNOTATIONS_VERSION), String.join(COMMA, bundleConfiguration.channels));
bundleConfiguration.defaultChannel().orElse(bundleConfiguration.channels().get(0)));
values.put(join(BUNDLE_PREFIX, CHANNELS, ANNOTATIONS_VERSION), String.join(COMMA, bundleConfiguration.channels()));
values.put(join(BUNDLE_PREFIX, MANIFESTS, ANNOTATIONS_VERSION), MANIFESTS + SLASH);
values.put(join(BUNDLE_PREFIX, MEDIA_TYPE, ANNOTATIONS_VERSION), REGISTRY_PLUS + ANNOTATIONS_VERSION);
values.put(join(BUNDLE_PREFIX, METADATA, ANNOTATIONS_VERSION), METADATA + SLASH);
Expand Down
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 All @@ -63,7 +62,7 @@ private static class IsGenerationEnabled implements BooleanSupplier {

@Override
public boolean getAsBoolean() {
return config.enabled;
return config.enabled();
}
}

Expand All @@ -75,16 +74,16 @@ CSVMetadataBuildItem gatherCSVMetadata(KubernetesConfig kubernetesConfig,
CombinedIndexBuildItem combinedIndexBuildItem,
JarBuildItem jarBuildItem) {
final var index = combinedIndexBuildItem.getIndex();
final var defaultName = bundleConfiguration.packageName
final var defaultName = bundleConfiguration.packageName()
.orElse(ResourceNameUtil.getResourceName(kubernetesConfig, appConfiguration));

// note that version, replaces, etc. should probably be settable at the reconciler level
// use version specified in bundle configuration, if not use the one extracted from the project, if available
final var version = kubernetesConfig.getVersion().orElse(appConfiguration.getVersion());
final var defaultVersion = bundleConfiguration.version
final var defaultVersion = bundleConfiguration.version()
.orElse(ApplicationInfoBuildItem.UNSET_VALUE.equals(version) ? null : version);

final var defaultReplaces = bundleConfiguration.replaces.orElse(null);
final var defaultReplaces = bundleConfiguration.replaces().orElse(null);

final var sharedMetadataHolders = getSharedMetadataHolders(defaultName, defaultVersion, defaultReplaces, index);
final var csvGroups = new HashMap<CSVMetadataHolder, List<ReconcilerAugmentedClassInfo>>();
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 @@ -4,49 +4,47 @@
import java.util.Optional;

import io.quarkiverse.operatorsdk.annotations.CSVMetadata;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(name = "operator-sdk.bundle")
public class BundleGenerationConfiguration {
@ConfigMapping(prefix = "quarkus.operator-sdk.bundle")
@ConfigRoot
public interface BundleGenerationConfiguration {
/**
* Whether the extension should generate the Operator bundle.
*/
@ConfigItem(defaultValue = "true")
public Boolean enabled;
@WithDefault("true")
Boolean enabled();

/**
* The list of channels that bundle belongs to. By default, it's "alpha".
*/
@ConfigItem(defaultValue = "alpha")
public List<String> channels;
@WithDefault("alpha")
List<String> channels();

/**
* The default channel for the bundle.
*/
@ConfigItem
public Optional<String> defaultChannel;
Optional<String> defaultChannel();

/**
* The name of the package that bundle belongs to.
*
* @deprecated Use {@link CSVMetadata#bundleName()} instead
*/
@Deprecated(forRemoval = true)
@ConfigItem
public Optional<String> packageName;
Optional<String> packageName();

/**
* The replaces value that should be used in the generated CSV.
*/
@ConfigItem
public Optional<String> replaces;
Optional<String> replaces();

/**
* The version value that should be used in the generated CSV instead of the automatically detected one extracted from the
* project information.
*/
@ConfigItem
public Optional<String> version;
Optional<String> version();

}
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