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

optimize:move utils func to pkg #1381

Merged
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
4 changes: 2 additions & 2 deletions apply/applydriver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ func (c *Applier) reconcileCluster() error {
return c.installApp()
}

mj, md := utils.GetDiffHosts(c.ClusterCurrent.GetMasterIPList(), c.ClusterDesired.GetMasterIPList())
nj, nd := utils.GetDiffHosts(c.ClusterCurrent.GetNodeIPList(), c.ClusterDesired.GetNodeIPList())
mj, md := utils.DiffSlice(c.ClusterCurrent.GetMasterIPList(), c.ClusterDesired.GetMasterIPList())
nj, nd := utils.DiffSlice(c.ClusterCurrent.GetNodeIPList(), c.ClusterDesired.GetNodeIPList())
if len(mj) == 0 && len(md) == 0 && len(nj) == 0 && len(nd) == 0 {
return c.upgrade()
}
Expand Down
4 changes: 3 additions & 1 deletion apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package processor
import (
"fmt"

"github.com/sealerio/sealer/utils/net"

"github.com/sealerio/sealer/pkg/clusterfile"
"github.com/sealerio/sealer/pkg/config"
"github.com/sealerio/sealer/pkg/filesystem"
Expand Down Expand Up @@ -102,7 +104,7 @@ func (c *CreateProcessor) RunConfig(cluster *v2.Cluster) error {
func (c *CreateProcessor) MountRootfs(cluster *v2.Cluster) error {
hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
regConfig := runtime.GetRegistryConfig(platform.DefaultMountCloudImageDir(cluster.Name), cluster.GetMaster0IP())
if utils.NotInIPList(regConfig.IP, hosts) {
if net.NotInIPList(regConfig.IP, hosts) {
hosts = append(hosts, regConfig.IP)
}

Expand Down
4 changes: 3 additions & 1 deletion apply/processor/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"strconv"

"github.com/sealerio/sealer/utils/net"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/client/k8s"
"github.com/sealerio/sealer/pkg/filesystem"
Expand Down Expand Up @@ -144,7 +146,7 @@ func (g *GenerateProcessor) MountRootfs(cluster *v2.Cluster) error {
}
hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
regConfig := runtime.GetRegistryConfig(common.DefaultTheClusterRootfsDir(cluster.Name), cluster.GetMaster0IP())
if utils.NotInIPList(regConfig.IP, hosts) {
if net.NotInIPList(regConfig.IP, hosts) {
hosts = append(hosts, regConfig.IP)
}
return fs.MountRootfs(cluster, hosts, false)
Expand Down
4 changes: 2 additions & 2 deletions apply/processor/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/sealerio/sealer/pkg/filesystem/cloudfilesystem"
"github.com/sealerio/sealer/pkg/runtime"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils"
"github.com/sealerio/sealer/utils/net"
)

type UpgradeProcessor struct {
Expand All @@ -46,7 +46,7 @@ func (u UpgradeProcessor) MountRootfs(cluster *v2.Cluster) error {
//some hosts already mounted when scaled cluster.
hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
regConfig := runtime.GetRegistryConfig(common.DefaultTheClusterRootfsDir(cluster.Name), cluster.GetMaster0IP())
if utils.NotInIPList(regConfig.IP, hosts) {
if net.NotInIPList(regConfig.IP, hosts) {
hosts = append(hosts, regConfig.IP)
}
return u.fileSystem.MountRootfs(cluster, hosts, false)
Expand Down
4 changes: 3 additions & 1 deletion build/buildinstruction/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package buildinstruction
import (
"fmt"

"github.com/sealerio/sealer/utils/os"

"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/opencontainers/go-digest"

Expand Down Expand Up @@ -60,7 +62,7 @@ func (c CmdInstruction) Exec(execContext ExecContext) (out Out, err error) {
}
defer c.mounter.CleanUp()

err = utils.SetRootfsBinToSystemEnv(c.mounter.GetMountTarget())
err = os.SetRootfsBinToSystemEnv(c.mounter.GetMountTarget())
if err != nil {
return out, fmt.Errorf("failed to set temp rootfs %s to system $PATH : %v", c.mounter.GetMountTarget(), err)
}
Expand Down
10 changes: 1 addition & 9 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,7 @@ const (
const APIServerDomain = "apiserver.cluster.local"

const (
DeleteCmd = "rm -rf %s"
ChmodCmd = "chmod +x %s"
TmpTarFile = "/tmp/%s.tar"
ZipCmd = "tar zcvf %s %s"
UnzipCmd = "mkdir -p %s && tar xvf %s -C %s"
CdAndExecCmd = "cd %s && %s"
TagImageCmd = "%s tag %s %s"
PushImageCmd = "%s push %s"
BuildClusterCmd = "%s build -f %s -t %s -m %s %s"
CdAndExecCmd = "cd %s && %s"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (a *ApplyProvider) applyToJoin(toJoinNumber int, role string) ([]string, er
// run container and return append ip list
var toJoinIPList []string
for i := 0; i < toJoinNumber; i++ {
name := fmt.Sprintf("sealer-%s-%s", role, utils.GenUniqueID(10))
name := fmt.Sprintf("sealer-%s-%s", role, GenUniqueID(10))
opts := &client.CreateOptsForContainer{
ImageName: ImageName,
NetworkName: NetworkName,
Expand Down
10 changes: 10 additions & 0 deletions pkg/infra/container/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package container

import (
"crypto/rand"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -49,3 +51,11 @@ func getDiff(host v1.Hosts) (int, []string, error) {

return num, iplist, nil
}

func GenUniqueID(n int) string {
randBytes := make([]byte, n/2)
if _, err := rand.Read(randBytes); err != nil {
return ""
}
return fmt.Sprintf("%x", randBytes)
}
4 changes: 3 additions & 1 deletion pkg/infra/infra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"io/ioutil"
"testing"

"github.com/sealerio/sealer/utils/net"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -247,6 +249,6 @@ func TestReleaseEIP(t *testing.T) {

func TestSort(t *testing.T) {
iplist := []string{"192.168.0.3", "192.168.0.16", "192.168.0.4", "192.168.0.1"}
utils.SortIPList(iplist)
net.SortIPList(iplist)
fmt.Printf("%v", iplist)
}
2 changes: 1 addition & 1 deletion pkg/runtime/masters.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (k *KubeadmRuntime) Command(version string, name CommandType) (cmd string)
return ""
}

if utils.IsInContainer() {
if IsInContainer() {
return fmt.Sprintf("%s%s%s", v, vlogToStr(k.Vlog), " --ignore-preflight-errors=all")
}
if name == InitMaster || name == JoinMaster {
Expand Down
46 changes: 8 additions & 38 deletions pkg/runtime/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,6 @@ func VersionCompare(v1, v2 string) bool {
return true
}

func PreInitMaster0(sshClient ssh.Interface, remoteHostIP string) error {
err := ssh.WaitSSHReady(sshClient, 6, remoteHostIP)
if err != nil {
return fmt.Errorf("apply cloud cluster failed: %s", err)
}
// send sealer and cluster file to remote host
sealerPath := utils.ExecutableFilePath()
err = sshClient.Copy(remoteHostIP, sealerPath, common.RemoteSealerPath)
if err != nil {
return fmt.Errorf("send sealer to remote host %s failed:%v", remoteHostIP, err)
}
err = sshClient.CmdAsync(remoteHostIP, fmt.Sprintf(common.ChmodCmd, common.RemoteSealerPath))
if err != nil {
return fmt.Errorf("chmod +x sealer on remote host %s failed:%v", remoteHostIP, err)
}
logger.Info("send sealer cmd to %s success !", remoteHostIP)

// send tmp cluster file
err = sshClient.Copy(remoteHostIP, common.TmpClusterfile, common.TmpClusterfile)
if err != nil {
return fmt.Errorf("send cluster file to remote host %s failed:%v", remoteHostIP, err)
}
logger.Info("send cluster file to %s success !", remoteHostIP)

// send register login info
authFile := common.DefaultRegistryAuthConfigDir()
if utils.IsFileExist(authFile) {
err = sshClient.Copy(remoteHostIP, authFile, common.DefaultRegistryAuthDir)
if err != nil {
return fmt.Errorf("failed to send register config %s to remote host %s err: %v", authFile, remoteHostIP, err)
}
logger.Info("send register info to %s success !", remoteHostIP)
} else {
logger.Warn("failed to find %s, if image registry is private, please login first", authFile)
}
return nil
}

func GetKubectlAndKubeconfig(ssh ssh.Interface, host, rootfs string) error {
// fetch the cluster kubeconfig, and add /etc/hosts "EIP apiserver.cluster.local" so we can get the current cluster status later
err := ssh.Fetch(host, path.Join(common.DefaultKubeConfigDir(), "config"), common.KubeAdminConf)
Expand Down Expand Up @@ -275,3 +237,11 @@ func RemoteCerts(altNames []string, hostIP, hostName, serviceCIRD, DNSDomain str

return cmd
}

func IsInContainer() bool {
data, err := utils.ReadAll("/proc/1/environ")
if err != nil {
return false
}
return strings.Contains(string(data), "container=docker")
}
104 changes: 0 additions & 104 deletions utils/env.go

This file was deleted.

2 changes: 1 addition & 1 deletion utils/aes.go → utils/hash/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utils
package hash

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion utils/hash.go → utils/hash/md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utils
package hash

import (
"crypto/md5" // #nosec
Expand Down
2 changes: 1 addition & 1 deletion utils/hash_test.go → utils/hash/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utils
package hash

import "testing"

Expand Down
Loading