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

feat: add create etcd cluster and weed cluster function #2304

Merged
merged 6 commits into from
Oct 31, 2023
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
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
Loading