Add batch execution commands in Keadm.

Signed-off-by: Yeeaoo <15901181137@163.com>
This commit is contained in:
Yeeaoo 2026-02-27 16:32:32 +08:00
parent 00cf138b5b
commit 3617329987
3 changed files with 46 additions and 21 deletions

View file

@ -171,6 +171,7 @@ type Keadm struct {
ArchGroup []string `yaml:"archGroup"`
OfflinePackageDir *string `yaml:"offlinePackageDir,omitempty"`
CmdTplArgs map[string]string `yaml:"cmdTplArgs,omitempty"`
KeadmBinDir *string `yaml:"keadmBinDir,omitempty"`
}
// Download defines the url and enable flag
@ -181,10 +182,11 @@ type Download struct {
// Node defines the node information used in batch-process config file
type Node struct {
NodeName string `yaml:"nodeName"`
KeadmCmd string `yaml:"keadmCmd"`
CopyFrom *string `yaml:"copyFrom,omitempty"`
SSH SSH `yaml:"ssh"`
NodeName string `yaml:"nodeName"`
KeadmCmd string `yaml:"keadmCmd"`
CopyFrom *string `yaml:"copyFrom,omitempty"`
SSH SSH `yaml:"ssh"`
KeadmBinDir *string `yaml:"keadmBinDir,omitempty"`
}
// SSH defines the ssh information used in batch-process config file

View file

@ -112,12 +112,21 @@ func processBatchProcess(cfg *common.Config, step *common.Step) error {
// Ensure all log entries are written to file
defer logWriter.Flush()
// Get keadm packages
step.Printf("Preparing keadm packages.")
if err = prepareKeadmPackages(cfg); err != nil {
return errors.Errorf("failed to prepare keadm packages, %v", err)
globalKeadmEmpty := cfg.Keadm.KeadmBinDir == nil || *cfg.Keadm.KeadmBinDir == ""
hasEmptyNodeKeadm := false
for _, node := range cfg.Nodes {
if node.KeadmBinDir == nil || *node.KeadmBinDir == "" {
hasEmptyNodeKeadm = true
break
}
}
if globalKeadmEmpty && hasEmptyNodeKeadm {
// Get keadm packages
step.Printf("Preparing keadm packages.")
if err = prepareKeadmPackages(cfg); err != nil {
return errors.Errorf("failed to prepare keadm packages, %v", err)
}
}
step.Printf("Batch process nodes.")
// Batch process edge nodes
if err = batchProcessNodes(cfg, logWriter); err != nil {
@ -274,17 +283,25 @@ func processNode(node *common.Node, cfg *common.Config) error {
}
}
// get node Arch
arch, err := getNodeArch(client)
if err != nil {
return err
}
keadmPath := filepath.Join(binDir, arch, fmt.Sprintf("keadm-%s-linux-%s/keadm/keadm", cfg.Keadm.KeadmVersion, arch))
if err = uploadFile(client, node.NodeName, keadmPath, filepath.Join(baseDir, "keadm")); err != nil {
return err
if (cfg.Keadm.KeadmBinDir == nil || *cfg.Keadm.KeadmBinDir == "") &&
(node.KeadmBinDir == nil || *node.KeadmBinDir == "") {
// get node Arch
arch, err := getNodeArch(client)
if err != nil {
return err
}
keadmPath := filepath.Join(binDir, arch, fmt.Sprintf("keadm-%s-linux-%s/keadm/keadm", cfg.Keadm.KeadmVersion, arch))
if err = uploadFile(client, node.NodeName, keadmPath, filepath.Join(baseDir, "keadm")); err != nil {
return err
}
}
if err = executeKeadmCommand(client, node.NodeName, node.KeadmCmd); err != nil {
keadmBinDir := cfg.Keadm.KeadmBinDir
if node.KeadmBinDir != nil && *node.KeadmBinDir != "" {
keadmBinDir = node.KeadmBinDir
}
if err = executeKeadmCommand(client, node.NodeName, node.KeadmCmd, keadmBinDir); err != nil {
return err
}
@ -410,7 +427,7 @@ func uploadFiles(client *ssh.Client, nodeName, srcDir, destDir string) error {
}
// execute keadm command
func executeKeadmCommand(client *ssh.Client, nodeName, cmd string) error {
func executeKeadmCommand(client *ssh.Client, nodeName, cmd string, keadmBinDirectory *string) error {
session, err := client.NewSession()
if err != nil {
return errors.Errorf("failed to create new SSH session: %v", err)
@ -418,10 +435,14 @@ func executeKeadmCommand(client *ssh.Client, nodeName, cmd string) error {
defer session.Close()
var execCmd string
parts := strings.Fields(cmd)
execDir := baseDir
if keadmBinDirectory != nil && *keadmBinDirectory != "" {
execDir = *keadmBinDirectory
}
if len(parts) >= 1 && parts[0] == "reset" {
execCmd = fmt.Sprintf("cd %s && yes |./keadm %s", baseDir, cmd)
execCmd = fmt.Sprintf("cd %s && yes |./keadm %s", execDir, cmd)
} else {
execCmd = fmt.Sprintf("cd %s && ./keadm %s", baseDir, cmd)
execCmd = fmt.Sprintf("cd %s && ./keadm %s", execDir, cmd)
}
klog.Infof("%s: Executing command %s", nodeName, execCmd)

View file

@ -57,10 +57,12 @@ keadm:
cmdTplArgs: # <Optional> This parameter is the execution command template, which can be optionally configured and used in conjunction with nodes[x].keadmCmd.
cmd: "" # This is an example parameter, which can be used in conjunction with nodes[x].keadmCmd.
token: "" # This is an example parameter, which can be used in conjunction with nodes[x].keadmCmd.
keadmBinDir: "" # This parameter is used to configure the path where the existing keadm binary file is located (all nodes are the same).
nodes:
- nodeName: edge-node # <Required> Unique name, used to identify the node
keadmCmd: "" # <Required> The command to be executed on the node, can used in conjunction with keadm.cmdTplArgs. for example: "{{.cmd}} --edgenode-name=containerd-node1 --token={{.token}}"
copyFrom: "" # <Optional> The path of the file to be copied from the local machine to the node, which can be left unconfigured.
keadmBinDir: "" # This parameter is used to configure the path of existing keadm binary files on the current node.
ssh:
ip: "" # <Required> The IP address of the node.
username: root # <Required> The username of the node, need administrator permissions.