Skip to content

Commit f3c73c4

Browse files
committed
ci: cache k3d registry
1 parent 7d7fb70 commit f3c73c4

File tree

3 files changed

+79
-62
lines changed

3 files changed

+79
-62
lines changed

.circleci/config.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,51 @@ jobs:
8585
- run:
8686
name: Install ginkgo
8787
command: make ginkgo
88+
- restore_cache:
89+
keys:
90+
- k3d-registry.docker
91+
- run:
92+
name: Prepare k3d images registry
93+
command: |
94+
if [ -f k3d-registry.image.tar ]; then
95+
docker load -i k3d-registry.image.tar
96+
tar -xf k3d-registry.volume.tar
97+
docker run --name k3d-graviteeio.docker.localhost \
98+
--net=bridge \
99+
-h k3d-graviteeio.docker.localhost \
100+
-p 12345:12345 \
101+
-e REGISTRY_HTTP_ADDR=:12345 \
102+
-v $(pwd)/var/lib/registry:/var/lib/registry \
103+
-d gko/k3d-registry:latest
104+
echo "export K3D_PULL=apim" >> $BASH_ENV
105+
else
106+
echo "No registry archive found. All images will be pulled from network."
107+
echo "export K3D_PULL=all" >> $BASH_ENV
108+
fi
88109
- run:
89110
name: Start APIM using k3d
90111
command: |
91112
export APIM_IMAGE_REGISTRY=graviteeio.azurecr.io
92113
export APIM_IMAGE_TAG=master-latest
93-
make k3d-apim-init
114+
K3DARGS="--pull=${K3D_PULL}" make k3d-apim-init
94115
- run:
95116
name: Run tests
96117
command: GOTESTARGS="--output-dir=/tmp/junit/reports --junit-report=junit.xml" make test
118+
- run:
119+
name: Archive k3d registry image and volume for caching
120+
command: |
121+
if [ -f k3d-registry.image.tar ]; then
122+
echo "K3d registry already saved on disk. Nothing to do"
123+
else
124+
docker commit k3d-graviteeio.docker.localhost gko/k3d-registry:latest
125+
docker save -o k3d-registry.image.tar gko/k3d-registry:latest
126+
docker run --rm --volumes-from $(docker ps -aqf "name=graviteeio.docker.localhost") -v $(pwd):/backup alpine tar cf /backup/k3d-registry.volume.tar /var/lib/registry
127+
fi
128+
- save_cache:
129+
paths:
130+
- k3d-registry.image.tar
131+
- k3d-registry.volume.tar
132+
key: k3d-registry.docker
97133
- store_test_results:
98134
path: /tmp/junit/reports
99135
- go/save-cache

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ test: manifests generate install ## Run tests.
124124
kubectl config use-context k3d-graviteeio
125125
KUBEBUILDER_ASSETS=USE_EXISTING_CLUSTER=true $(GINKGO) $(GOTESTARGS) --timeout 380s --cover --coverprofile=cover.out ./...
126126

127+
K3DARGS ?= ""
127128
.PHONY: k3d-apim-init
128129
k3d-apim-init: ## Init APIM locally using k3d
129-
npx zx ./scripts/k3d.mjs
130+
npx zx ./scripts/k3d.mjs $(K3DARGS)
130131

131132
.PHONY: k3d-apim-start
132133
k3d-apim-start: ## Start exiting APIM node in k3d

scripts/k3d.mjs

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function time(fn) {
2929
await fn();
3030
const end = Date.now();
3131
green(`
32-
Done in ${(end - start) / 1000}s
32+
Done in ${(end - start) / 1000}s
3333
`);
3434
}
3535

@@ -70,9 +70,9 @@ green("Starting k3d cluster with APIM dependencies...");
7070

7171
blue(`
7272
73-
Installing the latest version of k3d (if not present) ...
73+
Installing the latest version of k3d (if not present) ...
7474
75-
See https://k3d.io/
75+
See https://k3d.io/
7676
`);
7777

7878
await time(installK3d);
@@ -87,7 +87,7 @@ async function installK3d() {
8787

8888
blue(`
8989
90-
Initializing a local docker images registry for k3d images (if not present) ...
90+
🐳 Initializing a local docker images registry for k3d images (if not present) ...
9191
`);
9292

9393
await time(initRegistry);
@@ -98,13 +98,13 @@ async function initRegistry() {
9898

9999
magenta(`
100100
101-
K3d images registry ${K3D_IMAGES_REGISTRY_NAME} already exists, skipping.
102-
`);
101+
K3d images registry ${K3D_IMAGES_REGISTRY_NAME} already exists, skipping.
102+
`);
103103
} catch (error) {
104104
magenta(`
105105
106-
Initializing registry ${K3D_IMAGES_REGISTRY_NAME}
107-
`);
106+
Initializing registry ${K3D_IMAGES_REGISTRY_NAME}
107+
`);
108108

109109
await $`k3d registry create ${K3D_IMAGES_REGISTRY_NAME} --port ${K3D_IMAGES_REGISTRY_PORT}`;
110110
}
@@ -114,54 +114,34 @@ async function initRegistry() {
114114

115115
yellow(`
116116
117-
/!\ Warning /!\
117+
⚠️ WARNING ⚠️
118118
119-
Assuming that host "${K3D_IMAGES_REGISTRY_NAME}" points to 127.0.0.1
119+
Assuming that host "${K3D_IMAGES_REGISTRY_NAME}" points to 127.0.0.1
120120
121-
You might need to edit your /etc/hosts file before going further.
121+
You might need to edit your /etc/hosts file before going further.
122122
`);
123123

124124
await time(initCluster);
125125

126126
async function initCluster() {
127127
blue(`
128128
129-
Deleting K3d cluster ${K3D_CLUSTER_NAME} (if present) ...
129+
Deleting K3d cluster ${K3D_CLUSTER_NAME} (if present) ...
130130
`);
131131

132-
try {
133-
magenta(`
134-
135-
K3d images registry ${K3D_IMAGES_REGISTRY_NAME} already exists, skipping.
136-
`);
137-
138-
await $`k3d registry list | grep -q "${K3D_IMAGES_REGISTRY_NAME}"`;
139-
} catch (error) {
140-
magenta(`
141-
142-
Initializing registry ${K3D_IMAGES_REGISTRY_NAME}
143-
`);
144-
145-
await $`k3d registry create ${K3D_IMAGES_REGISTRY_NAME} --port ${K3D_IMAGES_REGISTRY_PORT}`;
146-
}
147-
148132
try {
149133
await $`k3d cluster list| grep -q "${K3D_CLUSTER_NAME}"`;
150134
await $`k3d cluster delete ${K3D_CLUSTER_NAME}`;
151-
magenta(`
152-
153-
Cluster ${K3D_CLUSTER_NAME} has been deleted
154-
`);
155135
} catch (error) {
156136
magenta(`
157137
158-
no K3d cluster with name ${K3D_CLUSTER_NAME}
138+
No K3d cluster with name ${K3D_CLUSTER_NAME}
159139
`);
160140
}
161141

162142
blue(`
163143
164-
Creating a K3d cluster with name ${K3D_CLUSTER_NAME}
144+
Creating a K3d cluster with name ${K3D_CLUSTER_NAME}
165145
`);
166146

167147
await $`k3d cluster create --wait \
@@ -177,7 +157,7 @@ async function initCluster() {
177157
if (pullMode !== "none") {
178158
blue(`
179159
180-
Registering docker images to ${K3D_IMAGES_REGISTRY} ...
160+
🐳 Registering docker images to ${K3D_IMAGES_REGISTRY} ...
181161
`);
182162

183163
await time(registerImages);
@@ -222,22 +202,22 @@ async function registerImages() {
222202

223203
const images = pullMode === "all" ? allImages : apimImages;
224204

205+
$.quote = $NoQuote;
206+
225207
magenta(`
226208
227-
Pulling docker images ...
209+
Pulling docker images ...
228210
`);
229211

230212
await Promise.all(
231-
Array.from(images.keys()).map((image) => $`docker pull ${image}`)
213+
Array.from(images.keys()).map((image) => $`docker pull ${image} > /dev/null`)
232214
);
233215

234216
magenta(`
235217
236-
Tagging docker images ...
218+
Tagging docker images ...
237219
`);
238220

239-
$.quote = $NoQuote;
240-
241221
await Promise.all(
242222
Array.from(images.entries()).map(
243223
([image, tag]) => $`docker tag ${image} ${tag}`
@@ -246,19 +226,19 @@ async function registerImages() {
246226

247227
magenta(`
248228
249-
Pushing docker images to ${K3D_IMAGES_REGISTRY}
229+
Pushing docker images to ${K3D_IMAGES_REGISTRY}
250230
`);
251231

252232
await Promise.all(
253-
Array.from(images.values()).map((tag) => $`docker push ${tag}`)
233+
Array.from(images.values()).map((tag) => $`docker push ${tag} > /dev/null`)
254234
);
255235

256236
$.quote = $Quote;
257237
}
258238

259239
blue(`
260240
261-
Creating Kubernetes namespace ${K3D_NAMESPACE_NAME} ...
241+
Creating Kubernetes namespace ${K3D_NAMESPACE_NAME} ...
262242
`);
263243

264244
await time(createNamespace);
@@ -270,13 +250,13 @@ async function createNamespace() {
270250

271251
blue(`
272252
273-
Storing APIM context credentials as a secret ...
253+
Storing APIM context credentials as a secret ...
274254
275-
The following declaration can be used in your management context to reference this secret:
255+
The following declaration can be used in your management context to reference this secret:
276256
277-
secretRef:
278-
name: ${APIM_CONTEXT_SECRET_NAME}
279-
namespace: ${K3D_NAMESPACE_NAME}
257+
secretRef:
258+
name: ${APIM_CONTEXT_SECRET_NAME}
259+
namespace: ${K3D_NAMESPACE_NAME}
280260
`);
281261

282262
await time(createSecret);
@@ -291,7 +271,7 @@ async function createSecret() {
291271

292272
blue(`
293273
294-
Adding Helm repositories (if not presents) ...
274+
Adding Helm repositories (if not presents) ...
295275
`);
296276

297277
await time(addHelmRepos);
@@ -303,13 +283,13 @@ async function addHelmRepos() {
303283
}
304284

305285
blue(`
306-
Installing components in namespace ${K3D_NAMESPACE_NAME}
286+
Installing components in namespace ${K3D_NAMESPACE_NAME}
307287
308-
Mongodb ${MONGO_IMAGE_TAG}
309-
Elasticsearch ${ELASTIC_IMAGE_TAG}
310-
Nginx ingress ${NGINX_CONTROLLER_IMAGE_TAG}
311-
Nginx backend ${NGINX_BACKEND_IMAGE_TAG}
312-
Gravitee APIM ${APIM_IMAGE_TAG}
288+
Mongodb ${MONGO_IMAGE_TAG}
289+
Elasticsearch ${ELASTIC_IMAGE_TAG}
290+
Nginx ingress ${NGINX_CONTROLLER_IMAGE_TAG}
291+
Nginx backend ${NGINX_BACKEND_IMAGE_TAG}
292+
Gravitee APIM ${APIM_IMAGE_TAG}
313293
314294
`);
315295

@@ -361,7 +341,7 @@ helm install \
361341
--set "mongo.dbhost=mongodb" \
362342
--set "mongodb-replicaset=false" \
363343
--set "mongo.rsEnabled=false" \
364-
apim graviteeio/apim3
344+
apim graviteeio/apim3 > /dev/null
365345
`;
366346

367347
const helmInstallMongo = $`
@@ -378,7 +358,7 @@ helm install \
378358
--set resources.requests.memory=2048Mi \
379359
--set resources.limits.cpu=2000m \
380360
--set resources.requests.cpu=2000m \
381-
mongodb bitnami/mongodb
361+
mongodb bitnami/mongodb > /dev/null
382362
`;
383363

384364
const helmInstallElastic = $`
@@ -387,7 +367,7 @@ helm install \
387367
--set replicas=1 \
388368
--set "image=${K3D_IMAGES_REGISTRY}/elasticsearch" \
389369
--set "imageTag=${ELASTIC_IMAGE_TAG}" \
390-
elastic elastic/elasticsearch
370+
elastic elastic/elasticsearch > /dev/null
391371
`;
392372

393373
const helmInstallNginxIngress = $`
@@ -399,7 +379,7 @@ helm install \
399379
--set "defaultBackend.image.repository=nginx" \
400380
--set "image.tag=${NGINX_CONTROLLER_IMAGE_TAG}" \
401381
--set "defaultBackend.image.tag=${NGINX_BACKEND_IMAGE_TAG}" \
402-
nginx-ingress bitnami/nginx-ingress-controller
382+
nginx-ingress bitnami/nginx-ingress-controller > /dev/null
403383
`;
404384

405385
await time(helmInstall);
@@ -448,5 +428,5 @@ async function waitForApim() {
448428

449429
green(`
450430
451-
Cluster is ready!
431+
🚀 Cluster is ready!
452432
`);

0 commit comments

Comments
 (0)