From ca0256c966676ea4c1ccae1fc5a0712903092ced Mon Sep 17 00:00:00 2001 From: carlosthe19916 <2582866+carlosthe19916@users.noreply.github.com> Date: Tue, 27 Aug 2024 16:50:20 -0500 Subject: [PATCH] fake pull-secret.txt --- .github/workflows/ci.yml | 52 ++++++++++++++++++- README.md | 39 +++++--------- pom.xml | 7 +++ pull-secret.txt | 1 + .../controllers/setup/K3sConfigProducer.java | 33 +++++++++--- .../controllers/setup/K3sResource.java | 48 ++++++++++++++--- 6 files changed, 138 insertions(+), 42 deletions(-) create mode 100644 pull-secret.txt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 193d749..bcbd877 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,13 +11,14 @@ on: - "release-*" jobs: - test: + rancher: name: test runs-on: ubuntu-latest strategy: matrix: kubernetes_version: [ + latest, v1.23.17-k3s1, v1.24.17-k3s1, v1.25.16-k3s4, @@ -36,7 +37,56 @@ jobs: - name: Test env: KUBERNETES_VERSION: ${{ matrix.kubernetes_version }} + run: mvn verify + + crc: + runs-on: ubuntu-latest + strategy: + matrix: + preset: [ okd, microshift ] + fail-fast: false + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: "temurin" + java-version: "21" + cache: "maven" + - name: Install required virtualization software + run: | + sudo apt-get update + sudo apt install qemu-kvm libvirt-daemon libvirt-daemon-system + sudo usermod -a -G libvirt $USER + - name: Remove unwanted stuff to free up disk image + run: | + df -h + sudo rm -rf /usr/share/dotnet + sudo rm -rf /usr/local/lib/android + sudo rm -rf /opt/ghc + sudo rm -rf "/usr/local/share/boost" + sudo rm -rf /opt/hostedtoolcache/CodeQL + sudo docker image prune --all --force + sudo swapoff -a + sudo rm -f /mnt/swapfile + df -h + - name: Download crc + run: | + wget https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.40.0/crc-linux-amd64.tar.xz + mkdir -p bin/ && tar -xJf crc-linux-amd64.tar.xz --strip-components=1 -C bin/ + sudo mv ./bin/crc /usr/local/bin/crc && rm -r bin/ + - name: Set the crc config + run: | + crc config set consent-telemetry no + crc config set network-mode user + crc config set preset ${{ matrix.preset }} + - name: Setup the crc + run: sudo -su $USER crc setup + - name: Start the crc + run: sudo -su $USER crc start -p pull-secret.txt + - name: Test run: | + export HOST_KUBERNETES_CONFIG_FILE=${HOME}/.kube/config + echo "Using ${HOST_KUBERNETES_CONFIG_FILE} as host k8s" mvn verify build-operator-bundle: diff --git a/README.md b/README.md index baa5569..f4c5e8a 100644 --- a/README.md +++ b/README.md @@ -31,45 +31,30 @@ At this point the container images will be generated by the operator. # Test Operator -- Create operator container: - ```shell -mvn clean package -DskipTests \ --Dquarkus.native.container-build=true \ --Dquarkus.container-image.build=true \ --Dquarkus.container-image.push=false \ --Dquarkus.container-image.registry=quay.io \ --Dquarkus.container-image.group=$USER \ --Dquarkus.container-image.name=trustify-operator \ --Dquarkus.operator-sdk.bundle.package-name=trustify-operator \ --Dquarkus.operator-sdk.bundle.channels=alpha \ --Dquarkus.application.version=0.0.0 -docker push quay.io/$USER/trustify-operator:0.0.0 +export IMG=quay.io/${USER}/trustify-operator:v0.0.0 +export BUNDLE_IMG=quay.io/${USER}/trustify-operator-bundle:v0.0.0 +export CATALOG_IMG=quay.io/${USER}/trustify-operator-catalog:v0.0.0 ``` -Enrich bundle with cluster permissions (only if generating a catalog for OCP) +> use `export DOCKERFILE=Dockerfile.jvm` if you want to be faster and create an operator based on JVM + +- Create operator: ```shell -groovy scripts/enrichCSV.groovy target/bundle/trustify-operator/manifests/trustify-operator.clusterserviceversion.yaml +make docker-build docker-push ``` - Create bundle: ```shell -BUNDLE_IMAGE=quay.io/$USER/trustify-operator-bundle:0.0.0 -docker build -t $BUNDLE_IMAGE -f target/bundle/trustify-operator/bundle.Dockerfile target/bundle/trustify-operator -docker push $BUNDLE_IMAGE +make bundle-build bundle-push ``` -- Create catalog image: +- Create catalog: ```shell -CATALOG_IMAGE=quay.io/$USER/trustify-operator-catalog:0.0.0 -opm index add \ - --bundles $BUNDLE_IMAGE \ - --tag $CATALOG_IMAGE \ - --build-tool docker -docker push $CATALOG_IMAGE +make catalog-build catalog-push ``` ### Instantiate Catalog @@ -97,7 +82,7 @@ metadata: namespace: $CATALOG_NAMESPACE spec: sourceType: grpc - image: $CATALOG_IMAGE + image: $CATALOG_IMG EOF ``` @@ -139,7 +124,7 @@ spec: channel: alpha name: trustify-operator source: trustify-catalog-source - sourceNamespace: olm + sourceNamespace: ${CATALOG_NAMESPACE} EOF ``` diff --git a/pom.xml b/pom.xml index b369b21..e6ffec5 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,13 @@ bcpkix-jdk18on + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.17.2 + test + + org.testcontainers k3s diff --git a/pull-secret.txt b/pull-secret.txt new file mode 100644 index 0000000..75d00b2 --- /dev/null +++ b/pull-secret.txt @@ -0,0 +1 @@ +{"auths":{"cloud.openshift.com":{"auth":"something","email":"trust.git.bot@gmail.com"},"quay.io":{"auth":"something","email":"trust.git.bot@gmail.com"},"registry.connect.redhat.com":{"auth":"something","email":"trust.git.bot@gmail.com"},"registry.redhat.io":{"auth":"something","email":"trust.git.bot@gmail.com"}}} diff --git a/src/test/java/org/trustify/operator/controllers/setup/K3sConfigProducer.java b/src/test/java/org/trustify/operator/controllers/setup/K3sConfigProducer.java index 0a7e0a3..7aa2b13 100644 --- a/src/test/java/org/trustify/operator/controllers/setup/K3sConfigProducer.java +++ b/src/test/java/org/trustify/operator/controllers/setup/K3sConfigProducer.java @@ -9,6 +9,11 @@ import jakarta.enterprise.inject.Produces; import jakarta.inject.Singleton; import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; +import org.testcontainers.shaded.com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + +import java.io.IOException; +import java.util.*; @Alternative @Priority(1) @@ -22,15 +27,29 @@ public class K3sConfigProducer extends KubernetesConfigProducer { String namespace; //Returns the kubeConfigYaml as the config + @SuppressWarnings(value = {"unchecked", "rawtypes"}) @Singleton @Produces public Config config(KubernetesClientBuildConfig buildConfig, TlsConfig tlsConfig) { - String kubeConfigYamlWithDefaultNamespace = kubeConfigYaml.replace(""" - user: "default" - """, """ - user: "default" - namespace: "%s" - """.formatted(namespace)); - return Config.fromKubeconfig(kubeConfigYamlWithDefaultNamespace); + try { + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + Map yaml = mapper.readValue(kubeConfigYaml, Map.class); + + Optional.ofNullable(yaml.get("current-context")) + .flatMap(currentContext -> ((List) yaml.getOrDefault("contexts", Collections.emptyMap())) + .stream() + .filter(context -> Objects.equals(((Map) context).get("name"), currentContext)) + .findAny() + ) + .ifPresent(context -> { + Map ctxConfig = (Map) ((Map) context).get("context"); + ctxConfig.put("namespace", namespace); + }); + + String kubeConfigYamlWithDefaultNamespace = mapper.writeValueAsString(yaml); + return Config.fromKubeconfig(kubeConfigYamlWithDefaultNamespace); + } catch (IOException e) { + throw new RuntimeException(e); + } } } diff --git a/src/test/java/org/trustify/operator/controllers/setup/K3sResource.java b/src/test/java/org/trustify/operator/controllers/setup/K3sResource.java index 58f0dda..ffe694f 100644 --- a/src/test/java/org/trustify/operator/controllers/setup/K3sResource.java +++ b/src/test/java/org/trustify/operator/controllers/setup/K3sResource.java @@ -1,26 +1,60 @@ package org.trustify.operator.controllers.setup; import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; +import org.jboss.logging.Logger; import org.testcontainers.k3s.K3sContainer; import org.testcontainers.utility.DockerImageName; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; import java.util.Map; import java.util.Optional; public class K3sResource implements QuarkusTestResourceLifecycleManager { - static K3sContainer k3sContainer = new K3sContainer(DockerImageName.parse("rancher/k3s:" + Optional.ofNullable(System.getenv("KUBERNETES_VERSION")).orElse("latest"))); + private static final Logger logger = Logger.getLogger(K3sConfigProducer.class); + + static K3sContainer k3sContainer; + + // If ENV HOST_KUBERNETES_CONFIG_FILE is set then use the host k8s config + public static final String HOST_KUBERNETES_CONFIG_FILE = "HOST_KUBERNETES_CONFIG_FILE"; + + // If ENV HOST_KUBERNETES_CONFIG_FILE is not set then rancher/k3s for k8s. If KUBERNETES_VERSION is not set then "latest" is used + public static final String KUBERNETES_VERSION = "KUBERNETES_VERSION"; @Override public Map start() { - k3sContainer.start(); - return Map.of( - "kubeConfigYaml", k3sContainer.getKubeConfigYaml(), - "quarkus.kubernetes.namespace", "trustify-operator" - ); + Map result = new HashMap<>(); + result.put("quarkus.kubernetes.namespace", "trustify-operator"); + + String kubeConfigYaml; + Optional hostKubernetesConfigFile = Optional.ofNullable(System.getenv(HOST_KUBERNETES_CONFIG_FILE)); + if (hostKubernetesConfigFile.isPresent()) { + logger.info("Using " + hostKubernetesConfigFile.get() + " as kubernetes config file"); + try { + kubeConfigYaml = Files.readString(Paths.get(hostKubernetesConfigFile.get())); + } catch (IOException e) { + throw new RuntimeException(e); + } + } else { + String rancherVersion = Optional.ofNullable(System.getenv(KUBERNETES_VERSION)).orElse("latest"); + logger.info("Using rancher/k3s:" + rancherVersion); + + k3sContainer = new K3sContainer(DockerImageName.parse("rancher/k3s:" + rancherVersion)); + k3sContainer.start(); + + kubeConfigYaml = k3sContainer.getKubeConfigYaml(); + } + + result.put("kubeConfigYaml", kubeConfigYaml); + return result; } @Override public void stop() { - k3sContainer.stop(); + if (k3sContainer != null) { + k3sContainer.stop(); + } } }