Skip to content

Commit

Permalink
style: remove unused utils (#3830) (#3833)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1557b7d)
Signed-off-by: cuisongliu <cuisongliu@qq.com>

Co-authored-by: fengxsong <fengxsong@outlook.com>
  • Loading branch information
cuisongliu and fengxsong authored Sep 4, 2023
1 parent fecb3d1 commit 5a65c83
Show file tree
Hide file tree
Showing 39 changed files with 1,100 additions and 439 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
go.mongodb.org/mongo-driver v1.11.3
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.10.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/sync v0.2.0
golang.org/x/sys v0.9.0
golang.org/x/term v0.9.0
Expand Down Expand Up @@ -227,7 +228,6 @@ require (
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
Expand Down
935 changes: 935 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/apply/applydrivers/apply_drivers_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *Applier) Apply() error {
return
}
logger.Debug("save objects into local: %s, objects: %v", clusterPath, c.getWriteBackObjects())
saveErr := yaml.MarshalYamlToFile(clusterPath, c.getWriteBackObjects()...)
saveErr := yaml.MarshalFile(clusterPath, c.getWriteBackObjects()...)
if saveErr != nil {
logger.Error("failed to serialize into file: %s error, %s", clusterPath, saveErr)
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (c *Applier) Delete() error {
cfPath := constants.Clusterfile(c.ClusterDesired.Name)
target := fmt.Sprintf("%s.%d", cfPath, t.Unix())
logger.Debug("write reset cluster file to local: %s", target)
if err := yaml.MarshalYamlToFile(cfPath, c.getWriteBackObjects()...); err != nil {
if err := yaml.MarshalFile(cfPath, c.getWriteBackObjects()...); err != nil {
logger.Error("failed to store cluster file: %v", err)
}
_ = os.Rename(cfPath, target)
Expand Down
2 changes: 1 addition & 1 deletion pkg/apply/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewClusterFromGenArgs(cmd *cobra.Command, args *RunArgs, imageNames []strin
if err != nil {
return nil, err
}
if img.Type != v1beta1.RootfsImage {
if !img.IsRootFs() {
return nil, fmt.Errorf("input first image %s is not kubernetes image", imageNames)
}
cluster.Status.Mounts = append(cluster.Status.Mounts, *img)
Expand Down
4 changes: 2 additions & 2 deletions pkg/apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *CreateProcessor) preProcess(cluster *v2.Cluster) error {
}
// extra env must been set at the very first
for i := range cluster.Status.Mounts {
cluster.Status.Mounts[i].Env = maps.MergeMap(cluster.Status.Mounts[i].Env, c.ExtraEnvs)
cluster.Status.Mounts[i].Env = maps.Merge(cluster.Status.Mounts[i].Env, c.ExtraEnvs)
}
distribution := cluster.GetDistribution()
rt, err := factory.New(distribution, cluster, c.ClusterFile.GetRuntimeConfig())
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *CreateProcessor) Join(cluster *v2.Cluster) error {
if err != nil {
return err
}
return yaml.MarshalYamlToFile(constants.Clusterfile(cluster.Name), cluster)
return yaml.MarshalFile(constants.Clusterfile(cluster.Name), cluster)
}

func (c *CreateProcessor) RunGuest(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 @@ -95,7 +95,7 @@ func (d *DeleteProcessor) Reset(cluster *v2.Cluster) error {

func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error {
hosts := append(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()...)
if strings.NotInIPList(cluster.GetRegistryIPAndPort(), hosts) {
if strings.NotInIPList(hosts, cluster.GetRegistryIPAndPort()) {
hosts = append(hosts, cluster.GetRegistryIPAndPort())
}
// umount don't care imageMounts
Expand Down
2 changes: 1 addition & 1 deletion pkg/apply/processor/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error {
if err = OCIToImageMount(c.Buildah, mount); err != nil {
return err
}
mount.Env = maps.MergeMap(mount.Env, c.ExtraEnvs)
mount.Env = maps.Merge(mount.Env, c.ExtraEnvs)

cluster.SetMountImage(mount)
c.NewMounts = append(c.NewMounts, *mount)
Expand Down
8 changes: 4 additions & 4 deletions pkg/apply/processor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path"

"github.com/containers/storage"
"golang.org/x/exp/slices"

"github.com/labring/sealos/pkg/buildah"
"github.com/labring/sealos/pkg/constants"
Expand All @@ -32,7 +33,6 @@ import (
"github.com/labring/sealos/pkg/utils/logger"
"github.com/labring/sealos/pkg/utils/maps"
"github.com/labring/sealos/pkg/utils/rand"
stringsutil "github.com/labring/sealos/pkg/utils/strings"
)

type Interface interface {
Expand Down Expand Up @@ -76,7 +76,7 @@ func SyncClusterStatus(cluster *v2.Cluster, bdah buildah.Interface, reset bool)
return err
}

if reset || stringsutil.InList(ctr.ImageName, cluster.Spec.Image) {
if reset || slices.Contains(cluster.Spec.Image, ctr.ImageName) {
cluster.Status.Mounts = append(cluster.Status.Mounts, *mount)
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func OCIToImageMount(inspector imageInspector, mount *v2.MountImage) error {
return err
}

mount.Env = maps.ListToMap(oci.OCIv1.Config.Env)
mount.Env = maps.FromSlice(oci.OCIv1.Config.Env)
delete(mount.Env, "PATH")
// mount.Entrypoint
var entrypoint []string
Expand Down Expand Up @@ -186,7 +186,7 @@ func MountClusterImages(bdah buildah.Interface, cluster *v2.Cluster, skipApp boo
imageType = maps.GetFromKeys(info.OCIv1.Config.Labels, v2.ImageTypeKeys...)
imageVersion := maps.GetFromKeys(info.OCIv1.Config.Labels, v2.ImageVersionKeys...)
if imageType == string(v2.RootfsImage) {
if !stringsutil.InList(imageVersion, v2.ImageVersionList) {
if !slices.Contains(v2.ImageVersionList, imageVersion) {
return fmt.Errorf("can't apply rootfs type images and version %s not %+v",
imageVersion, v2.ImageVersionList)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apply/processor/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (c *ScaleProcessor) preProcess(cluster *v2.Cluster) error {
obj = append(obj, configs[i])
}
}
if err = yaml.MarshalYamlToFile(clusterPath, obj...); err != nil {
if err = yaml.MarshalFile(clusterPath, obj...); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apply/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (r *ClusterArgs) resetArgs(cmd *cobra.Command, args *ResetArgs) error {
}

if len(args.Cluster.Masters) > 0 {
masters := stringsutil.SplitRemoveEmpty(args.Cluster.Masters, ",")
nodes := stringsutil.SplitRemoveEmpty(args.Cluster.Nodes, ",")
masters := stringsutil.FilterNonEmptyFromString(args.Cluster.Masters, ",")
nodes := stringsutil.FilterNonEmptyFromString(args.Cluster.Nodes, ",")
r.hosts = []v2.Host{}

sshClient := ssh.NewCacheClientFromCluster(r.cluster, true)
Expand Down
11 changes: 6 additions & 5 deletions pkg/apply/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"

"github.com/spf13/cobra"
"golang.org/x/exp/slices"

"github.com/labring/sealos/pkg/apply/applydrivers"
"github.com/labring/sealos/pkg/apply/processor"
Expand Down Expand Up @@ -83,7 +84,7 @@ func withCommonContext(ctx context.Context, cmd *cobra.Command) context.Context
}
if flagChanged(cmd, "env") {
v, _ := cmd.Flags().GetStringSlice("env")
ctx = processor.WithEnvs(ctx, maps.ListToMap(v))
ctx = processor.WithEnvs(ctx, maps.FromSlice(v))
}
return ctx
}
Expand Down Expand Up @@ -118,8 +119,8 @@ func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []str
r.cluster.SetNewImages(imageList)

defaultPort := defaultSSHPort(r.cluster.Spec.SSH.Port)
masters := stringsutil.SplitRemoveEmpty(args.Cluster.Masters, ",")
nodes := stringsutil.SplitRemoveEmpty(args.Cluster.Nodes, ",")
masters := stringsutil.FilterNonEmptyFromString(args.Cluster.Masters, ",")
nodes := stringsutil.FilterNonEmptyFromString(args.Cluster.Nodes, ",")
r.hosts = []v2.Host{}

sshClient := ssh.NewCacheClientFromCluster(r.cluster, true)
Expand All @@ -145,7 +146,7 @@ func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) {
ip, port := iputils.GetHostIPAndPortOrDefault(ips[i], defaultPort)
logger.Debug("defaultPort: %s", defaultPort)
socket := fmt.Sprintf("%s:%s", ip, port)
if stringsutil.In(socket, r.cluster.GetAllIPS()) {
if slices.Contains(r.cluster.GetAllIPS(), socket) {
continue
}
if _, ok := hostMap[port]; !ok {
Expand All @@ -157,7 +158,7 @@ func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) {
_, master0Port := iputils.GetHostIPAndPortOrDefault(ips[0], defaultPort)
for port, host := range hostMap {
host.IPS = removeIPListDuplicatesAndEmpty(host.IPS)
if port == master0Port && stringsutil.InList(v2.MASTER, roles) {
if port == master0Port && slices.Contains(roles, v2.MASTER) {
r.hosts = append([]v2.Host{*host}, r.hosts...)
continue
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/apply/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/labring/sealos/pkg/apply/applydrivers"
Expand All @@ -29,7 +30,6 @@ import (
v2 "github.com/labring/sealos/pkg/types/v1beta1"
fileutil "github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/iputils"
strings2 "github.com/labring/sealos/pkg/utils/strings"
)

// NewScaleApplierFromArgs will filter ip list from command parameters.
Expand Down Expand Up @@ -134,7 +134,7 @@ func verifyAndSetNodes(cmd *cobra.Command, cluster *v2.Cluster, scaleArgs *Scale
// add already joined masters and nodes
for i := range cluster.Spec.Hosts {
h := cluster.Spec.Hosts[i]
if strings2.InList(v2.MASTER, h.Roles) {
if slices.Contains(h.Roles, v2.MASTER) {
hasMaster = true
}
ips := iputils.GetHostIPAndPortSlice(h.IPS, defaultPort)
Expand Down Expand Up @@ -163,7 +163,7 @@ func verifyAndSetNodes(cmd *cobra.Command, cluster *v2.Cluster, scaleArgs *Scale
if alreadyIn.Has(addr) {
return nil, fmt.Errorf("host %s already joined", addr)
}
if !strings2.InList(addr, exclude) {
if !slices.Contains(exclude, addr) {
addrs = append(addrs, addr)
}
}
Expand Down Expand Up @@ -220,9 +220,10 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error {
}

//master0 machine cannot be deleted
if strings2.InList(cluster.GetMaster0IPAndPort(), strings.Split(masters, ",")) ||
strings2.InList(cluster.GetMaster0IP(), strings.Split(masters, ",")) {
return fmt.Errorf("master0 machine cannot be deleted")
if set := strings.Split(masters, ","); len(set) > 0 {
if slices.Contains(set, cluster.GetMaster0IPAndPort()) || slices.Contains(set, cluster.GetMaster0IP()) {
return fmt.Errorf("master0 machine cannot be deleted")
}
}

defaultPort := defaultSSHPort(cluster.Spec.SSH.Port)
Expand Down Expand Up @@ -251,14 +252,14 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error {

if masters != "" && IsIPList(masters) {
for i := range cluster.Spec.Hosts {
if strings2.InList(v2.MASTER, cluster.Spec.Hosts[i].Roles) {
if slices.Contains(cluster.Spec.Hosts[i].Roles, v2.MASTER) {
cluster.Spec.Hosts[i].IPS = returnFilteredIPList(cluster.Spec.Hosts[i].IPS, strings.Split(masters, ","), defaultPort)
}
}
}
if nodes != "" && IsIPList(nodes) {
for i := range cluster.Spec.Hosts {
if strings2.InList(v2.NODE, cluster.Spec.Hosts[i].Roles) {
if slices.Contains(cluster.Spec.Hosts[i].Roles, v2.NODE) {
cluster.Spec.Hosts[i].IPS = returnFilteredIPList(cluster.Spec.Hosts[i].IPS, strings.Split(nodes, ","), defaultPort)
}
}
Expand All @@ -276,7 +277,7 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error {
func returnFilteredIPList(clusterIPList []string, toBeDeletedIPList []string, defaultPort string) (res []string) {
toBeDeletedIPList = fillIPAndPort(toBeDeletedIPList, defaultPort)
for _, ip := range clusterIPList {
if !strings2.In(ip, toBeDeletedIPList) {
if !slices.Contains(toBeDeletedIPList, ip) {
res = append(res, net.JoinHostPort(iputils.GetHostIPAndPortOrDefault(ip, defaultPort)))
}
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/apply/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func PreProcessIPList(joinArgs *Cluster) error {
}

func removeIPListDuplicatesAndEmpty(ipList []string) []string {
return stringsutil.RemoveDuplicate(stringsutil.RemoveStrSlice(ipList, []string{""}))
return stringsutil.RemoveDuplicate(stringsutil.RemoveSubSlice(ipList, []string{""}))
}

func IsIPList(args string) bool {
Expand Down Expand Up @@ -116,7 +116,7 @@ func GetHostArch(sshClient ssh.Interface, ip string) string {
}

func GetImagesDiff(current, desired []string) []string {
return stringsutil.RemoveDuplicate(stringsutil.RemoveStrSlice(desired, current))
return stringsutil.RemoveDuplicate(stringsutil.RemoveSubSlice(desired, current))
}

func CompareImageSpecHash(currentImages []string, desiredImages []string) bool {
Expand Down Expand Up @@ -147,8 +147,7 @@ func CheckAndInitialize(cluster *v2.Cluster) {
}

if len(cluster.Spec.Hosts) == 0 {
clusterSSH := cluster.GetSSH()
sshClient := ssh.MustNewClient(&clusterSSH, true)
sshClient := ssh.MustNewClient(cluster.Spec.SSH.DeepCopy(), true)

localIpv4 := iputils.GetLocalIpv4()
defaultPort := defaultSSHPort(cluster.Spec.SSH.Port)
Expand Down
6 changes: 3 additions & 3 deletions pkg/bootstrap/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func NewContextFrom(cluster *v2.Cluster) Context {

// bootstrap process depends on the envs in the rootfs image
shellWrapper := func(host, shell string) string {
envs := maps.MergeMap(rootfsEnvs, envProcessor.Getenv(host))
return stringsutil.RenderShellFromEnv(shell, envs)
envs := maps.Merge(rootfsEnvs, envProcessor.Getenv(host))
return stringsutil.RenderShellWithEnv(shell, envs)
}
return &realContext{
cluster: cluster,
execer: execer,
bash: constants.NewBash(cluster.GetName(), cluster.GetImageLabels(), shellWrapper),
bash: constants.NewBash(cluster.GetName(), cluster.GetAllLabels(), shellWrapper),
pathResolver: constants.NewPathResolver(cluster.GetName()),
remoter: remoter,
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/checker/crictl_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ func (n *CRICtlChecker) Check(cluster *v2.Cluster, phase string) error {

pauseImage := ""
for _, mountImg := range cluster.Status.Mounts {
if mountImg.Type == v2.RootfsImage || mountImg.Type == v2.PatchImage {
pauseImage = mountImg.Env["sandboxImage"]
if mountImg.IsRootFs() || mountImg.IsPatch() {
if v, ok := mountImg.Env["sandboxImage"]; ok {
pauseImage = v
break
}
}
}
sshCtx := ssh.NewCacheClientFromCluster(cluster, false)
Expand Down
2 changes: 1 addition & 1 deletion pkg/clusterfile/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetClusterFromName(clusterName string) (cluster *v2.Cluster, err error) {
}
func GetClusterFromFile(filepath string) (cluster *v2.Cluster, err error) {
cluster = &v2.Cluster{}
if err = yaml2.UnmarshalYamlFromFile(filepath, cluster); err != nil {
if err = yaml2.UnmarshalFile(filepath, cluster); err != nil {
return nil, fmt.Errorf("failed to get cluster from %s, %v", filepath, err)
}
return cluster, nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (p *processor) Getenv(host string) map[string]string {

func (p *processor) WrapShell(host, shell string) string {
envs := p.getHostEnvInCache(host)
return stringsutil.RenderShellFromEnv(shell, envs)
return stringsutil.RenderShellWithEnv(shell, envs)
}

func (p *processor) RenderAll(host, dir string, envs map[string]string) error {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (p *processor) RenderAll(host, dir string, envs map[string]string) error {
return fmt.Errorf("failed to create template: %s %v", path, err)
}
if host != "" {
data := maps.MergeMap(envs, p.getHostEnvInCache(host))
data := maps.Merge(envs, p.getHostEnvInCache(host))
if err := t.Execute(writer, data); err != nil {
return fmt.Errorf("failed to render env template: %s %v", path, err)
}
Expand Down Expand Up @@ -139,8 +139,8 @@ func (p *processor) getHostEnv(hostIP string) map[string]string {
}
}

hostEnvMap := maps.ListToMap(hostEnv)
specEnvMap := maps.ListToMap(p.Spec.Env)
hostEnvMap := maps.FromSlice(hostEnv)
specEnvMap := maps.FromSlice(p.Spec.Env)

excludeSysEnv := func(m map[string]string) map[string]string {
m, exclude := ExcludeKeysWithPrefix(m, "SEALOS_SYS")
Expand All @@ -150,7 +150,7 @@ func (p *processor) getHostEnv(hostIP string) map[string]string {
return m
}

envs := maps.MergeMap(excludeSysEnv(specEnvMap), excludeSysEnv(hostEnvMap))
envs := maps.Merge(excludeSysEnv(specEnvMap), excludeSysEnv(hostEnvMap))
return envs
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (d *Default) Apply(cluster *v2.Cluster, mounts []v2.MountImage, targetHosts
cmds := formalizeImageCommands(cluster, i, m, envs)
eg.Go(func() error {
return execer.CmdAsyncWithContext(ctx, node,
stringsutil.RenderShellFromEnv(strings.Join(cmds, "; "), envs),
stringsutil.RenderShellWithEnv(strings.Join(cmds, "; "), envs),
)
})
}
Expand All @@ -67,7 +67,7 @@ func (d *Default) Apply(cluster *v2.Cluster, mounts []v2.MountImage, targetHosts
envs := envWrapper.Getenv(cluster.GetMaster0IP())
cmds := formalizeImageCommands(cluster, i, m, envs)
if err := execer.CmdAsync(cluster.GetMaster0IPAndPort(),
stringsutil.RenderShellFromEnv(strings.Join(cmds, "; "), envs),
stringsutil.RenderShellWithEnv(strings.Join(cmds, "; "), envs),
); err != nil {
return err
}
Expand All @@ -90,7 +90,7 @@ func formalizeWorkingCommand(clusterName string, imageName string, t v2.ImageTyp
}

func formalizeImageCommands(cluster *v2.Cluster, index int, m v2.MountImage, extraEnvs map[string]string) []string {
envs := maps.MergeMap(m.Env, extraEnvs)
envs := maps.Merge(m.Env, extraEnvs)
envs = v2.MergeEnvWithBuiltinKeys(envs, m)
mapping := expansion.MappingFuncFor(envs)

Expand Down
Loading

0 comments on commit 5a65c83

Please sign in to comment.