Skip to content

Commit

Permalink
bugfix: use new clusterfile logic to update api server cert (#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakaZhou719 authored Oct 20, 2022
1 parent bc52c8b commit 4cf0a41
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 60 deletions.
48 changes: 26 additions & 22 deletions cmd/sealer/cmd/alpha/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
},
}

Expand Down
39 changes: 1 addition & 38 deletions pkg/clusterfile/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}

0 comments on commit 4cf0a41

Please sign in to comment.