From a9b6c299b4123a0bfdf487380db3466b80af3900 Mon Sep 17 00:00:00 2001 From: fengxsong Date: Mon, 4 Sep 2023 07:53:17 +0000 Subject: [PATCH] style: remove unused utils --- go.mod | 2 +- .../applydrivers/apply_drivers_default.go | 4 +- pkg/apply/gen.go | 2 +- pkg/apply/processor/create.go | 4 +- pkg/apply/processor/delete.go | 2 +- pkg/apply/processor/install.go | 2 +- pkg/apply/processor/interface.go | 8 +- pkg/apply/processor/scale.go | 2 +- pkg/apply/reset.go | 4 +- pkg/apply/run.go | 11 +- pkg/apply/scale.go | 19 +-- pkg/apply/utils.go | 7 +- pkg/bootstrap/context.go | 6 +- pkg/checker/crictl_checker.go | 7 +- pkg/clusterfile/util.go | 2 +- pkg/env/env.go | 10 +- pkg/guest/guest.go | 6 +- pkg/image/merge.go | 8 +- pkg/runtime/k3s/bootstrap.go | 2 +- pkg/runtime/k3s/k3s.go | 2 +- pkg/runtime/kubernetes/certs.go | 2 +- pkg/runtime/kubernetes/kubeadm.go | 6 +- pkg/runtime/kubernetes/master.go | 2 +- pkg/runtime/kubernetes/runtime.go | 2 +- pkg/runtime/kubernetes/runtime_getter.go | 4 +- .../kubernetes/types/kubeadm_config.go | 4 +- .../kubernetes/types/kubeadm_config_test.go | 4 +- pkg/runtime/kubernetes/upgrade.go | 4 +- pkg/types/v1beta1/helper.go | 105 ------------- pkg/types/v1beta1/utils.go | 145 +++++++----------- pkg/utils/hosts/hosts.go | 6 +- pkg/utils/maps/maps.go | 14 +- pkg/utils/maps/maps_test.go | 4 +- pkg/utils/strings/strings.go | 114 +++----------- pkg/utils/sync/pool.go | 51 ------ pkg/utils/toml/toml.go | 4 +- pkg/utils/yaml/yaml.go | 21 +-- service/auth/conf/config.go | 2 +- 38 files changed, 165 insertions(+), 439 deletions(-) delete mode 100644 pkg/types/v1beta1/helper.go delete mode 100644 pkg/utils/sync/pool.go diff --git a/go.mod b/go.mod index 0f58b93d208..8c9ef32fe17 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,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 @@ -245,7 +246,6 @@ require ( go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.3.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 diff --git a/pkg/apply/applydrivers/apply_drivers_default.go b/pkg/apply/applydrivers/apply_drivers_default.go index d334d960812..d072ecdeb91 100644 --- a/pkg/apply/applydrivers/apply_drivers_default.go +++ b/pkg/apply/applydrivers/apply_drivers_default.go @@ -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) } @@ -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) diff --git a/pkg/apply/gen.go b/pkg/apply/gen.go index c4f8e72b41a..2a2497428a6 100644 --- a/pkg/apply/gen.go +++ b/pkg/apply/gen.go @@ -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) diff --git a/pkg/apply/processor/create.go b/pkg/apply/processor/create.go index 436e21aa11f..94d5df78ae1 100644 --- a/pkg/apply/processor/create.go +++ b/pkg/apply/processor/create.go @@ -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()) @@ -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 { diff --git a/pkg/apply/processor/delete.go b/pkg/apply/processor/delete.go index 302665cf85f..7a14ad12856 100644 --- a/pkg/apply/processor/delete.go +++ b/pkg/apply/processor/delete.go @@ -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 diff --git a/pkg/apply/processor/install.go b/pkg/apply/processor/install.go index 484b8fbde2b..a03ad4f5fc5 100644 --- a/pkg/apply/processor/install.go +++ b/pkg/apply/processor/install.go @@ -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) diff --git a/pkg/apply/processor/interface.go b/pkg/apply/processor/interface.go index ed99275ad7a..80afc7cf560 100644 --- a/pkg/apply/processor/interface.go +++ b/pkg/apply/processor/interface.go @@ -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" @@ -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 { @@ -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) } } @@ -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 @@ -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) } diff --git a/pkg/apply/processor/scale.go b/pkg/apply/processor/scale.go index 82efedcce89..02c76d9dc4e 100644 --- a/pkg/apply/processor/scale.go +++ b/pkg/apply/processor/scale.go @@ -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 } } diff --git a/pkg/apply/reset.go b/pkg/apply/reset.go index 3b7c884a399..fc37587ba6e 100644 --- a/pkg/apply/reset.go +++ b/pkg/apply/reset.go @@ -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) diff --git a/pkg/apply/run.go b/pkg/apply/run.go index b5302685c1b..9decee331d9 100644 --- a/pkg/apply/run.go +++ b/pkg/apply/run.go @@ -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" @@ -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 } @@ -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) @@ -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 { @@ -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 } diff --git a/pkg/apply/scale.go b/pkg/apply/scale.go index 6103c7e3634..646c4950846 100644 --- a/pkg/apply/scale.go +++ b/pkg/apply/scale.go @@ -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" @@ -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. @@ -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) @@ -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) } } @@ -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) @@ -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) } } @@ -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))) } } diff --git a/pkg/apply/utils.go b/pkg/apply/utils.go index c52cdd89e84..a62470c9d1d 100644 --- a/pkg/apply/utils.go +++ b/pkg/apply/utils.go @@ -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 { @@ -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 { @@ -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) diff --git a/pkg/bootstrap/context.go b/pkg/bootstrap/context.go index d29a9553803..ebff342fc06 100644 --- a/pkg/bootstrap/context.go +++ b/pkg/bootstrap/context.go @@ -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, } diff --git a/pkg/checker/crictl_checker.go b/pkg/checker/crictl_checker.go index 5a940753ea7..aac0f485639 100644 --- a/pkg/checker/crictl_checker.go +++ b/pkg/checker/crictl_checker.go @@ -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) diff --git a/pkg/clusterfile/util.go b/pkg/clusterfile/util.go index 18d49c2b5c0..fabfd7bd3d2 100644 --- a/pkg/clusterfile/util.go +++ b/pkg/clusterfile/util.go @@ -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 diff --git a/pkg/env/env.go b/pkg/env/env.go index 722a7e4e742..21cb2e5dac6 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -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 { @@ -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) } @@ -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") @@ -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 } diff --git a/pkg/guest/guest.go b/pkg/guest/guest.go index f04cf041199..2330b2e7f52 100644 --- a/pkg/guest/guest.go +++ b/pkg/guest/guest.go @@ -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), ) }) } @@ -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 } @@ -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) diff --git a/pkg/image/merge.go b/pkg/image/merge.go index 232f22033c6..c2d97d6390e 100644 --- a/pkg/image/merge.go +++ b/pkg/image/merge.go @@ -57,14 +57,14 @@ func MergeDockerfileFromImages(imageObjList []map[string]v1.Image) (string, erro for _, oci := range imageObjList { for name, val := range oci { imageNames = append(imageNames, name) - labels = maps.MergeMap(labels, val.Config.Labels) + labels = maps.Merge(labels, val.Config.Labels) if val.Config.Labels != nil { if key := maps.GetFromKeys(val.Config.Labels, v1beta1.ImageTypeKeys...); key == string(v1beta1.RootfsImage) { isRootfs = true } } - envs = maps.MergeMap(envs, maps.ListToMap(val.Config.Env)) + envs = maps.Merge(envs, maps.FromSlice(val.Config.Env)) cmds = append(cmds, val.Config.Cmd...) entrypoints = append(entrypoints, val.Config.Entrypoint...) } @@ -107,8 +107,8 @@ COPY --from={{.}} . . return "", err } data := map[string]any{ - "Labels": maps.MapToStringBySpilt(labels, " \\\n\t"), - "Envs": maps.MapToStringBySpilt(envs, " \\\n\t"), + "Labels": maps.ToString(labels, " \\\n\t"), + "Envs": maps.ToString(envs, " \\\n\t"), "Entrypoints": strings.Join(entrypoints, ","), "CMDs": strings.Join(cmds, ","), "Images": imageNames, diff --git a/pkg/runtime/k3s/bootstrap.go b/pkg/runtime/k3s/bootstrap.go index 609998610bf..d1604c3c2d6 100644 --- a/pkg/runtime/k3s/bootstrap.go +++ b/pkg/runtime/k3s/bootstrap.go @@ -151,7 +151,7 @@ func (k *K3s) getRawInitConfig(callbacks ...callback) ([]byte, error) { if err != nil { return nil, err } - return yaml.MarshalYamlConfigs(cfg) + return yaml.MarshalConfigs(cfg) } func (k *K3s) generateAndSendInitConfig() error { diff --git a/pkg/runtime/k3s/k3s.go b/pkg/runtime/k3s/k3s.go index 0b5b8f297d2..6105da10ded 100644 --- a/pkg/runtime/k3s/k3s.go +++ b/pkg/runtime/k3s/k3s.go @@ -85,7 +85,7 @@ func (k *K3s) GetRawConfig() ([]byte, error) { } cluster := k.cluster.DeepCopy() cluster.Status = v2.ClusterStatus{} - return yaml.MarshalYamlConfigs(cluster, cfg) + return yaml.MarshalConfigs(cluster, cfg) } func (k *K3s) SyncNodeIPVS(_, _ []string) error { diff --git a/pkg/runtime/kubernetes/certs.go b/pkg/runtime/kubernetes/certs.go index 55ff3589eae..f6d03c3e1fc 100644 --- a/pkg/runtime/kubernetes/certs.go +++ b/pkg/runtime/kubernetes/certs.go @@ -89,7 +89,7 @@ func (k *KubeadmRuntime) saveNewKubeadmConfig() error { return err } certPath := path.Join(k.pathResolver.EtcPath(), defaultUpdateKubeadmFileName) - return yaml.MarshalYamlToFile(certPath, obj) + return yaml.MarshalFile(certPath, obj) } func (k *KubeadmRuntime) uploadConfigFromKubeadm() error { diff --git a/pkg/runtime/kubernetes/kubeadm.go b/pkg/runtime/kubernetes/kubeadm.go index 9459b5563b7..a5767700f99 100644 --- a/pkg/runtime/kubernetes/kubeadm.go +++ b/pkg/runtime/kubernetes/kubeadm.go @@ -473,7 +473,7 @@ func (k *KubeadmRuntime) generateInitConfigs() ([]byte, error) { if err != nil { return nil, err } - return yaml.MarshalYamlConfigs(&conversion.InitConfiguration, + return yaml.MarshalConfigs(&conversion.InitConfiguration, &conversion.ClusterConfiguration, &conversion.KubeletConfiguration, &conversion.KubeProxyConfiguration) @@ -516,7 +516,7 @@ func (k *KubeadmRuntime) generateJoinNodeConfigs(node string) ([]byte, error) { if err != nil { return nil, err } - return yaml.MarshalYamlConfigs( + return yaml.MarshalConfigs( &conversion.KubeletConfiguration, &conversion.JoinConfiguration) } @@ -535,7 +535,7 @@ func (k *KubeadmRuntime) generateJoinMasterConfigs(masterIP string) ([]byte, err if err != nil { return nil, err } - return yaml.MarshalYamlConfigs(&conversion.JoinConfiguration, &conversion.KubeletConfiguration) + return yaml.MarshalConfigs(&conversion.JoinConfiguration, &conversion.KubeletConfiguration) } func (k *KubeadmRuntime) setCGroupDriverAndSocket(node string) error { diff --git a/pkg/runtime/kubernetes/master.go b/pkg/runtime/kubernetes/master.go index 6b7af582e9c..eb90fe049f6 100644 --- a/pkg/runtime/kubernetes/master.go +++ b/pkg/runtime/kubernetes/master.go @@ -173,7 +173,7 @@ func (k *KubeadmRuntime) deleteMasters(masters []string) error { func (k *KubeadmRuntime) deleteMaster(master string) error { return k.resetNode(master, func() { //remove master - masterIPs := strings.SliceRemoveStr(k.getMasterIPList(), master) + masterIPs := strings.RemoveFromSlice(k.getMasterIPList(), master) if len(masterIPs) > 0 { // TODO: do we need draining first? if err := k.removeNode(master); err != nil { diff --git a/pkg/runtime/kubernetes/runtime.go b/pkg/runtime/kubernetes/runtime.go index b29b84072e2..53feafb98b6 100644 --- a/pkg/runtime/kubernetes/runtime.go +++ b/pkg/runtime/kubernetes/runtime.go @@ -79,7 +79,7 @@ func (k *KubeadmRuntime) GetRawConfig() ([]byte, error) { conversion.KubeProxyConfiguration, conversion.KubeletConfiguration, } - data, err := yaml.MarshalYamlConfigs(objects...) + data, err := yaml.MarshalConfigs(objects...) if err != nil { return nil, err } diff --git a/pkg/runtime/kubernetes/runtime_getter.go b/pkg/runtime/kubernetes/runtime_getter.go index a2cf87b03f7..ff6a2e764f4 100644 --- a/pkg/runtime/kubernetes/runtime_getter.go +++ b/pkg/runtime/kubernetes/runtime_getter.go @@ -103,8 +103,8 @@ func (k *KubeadmRuntime) getVIPFromImage() string { img := k.cluster.GetRootfsImage() if img != nil { vip := getLabelValueOrDefault(img.Labels, v1beta1.ImageVIPKey, defaultVIP) - envs := maps.MergeMap(img.Env, k.getEnvInterface().Getenv(k.getMaster0IP())) - return stringsutil.RenderTextFromEnv(vip, envs) + envs := maps.Merge(img.Env, k.getEnvInterface().Getenv(k.getMaster0IP())) + return stringsutil.RenderTextWithEnv(vip, envs) } return defaultVIP } diff --git a/pkg/runtime/kubernetes/types/kubeadm_config.go b/pkg/runtime/kubernetes/types/kubeadm_config.go index a9680c74cb1..b08d853f659 100644 --- a/pkg/runtime/kubernetes/types/kubeadm_config.go +++ b/pkg/runtime/kubernetes/types/kubeadm_config.go @@ -248,9 +248,9 @@ func deleteFeatureMap[T string | bool](currentFeature map[string]T, versionStr s func updateFeatureGatesConfiguration(featureGates any, version string) any { switch x := featureGates.(type) { case string: - currentFeature := maps.StringToMap(x, ",") + currentFeature := maps.FromString(x, ",") currentFeature = deleteFeatureMap(currentFeature, version) - return maps.MapToStringBySpilt(currentFeature, ",") + return maps.ToString(currentFeature, ",") case map[string]bool: newFeature := x newFeature = deleteFeatureMap(newFeature, version) diff --git a/pkg/runtime/kubernetes/types/kubeadm_config_test.go b/pkg/runtime/kubernetes/types/kubeadm_config_test.go index b452305eb0c..5a00a0aae2d 100644 --- a/pkg/runtime/kubernetes/types/kubeadm_config_test.go +++ b/pkg/runtime/kubernetes/types/kubeadm_config_test.go @@ -54,7 +54,7 @@ func TestKubeadmRuntime_setFeatureGatesConfiguration(t *testing.T) { k.SetKubeVersion(tt.version) k.FinalizeFeatureGatesConfiguration() - data, err := yaml.MarshalYamlConfigs( + data, err := yaml.MarshalConfigs( &k.InitConfiguration, &k.ClusterConfiguration, &k.KubeletConfiguration, @@ -163,7 +163,7 @@ etcd: k.SetKubeVersion(tt.version) k.FinalizeFeatureGatesConfiguration() - data, err := yaml.MarshalYamlConfigs( + data, err := yaml.MarshalConfigs( &k.ClusterConfiguration, ) if err != nil { diff --git a/pkg/runtime/kubernetes/upgrade.go b/pkg/runtime/kubernetes/upgrade.go index 37a9221382d..663cfb2e00d 100644 --- a/pkg/runtime/kubernetes/upgrade.go +++ b/pkg/runtime/kubernetes/upgrade.go @@ -188,7 +188,7 @@ func (k *KubeadmRuntime) autoUpdateConfig(version string) error { if err != nil { return err } - newClusterData, err := yaml.MarshalYamlConfigs(&conversion.ClusterConfiguration) + newClusterData, err := yaml.MarshalConfigs(&conversion.ClusterConfiguration) if err != nil { logger.Error("failed to encode ClusterConfiguration: %s", err) return err @@ -200,7 +200,7 @@ func (k *KubeadmRuntime) autoUpdateConfig(version string) error { return err } - newKubeletData, err := yaml.MarshalYamlConfigs(&conversion.KubeletConfiguration) + newKubeletData, err := yaml.MarshalConfigs(&conversion.KubeletConfiguration) if err != nil { logger.Error("failed to encode KubeletConfiguration: %s", err) return err diff --git a/pkg/types/v1beta1/helper.go b/pkg/types/v1beta1/helper.go deleted file mode 100644 index fc40a443b2c..00000000000 --- a/pkg/types/v1beta1/helper.go +++ /dev/null @@ -1,105 +0,0 @@ -/* -Copyright 2022 The labring Authors. - -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 v1beta1 - -import ( - "fmt" - - v1 "k8s.io/api/core/v1" - utilerrors "k8s.io/apimachinery/pkg/util/errors" - "k8s.io/apimachinery/pkg/util/sets" -) - -// ToAggregate converts the ErrorList into an errors.Aggregate. -func ToAggregate(list []error) utilerrors.Aggregate { - errs := make([]error, 0, len(list)) - errorMsgs := sets.NewString() - for _, err := range list { - msg := fmt.Sprintf("%v", err) - if errorMsgs.Has(msg) { - continue - } - errorMsgs.Insert(msg) - errs = append(errs, err) - } - return utilerrors.NewAggregate(errs) -} - -func IsConditionTrue(conditions []ClusterCondition, condition ClusterCondition) bool { - for _, con := range conditions { - if con.Type == condition.Type && con.Status == condition.Status { - return true - } - } - return false -} -func IsConditionsTrue(conditions []ClusterCondition) bool { - if len(conditions) == 0 { - return false - } - for _, condition := range conditions { - if condition.Status != v1.ConditionTrue { - return false - } - } - return true -} - -// UpdateCondition updates condition in cluster conditions using giving condition -// adds condition if not existed -func UpdateCondition(conditions []ClusterCondition, condition ClusterCondition) []ClusterCondition { - if conditions == nil { - conditions = make([]ClusterCondition, 0) - } - hasCondition := false - for i, cond := range conditions { - if cond.Type == condition.Type { - hasCondition = true - if cond.Reason != condition.Reason || cond.Status != condition.Status || cond.Message != condition.Message { - conditions[i] = condition - } - } - } - if !hasCondition { - conditions = append(conditions, condition) - } - return conditions -} - -// UpdateCommandCondition updates condition in cluster conditions using giving condition, append only -func UpdateCommandCondition(cmdConditions []CommandCondition, cmdCondition CommandCondition) []CommandCondition { - if cmdConditions == nil { - cmdConditions = make([]CommandCondition, 0) - } - cmdConditions = append(cmdConditions, cmdCondition) - return cmdConditions -} - -func DeleteCondition(conditions []ClusterCondition, conditionType string) []ClusterCondition { - if conditions == nil { - conditions = make([]ClusterCondition, 0) - } - newConditions := make([]ClusterCondition, 0) - for _, cond := range conditions { - if cond.Type == conditionType { - continue - } - newConditions = append(newConditions, cond) - } - conditions = newConditions - return conditions -} diff --git a/pkg/types/v1beta1/utils.go b/pkg/types/v1beta1/utils.go index 89e2f259331..80456423e1f 100644 --- a/pkg/types/v1beta1/utils.go +++ b/pkg/types/v1beta1/utils.go @@ -17,32 +17,14 @@ limitations under the License. package v1beta1 import ( - "fmt" - - "golang.org/x/exp/slices" - "github.com/Masterminds/semver/v3" + "golang.org/x/exp/slices" + "k8s.io/apimachinery/pkg/util/sets" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/maps" ) -func (c *Cluster) GetSSH() SSH { - return c.Spec.SSH -} - -func (c *Cluster) SetSSH(ssh SSH) { - c.Spec.SSH = ssh -} - -func (c *Cluster) GetHosts() []Host { - return c.Spec.Hosts -} - -func (c *Cluster) SetHosts(hosts []Host) { - c.Spec.Hosts = hosts -} - func (c *Cluster) GetMasterIPList() []string { return iputils.GetHostIPs(c.GetMasterIPAndPortList()) } @@ -128,7 +110,7 @@ func (c *Cluster) GetRootfsImage() *MountImage { var image *MountImage if c.Status.Mounts != nil { for _, img := range c.Status.Mounts { - if img.Type == RootfsImage { + if img.IsRootFs() { image = &img break } @@ -137,35 +119,30 @@ func (c *Cluster) GetRootfsImage() *MountImage { return image } -func (c *Cluster) FindImage(targetImage string) *MountImage { - var image *MountImage +func (c *Cluster) FindImage(name string) *MountImage { if c.Status.Mounts != nil { for _, img := range c.Status.Mounts { - if img.ImageName == targetImage { - image = &img - break + if img.ImageName == name { + return &img } } } - return image + return nil } -func (c *Cluster) SetMountImage(targetMount *MountImage) { - tgMount := targetMount.DeepCopy() +func (c *Cluster) SetMountImage(mount *MountImage) { + if mount == nil { + return + } + if c.Status.Mounts != nil { - if tgMount != nil { - hasMount := false - for i, img := range c.Status.Mounts { - if img.Name == tgMount.Name && img.Type == tgMount.Type { - c.Status.Mounts[i] = *tgMount - hasMount = true - break - } - } - if !hasMount { - c.Status.Mounts = append(c.Status.Mounts, *tgMount) + for i, img := range c.Status.Mounts { + if img.Name == mount.Name && img.Type == mount.Type { + c.Status.Mounts[i] = *mount.DeepCopy() + return } } + c.Status.Mounts = append(c.Status.Mounts, *mount) } } @@ -174,7 +151,7 @@ func (c *Cluster) ReplaceRootfsImage() { var v1, v2 string for i := range c.Status.Mounts { img := c.Status.Mounts[i] - if img.Type == RootfsImage { + if img.IsRootFs() { if v1 == "" { v1, i1 = img.Labels[ImageKubeVersionKey], i } else { @@ -199,68 +176,22 @@ func (c *Cluster) ReplaceRootfsImage() { } func (c *Cluster) SetNewImages(images []string) { - imageSets := map[string]struct{}{} - for _, img := range c.Spec.Image { - imageSets[img] = struct{}{} - } + set := sets.NewString(c.Spec.Image...) for _, img := range images { - if _, ok := imageSets[img]; !ok { + if !set.Has(img) { c.Spec.Image = append(c.Spec.Image, img) } } } -func (c *Cluster) GetImageLabels() map[string]string { + +func (c *Cluster) GetAllLabels() map[string]string { var imageLabelMap map[string]string for _, img := range c.Status.Mounts { - imageLabelMap = maps.MergeMap(imageLabelMap, img.Labels) + imageLabelMap = maps.Merge(imageLabelMap, img.Labels) } return imageLabelMap } -func (c *Cluster) GetImageEnvs() map[string]string { - var imageEnvMap map[string]string - for _, img := range c.Status.Mounts { - imageEnvMap = maps.MergeMap(imageEnvMap, img.Env) - } - return imageEnvMap -} - -func (c *Cluster) GetAppImage(defaultImageName, defaultMount string) *MountImage { - var image *MountImage - if c.Status.Mounts != nil { - for _, img := range c.Status.Mounts { - if img.Type == AppImage && img.ImageName == defaultImageName { - image = &img - break - } - } - } - if image == nil { - for i, img := range c.Spec.Image { - if img == defaultImageName { - image = &MountImage{ - Name: fmt.Sprintf("%s-%d", c.Name, i), - Type: AppImage, - ImageName: defaultImageName, - MountPoint: defaultMount, - } - } - } - } - return image -} - -func (c *Cluster) HasAppImage() bool { - if c.Status.Mounts != nil { - for _, img := range c.Status.Mounts { - if img.Type == AppImage { - return true - } - } - } - return false -} - func (c *Cluster) GetRolesByIP(ip string) []string { var routes []string for _, host := range c.Spec.Hosts { @@ -278,3 +209,33 @@ func (c *Cluster) GetDistribution() string { } return "" } + +// UpdateCondition updates condition in cluster conditions using giving condition +// adds condition if not existed +func UpdateCondition(conditions []ClusterCondition, condition ClusterCondition) []ClusterCondition { + if conditions == nil { + conditions = make([]ClusterCondition, 0) + } + hasCondition := false + for i, cond := range conditions { + if cond.Type == condition.Type { + hasCondition = true + if cond.Reason != condition.Reason || cond.Status != condition.Status || cond.Message != condition.Message { + conditions[i] = condition + } + } + } + if !hasCondition { + conditions = append(conditions, condition) + } + return conditions +} + +// UpdateCommandCondition updates condition in cluster conditions using giving condition, append only +func UpdateCommandCondition(cmdConditions []CommandCondition, cmdCondition CommandCondition) []CommandCondition { + if cmdConditions == nil { + cmdConditions = make([]CommandCondition, 0) + } + cmdConditions = append(cmdConditions, cmdCondition) + return cmdConditions +} diff --git a/pkg/utils/hosts/hosts.go b/pkg/utils/hosts/hosts.go index 25d0540bb16..4cc50062b1d 100644 --- a/pkg/utils/hosts/hosts.go +++ b/pkg/utils/hosts/hosts.go @@ -25,7 +25,7 @@ import ( "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" - strings2 "github.com/labring/sealos/pkg/utils/strings" + stringsutils "github.com/labring/sealos/pkg/utils/strings" "github.com/emirpasic/gods/maps/linkedhashmap" ) @@ -83,7 +83,7 @@ func (h *HostFile) ParseHostFile(path string) (*linkedhashmap.Map, error) { if rErr == io.EOF { break } - if len(str) == 0 || str == "\r\n" || strings2.IsEmptyLine(str) { + if len(str) == 0 || str == "\r\n" || stringsutils.IsEmptyLine(str) { continue } @@ -97,7 +97,7 @@ func (h *HostFile) ParseHostFile(path string) (*linkedhashmap.Map, error) { //if !iputils.CheckDomain(curDomain) { // return lm, errors.New(" file contain error domain" + curDomain) //} - curIP := strings2.TrimSpaceWS(tmpHostnameArr[0]) + curIP := stringsutils.TrimSpaceWS(tmpHostnameArr[0]) checkIP := net.ParseIP(curIP) if checkIP == nil { diff --git a/pkg/utils/maps/maps.go b/pkg/utils/maps/maps.go index de74342a17d..805ed00b27b 100644 --- a/pkg/utils/maps/maps.go +++ b/pkg/utils/maps/maps.go @@ -23,20 +23,20 @@ import ( "sigs.k8s.io/kustomize/kyaml/yaml" ) -func MapToStringBySpilt(data map[string]string, spilt string) string { +func ToString(data map[string]string, sep string) string { result := make([]string, len(data)) for i, k := range yaml.SortedMapKeys(data) { result[i] = fmt.Sprintf("%s=%s", k, data[k]) } - return strings.Join(result, spilt) + return strings.Join(result, sep) } -func StringToMap(data string, spilt string) map[string]string { - list := strings.Split(data, spilt) - return ListToMap(list) +func FromString(data string, sep string) map[string]string { + list := strings.Split(data, sep) + return FromSlice(list) } -func ListToMap(data []string) map[string]string { +func FromSlice(data []string) map[string]string { m := make(map[string]string) for _, l := range data { if l != "" { @@ -49,7 +49,7 @@ func ListToMap(data []string) map[string]string { return m } -func MergeMap(ms ...map[string]string) map[string]string { +func Merge(ms ...map[string]string) map[string]string { res := map[string]string{} for _, m := range ms { for k, v := range m { diff --git a/pkg/utils/maps/maps_test.go b/pkg/utils/maps/maps_test.go index 2197df87b3c..281cbfe5070 100644 --- a/pkg/utils/maps/maps_test.go +++ b/pkg/utils/maps/maps_test.go @@ -110,7 +110,7 @@ func TestMergeMap(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - data := MergeMap(tt.args.dst, tt.args.src) + data := Merge(tt.args.dst, tt.args.src) if !reflect.DeepEqual(data, tt.want) { t.Errorf("MergeMap() = %v, want %v", data, tt.want) } @@ -119,6 +119,6 @@ func TestMergeMap(t *testing.T) { } func TestStringToMap(_ *testing.T) { - data := StringToMap("address=reg.real-ai.cn,auth=xxx", ",") + data := FromString("address=reg.real-ai.cn,auth=xxx", ",") fmt.Println(data["address"]) } diff --git a/pkg/utils/strings/strings.go b/pkg/utils/strings/strings.go index ab9acbe50ad..ca1e191e355 100644 --- a/pkg/utils/strings/strings.go +++ b/pkg/utils/strings/strings.go @@ -17,36 +17,18 @@ limitations under the License. package strings import ( - "bytes" "fmt" - "net" "regexp" - "sort" "strings" "unicode" + "golang.org/x/exp/slices" + "github.com/labring/sealos/pkg/utils/logger" ) -// In returns if the key is in the slice. -func In(key string, slice []string) bool { +func NotInIPList(slice []string, key string) bool { for _, s := range slice { - if key == s { - return true - } - } - return false -} - -func InList(key string, slice []string) bool { - return In(key, slice) -} - -func NotInIPList(key string, slice []string) bool { - for _, s := range slice { - if s == "" { - continue - } if key == strings.Split(s, ":")[0] { return false } @@ -54,68 +36,10 @@ func NotInIPList(key string, slice []string) bool { return true } -func ReduceIPList(src, dst []string) []string { - var ipList []string - for _, ip := range src { - if In(ip, dst) { - ipList = append(ipList, ip) - } - } - return ipList -} - -func AppendIPList(src, dst []string) []string { - for _, ip := range dst { - if !In(ip, src) { - src = append(src, ip) - } - } - return src -} - -func IPListRemove(ss []string, s string) (result []string) { - for _, v := range ss { - if v != s { - result = append(result, v) - } - } - return -} -func SortIPList(iplist []string) { - realIPs := make([]net.IP, 0, len(iplist)) - for _, ip := range iplist { - realIPs = append(realIPs, net.ParseIP(ip)) - } - - sort.Slice(realIPs, func(i, j int) bool { - return bytes.Compare(realIPs[i], realIPs[j]) < 0 - }) - - for i := range realIPs { - iplist[i] = realIPs[i].String() - } -} - -func Reverse(s []string) []string { - for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - s[i], s[j] = s[j], s[i] - } - return s -} - -func ContainList(list []string, toComplete string) (containerList []string) { - for i := range list { - if strings.Contains(list[i], toComplete) { - containerList = append(containerList, list[i]) - } - } - return -} +var emptyLineRe = regexp.MustCompile(`^\s*$`) func IsEmptyLine(str string) bool { - re := regexp.MustCompile(`^\s*$`) - - return re.MatchString(str) + return emptyLineRe.MatchString(str) } func TrimWS(str string) string { @@ -126,18 +50,18 @@ func TrimSpaceWS(str string) string { return strings.TrimRight(str, " \n\t") } -func RemoveSliceEmpty(list []string) (fList []string) { +func FilterNonEmptyFromSlice(list []string) (ret []string) { for i := range list { if strings.TrimSpace(list[i]) != "" { - fList = append(fList, list[i]) + ret = append(ret, list[i]) } } return } -func SplitRemoveEmpty(s, sep string) []string { +func FilterNonEmptyFromString(s, sep string) []string { data := strings.Split(s, sep) - return RemoveSliceEmpty(data) + return FilterNonEmptyFromSlice(data) } // RemoveDuplicate removes duplicate entry in the list. @@ -153,18 +77,18 @@ func RemoveDuplicate(list []string) []string { return result } -// RemoveStrSlice remove dst element from src slice -func RemoveStrSlice(src, dst []string) []string { - var ipList []string - for _, ip := range src { - if !In(ip, dst) { - ipList = append(ipList, ip) +// RemoveSubSlice remove dst element from src slice +func RemoveSubSlice(src, dst []string) []string { + var ret []string + for _, s := range src { + if !slices.Contains(dst, s) { + ret = append(ret, s) } } - return ipList + return ret } -func SliceRemoveStr(ss []string, s string) (result []string) { +func RemoveFromSlice(ss []string, s string) (result []string) { for _, v := range ss { if v != s { result = append(result, v) @@ -198,7 +122,7 @@ func IsLetterOrNumber(k string) bool { return true } -func RenderShellFromEnv(shell string, envs map[string]string) string { +func RenderShellWithEnv(shell string, envs map[string]string) string { var env string for k, v := range envs { env = fmt.Sprintf("%s%s=\"%s\" ", env, k, v) @@ -209,7 +133,7 @@ func RenderShellFromEnv(shell string, envs map[string]string) string { return fmt.Sprintf("export %s; %s", env, shell) } -func RenderTextFromEnv(text string, envs map[string]string) string { +func RenderTextWithEnv(text string, envs map[string]string) string { replaces := make(map[string]string, 0) for k, v := range envs { replaces[fmt.Sprintf("$(%s)", k)] = v diff --git a/pkg/utils/sync/pool.go b/pkg/utils/sync/pool.go deleted file mode 100644 index f63cc987d3b..00000000000 --- a/pkg/utils/sync/pool.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright © 2021 sealos. -// -// 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 sync - -import "sync" - -type Pool struct { - queue chan int - wg *sync.WaitGroup -} - -func NewPool(size int) *Pool { - if size <= 1 { - size = 1 - } - return &Pool{ - queue: make(chan int, size), - wg: &sync.WaitGroup{}, - } -} - -func (p *Pool) Add(delta int) { - for i := 0; i < delta; i++ { - p.queue <- 1 - } - for i := 0; i > delta; i-- { - <-p.queue - } - p.wg.Add(delta) -} - -func (p *Pool) Done() { - <-p.queue - p.wg.Done() -} - -func (p *Pool) Wait() { - p.wg.Wait() -} diff --git a/pkg/utils/toml/toml.go b/pkg/utils/toml/toml.go index ad80b43ccf2..b2001e78729 100644 --- a/pkg/utils/toml/toml.go +++ b/pkg/utils/toml/toml.go @@ -22,7 +22,7 @@ import ( fileutil "github.com/labring/sealos/pkg/utils/file" ) -func MarshalTomlToFile(file string, obj interface{}) error { +func MarshalFile(file string, obj interface{}) error { data, err := toml.Marshal(obj) if err != nil { return err @@ -30,7 +30,7 @@ func MarshalTomlToFile(file string, obj interface{}) error { return fileutil.WriteFile(file, data) } -func UnmarshalTomlFromFile(file string, obj interface{}) error { +func UnmarshalFile(file string, obj interface{}) error { metadata, err := fileutil.ReadAll(file) if err != nil { return err diff --git a/pkg/utils/yaml/yaml.go b/pkg/utils/yaml/yaml.go index 429dfaf5443..b1490d8a58a 100644 --- a/pkg/utils/yaml/yaml.go +++ b/pkg/utils/yaml/yaml.go @@ -21,14 +21,12 @@ import ( "bytes" "fmt" "io" - "path/filepath" - "strings" - - fileutil "github.com/labring/sealos/pkg/utils/file" "k8s.io/apimachinery/pkg/runtime" utilyaml "k8s.io/apimachinery/pkg/util/yaml" "sigs.k8s.io/yaml" + + fileutil "github.com/labring/sealos/pkg/utils/file" ) func Unmarshal(path string) (map[string]interface{}, error) { @@ -69,7 +67,7 @@ func ToJSON(bs []byte) (jsons []string) { return } -func ToYalms(bs string) (yamls []string) { +func ToYAMLs(bs string) (yamls []string) { buf := bytes.NewBuffer([]byte(bs)) reader := utilyaml.NewYAMLReader(bufio.NewReader(buf)) for { @@ -89,15 +87,15 @@ func ToYalms(bs string) (yamls []string) { return } -func MarshalYamlToFile(file string, obj ...interface{}) error { - data, err := MarshalYamlConfigs(obj...) +func MarshalFile(file string, obj ...interface{}) error { + data, err := MarshalConfigs(obj...) if err != nil { return err } return fileutil.WriteFile(file, data) } -func UnmarshalYamlFromFile(file string, obj interface{}) error { +func UnmarshalFile(file string, obj interface{}) error { metadata, err := fileutil.ReadAll(file) if err != nil { return err @@ -109,7 +107,7 @@ func UnmarshalYamlFromFile(file string, obj interface{}) error { return nil } -func MarshalYamlConfigs(configs ...interface{}) ([]byte, error) { +func MarshalConfigs(configs ...interface{}) ([]byte, error) { var cfgs [][]byte for _, cfg := range configs { data, err := yaml.Marshal(cfg) @@ -121,11 +119,6 @@ func MarshalYamlConfigs(configs ...interface{}) ([]byte, error) { return bytes.Join(cfgs, []byte("\n---\n")), nil } -func Matcher(path string) bool { - ext := strings.ToLower(filepath.Ext(path)) - return ext == ".yaml" || ext == ".yml" -} - func ShowStructYaml(s interface{}) { data, _ := yaml.Marshal(s) fmt.Println(string(data)) diff --git a/service/auth/conf/config.go b/service/auth/conf/config.go index c45dc3c8438..8c149dff066 100644 --- a/service/auth/conf/config.go +++ b/service/auth/conf/config.go @@ -28,5 +28,5 @@ type Config struct { } func InitConfig(configPath string) error { - return yaml.UnmarshalYamlFromFile(configPath, &GlobalConfig) + return yaml.UnmarshalFile(configPath, &GlobalConfig) }