Skip to content

Commit

Permalink
feat: add create etcd cluster and weed cluster function (#2304)
Browse files Browse the repository at this point in the history
 feat: add create etcd cluster and weed cluster function

---------

Signed-off-by: sjcsjc123 <1401189096@qq.com>
  • Loading branch information
sjcsjc123 authored Oct 31, 2023
1 parent 005f904 commit 59b6872
Show file tree
Hide file tree
Showing 155 changed files with 61,172 additions and 86 deletions.
20 changes: 12 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/spf13/viper v1.10.0
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5
go.etcd.io/etcd/client/v3 v3.5.1
golang.org/x/crypto v0.12.0
golang.org/x/sync v0.3.0
golang.org/x/sys v0.12.0
Expand Down Expand Up @@ -100,6 +101,8 @@ require (
github.com/containernetworking/cni v1.1.2 // indirect
github.com/containernetworking/plugins v1.1.1 // indirect
github.com/containers/libtrust v0.0.0-20200511145503-9c3a6c22cd9a // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/cskr/pubsub v1.0.2 // indirect
Expand Down Expand Up @@ -253,7 +256,6 @@ require (
github.com/libp2p/go-tcp-transport v0.5.0 // indirect
github.com/libp2p/go-ws-transport v0.5.0 // indirect
github.com/libp2p/go-yamux/v2 v2.3.0 // indirect
github.com/libp2p/go-yamux/v3 v3.1.2 // indirect
github.com/libp2p/zeroconf/v2 v2.1.1 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lithammer/dedent v1.1.0 // indirect
Expand Down Expand Up @@ -350,6 +352,8 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.etcd.io/etcd/api/v3 v3.5.1 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.1 // indirect
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
Expand Down Expand Up @@ -393,8 +397,15 @@ replace (
github.com/labring/lvscare => github.com/fanux/lvscare v1.1.3-beta.2.0.20220525083355-d8f3f86677d1
github.com/libp2p/go-libp2p => github.com/libp2p/go-libp2p v0.16.0
github.com/libp2p/go-libp2p-core => github.com/libp2p/go-libp2p-core v0.11.0
github.com/libp2p/go-libp2p-mplex => github.com/libp2p/go-libp2p-mplex v0.4.1
github.com/libp2p/go-libp2p-peerstore => github.com/libp2p/go-libp2p-peerstore v0.4.0
github.com/libp2p/go-libp2p-quic-transport => github.com/libp2p/go-libp2p-quic-transport v0.15.0
github.com/libp2p/go-libp2p-swarm => github.com/libp2p/go-libp2p-swarm v0.8.0
github.com/libp2p/go-libp2p-transport-upgrader => github.com/libp2p/go-libp2p-transport-upgrader v0.5.0
github.com/libp2p/go-libp2p-yamux => github.com/libp2p/go-libp2p-yamux v0.6.0
github.com/libp2p/go-mplex => github.com/libp2p/go-mplex v0.3.0
github.com/libp2p/go-tcp-transport => github.com/libp2p/go-tcp-transport v0.4.0
github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.25.0
github.com/opencontainers/runc => github.com/opencontainers/runc v1.1.2
golang.org/x/net => golang.org/x/net v0.12.0
k8s.io/api => k8s.io/api v0.24.2
Expand Down Expand Up @@ -424,11 +435,4 @@ replace (
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.21.0
k8s.io/sample-cli-plugin => k8s.io/sample-cli-plugin v0.21.0
k8s.io/sample-controller => k8s.io/sample-controller v0.21.0
github.com/libp2p/go-libp2p-mplex => github.com/libp2p/go-libp2p-mplex v0.4.1
github.com/libp2p/go-libp2p-quic-transport => github.com/libp2p/go-libp2p-quic-transport v0.15.0
github.com/libp2p/go-libp2p-transport-upgrader => github.com/libp2p/go-libp2p-transport-upgrader v0.5.0
github.com/libp2p/go-tcp-transport => github.com/libp2p/go-tcp-transport v0.4.0
github.com/libp2p/go-libp2p-peerstore => github.com/libp2p/go-libp2p-peerstore v0.4.0
github.com/libp2p/go-mplex => github.com/libp2p/go-mplex v0.3.0
github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.25.0
)
76 changes: 7 additions & 69 deletions go.sum

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions pkg/imagedistributor/weed_mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright © 2023 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 imagedistributor

import (
"context"
"fmt"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
v1 "github.com/sealerio/sealer/types/api/v1"
"github.com/sealerio/sealer/utils/os/fs"
"github.com/sealerio/sealer/utils/weed"
"path/filepath"
"strings"
)

type weedMounter struct {
imageEngine imageengine.Interface
weedClient weed.Deployer
}

func (w *weedMounter) Mount(imageName string, platform v1.Platform, dest string) (string, string, string, error) {
mountDir := filepath.Join(dest,
strings.ReplaceAll(imageName, "/", "_"),
strings.Join([]string{platform.OS, platform.Architecture, platform.Variant}, "_"))

imageID, err := w.imageEngine.Pull(&options.PullOptions{
Quiet: false,
PullPolicy: "missing",
Image: imageName,
Platform: platform.ToString(),
})
if err != nil {
return "", "", "", err
}

if err := fs.FS.MkdirAll(filepath.Dir(mountDir)); err != nil {
return "", "", "", err
}

id, err := w.imageEngine.CreateWorkingContainer(&options.BuildRootfsOptions{
DestDir: mountDir,
ImageNameOrID: imageID,
})

if err != nil {
return "", "", "", err
}

// Upload the mounted files to the WeedFS cluster
err = w.weedClient.UploadFile(context.Background(), mountDir)
if err != nil {
return "", "", "", err
}

return mountDir, id, imageID, nil
}

func (w *weedMounter) Umount(dir, containerID string) error {
// Download the files from WeedFS cluster
err := w.weedClient.DownloadFile(context.Background(), dir, dir)
if err != nil {
return err
}

// Umount the image and remove the working container
err = w.imageEngine.RemoveContainer(&options.RemoveContainerOptions{
ContainerNamesOrIDs: []string{containerID},
})
if err != nil {
return err
}

// Remove the mounted files from the WeedFS cluster
err = w.weedClient.RemoveFile(context.Background(), dir)
if err != nil {
return err
}

// Remove the local mount directory
if err := fs.FS.RemoveAll(dir); err != nil {
return fmt.Errorf("failed to remove mount dir %s: %v", dir, err)
}

return nil
}

func NewWeedMounter(imageEngine imageengine.Interface, config *weed.Config) Mounter {
deployer := weed.NewDeployer(config)
return &weedMounter{
imageEngine: imageEngine,
weedClient: deployer,
}
}
51 changes: 51 additions & 0 deletions utils/weed/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 weed

import (
"path"
"strconv"

"github.com/sealerio/sealer/utils/exec"
)

// checkPort checks if the port is available or can be used.
func checkPort(port int) bool {
// lsof -i:9333
err := exec.Cmd("lsof", "-i:"+strconv.Itoa(port))
return err == nil
}

// checkDir checks if the dir is available or can be used.
//func checkDir(dir string) bool {
// // ls /tmp
// err := exec.Cmd("ls", dir)
// if err != nil {
// return false
// }
// return true
//}

func checkBinFile(fileName string) bool {
binName := path.Base(fileName)
switch binName {
case "weed":

case "etcd":

default:
}
return false
}
27 changes: 27 additions & 0 deletions utils/weed/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 weed

//import "testing"
//
//func TestCheckPort(t *testing.T) {
// ok := checkPort(9333)
// t.Log(ok)
//}
//
//func TestCheckDir(t *testing.T) {
// ok := checkDir("/tmp")
// t.Log(ok)
//}
Loading

0 comments on commit 59b6872

Please sign in to comment.