diff --git a/cmd/sealer/cmd/alpha/cert.go b/cmd/sealer/cmd/alpha/cert.go index 19141826003..791dd0d5588 100644 --- a/cmd/sealer/cmd/alpha/cert.go +++ b/cmd/sealer/cmd/alpha/cert.go @@ -15,17 +15,15 @@ package alpha import ( - "context" "fmt" + "io/ioutil" + "path/filepath" "strings" - "golang.org/x/sync/errgroup" - - "github.com/sealerio/sealer/utils/ssh" - - "github.com/spf13/cobra" - + "github.com/sealerio/sealer/common" "github.com/sealerio/sealer/pkg/clusterfile" + "github.com/sealerio/sealer/pkg/infradriver" + "github.com/spf13/cobra" ) var longCertCmdDescription = `This command will add the new domain or IP address in cert to update cluster API server. @@ -54,26 +52,32 @@ func NewCertCmd() *cobra.Command { return fmt.Errorf("IP address or DNS domain needed for cert Subject Alternative Names") } - cluster, err := clusterfile.GetDefaultCluster() + workClusterfile := common.GetDefaultClusterfile() + clusterFileData, err := ioutil.ReadFile(filepath.Clean(workClusterfile)) + if err != nil { + return err + } + + cf, err := clusterfile.NewClusterFile(clusterFileData) + if err != nil { + return err + } + + cluster := cf.GetCluster() + infraDriver, err := infradriver.NewInfraDriver(&cluster) if err != nil { - return fmt.Errorf("failed to get default cluster: %v", err) + return err } certUpdateCmd := fmt.Sprintf("seautil cert update --alt-names %s", strings.Join(altNames, ",")) - // send new cert to all master. - ips := cluster.GetMasterIPList() - eg, _ := errgroup.WithContext(context.Background()) - for _, ip := range ips { - node := ip - eg.Go(func() error { - sshClient, err := ssh.NewStdoutSSHClient(node, cluster) - if err != nil { - return fmt.Errorf("failed to new ssh client: %v", err) - } - return sshClient.CmdAsync(node, certUpdateCmd) - }) + // modify new api cert to all master. + for _, ip := range cluster.GetMasterIPList() { + err = infraDriver.CmdAsync(ip, certUpdateCmd) + if err != nil { + return fmt.Errorf("failed to update cluster api server cert: %v", err) + } } - return eg.Wait() + return nil }, } diff --git a/pkg/clusterfile/util.go b/pkg/clusterfile/util.go index 3b55b62ccca..01d1b5611b8 100644 --- a/pkg/clusterfile/util.go +++ b/pkg/clusterfile/util.go @@ -16,37 +16,12 @@ package clusterfile import ( "fmt" - "os" - "strings" - - yamlUtils "github.com/sealerio/sealer/utils/yaml" "github.com/sealerio/sealer/common" v2 "github.com/sealerio/sealer/types/api/v2" + yamlUtils "github.com/sealerio/sealer/utils/yaml" ) -var ErrClusterNotExist = fmt.Errorf("no cluster exist") - -func GetDefaultClusterName() (string, error) { - files, err := os.ReadDir(fmt.Sprintf("%s/.sealer", common.GetHomeDir())) - if err != nil { - return "", err - } - var clusters []string - for _, f := range files { - if f.IsDir() { - clusters = append(clusters, f.Name()) - } - } - if len(clusters) == 1 { - return clusters[0], nil - } else if len(clusters) > 1 { - return "", fmt.Errorf("select a cluster through the -c parameter: " + strings.Join(clusters, ",")) - } - - return "", ErrClusterNotExist -} - func GetClusterFromFile(filepath string) (cluster *v2.Cluster, err error) { cluster = &v2.Cluster{} if err = yamlUtils.UnmarshalFile(filepath, cluster); err != nil { @@ -55,15 +30,3 @@ func GetClusterFromFile(filepath string) (cluster *v2.Cluster, err error) { cluster.SetAnnotations(common.ClusterfileName, filepath) return cluster, nil } - -func GetDefaultCluster() (cluster *v2.Cluster, err error) { - name, err := GetDefaultClusterName() - if err != nil { - return nil, err - } - userHome, err := os.UserHomeDir() - if err != nil { - return nil, err - } - return GetClusterFromFile(fmt.Sprintf("%s/.sealer/%s/Clusterfile", userHome, name)) -}