Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: use new clusterfile logic to update api server cert #1782

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}