kubekey/builtin/core/roles/image-registry/tasks/main.yaml
2025-12-02 15:20:59 +08:00

44 lines
1.7 KiB
YAML

---
- name: ImageRegistry | Ensure Harbor project exists for each image
when: .image_registry.type | eq "harbor"
command: |
# Traverse first-level subdirectories in images_dir, skipping 'blobs'
for registry_dir in {{ .binary_dir }}/images/*; do
if [ ! -d "$registry_dir" ] || [ "$(basename "$registry_dir")" = "blobs" ]; then
continue
fi
# Traverse second-level subdirectories in each registry_dir
for project_dir in "$registry_dir"/*; do
if [ ! -d "$project_dir" ]; then
continue
fi
project=$(basename "$project_dir")
# Check if the Harbor project exists; create it if it does not
resp=$(curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X GET "https://{{ .image_registry.auth.registry }}/api/v2.0/projects/${project}")
if echo "$resp" | grep -q '"code":"NOT_FOUND"'; then
curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X POST \
-H "Content-Type: application/json" \
"https://{{ .image_registry.auth.registry }}/api/v2.0/projects" \
-d "{ \"project_name\": \"${project}\", \"public\": true}"
fi
done
done
- name: ImageRegistry | Push images package to image registry
run_once: true
image:
push:
auths:
- repo: "{{ .image_registry.auth.registry }}"
username: "{{ .image_registry.auth.username }}"
password: "{{ .image_registry.auth.password }}"
insecure: true
images_dir: >-
{{ .binary_dir }}/images/
dest: >-
{{ .image_registry.auth.registry }}/{{ .module.image.src.reference.repository }}:{{ .module.image.src.reference.reference }}