Skip to content

Commit 3afebec

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 3afebec

File tree

4 files changed

+122
-26
lines changed

4 files changed

+122
-26
lines changed

Makefile

+15-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

@@ -102,8 +102,8 @@ KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use -p path $(KUBE_MINOR).x)
102102

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
105-
KIND_CLUSTER_IMAGE := kindest/node:v$(KUBE_MINOR).0
106-
KIND_CLUSTER_NAME ?= kind-olmv0
105+
export KIND_CLUSTER_IMAGE := kindest/node:v$(KUBE_MINOR).0
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) \
@@ -248,7 +256,7 @@ E2E_TEST_NS ?= operators
248256
E2E_INSTALL_NS ?= operator-lifecycle-manager
249257
E2E_CATALOG_NS ?= $(E2E_INSTALL_NS)
250258
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)')
259+
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)')
252260

253261
.PHONY: e2e
254262
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

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

0 commit comments

Comments
 (0)