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

[pull] main from labring:main #647

Merged
merged 2 commits into from
Sep 24, 2023
Merged
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
44 changes: 37 additions & 7 deletions pkg/apply/applydrivers/apply_drivers_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"

"golang.org/x/sync/errgroup"

"github.com/labring/sealos/pkg/apply/processor"
"github.com/labring/sealos/pkg/client-go/kubernetes"
"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/ssh"
v2 "github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/confirm"
"github.com/labring/sealos/pkg/utils/iputils"
Expand Down Expand Up @@ -79,20 +82,14 @@ type Applier struct {
}

func (c *Applier) Apply() error {
clusterPath := constants.Clusterfile(c.ClusterDesired.Name)
// clusterErr and appErr should not appear in the same time
var clusterErr, appErr error
// save cluster to file after apply
defer func() {
switch clusterErr.(type) {
case *processor.CheckError, *processor.PreProcessError:
return
}
logger.Debug("save objects into local: %s, objects: %v", clusterPath, c.getWriteBackObjects())
saveErr := yaml.MarshalFile(clusterPath, c.getWriteBackObjects()...)
if saveErr != nil {
logger.Error("failed to serialize into file: %s error, %s", clusterPath, saveErr)
}
c.applyAfter()
}()
c.initStatus()
if c.ClusterCurrent == nil || c.ClusterCurrent.CreationTimestamp.IsZero() {
Expand Down Expand Up @@ -273,3 +270,36 @@ func (c *Applier) deleteCluster() error {
logger.Info("succeeded in deleting current cluster")
return nil
}

func (c *Applier) syncWorkdir() {
workDir := constants.ClusterDir(c.ClusterDesired.Name)
logger.Debug("sync workdir: %s", workDir)
ipList := c.ClusterDesired.GetMasterIPAndPortList()
sshClient := ssh.NewCacheClientFromCluster(c.ClusterDesired, true)

eg, _ := errgroup.WithContext(context.Background())
for _, ipAddr := range ipList {
ip := ipAddr
eg.Go(func() error {
return sshClient.Copy(ip, workDir, workDir)
})
}
if err := eg.Wait(); err != nil {
logger.Error("failed to sync workdir: %s error, %v", workDir, err)
}
}

// save cluster to file after apply
func (c *Applier) saveClusterFile() {
clusterPath := constants.Clusterfile(c.ClusterDesired.Name)
logger.Debug("save objects into local: %s, objects: %v", clusterPath, c.getWriteBackObjects())
saveErr := yaml.MarshalFile(clusterPath, c.getWriteBackObjects()...)
if saveErr != nil {
logger.Error("failed to serialize into file: %s error, %s", clusterPath, saveErr)
}
}

func (c *Applier) applyAfter() {
c.saveClusterFile()
c.syncWorkdir()
}
Loading