Skip to content

Commit

Permalink
fake pull-secret.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Aug 27, 2024
1 parent 420ad1e commit ca0256c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 42 deletions.
52 changes: 51 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
39 changes: 12 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,7 +82,7 @@ metadata:
namespace: $CATALOG_NAMESPACE
spec:
sourceType: grpc
image: $CATALOG_IMAGE
image: $CATALOG_IMG
EOF
```

Expand Down Expand Up @@ -139,7 +124,7 @@ spec:
channel: alpha
name: trustify-operator
source: trustify-catalog-source
sourceNamespace: olm
sourceNamespace: ${CATALOG_NAMESPACE}
EOF
```

Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
<artifactId>bcpkix-jdk18on</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.17.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>k3s</artifactId>
Expand Down
1 change: 1 addition & 0 deletions pull-secret.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"auths":{"cloud.openshift.com":{"auth":"something","email":"[email protected]"},"quay.io":{"auth":"something","email":"[email protected]"},"registry.connect.redhat.com":{"auth":"something","email":"[email protected]"},"registry.redhat.io":{"auth":"something","email":"[email protected]"}}}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<String, Object> 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<String, String> ctxConfig = (Map) ((Map<String, Map>) context).get("context");
ctxConfig.put("namespace", namespace);
});

String kubeConfigYamlWithDefaultNamespace = mapper.writeValueAsString(yaml);
return Config.fromKubeconfig(kubeConfigYamlWithDefaultNamespace);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> start() {
k3sContainer.start();
return Map.of(
"kubeConfigYaml", k3sContainer.getKubeConfigYaml(),
"quarkus.kubernetes.namespace", "trustify-operator"
);
Map<String, String> result = new HashMap<>();
result.put("quarkus.kubernetes.namespace", "trustify-operator");

String kubeConfigYaml;
Optional<String> 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();
}
}
}

0 comments on commit ca0256c

Please sign in to comment.