Skip to content

Commit

Permalink
feat: options to configure local registry
Browse files Browse the repository at this point in the history
Signed-off-by: Thuan Vo <thuan.votann@gmail.com>
  • Loading branch information
tthvo committed Jul 30, 2024
1 parent b04abaf commit 5868eca
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 9 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,93 @@ jobs:
run: |
kubectl cluster-info
kubectl get nodes
test-without-registry:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4

- name: Create kind cluster without registry
uses: ./
with:
registry: false

- name: Test
run: |
kubectl cluster-info
kubectl get storageclass standard
if [[ -n "$(docker ps --filter "name=kind-registry" --format "{{.ID}}")" ]]; then
echo "Registry is present"
exit 1
fi
test-with-registry:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4

- name: Create kind cluster with registry
id: kind
uses: ./
with:
registry: true
registry_name: custom-registry
registry_port: 5001

- name: Test
env:
LOCAL_REGISTRY: ${{ steps.kind.outputs.LOCAL_REGISTRY }}
run: |
kubectl cluster-info
kubectl get storageclass standard
if [[ -z "$(docker ps --filter "name=custom-registry" --format "{{.ID}}")" ]]; then
echo "Registry is not present"
exit 1
fi
docker pull busybox
docker tag busybox $LOCAL_REGISTRY/localbusybox
docker push $LOCAL_REGISTRY/localbusybox
kubectl create job test --image=$LOCAL_REGISTRY/localbusybox
kubectl wait --for=condition=complete --timeout=30s job/test
test-with-registry-and-delete-enabled:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4

- name: Create kind cluster with registry and delete enabled
id: kind
uses: ./
with:
registry: true
registry_name: custom-registry
registry_port: 5001
registry_enable_delete: true

- name: Test
env:
LOCAL_REGISTRY: ${{ steps.kind.outputs.LOCAL_REGISTRY }}
run: |
kubectl cluster-info
kubectl get storageclass standard
if [[ -z "$(docker ps --filter "name=custom-registry" --format "{{.ID}}")" ]]; then
echo "Registry is not present"
exit 1
fi
docker pull busybox
docker tag busybox $LOCAL_REGISTRY/localbusybox
DIGEST=$(docker push $LOCAL_REGISTRY/localbusybox | grep -oE 'sha256:\w+')
curl -X DELETE $LOCAL_REGISTRY/v2/localbusybox/manifests/$DIGEST
[[ "$(curl -Ls $LOCAL_REGISTRY/v2/localbusybox/tags/list | jq .tags)" == null ]]
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Kind Action

[![](https://github.com/k8s-crafts/kind-action/workflows/Test/badge.svg?branch=main)](https://github.com/k8s-crafts/kind-action/actions)
[![Test](https://github.com/k8s-crafts/kind-action/actions/workflows/test.yaml/badge.svg)](https://github.com/k8s-crafts/kind-action/actions/workflows/test.yaml)

A GitHub Action for Kubernetes IN Docker - local clusters for testing Kubernetes using [kubernetes-sigs/kind](https://kind.sigs.k8s.io/).

Expand All @@ -22,6 +22,11 @@ For more information on inputs, see the [API Documentation](https://developer.gi
- `wait`: The duration to wait for the control plane to become ready (default: `60s`)
- `verbosity`: info log verbosity, higher value produces more output
- `kubectl_version`: The kubectl version to use (default: `v1.28.6`)
- `registry`: Whether to configure an insecure local registry (default: `false`)
- `registry_image`: The registry image to use (default: `registry:2`)
- `registry_name`: The registry name to use (default: `kind-registry`)
- `registry_port`: The local port used to bind the registry (default: `5000`)
- `registry_enable_delete`: Enable delete operations on the registry (default: `false`)
- `install_only`: Skips cluster creation, only install kind (default: `false`)
- `ignore_failed_clean`: Whether to ignore the post delete cluster action failing (default: `false`)

Expand All @@ -44,6 +49,43 @@ jobs:
This uses [@k8s-crafts/kind-action](https://github.com/k8s-crafts/kind-action) GitHub Action to spin up a [kind](https://kind.sigs.k8s.io/) Kubernetes cluster on every Pull Request.
### Configuring Local Registry
Create a workflow (eg: `.github/workflows/create-cluster-with-registry.yml`):


```yaml
name: Create Cluster with Registry
on: pull_request
jobs:
create-cluster-with-registry:
runs-on: ubuntu-latest
steps:
- name: Kubernetes KinD Cluster
id: kind
uses: k8s-crafts/kind-action@v1
with:
registry: true
registry_name: my-registry
registry_port: 5001
registry_enable_delete: true
```

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 }}`).

**Note**: If `config` option is used, you must manually configure the cluster with registry config dir enabled at `/etc/containerd/certs.d`. For example:

```yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
```

## Code of conduct

Participation is governed by the [Code of Conduct](CODE_OF_CONDUCT.md).
Expand Down
20 changes: 20 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ inputs:
description: "The kubectl version to use (default: v1.29.3)"
required: false
default: "v1.29.3"
registry:
description: "Whether to configure an insecure local registry (default: false)"
required: false
default: "false"
registry_image:
description: "The registry image to use (default: registry:2)"
required: false
default: "registry:2"
registry_name:
description: "The registry name to use (default: kind-registry)"
required: false
default: "kind-registry"
registry_port:
description: "The local port used to bind the registry (default: 5000)"
required: false
default: "5000"
registry_enable_delete:
description: "Enable delete operations on the registry (default: false)"
required: false
default: "false"
install_only:
description: "Skips cluster creation, only install kind (default: false)"
required: false
Expand Down
12 changes: 5 additions & 7 deletions cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ set -o nounset
set -o pipefail

DEFAULT_CLUSTER_NAME=chart-testing
DEFAULT_REGISTRY_NAME=kind-registry

main() {
args=()

if [[ -n "${INPUT_CLUSTER_NAME:-}" ]]; then
args+=(--name "${INPUT_CLUSTER_NAME}")
else
args+=(--name "${DEFAULT_CLUSTER_NAME}")
fi
args=(--name "${INPUT_CLUSTER_NAME:-$DEFAULT_CLUSTER_NAME}")
registry_args=("${INPUT_REGISTRY_NAME:-$DEFAULT_REGISTRY_NAME}")

docker rm -f "${registry_args[@]}" || "${INPUT_IGNORE_FAILED_CLEAN}"

kind delete cluster "${args[@]}" || "${INPUT_IGNORE_FAILED_CLEAN}"
}

Expand Down
34 changes: 34 additions & 0 deletions kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Usage: $(basename "$0") <options>
-l, --verbosity info log verbosity, higher value produces more output
-k, --kubectl-version The kubectl version to use (default: $DEFAULT_KUBECTL_VERSION)
-o, --install-only Skips cluster creation, only install kind (default: false)
--with-registry Enables registry config dir for the cluster (default: false)
EOF
}
Expand All @@ -48,6 +49,8 @@ main() {
local verbosity=
local kubectl_version="${DEFAULT_KUBECTL_VERSION}"
local install_only=false
local with_registry=false
local config_with_registry_path="/etc/kind-registry/config.yaml"

parse_command_line "$@"

Expand Down Expand Up @@ -175,6 +178,14 @@ parse_command_line() {
install_only=true
fi
;;
--with-registry)
if [[ -n "${2:-}" ]]; then
with_registry="$2"
shift
else
with_registry=true
fi
;;
*)
break
;;
Expand Down Expand Up @@ -208,6 +219,20 @@ install_kubectl() {
chmod +x "${kubectl_dir}/kubectl"
}

create_config_with_registry() {
sudo mkdir -p $(dirname "$config_with_registry_path")
cat <<EOF | sudo tee "$config_with_registry_path"
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
EOF
sudo chmod a+r "$config_with_registry_path"
}

create_kind_cluster() {
echo 'Creating kind cluster...'
local args=(create cluster "--name=${cluster_name}" "--wait=${wait}")
Expand All @@ -224,6 +249,15 @@ create_kind_cluster() {
args+=("--verbosity=${verbosity}")
fi

if [[ "${with_registry}" == true ]]; then
if [[ -n "${config}" ]]; then
echo 'WARNING: when using the "config" option, you need to manually configure the registry in the provided configurations'
else
create_config_with_registry
args+=(--config "$config_with_registry_path")
fi
fi

"${kind_dir}/kind" "${args[@]}"
}

Expand Down
28 changes: 27 additions & 1 deletion main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ set -o pipefail
SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}" || realpath "${BASH_SOURCE[0]}")")

main() {
args=()
local args=()
local registry_args=()

if [[ -n "${INPUT_VERSION:-}" ]]; then
args+=(--version "${INPUT_VERSION}")
Expand All @@ -37,6 +38,7 @@ main() {

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

if [[ -n "${INPUT_WAIT:-}" ]]; then
Expand All @@ -55,7 +57,31 @@ main() {
args+=(--install-only)
fi

if [[ -n "${INPUT_REGISTRY:-}" ]]; then
args+=(--with-registry "${INPUT_REGISTRY}")
fi

if [[ -n "${INPUT_REGISTRY_IMAGE:-}" ]]; then
registry_args+=(--registry-image "${INPUT_REGISTRY_IMAGE}")
fi

if [[ -n "${INPUT_REGISTRY_NAME:-}" ]]; then
registry_args+=(--registry-name "${INPUT_REGISTRY_NAME}")
fi

if [[ -n "${INPUT_REGISTRY_PORT:-}" ]]; then
registry_args+=(--registry-port "${INPUT_REGISTRY_PORT}")
fi

if [[ -n "${INPUT_REGISTRY_ENABLE_DELETE:-}" ]]; then
registry_args+=(--enable-delete "${INPUT_REGISTRY_ENABLE_DELETE}")
fi

"${SCRIPT_DIR}/kind.sh" ${args[@]+"${args[@]}"}

if [[ "${INPUT_REGISTRY:-}" == true ]]; then
"${SCRIPT_DIR}/registry.sh" "${registry_args[@]}"
fi
}

main
Loading

0 comments on commit 5868eca

Please sign in to comment.