Skip to content

Commit 4ade1d9

Browse files
author
Per Goncalves da Silva
committed
setup kind with local registry
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent dac494c commit 4ade1d9

File tree

4 files changed

+116
-26
lines changed

4 files changed

+116
-26
lines changed

Makefile

+14-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export CONFIGMAP_SERVER_IMAGE ?= quay.io/operator-framework/configmap-operator-r
6363

6464
PKG := github.com/operator-framework/operator-lifecycle-manager
6565
IMAGE_REPO ?= quay.io/operator-framework/olm
66-
IMAGE_TAG ?= "dev"
66+
IMAGE_TAG ?= "local"
6767

6868
# Go build settings #
6969

@@ -103,7 +103,7 @@ KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use -p path $(KUBE_MINOR).x)
103103
# Kind node image tags are in the format x.y.z we pin to version x.y.0 because patch releases and node images
104104
# are not guaranteed to be available when a new version of the kube apis is released
105105
KIND_CLUSTER_IMAGE := kindest/node:v$(KUBE_MINOR).0
106-
KIND_CLUSTER_NAME ?= kind-olmv0
106+
export KIND_CLUSTER_NAME ?= kind-olmv0
107107

108108
# Targets #
109109
# Disable -j flag for make
@@ -145,6 +145,10 @@ image: export GOOS = linux
145145
image: clean build #HELP Build image image for linux on host architecture
146146
docker build -t $(IMAGE_REPO):$(IMAGE_TAG) -f Dockerfile bin
147147

148+
.PHONE: build-e2e-fixture-images
149+
build-e2e-fixture-images: #HELP Build images for e2e testing
150+
./scripts/e2e_test_fixtures.sh
151+
148152
.PHONY: e2e-build
149153
# the e2e and experimental_metrics tags are required to get e2e tests to pass
150154
# search the code for go:build e2e or go:build experimental_metrics to see where these tags are used
@@ -207,13 +211,17 @@ kind-clean: $(KIND) #HELP Delete kind cluster $KIND_CLUSTER_NAME (default: kind-
207211

208212
.PHONY: kind-create
209213
kind-create: kind-clean #HELP Create a new kind cluster $KIND_CLUSTER_NAME (default: kind-olmv0)
210-
$(KIND) create cluster --name $(KIND_CLUSTER_NAME) --image $(KIND_CLUSTER_IMAGE) $(KIND_CREATE_OPTS)
214+
./scripts/kind_with_registry.sh
211215
$(KIND) export kubeconfig --name $(KIND_CLUSTER_NAME)
212216

217+
.PHONY: load-test-images
218+
load-test-images: #HELP Load the OLM images into the kind cluster's registry
219+
./scripts/e2e_test_fixtures.sh --push-to="localhost:5001"
220+
213221
.PHONY: deploy
214-
OLM_IMAGE := quay.io/operator-framework/olm:local
222+
OLM_IMAGE ?= $(IMAGE_REPO):$(IMAGE_TAG)
215223
deploy: $(KIND) $(HELM) #HELP Deploy OLM to kind cluster $KIND_CLUSTER_NAME (default: kind-olmv0) using $OLM_IMAGE (default: quay.io/operator-framework/olm:local)
216-
$(KIND) load docker-image $(OLM_IMAGE) --name $(KIND_CLUSTER_NAME); \
224+
$(KIND) load docker-image $(OLM_IMAGE) --name $(KIND_CLUSTER_NAME)
217225
$(HELM) upgrade --install olm deploy/chart \
218226
--set debug=true \
219227
--set olm.image.ref=$(OLM_IMAGE) \
@@ -247,8 +255,7 @@ E2E_TIMEOUT ?= 90m
247255
E2E_TEST_NS ?= operators
248256
E2E_INSTALL_NS ?= operator-lifecycle-manager
249257
E2E_CATALOG_NS ?= $(E2E_INSTALL_NS)
250-
E2E_FLAKE_ATTEMPTS ?= 1
251-
GINKGO_OPTS += -v -randomize-suites -race -trace --show-node-events --flake-attempts=$(E2E_FLAKE_ATTEMPTS) $(if $(E2E_SEED),-seed '$(E2E_SEED)') $(if $(TEST),-focus '$(TEST)',) $(if $(SKIP), -skip '$(SKIP)')
258+
GINKGO_OPTS += -v -randomize-suites -race -trace --show-node-events $(if $(E2E_FLAKE_ATTEMPTS),-flake-attempts=$(E2E_FLAKE_ATTEMPTS) ) $(if $(E2E_SEED),-seed '$(E2E_SEED)' ) $(if $(TEST),-focus '$(TEST)', ) $(if $(SKIP), -skip '$(SKIP)')
252259

253260
.PHONY: e2e
254261
e2e: #HELP Run e2e tests against a cluster running OLM (params: $E2E_TEST_NS (operator), $E2E_INSTALL_NS (operator-lifecycle-manager), $E2E_CATALOG_NS (operator-lifecycle-manager), $E2E_TIMEOUT (90m), $E2E_FLAKE_ATTEMPTS (1), $TEST(undefined))

scripts/build_test_images.sh

-19
This file was deleted.

scripts/e2e_test_fixtures.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
KIND=${KIND:-kind}
4+
CONTAINER_RUNTIME=${CONTAINER_RUNTIME:-docker}
5+
6+
# Default values
7+
OPERATOR_REGISTRY_VERSION="${OPERATOR_REGISTRY_VERSION:-$(go list -m github.com/operator-framework/operator-registry | cut -d" " -f2 | sed 's/^v//')}"
8+
KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kind-olmv0}"
9+
REGISTRY="${REGISTRY:-localhost:5001}"
10+
11+
# Fixtures
12+
# Note: the following catalogs reference bundles stored in quay.io/olmtest
13+
INDEX_V1="${REGISTRY}/busybox-dependencies-index:1.0.0-with-ListBundles-method-${OPM_VERSION}"
14+
INDEX_V2="${REGISTRY}/busybox-dependencies-index:2.0.0-with-ListBundles-method-${OPM_VERSION}"
15+
TEST_CATALOG_IMAGE="${REGISTRY}/test-catalog:e2e"
16+
17+
## Build
18+
${CONTAINER_RUNTIME} build -t "${INDEX_V1}" --build-arg="OPM_VERSION=v${OPERATOR_REGISTRY_VERSION}" -f ./test/images/busybox-index/index.Dockerfile ./test/images/busybox-index/indexv1
19+
${CONTAINER_RUNTIME} build -t "${INDEX_V2}" --build-arg="OPM_VERSION=v${OPERATOR_REGISTRY_VERSION}" -f ./test/images/busybox-index/index.Dockerfile ./test/images/busybox-index/indexv2
20+
21+
# The following catalog used for e2e tests related to serving an extracted registry
22+
# See catalog_e2e_test.go
23+
# let's just reuse one of the other catalogs for this - the tests don't care about the content
24+
# only that a catalog's content can be extracted and served by a different container
25+
${CONTAINER_RUNTIME} tag "${INDEX_V2}" "${TEST_CATALOG_IMAGE}"
26+
27+
### Push
28+
${CONTAINER_RUNTIME} push "${INDEX_V1}"
29+
${CONTAINER_RUNTIME} push "${INDEX_V2}"
30+
${CONTAINER_RUNTIME} push "${TEST_CATALOG_IMAGE}"

scripts/kind_with_registry.sh

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/sh
2+
set -o errexit
3+
set -x
4+
5+
# Original source: https://kind.sigs.k8s.io/docs/user/local-registry/
6+
KIND_CLUSTER_NAME=${KIND_CLUSTER_NAME:-kind-olmv0}
7+
KIND=${KIND:-kind}
8+
DOCKER=${DOCKER:-docker}
9+
KUBECTL=${KUBECTL:-kubectl}
10+
11+
# 1. Create registry container unless it already exists
12+
reg_name='kind-registry'
13+
reg_port='5001'
14+
if [ "$(${DOCKER} inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then
15+
${DOCKER} run \
16+
-d --restart=always -p "127.0.0.1:${reg_port}:5000" --network bridge --name "${reg_name}" \
17+
registry:2
18+
fi
19+
20+
# 2. Create kind cluster with containerd registry config dir enabled
21+
# TODO: kind will eventually enable this by default and this patch will
22+
# be unnecessary.
23+
#
24+
# See:
25+
# https://github.com/kubernetes-sigs/kind/issues/2875
26+
# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration
27+
# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md
28+
cat <<EOF | ${KIND} create cluster --name="${KIND_CLUSTER_NAME}" ${KIND_CREATE_OPTS} --config=-
29+
kind: Cluster
30+
apiVersion: kind.x-k8s.io/v1alpha4
31+
containerdConfigPatches:
32+
- |-
33+
[plugins."io.containerd.grpc.v1.cri".registry]
34+
config_path = "/etc/containerd/certs.d"
35+
EOF
36+
37+
# 3. Add the registry config to the nodes
38+
#
39+
# This is necessary because localhost resolves to loopback addresses that are
40+
# network-namespace local.
41+
# In other words: localhost in the container is not localhost on the host.
42+
#
43+
# We want a consistent name that works from both ends, so we tell containerd to
44+
# alias localhost:${reg_port} to the registry container when pulling images
45+
REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}"
46+
for node in $(${KIND} --name="${KIND_CLUSTER_NAME}" get nodes); do
47+
${DOCKER} exec "${node}" mkdir -p "${REGISTRY_DIR}"
48+
cat <<EOF | ${DOCKER} exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml"
49+
[host."http://${reg_name}:5000"]
50+
EOF
51+
done
52+
53+
# 4. Connect the registry to the cluster network
54+
# This allows kind to bootstrap the network but ensures they're on the same network
55+
if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then
56+
docker network connect "kind" "${reg_name}"
57+
fi
58+
59+
# 5. Document the local registry
60+
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
61+
${KIND} export kubeconfig --name="${KIND_CLUSTER_NAME}"
62+
cat <<EOF | ${KUBECTL} apply -f -
63+
apiVersion: v1
64+
kind: ConfigMap
65+
metadata:
66+
name: local-registry-hosting
67+
namespace: kube-public
68+
data:
69+
localRegistryHosting.v1: |
70+
host: "localhost:${reg_port}"
71+
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
72+
EOF

0 commit comments

Comments
 (0)