diff --git a/api/core/v1alpha1/task_types.go b/api/core/v1alpha1/task_types.go index 325d78fc..c4b120de 100644 --- a/api/core/v1alpha1/task_types.go +++ b/api/core/v1alpha1/task_types.go @@ -53,8 +53,9 @@ type TaskSpec struct { FailedWhen []string `json:"failedWhen,omitempty"` Loop runtime.RawExtension `json:"loop,omitempty"` - Module Module `json:"module,omitempty"` - Register string `json:"register,omitempty"` + Module Module `json:"module,omitempty"` + Register string `json:"register,omitempty"` + RegisterType string `json:"register_type,omitempty"` } // Module of Task diff --git a/api/project/v1/block.go b/api/project/v1/block.go index c945ba38..41bd847c 100644 --- a/api/project/v1/block.go +++ b/api/project/v1/block.go @@ -62,8 +62,10 @@ type Task struct { LoopControl LoopControl `yaml:"loop_control,omitempty"` Poll int `yaml:"poll,omitempty"` Register string `yaml:"register,omitempty"` - Retries int `yaml:"retries,omitempty"` - Until When `yaml:"until,omitempty"` + // RegisterType how to register value to variable. support: string(default), json, yaml. + RegisterType string `yaml:"register_type,omitempty"` + Retries int `yaml:"retries,omitempty"` + Until When `yaml:"until,omitempty"` // deprecated, used to be loop and loop_args but loop has been repurposed //LoopWith string `yaml:"loop_with"` diff --git a/builtin/capkk/playbooks/add_node.yaml b/builtin/capkk/playbooks/add_node.yaml index 5a0f085a..4441f8ce 100644 --- a/builtin/capkk/playbooks/add_node.yaml +++ b/builtin/capkk/playbooks/add_node.yaml @@ -21,9 +21,10 @@ command: | cat {{ .cloud_config_dir }}/cloud-config/value register: cloud_config_out + register_type: yaml - name: set_fact of cloud-config value set_fact: - cloud_config: "{{ .cloud_config_out.stdout | fromYaml | toJson }}" + cloud_config: "{{ .cloud_config_out.stdout | toJson }}" roles: - role: init/init-artifacts when: .kubernetes_installed | default false | eq false diff --git a/builtin/capkk/roles/install/cni/tasks/calico.yaml b/builtin/capkk/roles/install/cni/tasks/calico.yaml index 127ef74b..32dc9c86 100644 --- a/builtin/capkk/roles/install/cni/tasks/calico.yaml +++ b/builtin/capkk/roles/install/cni/tasks/calico.yaml @@ -3,6 +3,7 @@ ignore_errors: true command: calicoctl version register: calicoctl_install_version + register_type: yaml - name: Install calicoctl when: .calicoctl_install_version.stderr | ne "" block: diff --git a/builtin/capkk/roles/install/cni/tasks/main.yaml b/builtin/capkk/roles/install/cni/tasks/main.yaml index a52b646c..140766da 100644 --- a/builtin/capkk/roles/install/cni/tasks/main.yaml +++ b/builtin/capkk/roles/install/cni/tasks/main.yaml @@ -2,6 +2,7 @@ - name: check cni by helm command: helm list -a -A -q -o json 2>/dev/null register: installed_helm_packages + register_type: json - include_tasks: calico.yaml when: diff --git a/builtin/capkk/roles/install/kubernetes/tasks/main.yaml b/builtin/capkk/roles/install/kubernetes/tasks/main.yaml index 196d2c75..6f5d2876 100644 --- a/builtin/capkk/roles/install/kubernetes/tasks/main.yaml +++ b/builtin/capkk/roles/install/kubernetes/tasks/main.yaml @@ -32,6 +32,7 @@ ignore_errors: true command: kubectl version --short register: kubectl_install_version + register_type: yaml - name: Sync kubectl to remote when: | or (.kubectl_install_version.stderr | ne "") ((get .kubectl_install_version.stdout "Server Version") | ne .kube_version) diff --git a/builtin/core/roles/install/cni/tasks/calico.yaml b/builtin/core/roles/install/cni/tasks/calico.yaml index 127ef74b..32dc9c86 100644 --- a/builtin/core/roles/install/cni/tasks/calico.yaml +++ b/builtin/core/roles/install/cni/tasks/calico.yaml @@ -3,6 +3,7 @@ ignore_errors: true command: calicoctl version register: calicoctl_install_version + register_type: yaml - name: Install calicoctl when: .calicoctl_install_version.stderr | ne "" block: diff --git a/builtin/core/roles/precheck/env_check/tasks/etcd.yaml b/builtin/core/roles/precheck/env_check/tasks/etcd.yaml index 1fef2a88..d07e0e5a 100644 --- a/builtin/core/roles/precheck/env_check/tasks/etcd.yaml +++ b/builtin/core/roles/precheck/env_check/tasks/etcd.yaml @@ -57,9 +57,11 @@ ignore_errors: true command: etcd --version register: etcd_install_version + register_type: yaml - name: Check if etcd has match the version when: .etcd_install_service.stdout | eq "active" assert: - that: eq (get .etcd_install_version.stdout "etcd Version") (.etcd_version | default "" | trimPrefix "v") + that: | + eq (get .etcd_install_version.stdout "etcd Version") (.etcd_version | default "" | trimPrefix "v") fail_msg: | - etcd has installed with version:v{{ get .etcd_install_version.stdout "etcd Version" }}. but not match etcd_version: {{ .etcd_version }} + excepted install etcd with version: {{ .etcd_version }} but has installed with: {{ get .etcd_install_version.stdout "etcd Version" }}. diff --git a/pkg/converter/converter.go b/pkg/converter/converter.go index cfb0db60..b881a063 100644 --- a/pkg/converter/converter.go +++ b/pkg/converter/converter.go @@ -46,13 +46,14 @@ func MarshalBlock(hosts []string, when []string, block kkprojectv1.Block) *kkcor CreationTimestamp: metav1.Now(), }, Spec: kkcorev1alpha1.TaskSpec{ - Name: block.Name, - Hosts: hosts, - IgnoreError: block.IgnoreErrors, - Retries: block.Retries, - When: when, - FailedWhen: block.FailedWhen.Data, - Register: block.Register, + Name: block.Name, + Hosts: hosts, + IgnoreError: block.IgnoreErrors, + Retries: block.Retries, + When: when, + FailedWhen: block.FailedWhen.Data, + Register: block.Register, + RegisterType: block.RegisterType, }, } if annotation, ok := block.UnknownField["annotations"].(map[string]string); ok { diff --git a/pkg/executor/task_executor.go b/pkg/executor/task_executor.go index e3b9bb73..2955cab9 100644 --- a/pkg/executor/task_executor.go +++ b/pkg/executor/task_executor.go @@ -13,6 +13,7 @@ import ( kkcorev1 "github.com/kubesphere/kubekey/api/core/v1" kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1" "github.com/schollz/progressbar/v3" + "gopkg.in/yaml.v3" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" @@ -377,10 +378,13 @@ func (e *taskExecutor) dealRegister(stdout, stderr, host string) error { if e.task.Spec.Register != "" { var stdoutResult any = stdout var stderrResult any = stderr - // try to convert by json - if json.Valid([]byte(stdout)) { + switch e.task.Spec.RegisterType { + case "json": _ = json.Unmarshal([]byte(stdout), &stdoutResult) - _ = json.Unmarshal([]byte(stderr), &stderrResult) + case "yaml", "yml": + _ = yaml.Unmarshal([]byte(stdout), &stdoutResult) + default: + // store by string } // set variable to parent location if err := e.variable.Merge(variable.MergeRuntimeVariable(map[string]any{