Skip to content

Commit

Permalink
feat: provide a CSV.spec.version value when possible (#686)
Browse files Browse the repository at this point in the history
Fixes #682
  • Loading branch information
metacosm authored Aug 17, 2023
1 parent 4fe7cfd commit bddef54
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ CSVMetadataBuildItem gatherCSVMetadata(ApplicationInfoBuildItem configuration,
CombinedIndexBuildItem combinedIndexBuildItem) {
final var index = combinedIndexBuildItem.getIndex();
final var defaultName = bundleConfiguration.packageName.orElse(configuration.getName());
final var sharedMetadataHolders = getSharedMetadataHolders(defaultName, index);
final var version = configuration.getVersion();
final var defaultVersion = ApplicationInfoBuildItem.UNSET_VALUE.equals(version) ? null : version;
final var sharedMetadataHolders = getSharedMetadataHolders(defaultName, defaultVersion, index);
final var csvGroups = new HashMap<CSVMetadataHolder, List<ReconcilerAugmentedClassInfo>>();

ClassUtils.getKnownReconcilers(index, log)
Expand Down Expand Up @@ -107,7 +109,7 @@ CSVMetadataBuildItem gatherCSVMetadata(ApplicationInfoBuildItem configuration,
}
}
csvMetadata = createMetadataHolder(csvMetadataAnnotation,
new CSVMetadataHolder(csvMetadataName, origin));
new CSVMetadataHolder(csvMetadataName, defaultVersion, origin));
}
log.infov("Assigning ''{0}'' reconciler to {1}",
reconcilerInfo.nameOrFailIfUnset(),
Expand Down Expand Up @@ -252,8 +254,8 @@ void generateBundle(ApplicationInfoBuildItem configuration,
}
}

private Map<String, CSVMetadataHolder> getSharedMetadataHolders(String name, IndexView index) {
CSVMetadataHolder csvMetadata = new CSVMetadataHolder(name, "default");
private Map<String, CSVMetadataHolder> getSharedMetadataHolders(String name, String version, IndexView index) {
CSVMetadataHolder csvMetadata = new CSVMetadataHolder(name, version, "default");
final var sharedMetadataImpls = index.getAllKnownImplementors(SHARED_CSV_METADATA);
final var result = new HashMap<String, CSVMetadataHolder>(sharedMetadataImpls.size() + 1);
sharedMetadataImpls.forEach(sharedMetadataImpl -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package io.quarkiverse.operatorsdk.bundle;

import static io.quarkiverse.operatorsdk.bundle.Utils.assertFileExistsIn;
import static io.quarkiverse.operatorsdk.bundle.Utils.checkBundleFor;
import static io.quarkiverse.operatorsdk.bundle.Utils.getCRDNameFor;
import static io.quarkiverse.operatorsdk.bundle.Utils.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.file.Files;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion;
import io.quarkiverse.operatorsdk.bundle.sources.*;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class MultipleOperatorsBundleTest {

private static final String VERSION = "test-version";
@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.setApplicationVersion(VERSION)
.withApplicationRoot((jar) -> jar
.addClasses(First.class, FirstReconciler.class,
Second.class, SecondReconciler.class,
Expand All @@ -39,13 +36,18 @@ public class MultipleOperatorsBundleTest {
public void shouldWriteBundleForTheOperators() throws IOException {
final var bundle = prodModeTestResults.getBuildDir().resolve(Utils.BUNDLE);
checkBundleFor(bundle, "first-operator", First.class);
// check that version is properly overridden
var csv = getCSVFor(bundle, "first-operator");
assertEquals(FirstReconciler.VERSION, csv.getSpec().getVersion());

checkBundleFor(bundle, "second-operator", Second.class);

checkBundleFor(bundle, "third-operator", Third.class);
// also check that external CRD is present
final var thirdManifests = bundle.resolve("third-operator").resolve("manifests");
assertFileExistsIn(thirdManifests.resolve(getCRDNameFor(External.class)), thirdManifests);
final var csvAsString = Files.readString(thirdManifests.resolve("third-operator.clusterserviceversion.yaml"));
final var csv = Serialization.unmarshal(csvAsString, ClusterServiceVersion.class);

csv = getCSVFor(bundle, "third-operator");
final var crds = csv.getSpec().getCustomresourcedefinitions();
final var thirdCRD = crds.getOwned().get(0);
assertEquals(HasMetadata.getFullResourceName(Third.class), thirdCRD.getName());
Expand All @@ -66,5 +68,7 @@ public void shouldWriteBundleForTheOperators() throws IOException {
assertEquals(">=1.0.0 <1.0.3", csv.getMetadata().getAnnotations().get("olm.skipRange"));
assertEquals("Test", csv.getMetadata().getAnnotations().get("capabilities"));
assertEquals("bar", csv.getMetadata().getAnnotations().get("foo"));
// version should be the default application's version since it's not provided for this reconciler
assertEquals(VERSION, csv.getSpec().getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.junit.jupiter.api.Assertions;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion;

public class Utils {

Expand All @@ -19,7 +21,7 @@ static void checkBundleFor(Path bundle, String operatorName,
assertFileExistsIn(operatorManifests.resolve("bundle.Dockerfile"), bundle);
final var manifests = operatorManifests.resolve("manifests");
assertFileExistsIn(manifests, bundle);
assertFileExistsIn(manifests.resolve(operatorName + ".clusterserviceversion.yaml"), manifests);
assertFileExistsIn(manifests.resolve(getCSVFileNameFor(operatorName)), manifests);
if (resourceClass != null) {
assertFileExistsIn(manifests.resolve(getCRDNameFor(resourceClass)), manifests);
}
Expand All @@ -28,6 +30,16 @@ static void checkBundleFor(Path bundle, String operatorName,
assertFileExistsIn(metadata.resolve("annotations.yaml"), metadata);
}

private static String getCSVFileNameFor(String operatorName) {
return operatorName + ".clusterserviceversion.yaml";
}

static ClusterServiceVersion getCSVFor(Path bundle, String operatorName) throws IOException {
final var csvPath = bundle.resolve(operatorName).resolve("manifests").resolve(getCSVFileNameFor(operatorName));
final var csvAsString = Files.readString(csvPath);
return Serialization.unmarshal(csvAsString, ClusterServiceVersion.class);
}

static String getCRDNameFor(Class<? extends HasMetadata> resourceClass) {
return HasMetadata.getFullResourceName(resourceClass) + "-v1.crd.yml";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.quarkiverse.operatorsdk.bundle.runtime.CSVMetadata;

@CSVMetadata(name = "first-operator")
@CSVMetadata(name = "first-operator", version = FirstReconciler.VERSION)
public class FirstReconciler implements Reconciler<First> {

public static final String VERSION = "first-version";

@Override
public UpdateControl<First> reconcile(First request, Context<First> context) {
return UpdateControl.noUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public RequiredCRD(String kind, String name, String version) {

}

public CSVMetadataHolder(String name, String origin) {
this(name, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
public CSVMetadataHolder(String name, String version, String origin) {
this(name, null, null, null, null, null, null, null, null, version, null, null, null, null, null, null, null, null,
origin);
}

Expand Down

0 comments on commit bddef54

Please sign in to comment.