Skip to content

Commit

Permalink
Merge branch 'main' into all-ns-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
csviri authored Aug 21, 2023
2 parents 9e722e7 + 0191e07 commit 4daaa7a
Show file tree
Hide file tree
Showing 50 changed files with 966 additions and 152 deletions.
4 changes: 2 additions & 2 deletions .github/project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Java Operator SDK Extension
release:
current-version: 6.2.1
next-version: 6.2.2-SNAPSHOT
current-version: 6.3.0
next-version: 6.3.1-SNAPSHOT

2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk-parent</artifactId>
<version>6.2.2-SNAPSHOT</version>
<version>6.3.1-SNAPSHOT</version>
</parent>
<artifactId>quarkus-operator-sdk-bom</artifactId>
<name>Quarkus - Operator SDK - BOM</name>
Expand Down
2 changes: 1 addition & 1 deletion build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk-parent</artifactId>
<version>6.2.2-SNAPSHOT</version>
<version>6.3.1-SNAPSHOT</version>
</parent>
<artifactId>quarkus-operator-sdk-build-parent</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion bundle-generator/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk-bundle-generator-parent</artifactId>
<version>6.2.2-SNAPSHOT</version>
<version>6.3.1-SNAPSHOT</version>
</parent>

<name>Quarkus - Operator SDK - Bundle Generator - Deployment</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static io.quarkiverse.operatorsdk.deployment.AddClusterRolesDecorator.ALL_VERBS;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -22,7 +21,6 @@
import org.jboss.jandex.IndexView;
import org.jboss.logging.Logger;

import io.dekorate.utils.Serialization;
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.rbac.ClusterRole;
Expand All @@ -34,10 +32,7 @@
import io.quarkiverse.operatorsdk.bundle.runtime.CSVMetadata.Icon;
import io.quarkiverse.operatorsdk.bundle.runtime.CSVMetadataHolder;
import io.quarkiverse.operatorsdk.bundle.runtime.SharedCSVMetadata;
import io.quarkiverse.operatorsdk.common.ClassUtils;
import io.quarkiverse.operatorsdk.common.ConfigurationUtils;
import io.quarkiverse.operatorsdk.common.ReconciledAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.ReconcilerAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.*;
import io.quarkiverse.operatorsdk.deployment.GeneratedCRDInfoBuildItem;
import io.quarkiverse.operatorsdk.deployment.VersionBuildItem;
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
Expand Down Expand Up @@ -75,7 +70,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 +104,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 @@ -190,44 +187,37 @@ void generateBundle(ApplicationInfoBuildItem configuration,
final var roles = new LinkedList<Role>();
final var deployments = new LinkedList<Deployment>();

generatedKubernetesManifests.stream()
.filter(bi -> bi.getName().equals("kubernetes.yml"))
.findAny()
.ifPresent(
bi -> {
final var resources = Serialization
.unmarshalAsList(new ByteArrayInputStream(bi.getContent()));
resources.getItems().forEach(r -> {
if (r instanceof ServiceAccount) {
serviceAccounts.add((ServiceAccount) r);
return;
}

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

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

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

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

if (r instanceof Deployment) {
deployments.add((Deployment) r);
}
});
});
final var resources = GeneratedResourcesUtils.loadFrom(generatedKubernetesManifests);
resources.forEach(r -> {
if (r instanceof ServiceAccount) {
serviceAccounts.add((ServiceAccount) r);
return;
}

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

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

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

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

if (r instanceof Deployment) {
deployments.add((Deployment) r);
}
});
final var generated = BundleGenerator.prepareGeneration(bundleConfiguration, versionBuildItem.getVersion(),
csvMetadata.getCsvGroups(), crds, outputTarget.getOutputDirectory());
generated.forEach(manifestBuilder -> {
Expand All @@ -252,8 +242,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
2 changes: 1 addition & 1 deletion bundle-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk-build-parent</artifactId>
<version>6.2.2-SNAPSHOT</version>
<version>6.3.1-SNAPSHOT</version>
<relativePath>../build-parent/pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion bundle-generator/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk-bundle-generator-parent</artifactId>
<version>6.2.2-SNAPSHOT</version>
<version>6.3.1-SNAPSHOT</version>
</parent>

<name>Quarkus - Operator SDK - Bundle Generator - Runtime</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "operator-sdk.bundle", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
@ConfigRoot(name = "operator-sdk.bundle")
public class BundleGenerationConfiguration {
/**
* Whether the extension should generate the Operator bundle.
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
2 changes: 1 addition & 1 deletion common-deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk-build-parent</artifactId>
<version>6.2.2-SNAPSHOT</version>
<version>6.3.1-SNAPSHOT</version>
<relativePath>../build-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkiverse.operatorsdk.common;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.List;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;

public class FileUtils {
private final static KubernetesSerialization serializer = new KubernetesSerialization();

public static void ensureDirectoryExists(File dir) {
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new IllegalArgumentException("Couldn't create " + dir.getAbsolutePath());
}
}
}

public static List<HasMetadata> unmarshalFrom(byte[] yamlOrJson) {
return serializer.unmarshal(new ByteArrayInputStream(yamlOrJson));
}

public static String asYaml(Object toSerialize) {
return serializer.asYaml(toSerialize);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkiverse.operatorsdk.common;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.quarkus.kubernetes.spi.GeneratedKubernetesResourceBuildItem;

public class GeneratedResourcesUtils {
public static final String KUBERNETES_YAML = "kubernetes.yml";
private static final Logger log = Logger.getLogger(GeneratedResourcesUtils.class.getName());

public static List<HasMetadata> loadFrom(List<GeneratedKubernetesResourceBuildItem> generatedResources,
String resourceName) {
if (generatedResources.isEmpty()) {
log.debugv("Couldn't load resource {0} because no resources were generated", resourceName);
return Collections.emptyList();
}
var buildItem = generatedResources.stream()
.filter(r -> resourceName.equals(r.getName()))
.findAny();
return buildItem.map(bi -> FileUtils.unmarshalFrom(bi.getContent()))
.orElseThrow(() -> new IllegalArgumentException("Couldn't find resource " + resourceName +
" in generated resources: " + generatedResources.stream()
.map(GeneratedKubernetesResourceBuildItem::getName).collect(Collectors.toSet())));
}

public static List<HasMetadata> loadFrom(List<GeneratedKubernetesResourceBuildItem> generatedResources) {
return loadFrom(generatedResources, KUBERNETES_YAML);
}
}
Loading

0 comments on commit 4daaa7a

Please sign in to comment.