Skip to content

Commit

Permalink
bundle: Remove duplicate nativeAPIs, sort by multi-field comparator
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Aug 22, 2024
1 parent e91327d commit 76ce3db
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
import static io.quarkiverse.operatorsdk.bundle.deployment.BundleGenerator.MANIFESTS;
import static io.quarkiverse.operatorsdk.bundle.deployment.BundleProcessor.CRD_DESCRIPTION;
import static io.quarkiverse.operatorsdk.bundle.deployment.BundleProcessor.CRD_DISPLAY_NAME;
import static java.util.Comparator.comparing;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -17,11 +30,27 @@
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpecBuilder;
import io.fabric8.kubernetes.api.model.rbac.*;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.*;
import io.fabric8.kubernetes.api.model.rbac.ClusterRole;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
import io.fabric8.kubernetes.api.model.rbac.PolicyRule;
import io.fabric8.kubernetes.api.model.rbac.PolicyRuleBuilder;
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.api.model.rbac.RoleRef;
import io.fabric8.kubernetes.api.model.rbac.Subject;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.CRDDescription;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.CRDDescriptionBuilder;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersionBuilder;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.NamedInstallStrategyFluent;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.StrategyDeploymentPermissionsBuilder;
import io.fabric8.openshift.api.model.operatorhub.v1alpha1.StrategyDeploymentPermissionsFluent;
import io.quarkiverse.operatorsdk.bundle.runtime.CSVMetadataHolder;
import io.quarkiverse.operatorsdk.bundle.runtime.CSVMetadataHolder.RequiredCRD;
import io.quarkiverse.operatorsdk.common.*;
import io.quarkiverse.operatorsdk.common.ConfigurationUtils;
import io.quarkiverse.operatorsdk.common.ReconciledAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.ReconciledResourceAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.ReconcilerAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.ResourceAssociatedAugmentedClassInfo;
import io.quarkiverse.operatorsdk.runtime.BuildTimeOperatorConfiguration;

public class CsvManifestsBuilder extends ManifestsBuilder {
Expand Down Expand Up @@ -195,9 +224,13 @@ public CsvManifestsBuilder(CSVMetadataHolder metadata, BuildTimeOperatorConfigur
}

// add sorted native APIs
final var nullsFirst = Comparator.nullsFirst(String::compareTo);
csvSpecBuilder.addAllToNativeAPIs(nativeApis.stream()
.sorted(Comparator.comparing(CsvManifestsBuilder::asString))
.collect(Collectors.toList()));
.distinct()
.sorted(comparing(GroupVersionKind::getGroup, nullsFirst)
.thenComparing(comparing(GroupVersionKind::getKind, nullsFirst))
.thenComparing(comparing(GroupVersionKind::getVersion, nullsFirst)))
.toList());

csvSpecBuilder.editOrNewCustomresourcedefinitions()
.addAllToOwned(ownedCRs)
Expand All @@ -217,10 +250,6 @@ private CRDDescription createCRDDescription(ReconciledResourceAugmentedClassInfo
.build();
}

private static String asString(GroupVersionKind gvk) {
return gvk.getGroup() + " " + gvk.getKind() + gvk.getVersion();
}

public Set<String> getOwnedCRs() {
return ownedCRs.stream()
.map(CRDDescription::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public void shouldWriteBundleForTheOperators() throws IOException {
assertEquals(HasMetadata.getFullResourceName(SecondExternal.class), crds.getRequired().get(1).getName());
// should list native APIs as well
final var spec = csv.getSpec();
final var podGVK = spec.getNativeAPIs().get(0);
final var nativeAPIs = spec.getNativeAPIs();
assertEquals(1, nativeAPIs.size());
final var podGVK = nativeAPIs.get(0);
assertEquals(HasMetadata.getGroup(Pod.class), podGVK.getGroup());
assertEquals(HasMetadata.getKind(Pod.class), podGVK.getKind());
assertEquals(HasMetadata.getVersion(Pod.class), podGVK.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
+ SecondExternal.GROUP, version = SecondExternal.VERSION), replaces = "1.0.0", annotations = @Annotations(skipRange = ">=1.0.0 <1.0.3", capabilities = "Test", others = @Annotation(name = "foo", value = "bar")))
@ControllerConfiguration(name = ThirdReconciler.NAME, dependents = {
@Dependent(type = ExternalDependentResource.class),
@Dependent(type = PodDependentResource.class)
@Dependent(name = "pod1", type = PodDependentResource.class),
@Dependent(name = "pod2", type = PodDependentResource.class)
})
public class ThirdReconciler implements Reconciler<Third> {

Expand Down

0 comments on commit 76ce3db

Please sign in to comment.