Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion builtin/core/playbooks/artifact_export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,26 @@
roles:
- download
- copy

- role: download/web-installer
when:
- .download.web_installer.download_web_installer
pre_tasks:
- name: Artifact | Set artifact file dir
when:
- .artifact_file | empty | not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be download.artifact_file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err := unstructured.SetNestedField(o.Config.Value(), o.Artifact, "artifact_file"); err != nil {

no , it IS artifact_file

set_fact:
artifact_file_dir: >-
{{ .artifact_file | dir }}/artifact
- name: Artifact | Create artifact dir
when:
- .artifact_file_dir | empty | not
command: >-
mkdir -p {{ .artifact_file_dir }}/kubekey/kubekey/
Comment on lines +32 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using the command module with mkdir -p is not idempotent and goes against Ansible best practices. It's better to use the file module to ensure a directory exists. This makes the task more declarative and reliable.

      file:
        path: "{{ .artifact_file_dir }}/kubekey/kubekey/"
        state: directory
        mode: '0755'

post_tasks:
- name: Artifact | Export artifact
when:
- .pack_artifact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what‘s this

Copy link
Contributor Author

@zuoxuesong-worker zuoxuesong-worker Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

artifact file will store in artifact_file_path before pack , add an arg named pack_artifact gave user a choice wheather pack artifact_file or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when user want to make a package with multi version k8s , then user can do

./kk artifact export -c first.yaml --workdir prepare -a $(pwd)/artifact.tgz
./kk artifact export -c second.yaml --workdir prepare -a $(pwd)/artifact.tgz
./kk artifact export -c last.yaml --workdir prepare -a $(pwd)/artifact.tgz --set pack_artifact=true,download.web_installer.download_web_installer=true

kk will download web-installer and make tgz only in the last cmd executed

- .artifact_file_dir | empty | not
command: |
cp kk {{ .artifact_file_dir }}/
cd {{ .artifact_file_dir }} && tar -czvf {{ .artifact_file }} *
Comment on lines +39 to +41

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using command with cp and tar is not idempotent and is less readable than using Ansible's dedicated modules. I suggest refactoring this to use the copy and archive modules. This improves idempotency, error handling, and clarity. I've wrapped them in a block to group the related actions.

      block:
        - name: Artifact | Copy kk binary to artifact dir
          copy:
            src: kk
            dest: "{{ .artifact_file_dir }}/"
            remote_src: yes
        - name: Artifact | Create artifact archive
          archive:
            path: "{{ .artifact_file_dir }}/"
            dest: "{{ .artifact_file }}"
            format: gz

14 changes: 0 additions & 14 deletions builtin/core/roles/copy/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
---
- 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:
Expand All @@ -23,6 +12,3 @@
tags: ["kubernetes", "image_registry"]
- include_tasks: iso.yaml

- name: Export artifact
command: |
cd {{ .artifact_file_dir }} && tar -czvf {{ .artifact_file }} *
4 changes: 4 additions & 0 deletions builtin/core/roles/defaults/vars/10-download.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ download:
nfs_provisioner: https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/releases/download/nfs-subdir-external-provisioner-4.0.18/nfs-subdir-external-provisioner-{{ .storage_class.nfs_provisioner_version }}.tgz
download_image: false
download_iso: false
web_installer:
download_web_installer: false
url: >-
https://kubekey.pek3b.qingstor.com/github.com/kubesphere/web-installer/releases/latest/download/web-installer.tgz
iso_url:
base_path: >-
{{- if .download.zone | eq "cn" -}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- hosts:
- kube_control_plane[0]
vars_files:
- vars/kubesphere.yaml
tasks:
- name: Copy KubeSphere Helm package to remote host
copy:
src: ks-core.tgz
dest: /etc/kubesphere/ks-core.tgz
- name: Generate values file from schema
run_once: true
copy:
content: >-
{{ index . "ks-core" | toYaml }}
dest: >-
/etc/kubesphere/value.yaml
- name: Deploy or upgrade KubeSphere using Helm
command: |
helm upgrade --install --wait --create-namespace --namespace kubesphere-system -f /etc/kubesphere/value.yaml \
ks-core /etc/kubesphere/ks-core.tgz --reset-values \
--set telemetry.posthog.storage.type=pvc,telemetry.posthog.storage.pvc.resources.requests.storage=10Gi
- name: Output KubeSphere console access information
result:
address: http://{{ .internal_ipv4 }}:30880
user: admin
password: P@88w0rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- hosts:
- kube_control_plane[0]
vars_files:
- vars/kubesphere.yaml
tasks:
- name: Copy KubeSphere Helm package to remote host
copy:
src: ks-core.tgz
dest: /etc/kubesphere/ks-core.tgz
- name: Generate values file from schema
run_once: true
copy:
content: >-
{{ index . "ks-core" | toYaml }}
dest: >-
/etc/kubesphere/value.yaml
- name: Deploy or upgrade KubeSphere using Helm
command: |
helm upgrade --install --wait --create-namespace --namespace kubesphere-system -f /etc/kubesphere/value.yaml \
ks-core /etc/kubesphere/ks-core.tgz --reset-values \
--set telemetry.posthog.storage.type=pvc,telemetry.posthog.storage.pvc.resources.requests.storage=10Gi
- name: Output KubeSphere console access information
result:
address: http://{{ .internal_ipv4 }}:30880
user: admin
password: P@88w0rd
29 changes: 29 additions & 0 deletions builtin/core/roles/download/web-installer/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: WebInstaller | Download web installer
when:
- .download.web_installer.download_web_installer
- .download.web_installer.url | empty | not
- .artifact_file_dir | empty | not
command: |
curl -L -o {{ .artifact_file_dir }}/web-installer.tgz {{ .download.web_installer.url }}
tar -xzf "{{ .artifact_file_dir }}/web-installer.tgz" --no-same-owner -C {{ .artifact_file_dir }}
Comment on lines +7 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of using command with curl and tar, it's highly recommended to use the get_url and unarchive modules. These modules are idempotent, provide better error handling, and make the playbook more readable and maintainable. I've replaced the command with a block containing these modules so they share the same when condition.

  block:
    - name: Download web installer
      get_url:
        url: "{{ .download.web_installer.url }}"
        dest: "{{ .artifact_file_dir }}/web-installer.tgz"

    - name: Extract web installer
      unarchive:
        src: "{{ .artifact_file_dir }}/web-installer.tgz"
        dest: "{{ .artifact_file_dir }}"
        remote_src: yes
        extra_opts: --no-same-owner


- name: WebInstaller | Set Ks Core
when:
- .download.kse_chart_version | empty | not
block:
- name: WebInstaller | Download ks core chart
command: |
helm pull oci://hub.kubesphere.com.cn/kse/ks-core --version {{ .download.kse_chart_version }} -d {{ .artifact_file_dir }}/kubesphere/playbooks/files/
mv {{ .artifact_file_dir }}/kubesphere/playbooks/files/ks-core-{{ .download.kse_chart_version }}.tgz {{ .artifact_file_dir }}/kubesphere/playbooks/files/ks-core.tgz
- name: WebInstaller | Set ks core install task
copy:
src: "install-{{ .download.kse_chart_version }}.yaml"
dest: "{{ .artifact_file_dir }}/kubesphere/playbooks/install.yaml"

- name: WebInstaller | Init config json
when:
- .artifact_file_dir | empty | not
template:
src: config.json
dest: "{{ .artifact_file_dir }}/schema/config.json"
17 changes: 17 additions & 0 deletions builtin/core/roles/download/web-installer/templates/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"kubernetes.json": {
"kubernetes": {
"kube_version": "{{ .kubernetes.kube_version }}",
"control_plane_endpoint": {
"host": "lb.kubesphere.local",
"port": 6443,
"type": "local"
}
},
"cri": {
{{- if .cri.container_manager | empty | not }}
"container_manager": "{{ .cri.container_manager }}"
{{- end }}
}
}
}