Skip to content

Commit 9315f6b

Browse files
authored
feat: options to configure local registry (#113)
* feat: options to configure local registry Signed-off-by: Thuan Vo <[email protected]> * set registry address in env var LOCAL_REGISTRY Signed-off-by: Thuan Vo <[email protected]> * add missing code block syntax in README Signed-off-by: Thuan Vo <[email protected]> * output registry address as output instead Signed-off-by: Thuan Vo <[email protected]> --------- Signed-off-by: Thuan Vo <[email protected]>
1 parent aed9fb9 commit 9315f6b

File tree

7 files changed

+391
-8
lines changed

7 files changed

+391
-8
lines changed

.github/workflows/test.yaml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,93 @@ jobs:
169169
run: |
170170
kubectl cluster-info
171171
kubectl get nodes
172+
173+
test-without-registry:
174+
runs-on: ubuntu-latest
175+
steps:
176+
- name: Checkout
177+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
178+
179+
- name: Create kind cluster without registry
180+
uses: ./
181+
with:
182+
registry: false
183+
184+
- name: Test
185+
run: |
186+
kubectl cluster-info
187+
kubectl get storageclass standard
188+
189+
if [[ -n "$(docker ps --filter "name=kind-registry" --format "{{.ID}}")" ]]; then
190+
echo "Registry is present"
191+
exit 1
192+
fi
193+
194+
test-with-registry:
195+
runs-on: ubuntu-latest
196+
steps:
197+
- name: Checkout
198+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
199+
200+
- name: Create kind cluster with registry
201+
id: kind
202+
uses: ./
203+
with:
204+
registry: true
205+
registry_name: custom-registry
206+
registry_port: 5001
207+
208+
- name: Test
209+
env:
210+
LOCAL_REGISTRY: ${{ steps.kind.outputs.LOCAL_REGISTRY }}
211+
run: |
212+
kubectl cluster-info
213+
kubectl get storageclass standard
214+
215+
if [[ -z "$(docker ps --filter "name=custom-registry" --format "{{.ID}}")" ]]; then
216+
echo "Registry is not present"
217+
exit 1
218+
fi
219+
220+
docker pull busybox
221+
docker tag busybox $LOCAL_REGISTRY/localbusybox
222+
docker push $LOCAL_REGISTRY/localbusybox
223+
224+
kubectl create job test --image=$LOCAL_REGISTRY/localbusybox
225+
kubectl wait --for=condition=complete --timeout=30s job/test
226+
227+
test-with-registry-and-delete-enabled:
228+
runs-on: ubuntu-latest
229+
steps:
230+
- name: Checkout
231+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
232+
233+
- name: Create kind cluster with registry and delete enabled
234+
id: kind
235+
uses: ./
236+
with:
237+
registry: true
238+
registry_name: custom-registry
239+
registry_port: 5001
240+
registry_enable_delete: true
241+
242+
- name: Test
243+
env:
244+
LOCAL_REGISTRY: ${{ steps.kind.outputs.LOCAL_REGISTRY }}
245+
run: |
246+
kubectl cluster-info
247+
kubectl get storageclass standard
248+
249+
if [[ -z "$(docker ps --filter "name=custom-registry" --format "{{.ID}}")" ]]; then
250+
echo "Registry is not present"
251+
exit 1
252+
fi
253+
254+
docker pull busybox
255+
docker tag busybox $LOCAL_REGISTRY/localbusybox
256+
257+
DIGEST=$(docker push $LOCAL_REGISTRY/localbusybox | grep -oE 'sha256:\w+')
258+
259+
curl -X DELETE $LOCAL_REGISTRY/v2/localbusybox/manifests/$DIGEST
260+
[[ "$(curl -Ls $LOCAL_REGISTRY/v2/localbusybox/tags/list | jq .tags)" == null ]]
261+

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ For more information on inputs, see the [API Documentation](https://developer.gi
2222
- `wait`: The duration to wait for the control plane to become ready (default: `60s`)
2323
- `verbosity`: info log verbosity, higher value produces more output
2424
- `kubectl_version`: The kubectl version to use (default: v1.30.4)
25+
- `registry`: Whether to configure an insecure local registry (default: false)
26+
- `registry_image`: The registry image to use (default: registry:2)
27+
- `registry_name`: The registry name to use (default: kind-registry)
28+
- `registry_port`: The local port used to bind the registry (default: 5000)
29+
- `registry_enable_delete`: Enable delete operations on the registry (default: false)
2530
- `install_only`: Skips cluster creation, only install kind (default: false)
2631
- `ignore_failed_clean`: Whether to ignore the post delete cluster action failing (default: false)
2732

@@ -45,6 +50,43 @@ jobs:
4550
This uses [@helm/kind-action](https://github.com/helm/kind-action) GitHub Action to spin up a [kind](https://kind.sigs.k8s.io/) Kubernetes cluster on every Pull Request.
4651
See [@helm/chart-testing-action](https://github.com/helm/chart-testing-action) for a more practical example.
4752
53+
### Configuring Local Registry
54+
55+
Create a workflow (eg: `.github/workflows/create-cluster-with-registry.yml`):
56+
57+
58+
```yaml
59+
name: Create Cluster with Registry
60+
61+
on: pull_request
62+
63+
jobs:
64+
create-cluster-with-registry:
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Kubernetes KinD Cluster
68+
id: kind
69+
uses: helm/kind-action@v1
70+
with:
71+
registry: true
72+
registry_name: my-registry
73+
registry_port: 5001
74+
registry_enable_delete: true
75+
```
76+
77+
This will configure the cluster with an insecure local registry at `my-registry:5001` on both the host and within cluster. Subsequent steps can refer to the registry address with the output of the kind setup step (i.e. `${{ steps.kind.outputs.LOCAL_REGISTRY }}`).
78+
79+
**Note**: If `config` option is used, you must manually configure the cluster nodes with registry config dir enabled at `/etc/containerd/certs.d`. For example:
80+
81+
```yaml
82+
kind: Cluster
83+
apiVersion: kind.x-k8s.io/v1alpha4
84+
containerdConfigPatches:
85+
- |-
86+
[plugins."io.containerd.grpc.v1.cri".registry]
87+
config_path = "/etc/containerd/certs.d"
88+
```
89+
4890
## Code of conduct
4991

5092
Participation in the Helm community is governed by the [Code of Conduct](CODE_OF_CONDUCT.md).

action.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ inputs:
3434
description: "The kubectl version to use (default: v1.31.4)"
3535
required: false
3636
default: "v1.31.4"
37+
registry:
38+
description: "Whether to configure an insecure local registry (default: false)"
39+
required: false
40+
default: "false"
41+
registry_image:
42+
description: "The registry image to use (default: registry:2)"
43+
required: false
44+
default: "registry:2"
45+
registry_name:
46+
description: "The registry name to use (default: kind-registry)"
47+
required: false
48+
default: "kind-registry"
49+
registry_port:
50+
description: "The local port used to bind the registry (default: 5000)"
51+
required: false
52+
default: "5000"
53+
registry_enable_delete:
54+
description: "Enable delete operations on the registry (default: false)"
55+
required: false
56+
default: "false"
3757
install_only:
3858
description: "Skips cluster creation, only install kind (default: false)"
3959
required: false

cleanup.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ set -o nounset
1919
set -o pipefail
2020

2121
DEFAULT_CLUSTER_NAME=chart-testing
22+
DEFAULT_REGISTRY_NAME=kind-registry
2223

2324
main() {
24-
args=()
25-
26-
if [[ -n "${INPUT_CLUSTER_NAME:-}" ]]; then
27-
args+=(--name "${INPUT_CLUSTER_NAME}")
28-
else
29-
args+=(--name "${DEFAULT_CLUSTER_NAME}")
30-
fi
25+
args=(--name "${INPUT_CLUSTER_NAME:-$DEFAULT_CLUSTER_NAME}")
26+
registry_args=("${INPUT_REGISTRY_NAME:-$DEFAULT_REGISTRY_NAME}")
3127

28+
docker rm -f "${registry_args[@]}" || "${INPUT_IGNORE_FAILED_CLEAN}"
29+
3230
kind delete cluster "${args[@]}" || "${INPUT_IGNORE_FAILED_CLEAN}"
3331
}
3432

kind.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Usage: $(basename "$0") <options>
3636
-l, --verbosity info log verbosity, higher value produces more output
3737
-k, --kubectl-version The kubectl version to use (default: $DEFAULT_KUBECTL_VERSION)
3838
-o, --install-only Skips cluster creation, only install kind (default: false)
39+
--with-registry Enables registry config dir for the cluster (default: false)
3940
4041
EOF
4142
}
@@ -50,6 +51,8 @@ main() {
5051
local verbosity=
5152
local kubectl_version="${DEFAULT_KUBECTL_VERSION}"
5253
local install_only=false
54+
local with_registry=false
55+
local config_with_registry_path="/etc/kind-registry/config.yaml"
5356

5457
parse_command_line "$@"
5558

@@ -187,6 +190,14 @@ parse_command_line() {
187190
install_only=true
188191
fi
189192
;;
193+
--with-registry)
194+
if [[ -n "${2:-}" ]]; then
195+
with_registry="$2"
196+
shift
197+
else
198+
with_registry=true
199+
fi
200+
;;
190201
*)
191202
break
192203
;;
@@ -220,6 +231,20 @@ install_kubectl() {
220231
chmod +x "${kubectl_dir}/kubectl"
221232
}
222233

234+
create_config_with_registry() {
235+
sudo mkdir -p $(dirname "$config_with_registry_path")
236+
cat <<EOF | sudo tee "$config_with_registry_path"
237+
kind: Cluster
238+
apiVersion: kind.x-k8s.io/v1alpha4
239+
containerdConfigPatches:
240+
- |-
241+
[plugins."io.containerd.grpc.v1.cri".registry]
242+
config_path = "/etc/containerd/certs.d"
243+
244+
EOF
245+
sudo chmod a+r "$config_with_registry_path"
246+
}
247+
223248
create_kind_cluster() {
224249
echo 'Creating kind cluster...'
225250
local args=(create cluster "--name=${cluster_name}" "--wait=${wait}")
@@ -240,6 +265,15 @@ create_kind_cluster() {
240265
args+=("--verbosity=${verbosity}")
241266
fi
242267

268+
if [[ "${with_registry}" == true ]]; then
269+
if [[ -n "${config}" ]]; then
270+
echo 'WARNING: when using the "config" option, you need to manually configure the registry in the provided configurations'
271+
else
272+
create_config_with_registry
273+
args+=(--config "$config_with_registry_path")
274+
fi
275+
fi
276+
243277
"${kind_dir}/kind" "${args[@]}"
244278
}
245279

main.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ set -o pipefail
2121
SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}" || realpath "${BASH_SOURCE[0]}")")
2222

2323
main() {
24-
args=()
24+
local args=()
25+
local registry_args=()
2526

2627
if [[ -n "${INPUT_VERSION:-}" ]]; then
2728
args+=(--version "${INPUT_VERSION}")
@@ -41,6 +42,7 @@ main() {
4142

4243
if [[ -n "${INPUT_CLUSTER_NAME:-}" ]]; then
4344
args+=(--cluster-name "${INPUT_CLUSTER_NAME}")
45+
registry_args+=(--cluster-name "${INPUT_CLUSTER_NAME}")
4446
fi
4547

4648
if [[ -n "${INPUT_WAIT:-}" ]]; then
@@ -59,7 +61,31 @@ main() {
5961
args+=(--install-only)
6062
fi
6163

64+
if [[ -n "${INPUT_REGISTRY:-}" ]]; then
65+
args+=(--with-registry "${INPUT_REGISTRY}")
66+
fi
67+
68+
if [[ -n "${INPUT_REGISTRY_IMAGE:-}" ]]; then
69+
registry_args+=(--registry-image "${INPUT_REGISTRY_IMAGE}")
70+
fi
71+
72+
if [[ -n "${INPUT_REGISTRY_NAME:-}" ]]; then
73+
registry_args+=(--registry-name "${INPUT_REGISTRY_NAME}")
74+
fi
75+
76+
if [[ -n "${INPUT_REGISTRY_PORT:-}" ]]; then
77+
registry_args+=(--registry-port "${INPUT_REGISTRY_PORT}")
78+
fi
79+
80+
if [[ -n "${INPUT_REGISTRY_ENABLE_DELETE:-}" ]]; then
81+
registry_args+=(--enable-delete "${INPUT_REGISTRY_ENABLE_DELETE}")
82+
fi
83+
6284
"${SCRIPT_DIR}/kind.sh" ${args[@]+"${args[@]}"}
85+
86+
if [[ "${INPUT_REGISTRY:-}" == true ]]; then
87+
"${SCRIPT_DIR}/registry.sh" "${registry_args[@]}"
88+
fi
6389
}
6490

6591
main

0 commit comments

Comments
 (0)