From bd780ee397ff2abcc5d03975c32dae056ddb86c5 Mon Sep 17 00:00:00 2001 From: zuoxuesong-worker Date: Fri, 28 Nov 2025 15:58:00 +0800 Subject: [PATCH] feat: add export copy func (#2875) feat: add export copy func feat: add export copy func Signed-off-by: xuesongzuo@yunify.com --- builtin/core/playbooks/artifact_export.yaml | 6 +- builtin/core/roles/copy/tasks/binary.yaml | 176 ++++++++++++++++++++ builtin/core/roles/copy/tasks/helm.yaml | 66 ++++++++ builtin/core/roles/copy/tasks/images.yaml | 12 ++ builtin/core/roles/copy/tasks/iso.yaml | 10 ++ builtin/core/roles/copy/tasks/main.yaml | 28 ++++ hack/downloadKubekey.sh | 4 +- 7 files changed, 296 insertions(+), 6 deletions(-) create mode 100644 builtin/core/roles/copy/tasks/binary.yaml create mode 100644 builtin/core/roles/copy/tasks/helm.yaml create mode 100644 builtin/core/roles/copy/tasks/images.yaml create mode 100644 builtin/core/roles/copy/tasks/iso.yaml create mode 100644 builtin/core/roles/copy/tasks/main.yaml diff --git a/builtin/core/playbooks/artifact_export.yaml b/builtin/core/playbooks/artifact_export.yaml index 034f586d..5495b5cd 100644 --- a/builtin/core/playbooks/artifact_export.yaml +++ b/builtin/core/playbooks/artifact_export.yaml @@ -15,7 +15,5 @@ - localhost roles: - download - tasks: - - name: Export artifact - command: | - cd {{ .binary_dir }} && tar -czvf {{ .artifact_file }} * + - copy + diff --git a/builtin/core/roles/copy/tasks/binary.yaml b/builtin/core/roles/copy/tasks/binary.yaml new file mode 100644 index 00000000..f9134489 --- /dev/null +++ b/builtin/core/roles/copy/tasks/binary.yaml @@ -0,0 +1,176 @@ +- name: Binary | Ensure etcd binary is present + tags: ["etcd"] + loop: "{{ .download.arch | toJson }}" + when: + - .etcd.deployment_type | eq "external" + - .etcd.etcd_version | empty | not + command: | + artifact_name={{ get .download.artifact_url.etcd .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/etcd/{{ .etcd.etcd_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/etcd/{{ .etcd.etcd_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure Kubernetes binaries are present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: .kubernetes.kube_version | empty | not + command: | + kube_path={{ .binary_dir }}/kube/{{ .kubernetes.kube_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/kube/{{ .kubernetes.kube_version }}/{{ .item }} + mkdir -p $target_path + cp $kube_path/kubelet $target_path/ + cp $kube_path/kubeadm $target_path/ + cp $kube_path/kubectl $target_path/ + +- name: Binary | Ensure CNI plugins are present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: .cni.cni_plugins_version | empty | not + command: | + artifact_name={{ get .download.artifact_url.cni_plugins .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cni/plugins/{{ .cni.cni_plugins_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/plugins/{{ .cni.cni_plugins_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure Helm binary is present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: .kubernetes.helm_version | empty | not + command: | + artifact_name={{ get .download.artifact_url.helm .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/helm/{{ .kubernetes.helm_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/helm/{{ .kubernetes.helm_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure crictl binary is present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: .cri.crictl_version | empty | not + command: | + artifact_name={{ get .download.artifact_url.crictl .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/crictl/{{ .cri.crictl_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/crictl/{{ .cri.crictl_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure Docker binary is present + tags: ["kubernetes","image_registry"] + loop: "{{ .download.arch | toJson }}" + when: + - .cri.docker_version | empty | not + - or (.image_registry.type | empty | not) (.cri.container_manager | eq "docker") + command: | + artifact_name={{ get .download.artifact_url.docker .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/docker/{{ .cri.docker_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/docker/{{ .cri.docker_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure cri-dockerd binary is present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: + - .cri.cridockerd_version | empty | not + - .cri.container_manager | eq "docker" + - .kubernetes.kube_version | semverCompare ">=v1.24.0" + command: | + artifact_name={{ get .download.artifact_url.cridockerd .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cri-dockerd/{{ .cri.cridockerd_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cri-dockerd/{{ .cri.cridockerd_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure containerd binary is present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: + - .cri.containerd_version | empty | not + - .cri.container_manager | eq "containerd" + command: | + artifact_name={{ get .download.artifact_url.containerd .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/containerd/{{ .cri.containerd_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/containerd/{{ .cri.containerd_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure runc binary is present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: + - .cri.runc_version | empty | not + - .cri.container_manager | eq "containerd" + command: | + artifact_name={{ get .download.artifact_url.runc .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/runc/{{ .cri.runc_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/runc/{{ .cri.runc_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure calicoctl binary is present + tags: ["kubernetes"] + loop: "{{ .download.arch | toJson }}" + when: + - .cni.calico_version | empty | not + - .cni.type | eq "calico" + command: | + artifact_name=calicoctl + artifact_path={{ .binary_dir }}/cni/calico/{{ .cni.calico_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/calico/{{ .cni.calico_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure Docker Registry binary is present + tags: ["image_registry"] + loop: "{{ .download.arch | toJson }}" + when: + - .image_registry.docker_registry_version | empty | not + - .image_registry.type | eq "docker-registry" + command: | + artifact_name={{ get .download.artifact_url.docker_registry .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/image-registry/docker-registry/{{ .image_registry.docker_registry_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/image-registry/docker-registry/{{ .image_registry.docker_registry_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Binary | Ensure docker-compose binary is present + tags: ["image_registry"] + loop: "{{ .download.arch | toJson }}" + when: + - .cri.dockercompose_version | empty | not + - .image_registry.type | eq "harbor" + command: | + compose_name=docker-compose + compose_path={{ .binary_dir }}/image-registry/docker-compose/{{ .cri.dockercompose_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/image-registry/docker-compose/{{ .cri.dockercompose_version }}/{{ .item }} + mkdir -p $target_path + cp $compose_path/$compose_name $target_path/ + +- name: Binary | Ensure Harbor binary is present + tags: ["image_registry"] + loop: "{{ .download.arch | toJson }}" + when: + - .image_registry.harbor_version | empty | not + - .image_registry.type | eq "harbor" + command: | + harbor_name={{ get .download.artifact_url.harbor .item | splitList "/" | last }} + harbor_path={{ .binary_dir }}/image-registry/harbor/{{ .image_registry.harbor_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/image-registry/harbor/{{ .image_registry.harbor_version }}/{{ .item }} + mkdir -p $target_path + cp $harbor_path/$harbor_name $target_path/ + +- name: Binary | Ensure keepalived binary is present + tags: ["image_registry"] + loop: "{{ .download.arch | toJson }}" + when: + - .image_registry.keepalived_version | empty | not + - .image_registry.ha_vip | empty | not + - .groups.image_registry | len | lt 1 + command: | + artifact_name={{ get .download.artifact_url.keepalived .item | splitList "/" | last }} + artifact_path={{ .binary_dir }}/image-registry/keepalived/{{ .keepalived_version }}/{{ .item }} + target_path={{ .artifact_file_dir }}/kubekey/kubekey/image-registry/keepalived/{{ .keepalived_version }}/{{ .item }} + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ diff --git a/builtin/core/roles/copy/tasks/helm.yaml b/builtin/core/roles/copy/tasks/helm.yaml new file mode 100644 index 00000000..11878fe5 --- /dev/null +++ b/builtin/core/roles/copy/tasks/helm.yaml @@ -0,0 +1,66 @@ +--- +- name: Helm | Ensure the Calico binary is available + when: + - .cni.calico_version | empty | not + - .cni.type | eq "calico" + command: | + artifact_name={{ .download.artifact_url.calico | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cni/calico + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/calico + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Helm | Ensure the Cilium binary is available + when: + - .cni.cilium_version | empty | not + - .cni.type | eq "cilium" + command: | + artifact_name={{ .download.artifact_url.cilium | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cni/cilium + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/cilium + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Helm | Ensure the Flannel binary is available + when: + - .cni.flannel_version | empty | not + - .cni.type | eq "flannel" + command: | + artifact_name={{ .download.artifact_url.flannel | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cni/flannel + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/flannel + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Helm | Ensure the Kube-OVN binary is available + when: + - .kubeovn_version | empty | not + - .cni.type | eq "kubeovn" + command: | + artifact_name={{ .download.artifact_url.kubeovn | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cni/kubeovn + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/kubeovn + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Helm | Ensure the Hybridnet binary is available + when: + - .cni.hybridnet_version | empty | not + - .cni.type | eq "hybridnet" + command: | + artifact_name={{ .download.artifact_url.hybridnet | splitList "/" | last }} + artifact_path={{ .binary_dir }}/cni/hybridnet + target_path={{ .artifact_file_dir }}/kubekey/kubekey/cni/hybridnet + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ + +- name: Helm | Ensure the NFS Provisioner binary is available + when: + - .storage_class.nfs_provisioner_version | empty | not + - .storage_class.nfs.enabled + command: | + artifact_name={{ .download.artifact_url.nfs_provisioner | splitList "/" | last }} + artifact_path={{ .binary_dir }}/sc + target_path={{ .artifact_file_dir }}/kubekey/kubekey/sc + mkdir -p $target_path + cp $artifact_path/$artifact_name $target_path/ diff --git a/builtin/core/roles/copy/tasks/images.yaml b/builtin/core/roles/copy/tasks/images.yaml new file mode 100644 index 00000000..f319b734 --- /dev/null +++ b/builtin/core/roles/copy/tasks/images.yaml @@ -0,0 +1,12 @@ +- name: Image | Ensure container images is available + image: + copy: + from: + path: >- + {{ .binary_dir }}/images/ + manifests: "{{ .image_manifests | toJson }}" + to: + path: >- + {{ .artifact_file_dir }}/kubekey/kubekey/images/ + when: + - .image_manifests | default list | empty | not diff --git a/builtin/core/roles/copy/tasks/iso.yaml b/builtin/core/roles/copy/tasks/iso.yaml new file mode 100644 index 00000000..9d1143cb --- /dev/null +++ b/builtin/core/roles/copy/tasks/iso.yaml @@ -0,0 +1,10 @@ +--- +- name: ISO | Ensure ISO files is available + when: .download.iso_url.urls | empty | not + loop: "{{ .download.iso_url.urls | toJson }}" + command: | + iso_path={{ .binary_dir }}/repository/{{ .item | splitList "/" | last }} + if [ ! -f {{ .artifact_file_dir }}/kubekey/kubekey/repository/ ];then + mkdir -p {{ .artifact_file_dir }}/kubekey/kubekey/repository/ + fi + cp $iso_path {{ .artifact_file_dir }}/kubekey/kubekey/repository/ \ No newline at end of file diff --git a/builtin/core/roles/copy/tasks/main.yaml b/builtin/core/roles/copy/tasks/main.yaml new file mode 100644 index 00000000..5b911d09 --- /dev/null +++ b/builtin/core/roles/copy/tasks/main.yaml @@ -0,0 +1,28 @@ +--- +- name: Copy | Set artifact file dir + when: + - .artifact_file | empty | not + set_fact: + artifact_file_dir: >- + {{ .artifact_file | dir }}/artifact + +- name: Copy | Create artifact file dir + command: >- + mkdir -p {{ .artifact_file_dir }}/kubekey/kubekey/ + +- name: Artifact | Copy required binaries and images + when: .artifact_file_dir | empty | not + block: + # Download core binaries + - include_tasks: binary.yaml + # Download Helm and CNI binaries + - include_tasks: helm.yaml + tags: ["kubernetes"] + # Download remote images to the local images directory + - include_tasks: images.yaml + tags: ["kubernetes", "image_registry"] + - include_tasks: iso.yaml + +- name: Export artifact + command: | + cd {{ .artifact_file_dir }} && tar -czvf {{ .artifact_file }} * diff --git a/hack/downloadKubekey.sh b/hack/downloadKubekey.sh index 10f314b7..012a6441 100755 --- a/hack/downloadKubekey.sh +++ b/hack/downloadKubekey.sh @@ -159,10 +159,10 @@ if [ $? -ne 0 ]; then fi echo "Preparing offline package directory..." -mkdir -p offline/kubekey/kubekey +mkdir -p offline/ echo "Extracting artifact.tgz to offline/ ..." -tar -xzf artifact.tgz -C offline/kubekey/kubekey --no-same-owner +tar -xzf artifact.tgz -C offline/ --no-same-owner echo "Extracting web-installer.tgz to offline/ ..." tar -xzf web-installer.tgz -C offline/ --no-same-owner