Skip to content

Commit

Permalink
feature(main): add patch package in run func (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu authored Apr 24, 2022
1 parent 2805329 commit 825469c
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 30 deletions.
15 changes: 14 additions & 1 deletion pkg/apply/applydrivers/apply_drivers_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Applier) Apply() error {
}

func (c *Applier) reconcileCluster() error {
return nil
return c.installApp()
}

func (c *Applier) initCluster() error {
Expand All @@ -84,6 +84,19 @@ func (c *Applier) initCluster() error {
return nil
}

func (c *Applier) installApp() error {
installProcessor, err := processor.NewInstallProcessor(c.ClusterFile)
if err != nil {
return err
}
err = installProcessor.Execute(c.ClusterDesired)
if err != nil {
return err
}

return nil
}

func (c *Applier) Delete() error {
t := metav1.Now()
c.ClusterDesired.DeletionTimestamp = &t
Expand Down
9 changes: 2 additions & 7 deletions pkg/apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,10 @@ type CreateProcessor struct {
}

func (c *CreateProcessor) Execute(cluster *v2.Cluster) error {
err := yaml.MarshalYamlToFile(contants.Clusterfile(cluster.Name), cluster)
if err != nil {
return err
}
pipLine, err := c.GetPipeLine()
if err != nil {
return err
}

for _, f := range pipLine {
if err = f(cluster); err != nil {
return err
Expand Down Expand Up @@ -93,7 +88,7 @@ func (c *CreateProcessor) CreateCluster(cluster *v2.Cluster) error {
return fmt.Errorf("failed to init runtime, %v", err)
}
c.Runtime = runTime
c.cManifestList, err = c.ClusterManager.Create(cluster.Name, cluster.Spec.Image...)
c.cManifestList, err = c.ClusterManager.Create(cluster.Name, 0, cluster.Spec.Image...)
return err
}

Expand All @@ -116,7 +111,7 @@ func (c *CreateProcessor) MountRootfs(cluster *v2.Cluster) error {
return err
}

return fs.MountRootfs(cluster, hosts)
return fs.MountRootfs(cluster, hosts, true)
}

func (c *CreateProcessor) Init(cluster *v2.Cluster) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apply/processor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DeleteProcessor struct {

// Execute :according to the different of desired cluster to delete cluster.
func (d DeleteProcessor) Execute(cluster *v2.Cluster) (err error) {
d.cManifestList, err = d.ClusterManager.Inspect(cluster.Name, len(cluster.Spec.Image))
d.cManifestList, err = d.ClusterManager.Inspect(cluster.Name, 0, len(cluster.Spec.Image))
if err != nil {
logger.Warn("delete process failed to inspect cluster, %v", err)
}
Expand Down
155 changes: 155 additions & 0 deletions pkg/apply/processor/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package processor

import (
"context"

"github.com/fanux/sealos/pkg/utils/strings"
"golang.org/x/sync/errgroup"

"github.com/fanux/sealos/pkg/clusterfile"
"github.com/fanux/sealos/pkg/config"
"github.com/fanux/sealos/pkg/filesystem"
"github.com/fanux/sealos/pkg/guest"
"github.com/fanux/sealos/pkg/image"
"github.com/fanux/sealos/pkg/image/types"
v2 "github.com/fanux/sealos/pkg/types/v1beta1"
"github.com/fanux/sealos/pkg/utils/contants"
)

type InstallProcessor struct {
ClusterFile clusterfile.Interface
ImageManager types.Service
ClusterManager types.ClusterService
RegistryManager types.RegistryService
Guest guest.Interface
imageList types.ImageListOCIV1
cManifestList types.ClusterManifestList
}

func (c *InstallProcessor) Execute(cluster *v2.Cluster) error {
pipLine, err := c.GetPipeLine()
if err != nil {
return err
}

for _, f := range pipLine {
if err = f(cluster); err != nil {
return err
}
}

return nil
}
func (c *InstallProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) {
var todoList []func(cluster *v2.Cluster) error
todoList = append(todoList,
c.ChangeCluster,
c.RunConfig,
c.MountRootfs,
//i.GetPhasePluginFunc(plugin.PhasePreGuest),
c.RunGuest,
//i.GetPhasePluginFunc(plugin.PhasePostInstall),
)
return todoList, nil
}

func diffImages(spec, curr *v2.Cluster) []string {
pullImages := make([]string, 0)
for _, img := range spec.Spec.Image {
if strings.NotIn(img, curr.Spec.Image) {
pullImages = append(pullImages, img)
}
}
return pullImages
}

func (c *InstallProcessor) ChangeCluster(cluster *v2.Cluster) error {
err := c.ClusterFile.Process()
if err != nil {
return err
}
current := c.ClusterFile.GetCluster()
pullImages := diffImages(cluster, current)
err = c.RegistryManager.Pull(pullImages...)
if err != nil {
return err
}
img, err := c.ImageManager.Inspect(pullImages...)
if err != nil {
return err
}
//TODO if app image is ok
c.imageList = img
c.cManifestList, err = c.ClusterManager.Create(cluster.Name, len(current.Spec.Image), pullImages...)
return err
}

func (c *InstallProcessor) RunConfig(cluster *v2.Cluster) error {
eg, _ := errgroup.WithContext(context.Background())
for _, cManifest := range c.cManifestList {
manifest := cManifest
eg.Go(func() error {
cfg := config.NewConfiguration(manifest.MountPoint, c.ClusterFile.GetConfigs())
return cfg.Dump(contants.Clusterfile(cluster.Name))
})
}
return eg.Wait()
}

func (c *InstallProcessor) MountRootfs(cluster *v2.Cluster) error {
hosts := append(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()...)
fs, err := filesystem.NewRootfsMounter(c.cManifestList, c.imageList)
if err != nil {
return err
}

return fs.MountRootfs(cluster, hosts, false)
}

func (c *InstallProcessor) RunGuest(cluster *v2.Cluster) error {
return c.Guest.Apply(cluster)
}

func NewInstallProcessor(clusterFile clusterfile.Interface) (Interface, error) {
imgSvc, err := image.NewImageService()
if err != nil {
return nil, err
}

clusterSvc, err := image.NewClusterService()
if err != nil {
return nil, err
}

registrySvc, err := image.NewRegistryService()
if err != nil {
return nil, err
}

gs, err := guest.NewGuestManager()
if err != nil {
return nil, err
}

return &InstallProcessor{
ClusterFile: clusterFile,
ImageManager: imgSvc,
ClusterManager: clusterSvc,
RegistryManager: registrySvc,
Guest: gs,
}, nil
}
1 change: 1 addition & 0 deletions pkg/apply/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewApplierFromArgs(imageName []string, args *RunArgs) (applydrivers.Interfa
}
cluster = clusterFile.GetCluster()
if args.Nodes == "" && args.Masters == "" {
cluster.Spec.Image = append(cluster.Spec.Image, imageName...)
return applydrivers.NewDefaultApplier(cluster)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/filesystem/rootfs/rootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import v2 "github.com/fanux/sealos/pkg/types/v1beta1"

type Interface interface {
// MountRootfs :send cloud rootfs to all hosts.
MountRootfs(cluster *v2.Cluster, hosts []string) error
MountRootfs(cluster *v2.Cluster, hosts []string, initFlag bool) error
// UnMountRootfs :umount rootfs on all hosts.
UnMountRootfs(cluster *v2.Cluster, hosts []string) error
}
27 changes: 14 additions & 13 deletions pkg/filesystem/rootfs/rootfs_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type defaultRootfs struct {
cluster types.ClusterManifestList
}

func (f *defaultRootfs) MountRootfs(cluster *v2.Cluster, hosts []string) error {
return f.mountRootfs(cluster, hosts)
func (f *defaultRootfs) MountRootfs(cluster *v2.Cluster, hosts []string, initFlag bool) error {
return f.mountRootfs(cluster, hosts, initFlag)
}

func (f *defaultRootfs) UnMountRootfs(cluster *v2.Cluster, hosts []string) error {
Expand All @@ -63,7 +63,7 @@ func (f *defaultRootfs) getSSH(cluster *v2.Cluster) ssh.Interface {
return ssh.NewSSHClient(&cluster.Spec.SSH, true)
}

func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error {
func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string, initFlag bool) error {
target := contants.NewData(f.getClusterName(cluster)).RootFSPath()
eg, _ := errgroup.WithContext(context.Background())
envProcessor := env.NewEnvProcessor(cluster, f.imgList)
Expand Down Expand Up @@ -106,16 +106,17 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error
if err := fileEg.Wait(); err != nil {
return err
}

checkBash := check.CheckBash()
if checkBash == "" {
return nil
}
if err := f.getSSH(cluster).CmdAsync(ip, envProcessor.WrapperShell(ip, check.CheckBash()), runtime.ApplyImageShimCMD(target)); err != nil {
return err
}
if err := f.getSSH(cluster).CmdAsync(ip, envProcessor.WrapperShell(ip, check.InitBash())); err != nil {
return err
if initFlag {
checkBash := check.CheckBash()
if checkBash == "" {
return nil
}
if err := f.getSSH(cluster).CmdAsync(ip, envProcessor.WrapperShell(ip, check.CheckBash()), runtime.ApplyImageShimCMD(target)); err != nil {
return err
}
if err := f.getSSH(cluster).CmdAsync(ip, envProcessor.WrapperShell(ip, check.InitBash())); err != nil {
return err
}
}
return nil
})
Expand Down
10 changes: 5 additions & 5 deletions pkg/image/binary/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ import (
type ClusterService struct {
}

func (d *ClusterService) Create(name string, images ...string) (types.ClusterManifestList, error) {
func (d *ClusterService) Create(name string, index int, images ...string) (types.ClusterManifestList, error) {
var cmd strings.Builder
for i, image := range images {
cmd.WriteString(fmt.Sprintf(" buildah from --pull=never --name %s-%d %s && buildah mount %s-%d ", name, i, image, name, i))
cmd.WriteString(fmt.Sprintf(" buildah from --pull=never --name %s-%d %s && buildah mount %s-%d ", name, index+i, image, name, index+i))
if i != len(images)-1 {
cmd.WriteString(" && ")
}
Expand All @@ -48,7 +48,7 @@ func (d *ClusterService) Create(name string, images ...string) (types.ClusterMan
return nil, err
}

return d.Inspect(name, len(images))
return d.Inspect(name, index, len(images))
}
func (*ClusterService) Delete(name string, imageNum int) error {
data := exec.BashEval("buildah containers --json")
Expand All @@ -72,11 +72,11 @@ func (*ClusterService) Delete(name string, imageNum int) error {
return nil
}

func (*ClusterService) Inspect(name string, imageNum int) (types.ClusterManifestList, error) {
func (*ClusterService) Inspect(name string, index int, imageNum int) (types.ClusterManifestList, error) {
var clusterList types.ClusterManifestList

for i := 0; i < imageNum; i++ {
data := exec.BashEval(fmt.Sprintf("buildah inspect %s-%d", name, i))
data := exec.BashEval(fmt.Sprintf("buildah inspect %s-%d", name, index+i))
manifest, err := inspectContainer(data)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/image/types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ type Service interface {
type ClusterService interface {
// Create 1. buildah from <image> 2. buildah mount <container(cluster)> 3. return container(cluster) manifest
// for CloudImage we can take container as cluster instance. type ClusterManifest storage.Container
Create(name string, images ...string) (ClusterManifestList, error)
Create(name string, index int, images ...string) (ClusterManifestList, error)
// Delete umount rootfs and delete it
Delete(name string, imageNum int) error
// Inspect return cluster(container) manifest
Inspect(name string, imageNum int) (ClusterManifestList, error)
Inspect(name string, index int, imageNum int) (ClusterManifestList, error)
List() ([]ClusterInfo, error)
}
1 change: 1 addition & 0 deletions pkg/utils/exec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

func Cmd(name string, args ...string) error {
logger.Debug("cmd for pipe in host: ", fmt.Sprintf("%s %s", name, strings.Join(args, " ")))
cmd := exec.Command(name, args[:]...) // #nosec
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
Expand Down

0 comments on commit 825469c

Please sign in to comment.