feat: add register_type (#2532)

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-04-16 10:03:30 +08:00 committed by GitHub
parent def153b0bc
commit ea4a19ec9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 32 additions and 17 deletions

View File

@ -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

View File

@ -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"`

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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)

View File

@ -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:

View File

@ -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" }}.

View File

@ -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 {

View File

@ -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{