From 1f609f5396ef245787d756c6691c707bc7b4330e Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Fri, 17 Oct 2025 18:24:16 +0800 Subject: [PATCH 01/11] refactor: update code style and improve error handling across multiple files Signed-off-by: cuisongliu --- lifecycle/cmd/image-cri-shim/cmd/root.go | 28 +- lifecycle/cmd/image-cri-shim/cmd/root_test.go | 5 +- lifecycle/cmd/lvscare/cmd/care.go | 4 +- lifecycle/cmd/lvscare/cmd/version.go | 6 +- lifecycle/cmd/sealctl/cmd/archive.go | 20 +- lifecycle/cmd/sealctl/cmd/cert.go | 22 +- lifecycle/cmd/sealctl/cmd/cri.go | 9 +- lifecycle/cmd/sealctl/cmd/hostname.go | 8 +- lifecycle/cmd/sealctl/cmd/hosts.go | 16 +- lifecycle/cmd/sealctl/cmd/initsystem.go | 13 +- lifecycle/cmd/sealctl/cmd/ipvs.go | 6 +- lifecycle/cmd/sealctl/cmd/render.go | 15 +- lifecycle/cmd/sealctl/cmd/root.go | 10 +- lifecycle/cmd/sealctl/cmd/static_pod.go | 34 +- lifecycle/cmd/sealctl/cmd/token.go | 7 +- lifecycle/cmd/sealctl/cmd/version.go | 8 +- lifecycle/cmd/sealctl/main.go | 1 - lifecycle/cmd/sealos/cmd/add.go | 5 +- lifecycle/cmd/sealos/cmd/apply.go | 8 +- lifecycle/cmd/sealos/cmd/cert.go | 19 +- lifecycle/cmd/sealos/cmd/delete.go | 8 +- lifecycle/cmd/sealos/cmd/docs.go | 4 +- lifecycle/cmd/sealos/cmd/exec.go | 22 +- lifecycle/cmd/sealos/cmd/gen.go | 9 +- lifecycle/cmd/sealos/cmd/registry.go | 7 +- lifecycle/cmd/sealos/cmd/reset.go | 8 +- lifecycle/cmd/sealos/cmd/root.go | 13 +- lifecycle/cmd/sealos/cmd/run.go | 11 +- lifecycle/cmd/sealos/cmd/scp.go | 17 +- lifecycle/cmd/sealos/cmd/status.go | 17 +- lifecycle/cmd/sealos/cmd/version.go | 34 +- lifecycle/cmd/sealos/main.go | 1 - lifecycle/fork/golang/expansion/expand.go | 26 +- lifecycle/go.mod | 3 +- lifecycle/go.work.sum | 9 +- lifecycle/pkg/apply/apply.go | 12 +- lifecycle/pkg/apply/apply_test.go | 9 +- .../applydrivers/apply_drivers_default.go | 96 +++-- lifecycle/pkg/apply/args.go | 66 +++- lifecycle/pkg/apply/args_test.go | 76 +++- lifecycle/pkg/apply/gen.go | 7 +- lifecycle/pkg/apply/gen_test.go | 3 +- lifecycle/pkg/apply/processor/context.go | 10 +- lifecycle/pkg/apply/processor/create.go | 26 +- lifecycle/pkg/apply/processor/delete.go | 7 +- lifecycle/pkg/apply/processor/install.go | 31 +- lifecycle/pkg/apply/processor/interface.go | 13 +- lifecycle/pkg/apply/processor/scale.go | 75 ++-- lifecycle/pkg/apply/reset.go | 10 +- lifecycle/pkg/apply/run.go | 31 +- lifecycle/pkg/apply/run_test.go | 21 +- lifecycle/pkg/apply/scale.go | 92 +++-- lifecycle/pkg/apply/scale_test.go | 278 +++++++++++--- lifecycle/pkg/apply/utils.go | 5 +- lifecycle/pkg/bootstrap/applier.go | 12 +- lifecycle/pkg/bootstrap/bootstrap.go | 15 +- lifecycle/pkg/bootstrap/hosts.go | 9 +- lifecycle/pkg/bootstrap/registry.go | 31 +- lifecycle/pkg/buildah/build.go | 44 ++- lifecycle/pkg/buildah/buildah.go | 118 ++++-- lifecycle/pkg/buildah/common.go | 68 ++-- lifecycle/pkg/buildah/constants.go | 17 +- lifecycle/pkg/buildah/containers.go | 75 +++- lifecycle/pkg/buildah/create.go | 88 +++-- lifecycle/pkg/buildah/diff.go | 72 ++-- lifecycle/pkg/buildah/from.go | 111 +++++- lifecycle/pkg/buildah/images.go | 46 ++- lifecycle/pkg/buildah/imagesaver.go | 29 +- lifecycle/pkg/buildah/inspect.go | 56 ++- lifecycle/pkg/buildah/interface.go | 29 +- lifecycle/pkg/buildah/interface_test.go | 2 +- lifecycle/pkg/buildah/internal/util/util.go | 5 +- lifecycle/pkg/buildah/load.go | 19 +- lifecycle/pkg/buildah/login.go | 41 ++- lifecycle/pkg/buildah/logout.go | 2 +- lifecycle/pkg/buildah/manifest.go | 344 ++++++++++++++---- lifecycle/pkg/buildah/merge.go | 42 ++- lifecycle/pkg/buildah/mount.go | 13 +- lifecycle/pkg/buildah/pull.go | 129 +++++-- lifecycle/pkg/buildah/push.go | 163 +++++++-- lifecycle/pkg/buildah/rm.go | 10 +- lifecycle/pkg/buildah/rmi.go | 13 +- lifecycle/pkg/buildah/runtime.go | 37 +- lifecycle/pkg/buildah/save.go | 47 ++- lifecycle/pkg/buildah/setup.go | 17 +- lifecycle/pkg/buildah/tag.go | 9 +- lifecycle/pkg/buildah/umount.go | 6 +- lifecycle/pkg/buildah/unshare.go | 45 ++- lifecycle/pkg/buildah/util.go | 1 - lifecycle/pkg/cert/cert.go | 64 ++-- lifecycle/pkg/cert/cert_cmd.go | 24 +- lifecycle/pkg/cert/kube_certs.go | 61 ++-- lifecycle/pkg/cert/kube_certs_test.go | 10 +- lifecycle/pkg/cert/kubeconfig.go | 105 ++++-- lifecycle/pkg/checker/check_list.go | 2 +- lifecycle/pkg/checker/cluster_checker.go | 16 +- lifecycle/pkg/checker/cri_shim_checker.go | 7 +- lifecycle/pkg/checker/crictl_checker.go | 41 ++- lifecycle/pkg/checker/host_checker.go | 14 +- lifecycle/pkg/checker/initsystem_check.go | 24 +- lifecycle/pkg/checker/node_checker.go | 24 +- lifecycle/pkg/checker/pod_checker.go | 15 +- lifecycle/pkg/checker/registry_checker.go | 18 +- lifecycle/pkg/checker/svc_checker.go | 28 +- lifecycle/pkg/client-go/kubernetes/client.go | 7 +- .../pkg/client-go/kubernetes/expansion.go | 78 ++-- lifecycle/pkg/client-go/kubernetes/healthy.go | 74 +++- .../pkg/client-go/kubernetes/idempotency.go | 47 ++- lifecycle/pkg/clusterfile/clusterfile_test.go | 35 +- lifecycle/pkg/clusterfile/decode.go | 42 ++- lifecycle/pkg/clusterfile/pre_process.go | 21 +- lifecycle/pkg/clusterfile/util.go | 24 +- lifecycle/pkg/config/config.go | 19 +- lifecycle/pkg/config/config_test.go | 17 +- lifecycle/pkg/constants/bash.go | 19 +- lifecycle/pkg/constants/pathresolver.go | 3 +- lifecycle/pkg/env/env.go | 5 +- lifecycle/pkg/env/env_shim_test.go | 23 +- lifecycle/pkg/exec/exec.go | 15 +- lifecycle/pkg/filesystem/filesystem.go | 2 +- lifecycle/pkg/filesystem/registry/sync.go | 34 +- .../pkg/filesystem/rootfs/rootfs_default.go | 49 ++- lifecycle/pkg/guest/guest.go | 30 +- lifecycle/pkg/guest/guest_test.go | 94 +++-- lifecycle/pkg/guest/util.go | 12 +- lifecycle/pkg/image/merge.go | 9 +- lifecycle/pkg/image/merge_test.go | 6 +- lifecycle/pkg/ipvs/lvscare.go | 16 +- lifecycle/pkg/registry/commands/password.go | 7 +- lifecycle/pkg/registry/helpers/helpers.go | 23 +- lifecycle/pkg/registry/password/apply.go | 56 ++- lifecycle/pkg/registry/password/password.go | 56 ++- lifecycle/pkg/runtime/decode/decode.go | 86 +++-- lifecycle/pkg/runtime/interface.go | 4 +- lifecycle/pkg/runtime/k3s/bootstrap.go | 88 +++-- lifecycle/pkg/runtime/k3s/config.go | 36 +- lifecycle/pkg/runtime/k3s/k3s.go | 44 ++- lifecycle/pkg/runtime/k3s/lifecycle.go | 34 +- lifecycle/pkg/runtime/kubernetes/certs.go | 52 ++- lifecycle/pkg/runtime/kubernetes/commands.go | 12 +- lifecycle/pkg/runtime/kubernetes/init.go | 12 +- lifecycle/pkg/runtime/kubernetes/kubeadm.go | 170 +++++---- .../pkg/runtime/kubernetes/kubeconfig.go | 1 - lifecycle/pkg/runtime/kubernetes/master.go | 69 ++-- lifecycle/pkg/runtime/kubernetes/node.go | 28 +- lifecycle/pkg/runtime/kubernetes/reset.go | 12 +- lifecycle/pkg/runtime/kubernetes/runtime.go | 26 +- .../pkg/runtime/kubernetes/runtime_getter.go | 46 ++- .../pkg/runtime/kubernetes/static_files.go | 11 +- .../types/default_kubeadm_config.go | 11 +- .../kubernetes/types/kubeadm_config.go | 84 +++-- .../kubernetes/types/kubeadm_config_test.go | 8 +- .../pkg/runtime/kubernetes/types/types.go | 1 - lifecycle/pkg/runtime/kubernetes/upgrade.go | 62 ++-- lifecycle/pkg/runtime/kubernetes/utils.go | 21 +- lifecycle/pkg/runtime/utils/token.go | 43 ++- lifecycle/pkg/ssh/clusterclient.go | 17 +- lifecycle/pkg/ssh/connect.go | 23 +- lifecycle/pkg/ssh/option.go | 27 +- lifecycle/pkg/ssh/remote.go | 70 ++-- lifecycle/pkg/ssh/scp.go | 65 ++-- lifecycle/pkg/ssh/ssh.go | 14 +- lifecycle/pkg/ssh/sshcmd.go | 28 +- lifecycle/pkg/ssh/suftp.go | 17 +- lifecycle/pkg/ssh/utils.go | 10 +- lifecycle/pkg/system/env.go | 9 +- lifecycle/pkg/system/env_cmd.go | 4 +- lifecycle/pkg/template/funcmap.go | 26 +- lifecycle/pkg/template/template.go | 2 +- lifecycle/pkg/template/template_test.go | 2 +- lifecycle/pkg/types/v1beta1/cluster.go | 11 +- lifecycle/pkg/types/v1beta1/config.go | 4 +- lifecycle/pkg/types/v1beta1/utils.go | 21 +- lifecycle/pkg/utils/archive/archive.go | 12 +- lifecycle/pkg/utils/archive/compress.go | 56 ++- lifecycle/pkg/utils/archive/compress_test.go | 2 +- lifecycle/pkg/utils/archive/hash.go | 3 +- lifecycle/pkg/utils/confirm/confirm.go | 7 +- lifecycle/pkg/utils/exec/bin_path.go | 3 +- lifecycle/pkg/utils/exec/cmd.go | 7 +- lifecycle/pkg/utils/file/file.go | 3 +- lifecycle/pkg/utils/file/file_v2.go | 27 +- lifecycle/pkg/utils/file/file_v3.go | 14 +- lifecycle/pkg/utils/file/fswriters.go | 4 +- lifecycle/pkg/utils/file/system.go | 4 +- lifecycle/pkg/utils/flags/flags.go | 5 +- lifecycle/pkg/utils/hash/hash.go | 10 +- lifecycle/pkg/utils/hosts/hosts.go | 18 +- lifecycle/pkg/utils/httpserver/cors.go | 3 +- lifecycle/pkg/utils/httpserver/go_restful.go | 10 +- lifecycle/pkg/utils/httpserver/response.go | 6 +- lifecycle/pkg/utils/initsystem/initsystem.go | 6 +- lifecycle/pkg/utils/iputils/iputils_v2.go | 51 +-- lifecycle/pkg/utils/logger/logger.go | 24 +- lifecycle/pkg/utils/logger/logger_test.go | 5 +- lifecycle/pkg/utils/maps/maps.go | 8 +- lifecycle/pkg/utils/net/net.go | 35 +- .../pkg/utils/progress/progress_download.go | 9 +- lifecycle/pkg/utils/rand/rand.go | 10 +- lifecycle/pkg/utils/retry/retry.go | 4 +- lifecycle/pkg/utils/strings/strings.go | 38 +- lifecycle/pkg/utils/toml/toml.go | 7 +- lifecycle/pkg/utils/yaml/yaml.go | 32 +- lifecycle/pkg/version/types.go | 40 +- lifecycle/pkg/version/utils/utils.go | 3 +- lifecycle/scripts/make-rules/golang.mk | 10 +- lifecycle/scripts/make-rules/tools.mk | 11 +- .../labring/image-cri-shim/pkg/cri/cri.go | 46 ++- .../image-cri-shim/pkg/server/auth_store.go | 8 +- .../pkg/server/auth_store_test.go | 2 +- .../image-cri-shim/pkg/server/const.go | 2 +- .../image-cri-shim/pkg/server/cri_client.go | 6 +- .../pkg/server/cri_server_v1.go | 24 +- .../image-cri-shim/pkg/server/server.go | 16 +- .../image-cri-shim/pkg/server/utils.go | 13 +- .../labring/image-cri-shim/pkg/shim/shim.go | 13 +- .../image-cri-shim/pkg/types/config.go | 17 +- .../pkg/types/configmap_sync.go | 39 +- .../pkg/types/configmap_sync_test.go | 6 +- .../labring/lvscare/care/iptables.go | 57 ++- .../labring/lvscare/care/options.go | 13 +- .../github.com/labring/lvscare/care/prober.go | 21 +- .../labring/lvscare/care/proxier.go | 19 +- .../github.com/labring/lvscare/care/route.go | 1 - .../github.com/labring/lvscare/care/runner.go | 22 +- .../labring/lvscare/pkg/route/route.go | 1 - .../labring/lvscare/pkg/version/version.go | 23 +- lifecycle/test/e2e/applier.go | 70 ++-- lifecycle/test/e2e/apply_docker_test.go | 33 +- lifecycle/test/e2e/apply_other_test.go | 15 +- lifecycle/test/e2e/apply_test.go | 33 +- lifecycle/test/e2e/cert_test.go | 56 ++- lifecycle/test/e2e/e2e_test.go | 7 +- lifecycle/test/e2e/filesystem_test.go | 18 +- lifecycle/test/e2e/image_cri_shim_test.go | 136 +++++-- .../test/e2e/images_buildrun_feature_test.go | 46 ++- lifecycle/test/e2e/images_buildrun_test.go | 44 ++- lifecycle/test/e2e/images_test.go | 80 +++- lifecycle/test/e2e/inspect_test.go | 36 +- lifecycle/test/e2e/k3s_125_test.go | 35 +- lifecycle/test/e2e/k8s_122_test.go | 2 - lifecycle/test/e2e/k8s_123_test.go | 2 - lifecycle/test/e2e/k8s_124_test.go | 2 - lifecycle/test/e2e/k8s_125_test.go | 2 - lifecycle/test/e2e/k8s_126_test.go | 2 - lifecycle/test/e2e/k8s_127_test.go | 2 - lifecycle/test/e2e/k8s_128_test.go | 2 - lifecycle/test/e2e/k8s_129_test.go | 2 - lifecycle/test/e2e/k8s_130_test.go | 2 - lifecycle/test/e2e/k8s_131_test.go | 2 - lifecycle/test/e2e/k8s_132_test.go | 2 - lifecycle/test/e2e/k8s_docker_122_test.go | 2 - lifecycle/test/e2e/k8s_docker_123_test.go | 2 - lifecycle/test/e2e/k8s_docker_124_test.go | 2 - lifecycle/test/e2e/k8s_docker_125_test.go | 2 - lifecycle/test/e2e/k8s_docker_126_test.go | 2 - lifecycle/test/e2e/k8s_docker_127_test.go | 2 - lifecycle/test/e2e/k8s_docker_128_test.go | 2 - lifecycle/test/e2e/k8s_docker_129_test.go | 2 - lifecycle/test/e2e/k8s_docker_130_test.go | 2 - lifecycle/test/e2e/k8s_docker_131_test.go | 2 - lifecycle/test/e2e/k8s_docker_132_test.go | 2 - lifecycle/test/e2e/multi_node_test.go | 49 +-- lifecycle/test/e2e/run_docker_test.go | 48 ++- lifecycle/test/e2e/run_other_test.go | 28 +- lifecycle/test/e2e/run_patch_test.go | 45 ++- lifecycle/test/e2e/run_test.go | 39 +- .../e2e/suites/checkers/cluster_cert_sans.go | 11 +- .../e2e/suites/checkers/cluster_cgroup.go | 12 +- .../test/e2e/suites/checkers/cluster_etcd.go | 18 +- .../e2e/suites/checkers/cluster_pod_cidr.go | 8 +- .../suites/checkers/cluster_service_cidr.go | 8 +- .../suites/checkers/cluster_single_taints.go | 3 +- .../e2e/suites/checkers/cluster_socket.go | 14 +- .../e2e/suites/checkers/cluster_taints.go | 8 +- lifecycle/test/e2e/suites/checkers/fake.go | 48 ++- .../e2e/suites/image/imageServiceClient.go | 40 +- lifecycle/test/e2e/suites/operators/cert.go | 4 +- lifecycle/test/e2e/suites/operators/cri.go | 5 +- lifecycle/test/e2e/suites/operators/image.go | 30 +- .../test/e2e/suites/operators/inspect.go | 12 +- lifecycle/test/e2e/suites/operators/types.go | 24 +- lifecycle/test/e2e/terraform/main.go | 1 - lifecycle/test/e2e/terraform/terraform.go | 90 +++-- .../test/e2e/terraform/terraform_test.go | 6 +- lifecycle/test/e2e/testhelper/cmd/cmd.go | 23 +- .../test/e2e/testhelper/cmd/imageOpts.go | 54 +-- .../test/e2e/testhelper/cmd/sealosCmd.go | 27 +- .../test/e2e/testhelper/cmd/sealosCmdOpts.go | 2 +- .../e2e/testhelper/cmd/sealosCmdOpts_test.go | 10 +- .../test/e2e/testhelper/config/clusterfile.go | 10 +- .../e2e/testhelper/config/clusterfile_test.go | 1 - .../test/e2e/testhelper/config/patchfs.go | 10 +- .../test/e2e/testhelper/config/rootfs.go | 11 +- .../test/e2e/testhelper/config/rootfs_test.go | 5 +- .../test/e2e/testhelper/consts/consts.go | 4 +- lifecycle/test/e2e/testhelper/etcd/etcd.go | 18 +- lifecycle/test/e2e/testhelper/kube/k8s.go | 30 +- .../test/e2e/testhelper/template/funcmap.go | 26 +- .../test/e2e/testhelper/template/template.go | 2 +- .../e2e/testhelper/template/template_test.go | 2 +- lifecycle/test/e2e/testhelper/utils/utils.go | 56 ++- lifecycle/test/e2e/utils.go | 3 +- lifecycle/test/testpg/pgstresstest.go | 62 +++- 304 files changed, 5327 insertions(+), 2722 deletions(-) diff --git a/lifecycle/cmd/image-cri-shim/cmd/root.go b/lifecycle/cmd/image-cri-shim/cmd/root.go index 028dba8dbd2f..5cc15fe33cf6 100644 --- a/lifecycle/cmd/image-cri-shim/cmd/root.go +++ b/lifecycle/cmd/image-cri-shim/cmd/root.go @@ -28,15 +28,16 @@ import ( "github.com/labring/image-cri-shim/pkg/shim" "github.com/labring/image-cri-shim/pkg/types" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/version" + "github.com/spf13/cobra" ) -var cfg *types.Config -var shimAuth *types.ShimAuthConfig -var cfgFile string +var ( + cfg *types.Config + shimAuth *types.ShimAuthConfig + cfgFile string +) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -73,11 +74,17 @@ func Execute() { } func init() { - rootCmd.Flags().StringVarP(&cfgFile, "file", "f", types.DefaultImageCRIShimConfig, "image shim root config") + rootCmd.Flags(). + StringVarP(&cfgFile, "file", "f", types.DefaultImageCRIShimConfig, "image shim root config") } func run(cfg *types.Config, auth *types.ShimAuthConfig) { - logger.Info("socket info shim: %v ,image: %v, registry: %v", cfg.ImageShimSocket, cfg.RuntimeSocket, cfg.Address) + logger.Info( + "socket info shim: %v ,image: %v, registry: %v", + cfg.ImageShimSocket, + cfg.RuntimeSocket, + cfg.Address, + ) imgShim, err := shim.NewShim(cfg, auth) if err != nil { logger.Fatal("failed to new image_shim, %s", err) @@ -115,7 +122,12 @@ func run(cfg *types.Config, auth *types.ShimAuthConfig) { logger.Info("shutting down the image_shim") } -func watchAuthConfig(ctx context.Context, path string, imgShim shim.Shim, interval time.Duration) error { +func watchAuthConfig( + ctx context.Context, + path string, + imgShim shim.Shim, + interval time.Duration, +) error { if path == "" { logger.Warn("config file path is empty, skip dynamic auth reload") return nil diff --git a/lifecycle/cmd/image-cri-shim/cmd/root_test.go b/lifecycle/cmd/image-cri-shim/cmd/root_test.go index 44440d5edc1c..ae085f7d13e0 100644 --- a/lifecycle/cmd/image-cri-shim/cmd/root_test.go +++ b/lifecycle/cmd/image-cri-shim/cmd/root_test.go @@ -146,7 +146,10 @@ registries: mirror, ok = auth.CRIConfigs["mirror.example.com"] if !ok { - t.Fatalf("expected registry credentials for mirror.example.com after update, got %#v", auth.CRIConfigs) + t.Fatalf( + "expected registry credentials for mirror.example.com after update, got %#v", + auth.CRIConfigs, + ) } if mirror.Password != "changed" { t.Fatalf("expected updated mirror password, got %q", mirror.Password) diff --git a/lifecycle/cmd/lvscare/cmd/care.go b/lifecycle/cmd/lvscare/cmd/care.go index c372360e262f..03576bd076ad 100644 --- a/lifecycle/cmd/lvscare/cmd/care.go +++ b/lifecycle/cmd/lvscare/cmd/care.go @@ -15,12 +15,10 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/labring/lvscare/care" - "github.com/labring/sealos/pkg/utils/flags" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) // careCmd represents the care command diff --git a/lifecycle/cmd/lvscare/cmd/version.go b/lifecycle/cmd/lvscare/cmd/version.go index e95b2e80164c..880d35671f47 100644 --- a/lifecycle/cmd/lvscare/cmd/version.go +++ b/lifecycle/cmd/lvscare/cmd/version.go @@ -19,14 +19,13 @@ import ( "fmt" "github.com/labring/sealos/pkg/version" - "github.com/spf13/cobra" ) var shortPrint bool func newVersionCmd() *cobra.Command { - var versionCmd = &cobra.Command{ + versionCmd := &cobra.Command{ Use: "version", Short: "version", Args: cobra.NoArgs, @@ -44,7 +43,8 @@ func newVersionCmd() *cobra.Command { return nil }, } - versionCmd.Flags().BoolVar(&shortPrint, "short", false, "if true, print just the version number.") + versionCmd.Flags(). + BoolVar(&shortPrint, "short", false, "if true, print just the version number.") return versionCmd } diff --git a/lifecycle/cmd/sealctl/cmd/archive.go b/lifecycle/cmd/sealctl/cmd/archive.go index 7f814ef9889e..5346b66f0b84 100644 --- a/lifecycle/cmd/sealctl/cmd/archive.go +++ b/lifecycle/cmd/sealctl/cmd/archive.go @@ -15,17 +15,16 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/utils/archive" "github.com/labring/sealos/pkg/utils/flags" + "github.com/spf13/cobra" ) func newTarCmd() *cobra.Command { var ( compressionF flags.Compression output string - clear bool + clearSource bool ) cmd := &cobra.Command{ Use: "tar", @@ -33,12 +32,13 @@ func newTarCmd() *cobra.Command { Args: cobra.ExactArgs(1), SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { - return archive.Tar(args[0], output, compressionF, clear) + return archive.Tar(args[0], output, compressionF, clearSource) }, } - cmd.Flags().Var(&compressionF, "compression", "compression algorithm, available options are tar/gzip/zstd/disable") + cmd.Flags(). + Var(&compressionF, "compression", "compression algorithm, available options are tar/gzip/zstd/disable") cmd.Flags().StringVarP(&output, "output", "o", "", "path of archive file") - cmd.Flags().BoolVar(&clear, "clear", false, "remove source after compression finished") + cmd.Flags().BoolVar(&clearSource, "clear", false, "remove source after compression finished") _ = cmd.MarkFlagRequired("output") _ = cmd.MarkFlagRequired("compression") return cmd @@ -46,8 +46,8 @@ func newTarCmd() *cobra.Command { func newUntarCmd() *cobra.Command { var ( - clear bool - output string + clearSource bool + output string ) cmd := &cobra.Command{ Use: "untar", @@ -55,11 +55,11 @@ func newUntarCmd() *cobra.Command { Args: cobra.MinimumNArgs(1), SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { - return archive.Untar(args, output, clear) + return archive.Untar(args, output, clearSource) }, } cmd.Flags().StringVarP(&output, "output", "o", "", "path to uncompress archive files") - cmd.Flags().BoolVar(&clear, "clear", false, "remove source after uncompression finished") + cmd.Flags().BoolVar(&clearSource, "clear", false, "remove source after uncompression finished") _ = cmd.MarkFlagRequired("output") return cmd } diff --git a/lifecycle/cmd/sealctl/cmd/cert.go b/lifecycle/cmd/sealctl/cmd/cert.go index 07619777a6a8..637ad5185faa 100755 --- a/lifecycle/cmd/sealctl/cmd/cert.go +++ b/lifecycle/cmd/sealctl/cmd/cert.go @@ -17,11 +17,10 @@ package cmd import ( "os" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/cert" "github.com/labring/sealos/pkg/utils/flags" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func newCertCmd() *cobra.Command { @@ -42,20 +41,31 @@ func newCertCmd() *cobra.Command { Long: `you can specify expire time`, Run: func(cmd *cobra.Command, args []string) { flags.PrintFlags(cmd.Flags()) - err := cert.GenerateCert(flag.CertPath, flag.CertEtcdPath, flag.AltNames, flag.NodeIP, flag.NodeName, flag.ServiceCIDR, flag.DNSDomain) + err := cert.GenerateCert( + flag.CertPath, + flag.CertEtcdPath, + flag.AltNames, + flag.NodeIP, + flag.NodeName, + flag.ServiceCIDR, + flag.DNSDomain, + ) if err != nil { logger.Error(err) os.Exit(1) } }, } - certCmd.Flags().StringSliceVar(&flag.AltNames, "alt-names", []string{}, "like sealos.io or 10.103.97.2") + certCmd.Flags(). + StringSliceVar(&flag.AltNames, "alt-names", []string{}, "like sealos.io or 10.103.97.2") certCmd.Flags().StringVar(&flag.NodeName, "node-name", "", "like master0") certCmd.Flags().StringVar(&flag.ServiceCIDR, "service-cidr", "", "like 10.103.97.2/24") certCmd.Flags().StringVar(&flag.NodeIP, "node-ip", "", "like 10.103.97.2") certCmd.Flags().StringVar(&flag.DNSDomain, "dns-domain", "cluster.local", "cluster dns domain") - certCmd.Flags().StringVar(&flag.CertPath, "cert-path", "/etc/kubernetes/pki", "kubernetes cert file path") - certCmd.Flags().StringVar(&flag.CertEtcdPath, "cert-etcd-path", "/etc/kubernetes/pki/etcd", "kubernetes etcd cert file path") + certCmd.Flags(). + StringVar(&flag.CertPath, "cert-path", "/etc/kubernetes/pki", "kubernetes cert file path") + certCmd.Flags(). + StringVar(&flag.CertEtcdPath, "cert-etcd-path", "/etc/kubernetes/pki/etcd", "kubernetes etcd cert file path") return certCmd } diff --git a/lifecycle/cmd/sealctl/cmd/cri.go b/lifecycle/cmd/sealctl/cmd/cri.go index 7225408f9353..16f04eaa41fa 100644 --- a/lifecycle/cmd/sealctl/cmd/cri.go +++ b/lifecycle/cmd/sealctl/cmd/cri.go @@ -21,10 +21,9 @@ import ( "os" "github.com/labring/image-cri-shim/pkg/cri" + "github.com/labring/sealos/pkg/utils/logger" "github.com/spf13/cobra" utilsexec "k8s.io/utils/exec" - - "github.com/labring/sealos/pkg/utils/logger" ) var ( @@ -33,7 +32,7 @@ var ( ) func newCRICmd() *cobra.Command { - var criCmd = &cobra.Command{ + criCmd := &cobra.Command{ Use: "cri", Short: "cri manager", } @@ -46,7 +45,7 @@ func newCRICmd() *cobra.Command { } func newCRISocketCmd() *cobra.Command { - var criSocketCmd = &cobra.Command{ + criSocketCmd := &cobra.Command{ Use: "socket", Short: "cri manager socket", Run: func(cmd *cobra.Command, args []string) { @@ -63,7 +62,7 @@ func newCRISocketCmd() *cobra.Command { func newCGroupDriverCmd() *cobra.Command { var shortPrint bool - var cGroupDriverCmd = &cobra.Command{ + cGroupDriverCmd := &cobra.Command{ Use: "cgroup-driver", Short: "cri manager cgroup-driver", PreRun: func(cmd *cobra.Command, args []string) { diff --git a/lifecycle/cmd/sealctl/cmd/hostname.go b/lifecycle/cmd/sealctl/cmd/hostname.go index 513dec96300d..9690fb9b1633 100755 --- a/lifecycle/cmd/sealctl/cmd/hostname.go +++ b/lifecycle/cmd/sealctl/cmd/hostname.go @@ -15,15 +15,15 @@ package cmd import ( + "fmt" "os" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func newHostsNameCmd() *cobra.Command { - var hostsNameCmd = &cobra.Command{ + hostsNameCmd := &cobra.Command{ Use: "hostname", Short: "get os.hostname", Run: func(cmd *cobra.Command, args []string) { @@ -32,7 +32,7 @@ func newHostsNameCmd() *cobra.Command { logger.Error(err) os.Exit(1) } - print(hostname) + fmt.Print(hostname) }, } return hostsNameCmd diff --git a/lifecycle/cmd/sealctl/cmd/hosts.go b/lifecycle/cmd/sealctl/cmd/hosts.go index c740d965c51c..7b114fdda3c3 100644 --- a/lifecycle/cmd/sealctl/cmd/hosts.go +++ b/lifecycle/cmd/sealctl/cmd/hosts.go @@ -19,33 +19,33 @@ package cmd import ( "os" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/utils/hosts" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) var hostsPath string func newHostsCmd() *cobra.Command { - var hostsCmd = &cobra.Command{ + hostsCmd := &cobra.Command{ Use: "hosts", Short: "hosts manager", //Run: func(cmd *cobra.Command, args []string) { // - //}, + // }, } // check route for host hostsCmd.AddCommand(newHostsListCmd()) hostsCmd.AddCommand(newHostsAddCmd()) hostsCmd.AddCommand(newHostsDeleteCmd()) - hostsCmd.PersistentFlags().StringVar(&hostsPath, "path", constants.DefaultHostsPath, "default hosts path") + hostsCmd.PersistentFlags(). + StringVar(&hostsPath, "path", constants.DefaultHostsPath, "default hosts path") return hostsCmd } func newHostsListCmd() *cobra.Command { - var hostsListCmd = &cobra.Command{ + hostsListCmd := &cobra.Command{ Use: "list", Short: "hosts manager list", Run: func(cmd *cobra.Command, args []string) { @@ -58,7 +58,7 @@ func newHostsListCmd() *cobra.Command { func newHostsAddCmd() *cobra.Command { var ip, domain string - var hostsAddCmd = &cobra.Command{ + hostsAddCmd := &cobra.Command{ Use: "add", Short: "hosts manager add", PreRun: func(cmd *cobra.Command, args []string) { @@ -89,7 +89,7 @@ func newHostsAddCmd() *cobra.Command { func newHostsDeleteCmd() *cobra.Command { var domain string - var hostsDeleteCmd = &cobra.Command{ + hostsDeleteCmd := &cobra.Command{ Use: "delete", Short: "hosts manager delete", PreRun: func(cmd *cobra.Command, args []string) { diff --git a/lifecycle/cmd/sealctl/cmd/initsystem.go b/lifecycle/cmd/sealctl/cmd/initsystem.go index 5f8386503f3c..3bae24f7e6f4 100644 --- a/lifecycle/cmd/sealctl/cmd/initsystem.go +++ b/lifecycle/cmd/sealctl/cmd/initsystem.go @@ -20,17 +20,14 @@ import ( "fmt" "strings" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/utils/initsystem" + "github.com/spf13/cobra" ) -var ( - initsystemInterface initsystem.InitSystem -) +var initsystemInterface initsystem.InitSystem func newInitSystemCmd() *cobra.Command { - var initsystemCmd = &cobra.Command{ + initsystemCmd := &cobra.Command{ Use: "initsystem", Short: "init system management", } @@ -65,9 +62,9 @@ func newInitSystemCmd() *cobra.Command { func createInitSystemSubCommand(verb string, runE func(string) error) *cobra.Command { var short string if strings.HasPrefix(verb, "is-") { - short = fmt.Sprintf("check if the initsystem service is %s", strings.TrimPrefix(verb, "is-")) + short = "check if the initsystem service is " + strings.TrimPrefix(verb, "is-") } else { - short = fmt.Sprintf("%s the initsystem service", verb) + short = verb + " the initsystem service" } return &cobra.Command{ Use: verb, diff --git a/lifecycle/cmd/sealctl/cmd/ipvs.go b/lifecycle/cmd/sealctl/cmd/ipvs.go index dabc87913595..ece19b2391f0 100644 --- a/lifecycle/cmd/sealctl/cmd/ipvs.go +++ b/lifecycle/cmd/sealctl/cmd/ipvs.go @@ -15,15 +15,13 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/labring/lvscare/care" - "github.com/labring/sealos/pkg/utils/flags" + "github.com/spf13/cobra" ) func newIPVSCmd() *cobra.Command { - var ipvsCmd = &cobra.Command{ + ipvsCmd := &cobra.Command{ Use: "ipvs", Short: "sealos create or care local ipvs lb", SilenceUsage: true, diff --git a/lifecycle/cmd/sealctl/cmd/render.go b/lifecycle/cmd/sealctl/cmd/render.go index 2581fa1b4726..93adca9e1fbc 100644 --- a/lifecycle/cmd/sealctl/cmd/render.go +++ b/lifecycle/cmd/sealctl/cmd/render.go @@ -18,15 +18,14 @@ import ( "path/filepath" "strings" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "helm.sh/helm/v3/pkg/cli/values" - "helm.sh/helm/v3/pkg/getter" - "github.com/labring/sealos/pkg/template" fileutils "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/maps" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "helm.sh/helm/v3/pkg/cli/values" + "helm.sh/helm/v3/pkg/getter" ) type renderOptions struct { @@ -55,7 +54,7 @@ func newRenderCommand() *cobra.Command { return cmd } -func loadValues(valueFiles, sets []string) (map[string]interface{}, error) { +func loadValues(valueFiles, sets []string) (map[string]any, error) { valueOpt := &values.Options{ ValueFiles: valueFiles, Values: sets, @@ -84,7 +83,7 @@ func runRender(opts *renderOptions, args []string) error { return err } envs := maps.FromSlice(os.Environ()) - data := make(map[string]interface{}) + data := make(map[string]any) // For compatibility with older templates for k, v := range envs { data[k] = v @@ -104,7 +103,7 @@ func runRender(opts *renderOptions, args []string) error { if fileutils.IsExist(trimed) { logger.Debug("found existing file %s, override it", trimed) } - file, err := os.OpenFile(trimed, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + file, err := os.OpenFile(trimed, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666) if err != nil { return err } diff --git a/lifecycle/cmd/sealctl/cmd/root.go b/lifecycle/cmd/sealctl/cmd/root.go index 3200538be2be..c252097c2fd0 100644 --- a/lifecycle/cmd/sealctl/cmd/root.go +++ b/lifecycle/cmd/sealctl/cmd/root.go @@ -18,18 +18,14 @@ import ( "fmt" "os" + "github.com/labring/sealos/pkg/buildah" + "github.com/labring/sealos/pkg/utils/logger" sreglog "github.com/labring/sreg/pkg/utils/logger" - "github.com/spf13/cobra" "k8s.io/kubectl/pkg/util/templates" - - "github.com/labring/sealos/pkg/buildah" - "github.com/labring/sealos/pkg/utils/logger" ) -var ( - debug bool -) +var debug bool // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ diff --git a/lifecycle/cmd/sealctl/cmd/static_pod.go b/lifecycle/cmd/sealctl/cmd/static_pod.go index 86273ac3f882..53327bab5fc1 100755 --- a/lifecycle/cmd/sealctl/cmd/static_pod.go +++ b/lifecycle/cmd/sealctl/cmd/static_pod.go @@ -15,34 +15,32 @@ package cmd import ( + "errors" "fmt" "os" "path" - "github.com/labring/sealos/pkg/types/v1beta1" - - v1 "k8s.io/api/core/v1" - - "github.com/labring/sealos/pkg/utils/yaml" - - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/ipvs" + "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/pkg/utils/yaml" + "github.com/spf13/cobra" + v1 "k8s.io/api/core/v1" ) var staticPodPath string func newStaticPodCmd() *cobra.Command { - var staticPodCmd = &cobra.Command{ + staticPodCmd := &cobra.Command{ Use: "static-pod", Short: "generator static pod", } // check route for host staticPodCmd.AddCommand(newLvscareCmd()) - staticPodCmd.PersistentFlags().StringVar(&staticPodPath, "path", "/etc/kubernetes/manifests", "default kubernetes static pod path") + staticPodCmd.PersistentFlags(). + StringVar(&staticPodPath, "path", "/etc/kubernetes/manifests", "default kubernetes static pod path") return staticPodCmd } @@ -58,12 +56,12 @@ type lvscarePod struct { func newLvscareCmd() *cobra.Command { var obj lvscarePod var setImage bool - var lvscareCmd = &cobra.Command{ + lvscareCmd := &cobra.Command{ Use: "lvscare", Short: "generator lvscare static pod file", PreRunE: func(cmd *cobra.Command, args []string) error { if len(obj.master) == 0 && !setImage { - return fmt.Errorf("master not allow empty") + return errors.New("master not allow empty") } return nil }, @@ -76,8 +74,10 @@ func newLvscareCmd() *cobra.Command { } // manually to set host via gateway lvscareCmd.Flags().StringVar(&obj.vip, "vip", "10.103.97.2:6443", "default vip IP") - lvscareCmd.Flags().StringVar(&obj.name, "name", constants.LvsCareStaticPodName, "generator lvscare static pod name") - lvscareCmd.Flags().StringVar(&obj.image, "image", v1beta1.DefaultLvsCareImage, "generator lvscare static pod image") + lvscareCmd.Flags(). + StringVar(&obj.name, "name", constants.LvsCareStaticPodName, "generator lvscare static pod name") + lvscareCmd.Flags(). + StringVar(&obj.image, "image", v1beta1.DefaultLvsCareImage, "generator lvscare static pod image") lvscareCmd.Flags().BoolVar(&setImage, "set-img", false, "update lvscare image to static pod") lvscareCmd.Flags().StringSliceVar(&obj.master, "masters", []string{}, "generator masters addrs") lvscareCmd.Flags().StringSliceVar(&obj.options, "options", []string{}, "lvscare args options") @@ -97,9 +97,9 @@ func genNewPod(obj lvscarePod) error { } logger.Debug("lvscare static pod yaml is %s", yaml) if err = file.MkDirs(staticPodPath); err != nil { - return fmt.Errorf("init dir is error: %v", err) + return fmt.Errorf("init dir is error: %w", err) } - err = os.WriteFile(path.Join(staticPodPath, fileName), []byte(yaml), 0755) + err = os.WriteFile(path.Join(staticPodPath, fileName), []byte(yaml), 0o755) if err != nil { return err } @@ -124,7 +124,7 @@ func setNewPodImage(obj lvscarePod) error { fmt.Println(string(data)) return nil } - err = os.WriteFile(path.Join(staticPodPath, fileName), data, 0755) + err = os.WriteFile(path.Join(staticPodPath, fileName), data, 0o755) if err != nil { return err } diff --git a/lifecycle/cmd/sealctl/cmd/token.go b/lifecycle/cmd/sealctl/cmd/token.go index c6fe13acc16a..91954f7808a6 100755 --- a/lifecycle/cmd/sealctl/cmd/token.go +++ b/lifecycle/cmd/sealctl/cmd/token.go @@ -18,15 +18,14 @@ import ( "fmt" "os" - "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/util/json" - runtimeutils "github.com/labring/sealos/pkg/runtime/utils" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/util/json" ) func newTokenCmd() *cobra.Command { - var tokenCmd = &cobra.Command{ + tokenCmd := &cobra.Command{ Use: "token", Short: "token generator", Run: func(cmd *cobra.Command, args []string) { diff --git a/lifecycle/cmd/sealctl/cmd/version.go b/lifecycle/cmd/sealctl/cmd/version.go index 0f8236360b60..838174ade421 100644 --- a/lifecycle/cmd/sealctl/cmd/version.go +++ b/lifecycle/cmd/sealctl/cmd/version.go @@ -18,15 +18,14 @@ import ( "encoding/json" "fmt" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/version" + "github.com/spf13/cobra" ) var shortPrint bool func newVersionCmd() *cobra.Command { - var versionCmd = &cobra.Command{ + versionCmd := &cobra.Command{ Use: "version", Short: "version", Args: cobra.NoArgs, @@ -44,7 +43,8 @@ func newVersionCmd() *cobra.Command { return nil }, } - versionCmd.Flags().BoolVar(&shortPrint, "short", false, "if true, print just the version number.") + versionCmd.Flags(). + BoolVar(&shortPrint, "short", false, "if true, print just the version number.") return versionCmd } diff --git a/lifecycle/cmd/sealctl/main.go b/lifecycle/cmd/sealctl/main.go index 32292f461339..846680067d6e 100644 --- a/lifecycle/cmd/sealctl/main.go +++ b/lifecycle/cmd/sealctl/main.go @@ -16,7 +16,6 @@ package main import ( "github.com/containers/buildah" - "github.com/labring/sealos/cmd/sealctl/cmd" ) diff --git a/lifecycle/cmd/sealos/cmd/add.go b/lifecycle/cmd/sealos/cmd/add.go index b2c2a8b16c70..ca053d3f33df 100644 --- a/lifecycle/cmd/sealos/cmd/add.go +++ b/lifecycle/cmd/sealos/cmd/add.go @@ -17,10 +17,9 @@ package cmd import ( "errors" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) const exampleAdd = ` @@ -42,7 +41,7 @@ func newAddCmd() *cobra.Command { Cluster: &apply.Cluster{}, SSH: &apply.SSH{}, } - var addCmd = &cobra.Command{ + addCmd := &cobra.Command{ Use: "add", Short: "Add nodes into cluster", Args: cobra.NoArgs, diff --git a/lifecycle/cmd/sealos/cmd/apply.go b/lifecycle/cmd/sealos/cmd/apply.go index 92c2539bf831..f8cef49d8b50 100644 --- a/lifecycle/cmd/sealos/cmd/apply.go +++ b/lifecycle/cmd/sealos/cmd/apply.go @@ -15,10 +15,9 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) var clusterFile string @@ -26,7 +25,7 @@ var clusterFile string func newApplyCmd() *cobra.Command { applyArgs := &apply.Args{} // applyCmd represents the apply command - var applyCmd = &cobra.Command{ + applyCmd := &cobra.Command{ Use: "apply", Short: "Run cloud images within a kubernetes cluster with Clusterfile", Example: `sealos apply -f Clusterfile`, @@ -43,7 +42,8 @@ func newApplyCmd() *cobra.Command { }, } setRequireBuildahAnnotation(applyCmd) - applyCmd.Flags().StringVarP(&clusterFile, "Clusterfile", "f", "Clusterfile", "apply a kubernetes cluster") + applyCmd.Flags(). + StringVarP(&clusterFile, "Clusterfile", "f", "Clusterfile", "apply a kubernetes cluster") applyArgs.RegisterFlags(applyCmd.Flags()) return applyCmd } diff --git a/lifecycle/cmd/sealos/cmd/cert.go b/lifecycle/cmd/sealos/cmd/cert.go index 6fbfc3a4b2d8..9abc3542b405 100644 --- a/lifecycle/cmd/sealos/cmd/cert.go +++ b/lifecycle/cmd/sealos/cmd/cert.go @@ -18,16 +18,14 @@ import ( "fmt" "path" - "github.com/labring/sealos/pkg/runtime" - - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" + "github.com/labring/sealos/pkg/runtime" "github.com/labring/sealos/pkg/runtime/factory" fileutils "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func newCertCmd() *cobra.Command { @@ -70,7 +68,10 @@ func newCertCmd() *cobra.Command { } var opts []clusterfile.OptionFunc if runtimeConfigPath != "" { - opts = append(opts, clusterfile.WithCustomRuntimeConfigFiles([]string{runtimeConfigPath})) + opts = append( + opts, + clusterfile.WithCustomRuntimeConfigFiles([]string{runtimeConfigPath}), + ) } cf := clusterfile.NewClusterFile(clusterPath, opts...) if err := cf.Process(); err != nil { @@ -81,7 +82,7 @@ func newCertCmd() *cobra.Command { rt, err := factory.New(cf.GetCluster(), cf.GetRuntimeConfig()) if err != nil { - return fmt.Errorf("create runtime failed: %v", err) + return fmt.Errorf("create runtime failed: %w", err) } if cm, ok := rt.(runtime.CertManager); ok { logger.Info("using %s cert update implement", cf.GetCluster().GetDistribution()) @@ -90,8 +91,10 @@ func newCertCmd() *cobra.Command { return nil }, } - cmd.Flags().StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to applied exec action") - cmd.Flags().StringSliceVar(&altNames, "alt-names", []string{}, "add extra Subject Alternative Names for certs, domain or ip, eg. sealos.io or 10.103.97.2") + cmd.Flags(). + StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to applied exec action") + cmd.Flags(). + StringSliceVar(&altNames, "alt-names", []string{}, "add extra Subject Alternative Names for certs, domain or ip, eg. sealos.io or 10.103.97.2") _ = cmd.MarkFlagRequired("alt-names") return cmd diff --git a/lifecycle/cmd/sealos/cmd/delete.go b/lifecycle/cmd/sealos/cmd/delete.go index e6bafb19376d..1b9fbb5d3a1a 100644 --- a/lifecycle/cmd/sealos/cmd/delete.go +++ b/lifecycle/cmd/sealos/cmd/delete.go @@ -17,11 +17,10 @@ package cmd import ( "errors" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply" "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) var exampleDelete = ` @@ -46,7 +45,7 @@ func newDeleteCmd() *cobra.Command { deleteArgs := &apply.ScaleArgs{ Cluster: &apply.Cluster{}, } - var deleteCmd = &cobra.Command{ + deleteCmd := &cobra.Command{ Use: "delete", Short: "Remove nodes from cluster", Args: cobra.NoArgs, @@ -73,6 +72,7 @@ func newDeleteCmd() *cobra.Command { } setRequireBuildahAnnotation(deleteCmd) deleteArgs.RegisterFlags(deleteCmd.Flags(), "removed", "remove") - deleteCmd.Flags().BoolVar(&processor.ForceDelete, "force", false, "we also can input an --force flag to delete cluster by force") + deleteCmd.Flags(). + BoolVar(&processor.ForceDelete, "force", false, "we also can input an --force flag to delete cluster by force") return deleteCmd } diff --git a/lifecycle/cmd/sealos/cmd/docs.go b/lifecycle/cmd/sealos/cmd/docs.go index b832f7cf51cb..60028360a353 100644 --- a/lifecycle/cmd/sealos/cmd/docs.go +++ b/lifecycle/cmd/sealos/cmd/docs.go @@ -24,13 +24,13 @@ import ( var docsPath string func newDocsCmd() *cobra.Command { - var docsCmd = &cobra.Command{ + docsCmd := &cobra.Command{ Use: "docs", Short: "generate API reference", Example: `sealos docs`, Args: cobra.NoArgs, PreRunE: func(cmd *cobra.Command, args []string) error { - return os.MkdirAll(docsPath, 0755) + return os.MkdirAll(docsPath, 0o755) }, RunE: func(cmd *cobra.Command, args []string) error { return doc.GenMarkdownTree(rootCmd, docsPath) diff --git a/lifecycle/cmd/sealos/cmd/exec.go b/lifecycle/cmd/sealos/cmd/exec.go index 0b239c226284..d7e1206f1538 100644 --- a/lifecycle/cmd/sealos/cmd/exec.go +++ b/lifecycle/cmd/sealos/cmd/exec.go @@ -19,13 +19,12 @@ package cmd import ( "context" - "github.com/spf13/cobra" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/ssh" v2 "github.com/labring/sealos/pkg/types/v1beta1" + "github.com/spf13/cobra" + "golang.org/x/sync/errgroup" ) var clusterName string @@ -47,7 +46,7 @@ func newExecCmd() *cobra.Command { ips []string cluster *v2.Cluster ) - var execCmd = &cobra.Command{ + execCmd := &cobra.Command{ Use: "exec", Short: "Execute shell command or script on specified nodes", Example: exampleExec, @@ -58,16 +57,18 @@ func newExecCmd() *cobra.Command { }, PreRunE: func(cmd *cobra.Command, args []string) (err error) { cluster, err = clusterfile.GetClusterFromName(clusterName) - return + return err }, } - execCmd.Flags().StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to run commands") - execCmd.Flags().StringSliceVarP(&roles, "roles", "r", []string{}, "run command on nodes with role") + execCmd.Flags(). + StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to run commands") + execCmd.Flags(). + StringSliceVarP(&roles, "roles", "r", []string{}, "run command on nodes with role") execCmd.Flags().StringSliceVar(&ips, "ips", []string{}, "run command on nodes with ip address") return execCmd } -func getTargets(cluster *v2.Cluster, ips []string, roles []string) []string { +func getTargets(cluster *v2.Cluster, ips, roles []string) []string { if len(ips) > 0 { return ips } @@ -81,16 +82,15 @@ func getTargets(cluster *v2.Cluster, ips []string, roles []string) []string { return targets } -func runCommand(cluster *v2.Cluster, targets []string, args []string) error { +func runCommand(cluster *v2.Cluster, targets, args []string) error { execer, err := exec.New(ssh.NewCacheClientFromCluster(cluster, true)) if err != nil { return err } eg, _ := errgroup.WithContext(context.Background()) for _, ipAddr := range targets { - ip := ipAddr eg.Go(func() error { - return execer.CmdAsync(ip, args...) + return execer.CmdAsync(ipAddr, args...) }) } return eg.Wait() diff --git a/lifecycle/cmd/sealos/cmd/gen.go b/lifecycle/cmd/sealos/cmd/gen.go index c01803309290..0923081dcb19 100644 --- a/lifecycle/cmd/sealos/cmd/gen.go +++ b/lifecycle/cmd/sealos/cmd/gen.go @@ -21,10 +21,9 @@ import ( "io" "os" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) var exampleGen = ` @@ -52,7 +51,7 @@ func newGenCmd() *cobra.Command { } var out string - var genCmd = &cobra.Command{ + genCmd := &cobra.Command{ Use: "gen", Short: "generate a Clusterfile with all default settings", Long: `generate a Clusterfile of the kubernetes cluster, which can be applied by 'sealos apply' command`, @@ -66,7 +65,9 @@ func newGenCmd() *cobra.Command { var outputWriter io.WriteCloser switch out { case "", "stdout": - logger.Info("if you want to save the output of gen command, use '--output' option instead of redirecting to file") + logger.Info( + "if you want to save the output of gen command, use '--output' option instead of redirecting to file", + ) outputWriter = os.Stdout default: outputWriter, err = os.Create(out) diff --git a/lifecycle/cmd/sealos/cmd/registry.go b/lifecycle/cmd/sealos/cmd/registry.go index 9bddecefe134..8011da9a20af 100644 --- a/lifecycle/cmd/sealos/cmd/registry.go +++ b/lifecycle/cmd/sealos/cmd/registry.go @@ -17,18 +17,17 @@ limitations under the License. package cmd import ( + "github.com/labring/sealos/pkg/registry/commands" sregcmd "github.com/labring/sreg/pkg/registry/commands" "github.com/spf13/cobra" - - "github.com/labring/sealos/pkg/registry/commands" ) func newRegistryCmd(examplePrefix string) *cobra.Command { - var cmd = &cobra.Command{ + cmd := &cobra.Command{ Use: "registry", Short: "registry related", } - examplePrefix = examplePrefix + " registry" + examplePrefix += " registry" cmd.AddCommand(commands.NewRegistryPasswdCmd()) cmd.AddCommand(sregcmd.NewServeRegistryCommand()) cmd.AddCommand(sregcmd.NewRegistryImageSaveCmd(examplePrefix)) diff --git a/lifecycle/cmd/sealos/cmd/reset.go b/lifecycle/cmd/sealos/cmd/reset.go index e98c6a83148f..28bc303b20e2 100644 --- a/lifecycle/cmd/sealos/cmd/reset.go +++ b/lifecycle/cmd/sealos/cmd/reset.go @@ -17,11 +17,10 @@ package cmd import ( "errors" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply" "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) var exampleReset = ` @@ -35,7 +34,7 @@ func newResetCmd() *cobra.Command { SSH: &apply.SSH{}, } - var resetCmd = &cobra.Command{ + resetCmd := &cobra.Command{ Use: "reset", Short: "Reset all, everything in the cluster", Example: exampleReset, @@ -59,6 +58,7 @@ func newResetCmd() *cobra.Command { } setRequireBuildahAnnotation(resetCmd) resetArgs.RegisterFlags(resetCmd.Flags()) - resetCmd.Flags().BoolVar(&processor.ForceDelete, "force", false, "we also can input an --force flag to reset cluster by force") + resetCmd.Flags(). + BoolVar(&processor.ForceDelete, "force", false, "we also can input an --force flag to reset cluster by force") return resetCmd } diff --git a/lifecycle/cmd/sealos/cmd/root.go b/lifecycle/cmd/sealos/cmd/root.go index 2b84de2362f4..1e9feb19e156 100644 --- a/lifecycle/cmd/sealos/cmd/root.go +++ b/lifecycle/cmd/sealos/cmd/root.go @@ -19,20 +19,17 @@ import ( "io" "os" - sreglog "github.com/labring/sreg/pkg/utils/logger" - "github.com/spf13/cobra" - "k8s.io/kubectl/pkg/util/templates" - "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/system" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + sreglog "github.com/labring/sreg/pkg/utils/logger" + "github.com/spf13/cobra" + "k8s.io/kubectl/pkg/util/templates" ) -var ( - debug bool -) +var debug bool // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -115,7 +112,7 @@ func onBootOnDie() { errExit(err) constants.DefaultRuntimeRootDir = val - var rootDirs = []string{ + rootDirs := []string{ constants.LogPath(), constants.WorkDir(), } diff --git a/lifecycle/cmd/sealos/cmd/run.go b/lifecycle/cmd/sealos/cmd/run.go index c8c7e86100ac..07feb9bf8eb5 100644 --- a/lifecycle/cmd/sealos/cmd/run.go +++ b/lifecycle/cmd/sealos/cmd/run.go @@ -17,12 +17,11 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply" "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) var exampleRun = ` @@ -59,7 +58,7 @@ func newRunCmd() *cobra.Command { SSH: &apply.SSH{}, } var transport string - var runCmd = &cobra.Command{ + runCmd := &cobra.Command{ Use: "run", Short: "Run cloud native applications with ease, with or without a existing cluster", Long: `sealos run labring/kubernetes:v1.24.0 --masters [arg] --nodes [arg]`, @@ -89,8 +88,10 @@ func newRunCmd() *cobra.Command { if err := runCmd.Flags().MarkDeprecated("single", "it defaults to running cluster in single mode when there are no master and node"); err != nil { logger.Fatal(err) } - runCmd.Flags().BoolVarP(&processor.ForceOverride, "force", "f", false, "force override app in this cluster") + runCmd.Flags(). + BoolVarP(&processor.ForceOverride, "force", "f", false, "force override app in this cluster") runCmd.Flags().StringVarP(&transport, "transport", "t", buildah.OCIArchive, - fmt.Sprintf("load image transport from tar archive file.(optional value: %s, %s)", buildah.OCIArchive, buildah.DockerArchive)) + fmt.Sprintf("load image transport from tar archive file.(optional value: %s, %s)", buildah.OCIArchive, buildah.DockerArchive), + ) return runCmd } diff --git a/lifecycle/cmd/sealos/cmd/scp.go b/lifecycle/cmd/sealos/cmd/scp.go index e97fe41d945e..b780447e2b84 100644 --- a/lifecycle/cmd/sealos/cmd/scp.go +++ b/lifecycle/cmd/sealos/cmd/scp.go @@ -19,14 +19,13 @@ package cmd import ( "context" - "github.com/spf13/cobra" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" + "golang.org/x/sync/errgroup" ) const exampleScp = ` @@ -46,7 +45,7 @@ func newScpCmd() *cobra.Command { ips []string cluster *v1beta1.Cluster ) - var scpCmd = &cobra.Command{ + scpCmd := &cobra.Command{ Use: "scp", Short: "Copy file to remote on specified nodes", Example: exampleScp, @@ -57,25 +56,25 @@ func newScpCmd() *cobra.Command { }, PreRunE: func(cmd *cobra.Command, args []string) (err error) { cluster, err = clusterfile.GetClusterFromName(clusterName) - return + return err }, } - scpCmd.Flags().StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to run scp action") + scpCmd.Flags(). + StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to run scp action") scpCmd.Flags().StringSliceVarP(&roles, "roles", "r", []string{}, "copy file to nodes with role") scpCmd.Flags().StringSliceVar(&ips, "ips", []string{}, "copy file to nodes with ip address") return scpCmd } -func runCopy(cluster *v1beta1.Cluster, targets []string, args []string) error { +func runCopy(cluster *v1beta1.Cluster, targets, args []string) error { execer, err := exec.New(ssh.NewCacheClientFromCluster(cluster, true)) if err != nil { return err } eg, _ := errgroup.WithContext(context.Background()) for _, ipAddr := range targets { - ip := ipAddr eg.Go(func() error { - return execer.Copy(ip, args[0], args[1]) + return execer.Copy(ipAddr, args[0], args[1]) }) } if err = eg.Wait(); err != nil { diff --git a/lifecycle/cmd/sealos/cmd/status.go b/lifecycle/cmd/sealos/cmd/status.go index c8031d24521e..2bfd503a90c4 100644 --- a/lifecycle/cmd/sealos/cmd/status.go +++ b/lifecycle/cmd/sealos/cmd/status.go @@ -19,7 +19,6 @@ import ( "github.com/labring/sealos/pkg/checker" "github.com/labring/sealos/pkg/clusterfile" - "github.com/spf13/cobra" ) @@ -32,12 +31,22 @@ func newStatusCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { cluster, err := clusterfile.GetClusterFromName(clusterName) if err != nil { - return fmt.Errorf("get default cluster failed, %v", err) + return fmt.Errorf("get default cluster failed, %w", err) + } + list := []checker.Interface{ + checker.NewRegistryChecker(), + checker.NewCRIShimChecker(), + checker.NewCRICtlChecker(), + checker.NewInitSystemChecker(), + checker.NewNodeChecker(), + checker.NewPodChecker(), + checker.NewSvcChecker(), + checker.NewClusterChecker(), } - list := []checker.Interface{checker.NewRegistryChecker(), checker.NewCRIShimChecker(), checker.NewCRICtlChecker(), checker.NewInitSystemChecker(), checker.NewNodeChecker(), checker.NewPodChecker(), checker.NewSvcChecker(), checker.NewClusterChecker()} return checker.RunCheckList(list, cluster, checker.PhasePost) }, } - checkCmd.Flags().StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to applied status action") + checkCmd.Flags(). + StringVarP(&clusterName, "cluster", "c", "default", "name of cluster to applied status action") return checkCmd } diff --git a/lifecycle/cmd/sealos/cmd/version.go b/lifecycle/cmd/sealos/cmd/version.go index 65128ef9801a..c26512876e0f 100644 --- a/lifecycle/cmd/sealos/cmd/version.go +++ b/lifecycle/cmd/sealos/cmd/version.go @@ -20,27 +20,28 @@ import ( "fmt" "strings" - "github.com/spf13/cobra" - "sigs.k8s.io/yaml" - "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/version" versionutils "github.com/labring/sealos/pkg/version/utils" + "github.com/spf13/cobra" + "sigs.k8s.io/yaml" ) -var shortPrint bool -var output string +var ( + shortPrint bool + output string +) func newVersionCmd() *cobra.Command { - var versionCmd = &cobra.Command{ + versionCmd := &cobra.Command{ Use: "version", Short: "Print version info", Args: cobra.NoArgs, Example: `sealos version`, RunE: func(cmd *cobra.Command, args []string) error { - //output default to be yaml + // output default to be yaml if output != "yaml" && output != "json" { return errors.New(`--output must be 'yaml' or 'json'`) } @@ -51,7 +52,8 @@ func newVersionCmd() *cobra.Command { return PrintInfo() }, } - versionCmd.Flags().BoolVar(&shortPrint, "short", false, "if true, print just the version number.") + versionCmd.Flags(). + BoolVar(&shortPrint, "short", false, "if true, print just the version number.") versionCmd.Flags().StringVarP(&output, "output", "o", "yaml", "One of 'yaml' or 'json'") return versionCmd } @@ -91,26 +93,29 @@ func PrintInfo() error { missinfo = append(missinfo, "cri runtime version") } if OutputInfo.KubernetesVersion == nil || OutputInfo.CriRuntimeVersion == nil { - fmt.Printf("WARNING: Failed to get %s.\nCheck kubernetes status or use command \"sealos run\" to launch kubernetes\n", strings.Join(missinfo, " and ")) + fmt.Printf( + "WARNING: Failed to get %s.\nCheck kubernetes status or use command \"sealos run\" to launch kubernetes\n", + strings.Join(missinfo, " and "), + ) } return nil } -func PrintToStd(OutputInfo *version.Output) error { +func PrintToStd(outputInfo *version.Output) error { var ( marshalled []byte err error ) switch output { case "yaml": - marshalled, err = yaml.Marshal(&OutputInfo) + marshalled, err = yaml.Marshal(outputInfo) if err != nil { return fmt.Errorf("fail to marshal yaml: %w", err) } fmt.Println(string(marshalled)) case "json": - marshalled, err = json.Marshal(&OutputInfo) + marshalled, err = json.Marshal(outputInfo) if err != nil { return fmt.Errorf("fail to marshal json: %w", err) } @@ -118,7 +123,10 @@ func PrintToStd(OutputInfo *version.Output) error { default: // There is a bug in the program if we hit this case. // However, we follow a policy of never panicking. - return fmt.Errorf("versionOptions were not validated: --output=%q should have been rejected", output) + return fmt.Errorf( + "versionOptions were not validated: --output=%q should have been rejected", + output, + ) } return nil } diff --git a/lifecycle/cmd/sealos/main.go b/lifecycle/cmd/sealos/main.go index bde43dcc5287..13c85023449f 100644 --- a/lifecycle/cmd/sealos/main.go +++ b/lifecycle/cmd/sealos/main.go @@ -16,7 +16,6 @@ package main import ( "github.com/containers/buildah" - "github.com/labring/sealos/cmd/sealos/cmd" ) diff --git a/lifecycle/fork/golang/expansion/expand.go b/lifecycle/fork/golang/expansion/expand.go index 32bf62e98750..c918f543e6fb 100644 --- a/lifecycle/fork/golang/expansion/expand.go +++ b/lifecycle/fork/golang/expansion/expand.go @@ -88,9 +88,10 @@ func tryReadVariableName(input string) (string, bool, int) { return input[0:1], false, 1 case referenceParenOpener: // Scan to expression closer - for i := 1; i < len(input); i++ { - if input[i] == referenceParenCloser { - return input[1:i], true, i + 1 + for offset, r := range input[1:] { + if r == referenceParenCloser { + idx := offset + 1 + return input[1:idx], true, idx + 1 } } @@ -98,9 +99,10 @@ func tryReadVariableName(input string) (string, bool, int) { return string(operator) + string(referenceParenOpener), false, 1 case referenceCurlyOpener: // Scan to expression closer - for i := 1; i < len(input); i++ { - if input[i] == referenceCurlyCloser { - return input[1:i], true, i + 1 + for offset, r := range input[1:] { + if r == referenceCurlyCloser { + idx := offset + 1 + return input[1:idx], true, idx + 1 } } @@ -111,15 +113,17 @@ func tryReadVariableName(input string) (string, bool, int) { // that doesn't begin an expression. Return the operator // and the first rune in the string. var variableName string - var i int - for i = 0; i < len(input); i++ { - if !unicode.IsLetter(rune(input[i])) && !unicode.IsDigit(rune(input[i])) && input[i] != '_' { + var consumed int + for idx, r := range input { + if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' { + consumed = idx break } - variableName += string(input[i]) + variableName += string(r) + consumed = idx + 1 } if len(variableName) > 0 { - return variableName, true, i + return variableName, true, consumed } return string(operator) + string(input[0]), false, 1 } diff --git a/lifecycle/go.mod b/lifecycle/go.mod index 073bd9fb7d08..9d0c74364772 100644 --- a/lifecycle/go.mod +++ b/lifecycle/go.mod @@ -15,7 +15,7 @@ require ( github.com/containers/ocicrypt v1.1.7 github.com/containers/storage v1.50.2 github.com/davecgh/go-spew v1.1.1 - github.com/docker/docker v25.0.1+incompatible + github.com/docker/docker v25.0.6+incompatible github.com/docker/go-units v0.5.0 github.com/emicklei/go-restful/v3 v3.11.0 github.com/emirpasic/gods v1.18.1 @@ -300,6 +300,7 @@ replace ( github.com/containers/storage => github.com/containers/storage v1.46.1 github.com/docker/docker => github.com/docker/docker v25.0.1+incompatible github.com/imdario/mergo => github.com/imdario/mergo v0.3.13 + github.com/mitchellh/osext => github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 github.com/vishvananda/netlink => github.com/vishvananda/netlink v1.1.0 ) diff --git a/lifecycle/go.work.sum b/lifecycle/go.work.sum index fa5960387880..b6d6464b1f0c 100644 --- a/lifecycle/go.work.sum +++ b/lifecycle/go.work.sum @@ -1845,7 +1845,6 @@ github.com/docker/docker v23.0.0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bc github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v23.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v23.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= -github.com/docker/docker v25.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avuGsjVBZsD05qeibJ1/TYlvc0Y= github.com/docker/docker-credential-helpers v0.6.4/go.mod h1:ofX3UI0Gz1TteYBjtgs07O36Pyasyp66D2uKT7H8W1c= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= @@ -2596,6 +2595,8 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5 h1:PJr+ZMXIecYc1Ey2zucXdR73SMBtgjPgwa31099IMv0= github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg= +github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= +github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw= github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI= @@ -2821,8 +2822,6 @@ github.com/moby/sys/signal v0.7.0/go.mod h1:GQ6ObYZfqacOwTtlXvcmh9A26dVRul/hbOZn github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ= github.com/moby/sys/symlink v0.2.0 h1:tk1rOM+Ljp0nFmfOIBtlV3rTDlWOwFRhjEeAhZB0nZc= github.com/moby/sys/symlink v0.2.0/go.mod h1:7uZVF2dqJjG/NsClqul95CqKOBRQyYSNnJ6BMgR/gFs= -github.com/moby/sys/user v0.1.0 h1:WmZ93f5Ux6het5iituh9x2zAG7NFY9Aqi49jjE1PaQg= -github.com/moby/sys/user v0.1.0/go.mod h1:fKJhFOnsCN6xZ5gSfbM6zaHGgDJMrqt9/reuj4T7MmU= github.com/moby/term v0.0.0-20210610120745-9d4ed1856297/go.mod h1:vgPCkQMyxTZ7IDy8SXRufE172gr8+K/JE/7hHFxHW3A= github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5 h1:8Q0qkMVC/MmWkpIdlvZgcv2o2jrlF6zqVOh7W5YHdMA= @@ -3454,8 +3453,6 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0/go.mod h go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.3.0/go.mod h1:QNX1aly8ehqqX1LEa6YniTU7VY9I6R3X/oPxhGdTceE= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0 h1:3jAYbRHQAqzLjd9I4tzxwJ8Pk/N6AqBcF6m1ZHrxG94= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0/go.mod h1:+N7zNjIJv4K+DeX67XXET0P+eIciESgaFDBqh+ZJFS4= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU= go.opentelemetry.io/otel/metric v0.30.0/go.mod h1:/ShZ7+TS4dHzDFmfi1kSXMhMVubNoP0oIaBp70J6UXU= go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A= go.opentelemetry.io/otel/metric v0.37.0 h1:pHDQuLQOZwYD+Km0eb657A25NaRzy0a+eLyKfDXedEs= @@ -4278,8 +4275,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= -gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY= -gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/lifecycle/pkg/apply/apply.go b/lifecycle/pkg/apply/apply.go index ce6011ad6849..a54af59bca23 100644 --- a/lifecycle/pkg/apply/apply.go +++ b/lifecycle/pkg/apply/apply.go @@ -17,18 +17,22 @@ limitations under the License. package apply import ( + "errors" "fmt" "os" "path/filepath" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply/applydrivers" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" + "github.com/spf13/cobra" ) -func NewApplierFromFile(cmd *cobra.Command, path string, args *Args) (applydrivers.Interface, error) { +func NewApplierFromFile( + cmd *cobra.Command, + path string, + args *Args, +) (applydrivers.Interface, error) { if !filepath.IsAbs(path) { pa, err := os.Getwd() if err != nil { @@ -57,7 +61,7 @@ func NewApplierFromFile(cmd *cobra.Command, path string, args *Args) (applydrive localpath := constants.Clusterfile(cluster.Name) cf := clusterfile.NewClusterFile(localpath) err := cf.Process() - if err != nil && err != clusterfile.ErrClusterFileNotExists { + if err != nil && !errors.Is(err, clusterfile.ErrClusterFileNotExists) { return nil, err } currentCluster := cf.GetCluster() diff --git a/lifecycle/pkg/apply/apply_test.go b/lifecycle/pkg/apply/apply_test.go index 1a695e1aca0a..558bf1d88658 100644 --- a/lifecycle/pkg/apply/apply_test.go +++ b/lifecycle/pkg/apply/apply_test.go @@ -20,9 +20,8 @@ import ( "reflect" "testing" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply/applydrivers" + "github.com/spf13/cobra" ) func Test_NewApplierFromFile(t *testing.T) { @@ -86,7 +85,11 @@ func Test_NewApplierFromFile(t *testing.T) { }) t.Log(err) if (err != nil) != tt.wantErr { - t.Errorf("NewApplierFromFile(string, ...OptionFunc) error = %v, wantErr %v", err, tt.wantErr) + t.Errorf( + "NewApplierFromFile(string, ...OptionFunc) error = %v, wantErr %v", + err, + tt.wantErr, + ) } }) } diff --git a/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go b/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go index 9eaa5a401485..487d65750d33 100644 --- a/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go +++ b/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go @@ -21,16 +21,11 @@ import ( "os" "strconv" - "github.com/labring/sealos/pkg/runtime/k3s" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" + "github.com/labring/sealos/pkg/runtime/k3s" "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/system" v2 "github.com/labring/sealos/pkg/types/v1beta1" @@ -38,11 +33,18 @@ import ( "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + "golang.org/x/sync/errgroup" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func NewDefaultApplier(ctx context.Context, cluster *v2.Cluster, cf clusterfile.Interface, images []string) (Interface, error) { +func NewDefaultApplier( + ctx context.Context, + cluster *v2.Cluster, + cf clusterfile.Interface, + images []string, +) (Interface, error) { if cluster.Name == "" { - return nil, fmt.Errorf("cluster name cannot be empty") + return nil, errors.New("cluster name cannot be empty") } if cf == nil { cf = clusterfile.NewClusterFile(constants.Clusterfile(cluster.Name)) @@ -100,7 +102,7 @@ func (c *Applier) Apply() error { if c.ClusterCurrent == nil || c.ClusterCurrent.CreationTimestamp.IsZero() { if !c.ClusterDesired.CreationTimestamp.IsZero() { if yes, _ := confirm.Confirm("Desired cluster CreationTimestamp is not zero, do you want to initialize it again?", "you have canceled to create cluster"); !yes { - clusterErr = processor.NewPreProcessError(fmt.Errorf("canceled to create cluster")) + clusterErr = processor.NewPreProcessError(errors.New("canceled to create cluster")) return clusterErr } } @@ -123,8 +125,8 @@ func (c *Applier) Apply() error { return clusterErr } -func (c *Applier) getWriteBackObjects() []interface{} { - obj := []interface{}{c.ClusterDesired} +func (c *Applier) getWriteBackObjects() []any { + obj := []any{c.ClusterDesired} distribution := c.ClusterFile.GetCluster().GetDistribution() if runtimeConfig := c.ClusterFile.GetRuntimeConfig(); runtimeConfig != nil { if components := runtimeConfig.GetComponents(); len(components) > 0 { @@ -150,10 +152,14 @@ func (c *Applier) initStatus() { // todo: atomic updating status after each installation for better reconcile? // todo: set up signal handler -func (c *Applier) updateStatus(clusterErr error, appErr error) { - switch clusterErr.(type) { - case *processor.CheckError, *processor.PreProcessError: - return +func (c *Applier) updateStatus(clusterErr, appErr error) { + { + var errCase0 *processor.CheckError + var errCase1 *processor.PreProcessError + switch { + case errors.As(clusterErr, &errCase0), errors.As(clusterErr, &errCase1): + return + } } // update cluster condition using clusterErr var condition v2.ClusterCondition @@ -165,7 +171,10 @@ func (c *Applier) updateStatus(clusterErr error, appErr error) { condition = v2.NewSuccessClusterCondition() c.ClusterDesired.Status.Phase = v2.ClusterSuccess } - c.ClusterDesired.Status.Conditions = v2.UpdateCondition(c.ClusterDesired.Status.Conditions, condition) + c.ClusterDesired.Status.Conditions = v2.UpdateCondition( + c.ClusterDesired.Status.Conditions, + condition, + ) // update command condition using appErr var cmdCondition v2.CommandCondition @@ -179,10 +188,13 @@ func (c *Applier) updateStatus(clusterErr error, appErr error) { return } cmdCondition.Images = c.RunNewImages - c.ClusterDesired.Status.CommandConditions = v2.UpdateCommandCondition(c.ClusterDesired.Status.CommandConditions, cmdCondition) + c.ClusterDesired.Status.CommandConditions = v2.UpdateCommandCondition( + c.ClusterDesired.Status.CommandConditions, + cmdCondition, + ) } -func (c *Applier) reconcileCluster() (clusterErr error, appErr error) { +func (c *Applier) reconcileCluster() (clusterErr, appErr error) { // sync newVersion pki and etc dir in `.sealos/default/pki` and `.sealos/default/etc` processor.SyncNewVersionConfig(c.ClusterDesired.Name) if len(c.RunNewImages) != 0 { @@ -191,14 +203,29 @@ func (c *Applier) reconcileCluster() (clusterErr error, appErr error) { return nil, appErr } } - mj, md := iputils.GetDiffHosts(c.ClusterCurrent.GetMasterIPAndPortList(), c.ClusterDesired.GetMasterIPAndPortList()) - nj, nd := iputils.GetDiffHosts(c.ClusterCurrent.GetNodeIPAndPortList(), c.ClusterDesired.GetNodeIPAndPortList()) + mj, md := iputils.GetDiffHosts( + c.ClusterCurrent.GetMasterIPAndPortList(), + c.ClusterDesired.GetMasterIPAndPortList(), + ) + nj, nd := iputils.GetDiffHosts( + c.ClusterCurrent.GetNodeIPAndPortList(), + c.ClusterDesired.GetNodeIPAndPortList(), + ) return c.scaleCluster(mj, md, nj, nd), nil } func (c *Applier) initCluster() error { - logger.Info("Start to create a new cluster: master %s, worker %s, registry %s", c.ClusterDesired.GetMasterIPList(), c.ClusterDesired.GetNodeIPList(), c.ClusterDesired.GetRegistryIP()) - createProcessor, err := processor.NewCreateProcessor(c.Context, c.ClusterDesired.Name, c.ClusterFile) + logger.Info( + "Start to create a new cluster: master %s, worker %s, registry %s", + c.ClusterDesired.GetMasterIPList(), + c.ClusterDesired.GetNodeIPList(), + c.ClusterDesired.GetRegistryIP(), + ) + createProcessor, err := processor.NewCreateProcessor( + c.Context, + c.ClusterDesired.Name, + c.ClusterFile, + ) if err != nil { return err } @@ -231,12 +258,28 @@ func (c *Applier) scaleCluster(mj, md, nj, nd []string) error { return nil } logger.Info("start to scale this cluster") - logger.Debug("current cluster: master %s, worker %s", c.ClusterCurrent.GetMasterIPAndPortList(), c.ClusterCurrent.GetNodeIPAndPortList()) - logger.Debug("desired cluster: master %s, worker %s", c.ClusterDesired.GetMasterIPAndPortList(), c.ClusterDesired.GetNodeIPAndPortList()) + logger.Debug( + "current cluster: master %s, worker %s", + c.ClusterCurrent.GetMasterIPAndPortList(), + c.ClusterCurrent.GetNodeIPAndPortList(), + ) + logger.Debug( + "desired cluster: master %s, worker %s", + c.ClusterDesired.GetMasterIPAndPortList(), + c.ClusterDesired.GetNodeIPAndPortList(), + ) localpath := constants.Clusterfile(c.ClusterDesired.Name) cf := clusterfile.NewClusterFile(localpath) - scaleProcessor, err := processor.NewScaleProcessor(cf, c.ClusterDesired.Name, c.ClusterDesired.Spec.Image, mj, md, nj, nd) + scaleProcessor, err := processor.NewScaleProcessor( + cf, + c.ClusterDesired.Name, + c.ClusterDesired.Spec.Image, + mj, + md, + nj, + nd, + ) if err != nil { return err } @@ -294,9 +337,8 @@ func (c *Applier) syncWorkdir() { } eg, _ := errgroup.WithContext(context.Background()) for _, ipAddr := range ipList { - ip := ipAddr eg.Go(func() error { - return execer.Copy(ip, workDir, workDir) + return execer.Copy(ipAddr, workDir, workDir) }) } if err := eg.Wait(); err != nil { diff --git a/lifecycle/pkg/apply/args.go b/lifecycle/pkg/apply/args.go index c42e6cb42d6e..d3388684bc8a 100644 --- a/lifecycle/pkg/apply/args.go +++ b/lifecycle/pkg/apply/args.go @@ -20,9 +20,8 @@ import ( "fmt" "path" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/constants" + "github.com/spf13/pflag" ) type Cluster struct { @@ -32,9 +31,14 @@ type Cluster struct { } func (c *Cluster) RegisterFlags(fs *pflag.FlagSet, verb, action string) { - fs.StringVar(&c.Masters, "masters", "", fmt.Sprintf("masters to %s", verb)) - fs.StringVar(&c.Nodes, "nodes", "", fmt.Sprintf("nodes to %s", verb)) - fs.StringVar(&c.ClusterName, "cluster", "default", fmt.Sprintf("name of cluster to applied %s action", action)) + fs.StringVar(&c.Masters, "masters", "", "masters to "+verb) + fs.StringVar(&c.Nodes, "nodes", "", "nodes to "+verb) + fs.StringVar( + &c.ClusterName, + "cluster", + "default", + fmt.Sprintf("name of cluster to applied %s action", action), + ) } type ClusterName struct { @@ -42,7 +46,12 @@ type ClusterName struct { } func (c *ClusterName) RegisterFlags(fs *pflag.FlagSet, _, action string) { - fs.StringVar(&c.ClusterName, "cluster", "default", fmt.Sprintf("name of cluster to applied %s action", action)) + fs.StringVar( + &c.ClusterName, + "cluster", + "default", + fmt.Sprintf("name of cluster to applied %s action", action), + ) } type SSH struct { @@ -56,9 +65,19 @@ type SSH struct { func (s *SSH) RegisterFlags(fs *pflag.FlagSet) { fs.StringVarP(&s.User, "user", "u", "", "username to authenticate as") fs.StringVarP(&s.Password, "passwd", "p", "", "use given password to authenticate with") - fs.StringVarP(&s.Pk, "pk", "i", path.Join(constants.GetHomeDir(), ".ssh", "id_rsa"), - "selects a file from which the identity (private key) for public key authentication is read") - fs.StringVar(&s.PkPassword, "pk-passwd", "", "passphrase for decrypting a PEM encoded private key") + fs.StringVarP( + &s.Pk, + "pk", + "i", + path.Join(constants.GetHomeDir(), ".ssh", "id_rsa"), + "selects a file from which the identity (private key) for public key authentication is read", + ) + fs.StringVar( + &s.PkPassword, + "pk-passwd", + "", + "passphrase for decrypting a PEM encoded private key", + ) fs.Uint16Var(&s.Port, "port", 22, "port to connect to on the remote host") } @@ -73,9 +92,20 @@ type RunArgs struct { func (arg *RunArgs) RegisterFlags(fs *pflag.FlagSet) { arg.Cluster.RegisterFlags(fs, "run with", "run") arg.SSH.RegisterFlags(fs) - fs.StringSliceVarP(&arg.CustomEnv, "env", "e", []string{}, "environment variables to be set for images") + fs.StringSliceVarP( + &arg.CustomEnv, + "env", + "e", + []string{}, + "environment variables to be set for images", + ) fs.StringSliceVar(&arg.CustomCMD, "cmd", []string{}, "override CMD directive in images") - fs.StringSliceVar(&arg.CustomConfigFiles, "config-file", []string{}, "path of custom config files, to use to replace the resource") + fs.StringSliceVar( + &arg.CustomConfigFiles, + "config-file", + []string{}, + "path of custom config files, to use to replace the resource", + ) } type Args struct { @@ -88,8 +118,18 @@ type Args struct { func (arg *Args) RegisterFlags(fs *pflag.FlagSet) { fs.StringSliceVar(&arg.Values, "values", []string{}, "values file to apply into Clusterfile") fs.StringSliceVar(&arg.Sets, "set", []string{}, "set values on the command line") - fs.StringSliceVar(&arg.CustomEnv, "env", []string{}, "environment variables to be set for images") - fs.StringSliceVar(&arg.CustomConfigFiles, "config-file", []string{}, "path of custom config files, to use to replace the resource") + fs.StringSliceVar( + &arg.CustomEnv, + "env", + []string{}, + "environment variables to be set for images", + ) + fs.StringSliceVar( + &arg.CustomConfigFiles, + "config-file", + []string{}, + "path of custom config files, to use to replace the resource", + ) } type ResetArgs struct { diff --git a/lifecycle/pkg/apply/args_test.go b/lifecycle/pkg/apply/args_test.go index a22dc4d88d86..d6e14095c395 100644 --- a/lifecycle/pkg/apply/args_test.go +++ b/lifecycle/pkg/apply/args_test.go @@ -23,19 +23,25 @@ import ( "strings" "testing" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/constants" + "github.com/spf13/pflag" ) func TestParseClusterFlagsCorrect(t *testing.T) { - var tests = []struct { + tests := []struct { args []string flags *Cluster desired *Cluster }{ { - []string{"--masters", "10.74.22.22:22", "--nodes", "10.74.22.44:22", "--cluster", "default"}, + []string{ + "--masters", + "10.74.22.22:22", + "--nodes", + "10.74.22.44:22", + "--cluster", + "default", + }, &Cluster{}, &Cluster{Masters: "10.74.22.22:22", Nodes: "10.74.22.44:22", ClusterName: "default"}, }, @@ -57,7 +63,7 @@ func TestParseClusterFlagsCorrect(t *testing.T) { } func TestParseSSHFlagsCorrect(t *testing.T) { - var tests = []struct { + tests := []struct { args []string flags *SSH desired *SSH @@ -65,7 +71,12 @@ func TestParseSSHFlagsCorrect(t *testing.T) { { []string{"-u", "root", "-p", "s3cret", "--port", "2222"}, &SSH{}, - &SSH{User: "root", Password: "s3cret", Port: 2222, Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa")}, + &SSH{ + User: "root", + Password: "s3cret", + Port: 2222, + Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa"), + }, }, } @@ -84,14 +95,20 @@ func TestParseSSHFlagsCorrect(t *testing.T) { } } -func equal(in, out interface{}) bool { - inByte, _ := json.Marshal(&in) - outByte, _ := json.Marshal(&out) +func equal(in, out any) bool { + inByte, err := json.Marshal(in) + if err != nil { + return false + } + outByte, err := json.Marshal(out) + if err != nil { + return false + } return bytes.Equal(inByte, outByte) } func TestParseRunArgsFlagsCorrect(t *testing.T) { - var tests = []struct { + tests := []struct { args []string flags *RunArgs desired *RunArgs @@ -108,8 +125,17 @@ func TestParseRunArgsFlagsCorrect(t *testing.T) { SSH: &SSH{}, }, &RunArgs{ - Cluster: &Cluster{Masters: "10.74.22.22:22", Nodes: "10.74.22.44:22", ClusterName: "default"}, - SSH: &SSH{User: "root", Password: "s3cret", Port: 2222, Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa")}, + Cluster: &Cluster{ + Masters: "10.74.22.22:22", + Nodes: "10.74.22.44:22", + ClusterName: "default", + }, + SSH: &SSH{ + User: "root", + Password: "s3cret", + Port: 2222, + Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa"), + }, CustomEnv: []string{"testk=testv"}, CustomCMD: []string{"echo test"}, CustomConfigFiles: []string{}, @@ -133,7 +159,7 @@ func TestParseRunArgsFlagsCorrect(t *testing.T) { } func TestParseArgsFlagsCorrect(t *testing.T) { - var tests = []struct { + tests := []struct { args []string flags *Args desired *Args @@ -168,7 +194,7 @@ func TestParseArgsFlagsCorrect(t *testing.T) { } func TestParseResetArgsFlagsCorrect(t *testing.T) { - var tests = []struct { + tests := []struct { args []string flags *ResetArgs desired *ResetArgs @@ -184,7 +210,12 @@ func TestParseResetArgsFlagsCorrect(t *testing.T) { }, &ResetArgs{ ClusterName: &ClusterName{ClusterName: "default"}, - SSH: &SSH{User: "root", Password: "s3cret", Port: 2222, Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa")}, + SSH: &SSH{ + User: "root", + Password: "s3cret", + Port: 2222, + Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa"), + }, }, }, } @@ -205,7 +236,7 @@ func TestParseResetArgsFlagsCorrect(t *testing.T) { } func TestParseScaleArgsFlagsCorrect(t *testing.T) { - var tests = []struct { + tests := []struct { args []string flags *ScaleArgs desired *ScaleArgs @@ -220,8 +251,17 @@ func TestParseScaleArgsFlagsCorrect(t *testing.T) { SSH: &SSH{}, }, &ScaleArgs{ - Cluster: &Cluster{Masters: "10.74.22.22:22", Nodes: "10.74.22.44:22", ClusterName: "default"}, - SSH: &SSH{User: "root", Password: "passwd", Port: 22, Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa")}, + Cluster: &Cluster{ + Masters: "10.74.22.22:22", + Nodes: "10.74.22.44:22", + ClusterName: "default", + }, + SSH: &SSH{ + User: "root", + Password: "passwd", + Port: 22, + Pk: path.Join(constants.GetHomeDir(), ".ssh", "id_rsa"), + }, }, }, } diff --git a/lifecycle/pkg/apply/gen.go b/lifecycle/pkg/apply/gen.go index 89fc9f3c3b4b..dee506016e75 100644 --- a/lifecycle/pkg/apply/gen.go +++ b/lifecycle/pkg/apply/gen.go @@ -19,8 +19,6 @@ package apply import ( "fmt" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/runtime/factory" @@ -28,6 +26,7 @@ import ( "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/maps" + "github.com/spf13/cobra" ) func NewClusterFromGenArgs(cmd *cobra.Command, args *RunArgs, imageNames []string) ([]byte, error) { @@ -37,9 +36,9 @@ func NewClusterFromGenArgs(cmd *cobra.Command, args *RunArgs, imageNames []strin cluster: cluster, } - if len(args.Cluster.Masters) == 0 { + if len(args.Masters) == 0 { localIpv4 := iputils.GetLocalIpv4() - args.Cluster.Masters = localIpv4 + args.Masters = localIpv4 } if err := c.runArgs(cmd, args, imageNames); err != nil { diff --git a/lifecycle/pkg/apply/gen_test.go b/lifecycle/pkg/apply/gen_test.go index e4d448d3e2af..090f2397d6ae 100644 --- a/lifecycle/pkg/apply/gen_test.go +++ b/lifecycle/pkg/apply/gen_test.go @@ -19,9 +19,8 @@ package apply import ( "testing" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/buildah" + "github.com/spf13/cobra" ) func TestNewClusterFromGenArgs(t *testing.T) { diff --git a/lifecycle/pkg/apply/processor/context.go b/lifecycle/pkg/apply/processor/context.go index 93ea12caaad2..14961d656a65 100644 --- a/lifecycle/pkg/apply/processor/context.go +++ b/lifecycle/pkg/apply/processor/context.go @@ -31,7 +31,9 @@ func WithCommands(ctx context.Context, commands []string) context.Context { func GetCommands(ctx context.Context) []string { v := ctx.Value(commandKey) if v != nil { - return v.([]string) + if commands, ok := v.([]string); ok { + return commands + } } return nil } @@ -42,9 +44,11 @@ func WithEnvs(ctx context.Context, envs map[string]string) context.Context { } func GetEnvs(ctx context.Context) map[string]string { - v := ctx.Value(commandKey) + v := ctx.Value(envKey) if v != nil { - return v.(map[string]string) + if envs, ok := v.(map[string]string); ok { + return envs + } } return nil } diff --git a/lifecycle/pkg/apply/processor/create.go b/lifecycle/pkg/apply/processor/create.go index 55a61243dd82..a967a110af65 100644 --- a/lifecycle/pkg/apply/processor/create.go +++ b/lifecycle/pkg/apply/processor/create.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/bootstrap" "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/checker" @@ -34,6 +32,7 @@ import ( "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/maps" "github.com/labring/sealos/pkg/utils/yaml" + "golang.org/x/sync/errgroup" ) type CreateProcessor struct { @@ -85,7 +84,13 @@ func (c *CreateProcessor) Check(cluster *v2.Cluster) error { // the order doesn't matter ips = append(ips, cluster.GetMasterIPAndPortList()...) ips = append(ips, cluster.GetNodeIPAndPortList()...) - return NewCheckError(checker.RunCheckList([]checker.Interface{checker.NewIPsHostChecker(ips), checker.NewContainerdChecker(ips)}, cluster, checker.PhasePre)) + return NewCheckError( + checker.RunCheckList( + []checker.Interface{checker.NewIPsHostChecker(ips), checker.NewContainerdChecker(ips)}, + cluster, + checker.PhasePre, + ), + ) } func (c *CreateProcessor) PreProcess(cluster *v2.Cluster) error { @@ -106,7 +111,7 @@ func (c *CreateProcessor) preProcess(cluster *v2.Cluster) error { rt, err := factory.New(cluster, c.ClusterFile.GetRuntimeConfig()) if err != nil { - return fmt.Errorf("failed to init runtime, %v", err) + return fmt.Errorf("failed to init runtime, %w", err) } c.Runtime = rt return nil @@ -116,9 +121,12 @@ func (c *CreateProcessor) RunConfig(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunConfig in CreateProcessor.") eg, _ := errgroup.WithContext(context.Background()) for _, cManifest := range cluster.Status.Mounts { - manifest := cManifest eg.Go(func() error { - cfg := config.NewConfiguration(manifest.ImageName, manifest.MountPoint, c.ClusterFile.GetConfigs()) + cfg := config.NewConfiguration( + cManifest.ImageName, + cManifest.MountPoint, + c.ClusterFile.GetConfigs(), + ) return cfg.Dump() }) } @@ -175,7 +183,11 @@ func (c *CreateProcessor) RunGuest(cluster *v2.Cluster) error { return nil } -func NewCreateProcessor(ctx context.Context, name string, clusterFile clusterfile.Interface) (Interface, error) { +func NewCreateProcessor( + ctx context.Context, + name string, + clusterFile clusterfile.Interface, +) (Interface, error) { bder, err := buildah.New(name) if err != nil { return nil, err diff --git a/lifecycle/pkg/apply/processor/delete.go b/lifecycle/pkg/apply/processor/delete.go index b619ea0a0e08..5598e108f052 100644 --- a/lifecycle/pkg/apply/processor/delete.go +++ b/lifecycle/pkg/apply/processor/delete.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/bootstrap" "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/clusterfile" @@ -30,6 +28,7 @@ import ( fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/strings" + "golang.org/x/sync/errgroup" ) var ForceDelete bool @@ -54,6 +53,7 @@ func (d DeleteProcessor) Execute(cluster *v2.Cluster) (err error) { return nil } + func (d DeleteProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) { var todoList []func(cluster *v2.Cluster) error todoList = append(todoList, @@ -87,7 +87,7 @@ func (d *DeleteProcessor) UndoBootstrap(cluster *v2.Cluster) error { func (d *DeleteProcessor) Reset(cluster *v2.Cluster) error { rt, err := factory.New(cluster, d.ClusterFile.GetRuntimeConfig()) if err != nil { - return fmt.Errorf("failed to delete runtime, %v", err) + return fmt.Errorf("failed to delete runtime, %w", err) } return rt.Reset() } @@ -115,7 +115,6 @@ func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error { func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error { eg, _ := errgroup.WithContext(context.Background()) for _, mount := range cluster.Status.Mounts { - mount := mount eg.Go(func() error { return d.Buildah.Delete(mount.Name) }) diff --git a/lifecycle/pkg/apply/processor/install.go b/lifecycle/pkg/apply/processor/install.go index 22da4a45392d..6ded0f5dfadd 100644 --- a/lifecycle/pkg/apply/processor/install.go +++ b/lifecycle/pkg/apply/processor/install.go @@ -20,9 +20,6 @@ import ( "sort" "strings" - "golang.org/x/sync/errgroup" - "k8s.io/apimachinery/pkg/util/sets" - "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/config" @@ -36,6 +33,8 @@ import ( "github.com/labring/sealos/pkg/utils/maps" "github.com/labring/sealos/pkg/utils/rand" stringsutil "github.com/labring/sealos/pkg/utils/strings" + "golang.org/x/sync/errgroup" + "k8s.io/apimachinery/pkg/util/sets" ) var ForceOverride bool @@ -110,7 +109,10 @@ func (c *InstallProcessor) ConfirmOverrideApps(_ *v2.Cluster) error { return nil } - prompt := fmt.Sprintf("are you sure to override these following apps? \n%s\t", strings.Join(c.imagesToOverride, "\n")) + prompt := fmt.Sprintf( + "are you sure to override these following apps? \n%s\t", + strings.Join(c.imagesToOverride, "\n"), + ) cancelledMsg := "you have canceled to override these apps" pass, err := confirm.Confirm(prompt, cancelledMsg) if err != nil { @@ -189,7 +191,9 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { mount.Env = maps.Merge(mount.Env, c.ExtraEnvs) // This code ensures that `cluster.Status.Mounts` always contains the latest `MountImage` instances if index >= 0 { - cluster.Status.Mounts = append(cluster.Status.Mounts[:index], cluster.Status.Mounts[index+1:]...) + cluster.Status.Mounts = append( + cluster.Status.Mounts[:index], + cluster.Status.Mounts[index+1:]...) } cluster.Status.Mounts = append(cluster.Status.Mounts, *mount) c.NewMounts = append(c.NewMounts, *mount) @@ -197,7 +201,7 @@ func (c *InstallProcessor) PreProcess(cluster *v2.Cluster) error { rt, err := factory.New(cluster, c.ClusterFile.GetRuntimeConfig()) if err != nil { - return fmt.Errorf("failed to init runtime, %v", err) + return fmt.Errorf("failed to init runtime, %w", err) } c.Runtime = rt return nil @@ -215,7 +219,7 @@ func (c *InstallProcessor) UpgradeIfNeed(cluster *v2.Cluster) error { logger.Error("upgrade cluster failed") return err } - //upgrade success; replace the old cluster mount + // upgrade success; replace the old cluster mount cluster.ReplaceRootfsImage() } return nil @@ -236,9 +240,12 @@ func (c *InstallProcessor) RunConfig(_ *v2.Cluster) error { } eg, _ := errgroup.WithContext(context.Background()) for _, cManifest := range c.NewMounts { - manifest := cManifest eg.Go(func() error { - cfg := config.NewConfiguration(manifest.ImageName, manifest.MountPoint, c.ClusterFile.GetConfigs()) + cfg := config.NewConfiguration( + cManifest.ImageName, + cManifest.MountPoint, + c.ClusterFile.GetConfigs(), + ) return cfg.Dump() }) } @@ -270,7 +277,11 @@ func (c *InstallProcessor) RunGuest(cluster *v2.Cluster) error { return c.Guest.Apply(cluster, c.NewMounts, cluster.GetAllIPS()) } -func NewInstallProcessor(ctx context.Context, clusterFile clusterfile.Interface, images []string) (Interface, error) { +func NewInstallProcessor( + ctx context.Context, + clusterFile clusterfile.Interface, + images []string, +) (Interface, error) { bder, err := buildah.New(clusterFile.GetCluster().Name) if err != nil { return nil, err diff --git a/lifecycle/pkg/apply/processor/interface.go b/lifecycle/pkg/apply/processor/interface.go index 5cb7ff62d903..cb9f32f208f2 100644 --- a/lifecycle/pkg/apply/processor/interface.go +++ b/lifecycle/pkg/apply/processor/interface.go @@ -19,10 +19,9 @@ import ( "errors" "fmt" "path" + "slices" "github.com/containers/storage" - "golang.org/x/exp/slices" - "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" @@ -110,7 +109,7 @@ func OCIToImageMount(inspector imageInspector, mount *v2.MountImage) error { mount.Env = maps.FromSlice(oci.OCIv1.Config.Env) delete(mount.Env, "PATH") // mount.Entrypoint - var entrypoint []string + entrypoint := make([]string, 0, len(oci.OCIv1.Config.Entrypoint)) for _, cmd := range oci.OCIv1.Config.Entrypoint { if cmd == "/bin/sh" || cmd == "-c" { continue @@ -119,9 +118,9 @@ func OCIToImageMount(inspector imageInspector, mount *v2.MountImage) error { } mount.Entrypoint = entrypoint - //mount.Cmd + // mount.Cmd cmds := oci.OCIv1.Config.Cmd - var newCMDs []string + newCMDs := make([]string, 0, len(cmds)) for _, cmd := range cmds { if cmd == "/bin/sh" || cmd == "-c" { continue @@ -237,7 +236,9 @@ func MountClusterImages(bdah buildah.Interface, cluster *v2.Cluster, skipApp boo } if !hasRootfsType { - return errors.New("can't apply application type images only since RootFS type image is not applied yet") + return errors.New( + "can't apply application type images only since RootFS type image is not applied yet", + ) } return nil } diff --git a/lifecycle/pkg/apply/processor/scale.go b/lifecycle/pkg/apply/processor/scale.go index 97a0b4618dda..bcb05d1e8874 100644 --- a/lifecycle/pkg/apply/processor/scale.go +++ b/lifecycle/pkg/apply/processor/scale.go @@ -16,10 +16,9 @@ package processor import ( "context" + "errors" "fmt" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/bootstrap" "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/checker" @@ -34,6 +33,7 @@ import ( fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + "golang.org/x/sync/errgroup" ) type ScaleProcessor struct { @@ -74,10 +74,10 @@ func (c *ScaleProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error c.RunConfig, c.MountRootfs, c.Bootstrap, - //s.GetPhasePluginFunc(plugin.PhasePreJoin), + // s.GetPhasePluginFunc(plugin.PhasePreJoin), c.Join, c.RunGuest, - //s.GetPhasePluginFunc(plugin.PhasePostJoin), + // s.GetPhasePluginFunc(plugin.PhasePostJoin), ) return todoList, nil } @@ -87,7 +87,7 @@ func (c *ScaleProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error c.PreProcess, c.Delete, c.UndoBootstrap, - //c.ApplyCleanPlugin, + // c.ApplyCleanPlugin, c.UnMountRootfs, ) return todoList, nil @@ -106,7 +106,7 @@ func (c *ScaleProcessor) skipAppMounts(allMount []v2.MountImage) []v2.MountImage func (c *ScaleProcessor) RunGuest(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunGuest in ScaleProcessor.") - hosts := append(c.MastersToJoin, c.NodesToJoin...) + hosts := append(append([]string{}, c.MastersToJoin...), c.NodesToJoin...) err := c.Guest.Apply(cluster, c.skipAppMounts(cluster.Status.Mounts), hosts) if err != nil { return fmt.Errorf("%s: %w", RunGuestFailed, err) @@ -121,7 +121,10 @@ func (c *ScaleProcessor) Delete(cluster *v2.Cluster) error { return err } if len(c.MastersToDelete) > 0 { - return c.Runtime.SyncNodeIPVS(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()) + return c.Runtime.SyncNodeIPVS( + cluster.GetMasterIPAndPortList(), + cluster.GetNodeIPAndPortList(), + ) } return nil } @@ -133,14 +136,17 @@ func (c *ScaleProcessor) Join(cluster *v2.Cluster) error { return err } if len(c.MastersToJoin) > 0 { - return c.Runtime.SyncNodeIPVS(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()) + return c.Runtime.SyncNodeIPVS( + cluster.GetMasterIPAndPortList(), + cluster.GetNodeIPAndPortList(), + ) } return c.Runtime.SyncNodeIPVS(cluster.GetMasterIPAndPortList(), c.NodesToJoin) } func (c ScaleProcessor) UnMountRootfs(cluster *v2.Cluster) error { logger.Info("Executing pipeline UnMountRootfs in ScaleProcessor.") - hosts := append(c.MastersToDelete, c.NodesToDelete...) + hosts := append(append([]string{}, c.MastersToDelete...), c.NodesToDelete...) if cluster.Status.Mounts == nil { logger.Warn("delete process unmount rootfs skip is cluster not mount rootfs") return nil @@ -156,18 +162,33 @@ func (c *ScaleProcessor) JoinCheck(cluster *v2.Cluster) error { logger.Info("Executing pipeline JoinCheck in ScaleProcessor.") var ips, scales []string ips = append(ips, cluster.GetMaster0IPAndPort()) - scales = append(c.MastersToJoin, c.NodesToJoin...) + scales = append(append([]string{}, c.MastersToJoin...), c.NodesToJoin...) ips = append(ips, scales...) - return NewCheckError(checker.RunCheckList([]checker.Interface{checker.NewIPsHostChecker(ips), checker.NewContainerdChecker(scales)}, cluster, checker.PhasePre)) + return NewCheckError( + checker.RunCheckList( + []checker.Interface{ + checker.NewIPsHostChecker(ips), + checker.NewContainerdChecker(scales), + }, + cluster, + checker.PhasePre, + ), + ) } func (c *ScaleProcessor) DeleteCheck(cluster *v2.Cluster) error { logger.Info("Executing pipeline DeleteCheck in ScaleProcessor.") var ips []string ips = append(ips, cluster.GetMaster0IPAndPort()) - //ips = append(ips, c.MastersToDelete...) - //ips = append(ips, c.NodesToDelete...) - return NewCheckError(checker.RunCheckList([]checker.Interface{checker.NewIPsHostChecker(ips)}, cluster, checker.PhasePre)) + // ips = append(ips, c.MastersToDelete...) + // ips = append(ips, c.NodesToDelete...) + return NewCheckError( + checker.RunCheckList( + []checker.Interface{checker.NewIPsHostChecker(ips)}, + cluster, + checker.PhasePre, + ), + ) } func (c *ScaleProcessor) PreProcess(cluster *v2.Cluster) error { @@ -186,10 +207,10 @@ func (c *ScaleProcessor) preProcess(cluster *v2.Cluster) error { return err } if cluster.GetRootfsImage().KubeVersion() == "" { - return fmt.Errorf("rootfs image not found kube version") + return errors.New("rootfs image not found kube version") } clusterPath := constants.Clusterfile(cluster.Name) - obj := []interface{}{cluster} + obj := []any{cluster} if configs := c.ClusterFile.GetConfigs(); len(configs) > 0 { for i := range configs { obj = append(obj, configs[i]) @@ -210,7 +231,7 @@ func (c *ScaleProcessor) preProcess(cluster *v2.Cluster) error { rt, err = factory.New(c.ClusterFile.GetCluster(), c.ClusterFile.GetRuntimeConfig()) } if err != nil { - return fmt.Errorf("failed to init runtime: %v", err) + return fmt.Errorf("failed to init runtime: %w", err) } c.Runtime = rt @@ -243,9 +264,12 @@ func (c *ScaleProcessor) RunConfig(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunConfig in ScaleProcessor.") eg, _ := errgroup.WithContext(context.Background()) for _, cManifest := range cluster.Status.Mounts { - manifest := cManifest eg.Go(func() error { - cfg := config.NewConfiguration(manifest.ImageName, manifest.MountPoint, c.ClusterFile.GetConfigs()) + cfg := config.NewConfiguration( + cManifest.ImageName, + cManifest.MountPoint, + c.ClusterFile.GetConfigs(), + ) return cfg.Dump() }) } @@ -254,7 +278,7 @@ func (c *ScaleProcessor) RunConfig(cluster *v2.Cluster) error { func (c *ScaleProcessor) MountRootfs(cluster *v2.Cluster) error { logger.Info("Executing pipeline MountRootfs in ScaleProcessor.") - hosts := append(c.MastersToJoin, c.NodesToJoin...) + hosts := append(append([]string{}, c.MastersToJoin...), c.NodesToJoin...) // since app type images are only sent to the first master, in // cluster scaling scenario we don't need to sent app images repeatedly. // so filter out rootfs/patch type @@ -285,19 +309,24 @@ func sortAndFilterNoneApplicationMounts(cluster *v2.Cluster) ([]v2.MountImage, e func (c *ScaleProcessor) Bootstrap(cluster *v2.Cluster) error { logger.Info("Executing pipeline Bootstrap in ScaleProcessor") - hosts := append(c.MastersToJoin, c.NodesToJoin...) + hosts := append(append([]string{}, c.MastersToJoin...), c.NodesToJoin...) bs := bootstrap.New(cluster) return bs.Apply(hosts...) } func (c *ScaleProcessor) UndoBootstrap(_ *v2.Cluster) error { logger.Info("Executing pipeline UndoBootstrap in ScaleProcessor") - hosts := append(c.MastersToDelete, c.NodesToDelete...) + hosts := append(append([]string{}, c.MastersToDelete...), c.NodesToDelete...) bs := bootstrap.New(c.ClusterFile.GetCluster()) return bs.Delete(hosts...) } -func NewScaleProcessor(clusterFile clusterfile.Interface, name string, images v2.ImageList, masterToJoin, masterToDelete, nodeToJoin, nodeToDelete []string) (Interface, error) { +func NewScaleProcessor( + clusterFile clusterfile.Interface, + name string, + images v2.ImageList, + masterToJoin, masterToDelete, nodeToJoin, nodeToDelete []string, +) (Interface, error) { bder, err := buildah.New(name) if err != nil { return nil, err diff --git a/lifecycle/pkg/apply/reset.go b/lifecycle/pkg/apply/reset.go index e308acec2c5a..584c9acb7f8a 100644 --- a/lifecycle/pkg/apply/reset.go +++ b/lifecycle/pkg/apply/reset.go @@ -16,15 +16,13 @@ package apply import ( "errors" - "fmt" - - "github.com/spf13/cobra" "github.com/labring/sealos/pkg/apply/applydrivers" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func NewApplierFromResetArgs(cmd *cobra.Command, args *ResetArgs) (applydrivers.Interface, error) { @@ -32,7 +30,7 @@ func NewApplierFromResetArgs(cmd *cobra.Command, args *ResetArgs) (applydrivers. cf := clusterfile.NewClusterFile(clusterPath) err := cf.Process() // incase we want to reset force - if err != nil && err != clusterfile.ErrClusterFileNotExists { + if err != nil && !errors.Is(err, clusterfile.ErrClusterFileNotExists) { return nil, err } cluster := cf.GetCluster() @@ -51,14 +49,14 @@ func NewApplierFromResetArgs(cmd *cobra.Command, args *ResetArgs) (applydrivers. func (r *ClusterArgs) resetArgs(cmd *cobra.Command, args *ResetArgs) error { if args.ClusterName.ClusterName == "" { - return fmt.Errorf("cluster name can not be empty") + return errors.New("cluster name can not be empty") } override := getSSHFromCommand(cmd) if override != nil { ssh.OverSSHConfig(&r.cluster.Spec.SSH, override) } - if r.cluster.ObjectMeta.CreationTimestamp.IsZero() { + if r.cluster.CreationTimestamp.IsZero() { return errors.New("creation time must be specified in clusterfile") } if len(r.cluster.Spec.Hosts) == 0 { diff --git a/lifecycle/pkg/apply/run.go b/lifecycle/pkg/apply/run.go index 8b3aaaabb078..ed6c862b1643 100644 --- a/lifecycle/pkg/apply/run.go +++ b/lifecycle/pkg/apply/run.go @@ -19,11 +19,9 @@ import ( "errors" "fmt" "net" + "slices" "strconv" - "github.com/spf13/cobra" - "golang.org/x/exp/slices" - "github.com/labring/sealos/pkg/apply/applydrivers" "github.com/labring/sealos/pkg/apply/processor" "github.com/labring/sealos/pkg/clusterfile" @@ -35,6 +33,7 @@ import ( "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/maps" stringsutil "github.com/labring/sealos/pkg/utils/strings" + "github.com/spf13/cobra" ) type ClusterArgs struct { @@ -43,14 +42,18 @@ type ClusterArgs struct { clusterName string } -func NewApplierFromArgs(cmd *cobra.Command, args *RunArgs, imageName []string) (applydrivers.Interface, error) { +func NewApplierFromArgs( + cmd *cobra.Command, + args *RunArgs, + imageName []string, +) (applydrivers.Interface, error) { clusterPath := constants.Clusterfile(args.ClusterName) cf := clusterfile.NewClusterFile(clusterPath, clusterfile.WithCustomConfigFiles(args.CustomConfigFiles), clusterfile.WithCustomEnvs(args.CustomEnv), ) err := cf.Process() - if err != nil && err != clusterfile.ErrClusterFileNotExists { + if err != nil && !errors.Is(err, clusterfile.ErrClusterFileNotExists) { return nil, err } @@ -91,7 +94,7 @@ func withCommonContext(ctx context.Context, cmd *cobra.Command) context.Context } func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []string) error { - if args.Cluster.ClusterName == "" { + if args.ClusterName == "" { return errors.New("cluster name can not be empty") } // the first run check @@ -99,13 +102,11 @@ func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []str if len(imageList) == 0 { return errors.New("image can not be empty") } - if len(args.Cluster.Masters) == 0 { + if len(args.Masters) == 0 { return errors.New("master ip(s) must specified") } - } else { - if r.cluster.Status.Phase != v2.ClusterSuccess { - return fmt.Errorf("cluster status is not %s", v2.ClusterSuccess) - } + } else if r.cluster.Status.Phase != v2.ClusterSuccess { + return fmt.Errorf("cluster status is not %s", v2.ClusterSuccess) } if err := PreProcessIPList(args.Cluster); err != nil { @@ -120,8 +121,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.FilterNonEmptyFromString(args.Cluster.Masters, ",") - nodes := stringsutil.FilterNonEmptyFromString(args.Cluster.Nodes, ",") + masters := stringsutil.FilterNonEmptyFromString(args.Masters, ",") + nodes := stringsutil.FilterNonEmptyFromString(args.Nodes, ",") r.hosts = []v2.Host{} sshClient := ssh.NewCacheClientFromCluster(r.cluster, true) @@ -144,13 +145,13 @@ func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []str return nil } -func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) { +func (r *ClusterArgs) setHostWithIpsPort(ips, roles []string) { defaultPort := defaultSSHPort(r.cluster.Spec.SSH.Port) hostMap := map[string]*v2.Host{} for i := range ips { ip, port := iputils.GetHostIPAndPortOrDefault(ips[i], defaultPort) logger.Debug("defaultPort: %s", defaultPort) - socket := fmt.Sprintf("%s:%s", ip, port) + socket := net.JoinHostPort(ip, port) if slices.Contains(r.cluster.GetAllIPS(), socket) { continue } diff --git a/lifecycle/pkg/apply/run_test.go b/lifecycle/pkg/apply/run_test.go index dc186a404684..ff8945366f7f 100644 --- a/lifecycle/pkg/apply/run_test.go +++ b/lifecycle/pkg/apply/run_test.go @@ -18,16 +18,14 @@ import ( "reflect" "testing" - "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - + "github.com/labring/sealos/pkg/apply/applydrivers" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/ssh" - "github.com/labring/sealos/pkg/utils/iputils" - - "github.com/labring/sealos/pkg/apply/applydrivers" v2 "github.com/labring/sealos/pkg/types/v1beta1" + "github.com/labring/sealos/pkg/utils/iputils" + "github.com/spf13/cobra" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestClusterArgs_SetClusterRunArgs(t *testing.T) { @@ -167,7 +165,16 @@ func TestNewApplierFromArgs(t *testing.T) { }, Spec: v2.ClusterSpec{ Hosts: []v2.Host{ - {IPS: []string{iputils.LocalIP(addr) + ":22"}, Roles: []string{v2.MASTER, GetHostArch(ssh.MustNewClient(&v2.SSH{}, true), iputils.LocalIP(addr)+":22")}}, + { + IPS: []string{iputils.LocalIP(addr) + ":22"}, + Roles: []string{ + v2.MASTER, + GetHostArch( + ssh.MustNewClient(&v2.SSH{}, true), + iputils.LocalIP(addr)+":22", + ), + }, + }, }, Image: []string{"labring/kubernetes:v1.24.0"}, SSH: v2.SSH{}, diff --git a/lifecycle/pkg/apply/scale.go b/lifecycle/pkg/apply/scale.go index 5ea79475c752..0e8945f0d013 100644 --- a/lifecycle/pkg/apply/scale.go +++ b/lifecycle/pkg/apply/scale.go @@ -15,14 +15,12 @@ package apply import ( + "errors" "fmt" "net" + "slices" "strings" - "github.com/spf13/cobra" - "golang.org/x/exp/slices" - "k8s.io/apimachinery/pkg/util/sets" - "github.com/labring/sealos/pkg/apply/applydrivers" "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" @@ -31,15 +29,20 @@ import ( v2 "github.com/labring/sealos/pkg/types/v1beta1" fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/iputils" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/util/sets" ) // NewScaleApplierFromArgs will filter ip list from command parameters. -func NewScaleApplierFromArgs(cmd *cobra.Command, scaleArgs *ScaleArgs) (applydrivers.Interface, error) { +func NewScaleApplierFromArgs( + cmd *cobra.Command, + scaleArgs *ScaleArgs, +) (applydrivers.Interface, error) { var cluster *v2.Cluster - clusterPath := constants.Clusterfile(scaleArgs.Cluster.ClusterName) + clusterPath := constants.Clusterfile(scaleArgs.ClusterName) if !fileutil.IsExist(clusterPath) { - cluster = initCluster(scaleArgs.Cluster.ClusterName) + cluster = initCluster(scaleArgs.ClusterName) } else { clusterFile := clusterfile.NewClusterFile(clusterPath) err := clusterFile.Process() @@ -51,8 +54,8 @@ func NewScaleApplierFromArgs(cmd *cobra.Command, scaleArgs *ScaleArgs) (applydri curr := cluster.DeepCopy() - if scaleArgs.Cluster.Nodes == "" && scaleArgs.Cluster.Masters == "" { - return nil, fmt.Errorf("the node or master parameter was not committed") + if scaleArgs.Nodes == "" && scaleArgs.Masters == "" { + return nil, errors.New("the node or master parameter was not committed") } var err error switch cmd.Name() { @@ -114,21 +117,28 @@ func verifyAndSetNodes(cmd *cobra.Command, cluster *v2.Cluster, scaleArgs *Scale return err } - masters, nodes := scaleArgs.Cluster.Masters, scaleArgs.Cluster.Nodes + masters, nodes := scaleArgs.Masters, scaleArgs.Nodes if len(masters) > 0 { if err := validateIPList(masters); err != nil { - return fmt.Errorf("%s in master list %s", err, masters) + return fmt.Errorf("%w in master list %s", err, masters) } } if len(nodes) > 0 { if err := validateIPList(nodes); err != nil { - return fmt.Errorf("%s in node list %s", err, nodes) + return fmt.Errorf("%w in node list %s", err, nodes) } } defaultPort := defaultSSHPort(cluster.Spec.SSH.Port) - var hosts []v2.Host + potentialHosts := len(cluster.Spec.Hosts) + if masters != "" { + potentialHosts += len(strings.Split(masters, ",")) + } + if nodes != "" { + potentialHosts += len(strings.Split(nodes, ",")) + } + hosts := make([]v2.Host, 0, potentialHosts) var hasMaster bool // check duplicate alreadyIn := sets.NewString() @@ -148,11 +158,11 @@ func verifyAndSetNodes(cmd *cobra.Command, cluster *v2.Cluster, scaleArgs *Scale }) } if !hasMaster { - return fmt.Errorf("`master` role not found, due to Clusterfile may have been corrupted?") + return errors.New("`master` role not found, due to Clusterfile may have been corrupted?") } override := getSSHFromCommand(cmd) - getHostFunc := func(sliceStr string, role string, exclude []string) (*v2.Host, error) { + getHostFunc := func(sliceStr, role string, exclude []string) (*v2.Host, error) { ss := strings.Split(sliceStr, ",") addrs := make([]string, 0) for _, s := range ss { @@ -211,22 +221,23 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error { if err := PreProcessIPList(scaleArgs.Cluster); err != nil { return err } - masters, nodes := scaleArgs.Cluster.Masters, scaleArgs.Cluster.Nodes + masters, nodes := scaleArgs.Masters, scaleArgs.Nodes if len(masters) > 0 { if err := validateIPList(masters); err != nil { - return fmt.Errorf("%s in master list %s", err, masters) + return fmt.Errorf("%w in master list %s", err, masters) } } if len(nodes) > 0 { if err := validateIPList(nodes); err != nil { - return fmt.Errorf("%s in node list %s", err, nodes) + return fmt.Errorf("%w in node list %s", err, nodes) } } - //master0 machine cannot be deleted + // 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") + if slices.Contains(set, cluster.GetMaster0IPAndPort()) || + slices.Contains(set, cluster.GetMaster0IP()) { + return errors.New("master0 machine cannot be deleted") } } @@ -241,7 +252,11 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error { for _, node := range strings.Split(nodes, ",") { targetIP, targetPort := iputils.GetHostIPAndPortOrDefault(node, defaultPort) if !hostsSet.Has(net.JoinHostPort(targetIP, targetPort)) { - return fmt.Errorf("parameter error: to delete node IP %s:%s must in cluster IP list", targetIP, targetPort) + return fmt.Errorf( + "parameter error: to delete node IP %s:%s must in cluster IP list", + targetIP, + targetPort, + ) } } } @@ -249,7 +264,11 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error { for _, node := range strings.Split(masters, ",") { targetIP, targetPort := iputils.GetHostIPAndPortOrDefault(node, defaultPort) if !hostsSet.Has(net.JoinHostPort(targetIP, targetPort)) { - return fmt.Errorf("parameter error: to delete master IP %s:%s must in cluster IP list", targetIP, targetPort) + return fmt.Errorf( + "parameter error: to delete master IP %s:%s must in cluster IP list", + targetIP, + targetPort, + ) } } } @@ -257,14 +276,22 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error { if masters != "" && IsIPList(masters) { for i := range cluster.Spec.Hosts { 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) + 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 slices.Contains(cluster.Spec.Hosts[i].Roles, v2.NODE) { - cluster.Spec.Hosts[i].IPS = returnFilteredIPList(cluster.Spec.Hosts[i].IPS, strings.Split(nodes, ","), defaultPort) + cluster.Spec.Hosts[i].IPS = returnFilteredIPList( + cluster.Spec.Hosts[i].IPS, + strings.Split(nodes, ","), + defaultPort, + ) } } } @@ -278,24 +305,29 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error { return nil } -func returnFilteredIPList(clusterIPList []string, toBeDeletedIPList []string, defaultPort string) (res []string) { +func returnFilteredIPList( + clusterIPList, toBeDeletedIPList []string, + defaultPort string, +) (res []string) { toBeDeletedIPList = fillIPAndPort(toBeDeletedIPList, defaultPort) + res = make([]string, 0, len(clusterIPList)) for _, ip := range clusterIPList { if !slices.Contains(toBeDeletedIPList, ip) { - res = append(res, net.JoinHostPort(iputils.GetHostIPAndPortOrDefault(ip, defaultPort))) + hostIP, hostPort := iputils.GetHostIPAndPortOrDefault(ip, defaultPort) + res = append(res, net.JoinHostPort(hostIP, hostPort)) } } - return + return res } func fillIPAndPort(ipList []string, defaultPort string) []string { - var ipAndPorts []string + ipAndPorts := make([]string, 0, len(ipList)) for _, ip := range ipList { if ip == "" { continue } targetIP, targetPort := iputils.GetHostIPAndPortOrDefault(ip, defaultPort) - ipAndPort := fmt.Sprintf("%s:%s", targetIP, targetPort) + ipAndPort := net.JoinHostPort(targetIP, targetPort) ipAndPorts = append(ipAndPorts, ipAndPort) } return ipAndPorts diff --git a/lifecycle/pkg/apply/scale_test.go b/lifecycle/pkg/apply/scale_test.go index 71d3e0522746..c51a52beaf2a 100644 --- a/lifecycle/pkg/apply/scale_test.go +++ b/lifecycle/pkg/apply/scale_test.go @@ -19,10 +19,9 @@ package apply import ( "testing" + v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - v2 "github.com/labring/sealos/pkg/types/v1beta1" ) func TestDelete(t *testing.T) { @@ -50,11 +49,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -86,11 +94,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -122,11 +139,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -158,11 +184,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -194,11 +229,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -230,11 +274,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -266,11 +319,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -302,11 +364,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -338,11 +409,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -374,11 +454,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -410,11 +499,20 @@ func TestDelete(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -468,11 +566,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -504,11 +611,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -540,11 +656,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -576,11 +701,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -612,11 +746,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -648,11 +791,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -684,11 +836,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -720,11 +881,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -756,11 +926,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -797,11 +976,20 @@ func TestJoin(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"192.168.16.99:22", "192.168.16.98:22", "192.168.16.97:22"}, + IPS: []string{ + "192.168.16.99:22", + "192.168.16.98:22", + "192.168.16.97:22", + }, Roles: []string{v2.MASTER}, }, { - IPS: []string{"192.168.16.1:22", "192.168.16.2:22", "192.168.16.3:22", "192.168.16.4:22"}, + IPS: []string{ + "192.168.16.1:22", + "192.168.16.2:22", + "192.168.16.3:22", + "192.168.16.4:22", + }, Roles: []string{v2.NODE}, }, }, @@ -825,7 +1013,7 @@ func TestJoin(t *testing.T) { }, } - var addCmd = &cobra.Command{ + addCmd := &cobra.Command{ Use: "add", } for _, tt := range tests { diff --git a/lifecycle/pkg/apply/utils.go b/lifecycle/pkg/apply/utils.go index 020b9a49d630..f4c724670436 100644 --- a/lifecycle/pkg/apply/utils.go +++ b/lifecycle/pkg/apply/utils.go @@ -22,8 +22,6 @@ import ( "path/filepath" "strings" - "k8s.io/apimachinery/pkg/util/sets" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/ssh" @@ -32,6 +30,7 @@ import ( "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" stringsutil "github.com/labring/sealos/pkg/utils/strings" + "k8s.io/apimachinery/pkg/util/sets" ) func initCluster(clusterName string) *v2.Cluster { @@ -117,7 +116,7 @@ func GetImagesDiff(current, desired []string) []string { return stringsutil.RemoveDuplicate(stringsutil.RemoveSubSlice(desired, current)) } -func CompareImageSpecHash(currentImages []string, desiredImages []string) bool { +func CompareImageSpecHash(currentImages, desiredImages []string) bool { currentHash := hash.ToString(currentImages) newHash := hash.ToString(desiredImages) diff --git a/lifecycle/pkg/bootstrap/applier.go b/lifecycle/pkg/bootstrap/applier.go index d4f00b289e23..8aea1401ef43 100644 --- a/lifecycle/pkg/bootstrap/applier.go +++ b/lifecycle/pkg/bootstrap/applier.go @@ -15,16 +15,16 @@ package bootstrap type Applier interface { - Filter(Context, string) bool - Apply(Context, string) error - Undo(Context, string) error + Filter(ctx Context, phase string) bool + Apply(ctx Context, phase string) error + Undo(ctx Context, phase string) error } type common struct{} -func (c *common) Filter(Context, string) bool { return true } -func (c *common) Apply(Context, string) error { return nil } -func (c *common) Undo(Context, string) error { return nil } +func (c *common) Filter(_ Context, _ string) bool { return true } +func (c *common) Apply(_ Context, _ string) error { return nil } +func (c *common) Undo(_ Context, _ string) error { return nil } var ( defaultPreflights []Applier diff --git a/lifecycle/pkg/bootstrap/bootstrap.go b/lifecycle/pkg/bootstrap/bootstrap.go index d18a1b7c50d2..23d3d902c297 100644 --- a/lifecycle/pkg/bootstrap/bootstrap.go +++ b/lifecycle/pkg/bootstrap/bootstrap.go @@ -18,10 +18,9 @@ import ( "context" "fmt" - "golang.org/x/sync/errgroup" - v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" + "golang.org/x/sync/errgroup" ) type Phase string @@ -34,7 +33,7 @@ const ( type Interface interface { Apply(hosts ...string) error - RegisterApplier(Phase, ...Applier) error + RegisterApplier(phase Phase, appliers ...Applier) error Delete(hosts ...string) error } @@ -154,7 +153,15 @@ func (initializer *defaultInitializer) Undo(ctx Context, host string) error { func init() { defaultPreflights = append(defaultPreflights, &defaultChecker{}) - defaultInitializers = append(defaultInitializers, ®istryHostApplier{}, ®istryApplier{}, &defaultCRIInitializer{}, &apiServerHostApplier{}, &lvscareHostApplier{}, &defaultInitializer{}) + defaultInitializers = append( + defaultInitializers, + ®istryHostApplier{}, + ®istryApplier{}, + &defaultCRIInitializer{}, + &apiServerHostApplier{}, + &lvscareHostApplier{}, + &defaultInitializer{}, + ) } func RegisterApplier(phase Phase, appliers ...Applier) error { diff --git a/lifecycle/pkg/bootstrap/hosts.go b/lifecycle/pkg/bootstrap/hosts.go index c7a9832c4db8..54da97b0ce5b 100644 --- a/lifecycle/pkg/bootstrap/hosts.go +++ b/lifecycle/pkg/bootstrap/hosts.go @@ -18,8 +18,7 @@ package bootstrap import ( "fmt" - - "golang.org/x/exp/slices" + "slices" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/utils/iputils" @@ -36,12 +35,12 @@ func (*apiServerHostApplier) Undo(ctx Context, host string) error { func (a *apiServerHostApplier) Apply(ctx Context, host string) error { if slices.Contains(ctx.GetCluster().GetMasterIPAndPortList(), host) { if err := ctx.GetRemoter().HostsAdd(host, ctx.GetCluster().GetMaster0IP(), constants.DefaultAPIServerDomain); err != nil { - return fmt.Errorf("failed to add hosts: %v", err) + return fmt.Errorf("failed to add hosts: %w", err) } return nil } if err := ctx.GetRemoter().HostsAdd(host, ctx.GetCluster().GetVIP(), constants.DefaultAPIServerDomain); err != nil { - return fmt.Errorf("failed to add hosts: %v", err) + return fmt.Errorf("failed to add hosts: %w", err) } return nil @@ -61,7 +60,7 @@ func (*lvscareHostApplier) Undo(ctx Context, host string) error { func (a *lvscareHostApplier) Apply(ctx Context, host string) error { if err := ctx.GetRemoter().HostsAdd(host, iputils.GetHostIP(host), constants.DefaultLvscareDomain); err != nil { - return fmt.Errorf("failed to add hosts: %v", err) + return fmt.Errorf("failed to add hosts: %w", err) } return nil } diff --git a/lifecycle/pkg/bootstrap/registry.go b/lifecycle/pkg/bootstrap/registry.go index c837358a6c66..514e2a50c72f 100644 --- a/lifecycle/pkg/bootstrap/registry.go +++ b/lifecycle/pkg/bootstrap/registry.go @@ -17,13 +17,12 @@ package bootstrap import ( "fmt" - "k8s.io/apimachinery/pkg/util/sets" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/registry/helpers" "github.com/labring/sealos/pkg/registry/password" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" + "k8s.io/apimachinery/pkg/util/sets" ) type registryApplier struct { @@ -40,11 +39,19 @@ func (a *registryApplier) Apply(ctx Context, host string) error { if a.upgrade == nil { a.upgrade = password.NewUpgrade(ctx.GetCluster().GetName(), ctx.GetExecer()) } - rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetPathResolver().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort()) - lnCmd := fmt.Sprintf(constants.DefaultLnFmt, ctx.GetPathResolver().RootFSRegistryPath(), rc.Data) + rc := helpers.GetRegistryInfo( + ctx.GetExecer(), + ctx.GetPathResolver().RootFSPath(), + ctx.GetCluster().GetRegistryIPAndPort(), + ) + lnCmd := fmt.Sprintf( + constants.DefaultLnFmt, + ctx.GetPathResolver().RootFSRegistryPath(), + rc.Data, + ) logger.Debug("make soft link: %s", lnCmd) if err := ctx.GetExecer().CmdAsync(host, lnCmd); err != nil { - return fmt.Errorf("failed to make link: %v", err) + return fmt.Errorf("failed to make link: %w", err) } if err := a.upgrade.UpdateRegistryPasswd(rc, "", host, ""); err != nil { return err @@ -62,15 +69,23 @@ type registryHostApplier struct{ common } func (*registryHostApplier) String() string { return "registry_host_applier" } func (*registryHostApplier) Undo(ctx Context, host string) error { - rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetPathResolver().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort()) + rc := helpers.GetRegistryInfo( + ctx.GetExecer(), + ctx.GetPathResolver().RootFSPath(), + ctx.GetCluster().GetRegistryIPAndPort(), + ) return ctx.GetRemoter().HostsDelete(host, rc.Domain) } func (a *registryHostApplier) Apply(ctx Context, host string) error { - rc := helpers.GetRegistryInfo(ctx.GetExecer(), ctx.GetPathResolver().RootFSPath(), ctx.GetCluster().GetRegistryIPAndPort()) + rc := helpers.GetRegistryInfo( + ctx.GetExecer(), + ctx.GetPathResolver().RootFSPath(), + ctx.GetCluster().GetRegistryIPAndPort(), + ) if err := ctx.GetRemoter().HostsAdd(host, iputils.GetHostIP(rc.IP), rc.Domain); err != nil { - return fmt.Errorf("failed to add hosts: %v", err) + return fmt.Errorf("failed to add hosts: %w", err) } return nil diff --git a/lifecycle/pkg/buildah/build.go b/lifecycle/pkg/buildah/build.go index d1731813e7d7..a50f2e58a21a 100644 --- a/lifecycle/pkg/buildah/build.go +++ b/lifecycle/pkg/buildah/build.go @@ -24,9 +24,8 @@ import ( "github.com/containers/buildah/imagebuildah" buildahcli "github.com/containers/buildah/pkg/cli" "github.com/containers/buildah/util" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func newBuildCommand() *cobra.Command { @@ -71,10 +70,19 @@ func newBuildCommand() *cobra.Command { // build is a all common flags buildFlags := buildahcli.GetBudFlags(&buildFlagResults) - buildFlags.StringVar(&buildFlagResults.Runtime, "runtime", util.Runtime(), "`path` to an alternate runtime. Use BUILDAH_RUNTIME environment variable to override.") + buildFlags.StringVar( + &buildFlagResults.Runtime, + "runtime", + util.Runtime(), + "`path` to an alternate runtime. Use BUILDAH_RUNTIME environment variable to override.", + ) layerFlags := buildahcli.GetLayerFlags(&layerFlagsResults) - fromAndBudFlags, err := buildahcli.GetFromAndBudFlags(&fromAndBudResults, &userNSResults, &namespaceResults) + fromAndBudFlags, err := buildahcli.GetFromAndBudFlags( + &fromAndBudResults, + &userNSResults, + &namespaceResults, + ) bailOnError(err, "failed to setup From and Build flags") sopts.RegisterFlags(flags) @@ -84,15 +92,25 @@ func newBuildCommand() *cobra.Command { flags.SetNormalizeFunc(buildahcli.AliasFlags) bailOnError(markFlagsHidden(flags, "tls-verify"), "") - bailOnError(markFlagsHidden(flags, append(flagsInBuildCommandToBeHidden(), flagsAssociatedWithPlatform()...)...), "") + bailOnError( + markFlagsHidden( + flags, + append(flagsInBuildCommandToBeHidden(), flagsAssociatedWithPlatform()...)...), + "", + ) buildCommand.SetUsageTemplate(UsageTemplate()) return buildCommand } -func buildCmd(c *cobra.Command, inputArgs []string, sopts saverOptions, iopts buildahcli.BuildOptions) error { +func buildCmd( + c *cobra.Command, + inputArgs []string, + sopts saverOptions, + iopts buildahcli.BuildOptions, +) error { if flagChanged(c, "logfile") { - logfile, err := os.OpenFile(iopts.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) + logfile, err := os.OpenFile(iopts.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600) if err != nil { return err } @@ -125,7 +143,10 @@ func buildCmd(c *cobra.Command, inputArgs []string, sopts saverOptions, iopts bu } } if len(iopts.File) == 0 { - return fmt.Errorf("cannot find any of %v in context directory", strings.Join(defaultFileNames, ", ")) + return fmt.Errorf( + "cannot find any of %v in context directory", + strings.Join(defaultFileNames, ", "), + ) } } if err := setDefaultFlagsWithSetters(c, setDefaultTLSVerifyFlag); err != nil { @@ -166,7 +187,7 @@ func buildCmd(c *cobra.Command, inputArgs []string, sopts saverOptions, iopts bu } func getContextDir(inputArgs []string) (string, error) { - contextDir := "" + var contextDir string cliArgs := inputArgs var err error // Nothing provided, we assume the current working directory as build @@ -174,7 +195,10 @@ func getContextDir(inputArgs []string) (string, error) { if len(cliArgs) == 0 { contextDir, err = os.Getwd() if err != nil { - return "", fmt.Errorf("unable to choose current working directory as build context: %w", err) + return "", fmt.Errorf( + "unable to choose current working directory as build context: %w", + err, + ) } } else { // The context directory could be a URL. Try to handle that. diff --git a/lifecycle/pkg/buildah/buildah.go b/lifecycle/pkg/buildah/buildah.go index 98ac7b24bc4d..80c570a05954 100644 --- a/lifecycle/pkg/buildah/buildah.go +++ b/lifecycle/pkg/buildah/buildah.go @@ -24,12 +24,11 @@ import ( "github.com/containers/common/pkg/config" "github.com/containers/storage" "github.com/containers/storage/pkg/unshare" + "github.com/labring/sealos/pkg/system" + "github.com/labring/sealos/pkg/utils/logger" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/labring/sealos/pkg/system" - "github.com/labring/sealos/pkg/utils/logger" ) type globalFlags struct { @@ -70,16 +69,17 @@ func (opts *globalFlags) HiddenFlags() []string { const logLevel = "log-level" func RegisterGlobalFlags(fs *pflag.FlagSet) error { - var ( - defaultStoreDriverOptions []string + var defaultStoreDriverOptions []string + storageOptions, err := storage.DefaultStoreOptions( + unshare.GetRootlessUID() > 0, + unshare.GetRootlessUID(), ) - storageOptions, err := storage.DefaultStoreOptions(unshare.GetRootlessUID() > 0, unshare.GetRootlessUID()) if err != nil { return err } if len(storageOptions.GraphDriverOptions) > 0 { - optionSlice := storageOptions.GraphDriverOptions[:] + optionSlice := storageOptions.GraphDriverOptions defaultStoreDriverOptions = optionSlice } @@ -89,20 +89,75 @@ func RegisterGlobalFlags(fs *pflag.FlagSet) error { } containerConfig.CheckCgroupsAndAdjustConfig() // TODO Need to allow for environment variable - fs.StringVar(&globalFlagResults.RegistriesConf, "registries-conf", "", "path to registries.conf file (not usually used)") - fs.StringVar(&globalFlagResults.RegistriesConfDir, "registries-conf-dir", "", "path to registries.conf.d directory (not usually used)") - fs.StringVar(&globalFlagResults.UserShortNameAliasConfPath, "short-name-alias-conf", "", "path to short name alias cache file (not usually used)") + fs.StringVar( + &globalFlagResults.RegistriesConf, + "registries-conf", + "", + "path to registries.conf file (not usually used)", + ) + fs.StringVar( + &globalFlagResults.RegistriesConfDir, + "registries-conf-dir", + "", + "path to registries.conf.d directory (not usually used)", + ) + fs.StringVar( + &globalFlagResults.UserShortNameAliasConfPath, + "short-name-alias-conf", + "", + "path to short name alias cache file (not usually used)", + ) fs.StringVar(&globalFlagResults.Root, "root", storageOptions.GraphRoot, "storage root dir") fs.StringVar(&globalFlagResults.RunRoot, "runroot", storageOptions.RunRoot, "storage state dir") - fs.StringVar(&globalFlagResults.CgroupManager, "cgroup-manager", containerConfig.Engine.CgroupManager, "cgroup manager") - fs.StringVar(&globalFlagResults.StorageDriver, "storage-driver", storageOptions.GraphDriverName, "storage-driver") - fs.StringSliceVar(&globalFlagResults.StorageOpts, "storage-opt", defaultStoreDriverOptions, "storage driver option") - fs.StringSliceVar(&globalFlagResults.UserNSUID, "userns-uid-map", []string{}, "default `ctrID:hostID:length` UID mapping to use") - fs.StringSliceVar(&globalFlagResults.UserNSGID, "userns-gid-map", []string{}, "default `ctrID:hostID:length` GID mapping to use") - fs.StringVar(&globalFlagResults.DefaultMountsFile, "default-mounts-file", "", "path to default mounts file") - fs.StringVar(&globalFlagResults.LogLevel, logLevel, "warn", `The log level to be used. Either "trace", "debug", "info", "warn", "error", "fatal", or "panic".`) + fs.StringVar( + &globalFlagResults.CgroupManager, + "cgroup-manager", + containerConfig.Engine.CgroupManager, + "cgroup manager", + ) + fs.StringVar( + &globalFlagResults.StorageDriver, + "storage-driver", + storageOptions.GraphDriverName, + "storage-driver", + ) + fs.StringSliceVar( + &globalFlagResults.StorageOpts, + "storage-opt", + defaultStoreDriverOptions, + "storage driver option", + ) + fs.StringSliceVar( + &globalFlagResults.UserNSUID, + "userns-uid-map", + []string{}, + "default `ctrID:hostID:length` UID mapping to use", + ) + fs.StringSliceVar( + &globalFlagResults.UserNSGID, + "userns-gid-map", + []string{}, + "default `ctrID:hostID:length` GID mapping to use", + ) + fs.StringVar( + &globalFlagResults.DefaultMountsFile, + "default-mounts-file", + "", + "path to default mounts file", + ) + fs.StringVar( + &globalFlagResults.LogLevel, + logLevel, + "warn", + `The log level to be used. Either "trace", "debug", "info", "warn", "error", "fatal", or "panic".`, + ) fs.StringVar(&globalFlagResults.CPUProfile, "cpu-profile", "", "`file` to write CPU profile") - fs.StringVar(&globalFlagResults.MemoryProfile, "memory-profile", "", "`file` to write memory profile") + fs.StringVar( + &globalFlagResults.MemoryProfile, + "memory-profile", + "", + "`file` to write memory profile", + ) return markFlagsHidden(fs, globalFlagResults.HiddenFlags()...) } @@ -115,7 +170,7 @@ var ( func markFlagsHidden(fs *pflag.FlagSet, names ...string) error { for _, name := range names { if err := fs.MarkHidden(name); err != nil { - return fmt.Errorf("unable to mark %s flag as hidden: %v", name, err) + return fmt.Errorf("unable to mark %s flag as hidden: %w", name, err) } } return nil @@ -191,10 +246,7 @@ func SetRequireBuildahAnnotation(cmds ...*cobra.Command) { } func requirePreRun(cmd *cobra.Command) bool { - for { - if cmd == nil { - break - } + for cmd != nil { if cmd.Annotations != nil && cmd.Annotations[requireBuildahAnnotationKey] == requireBuildahAnnotationVal { return true @@ -284,12 +336,20 @@ func after(cmd *cobra.Command) error { if globalFlagResults.MemoryProfile != "" { memoryProfileFile, err := os.Create(globalFlagResults.MemoryProfile) if err != nil { - logger.Fatal("could not create memory profile %s: %v", globalFlagResults.MemoryProfile, err) + logger.Fatal( + "could not create memory profile %s: %v", + globalFlagResults.MemoryProfile, + err, + ) } defer memoryProfileFile.Close() runtime.GC() if err := pprof.Lookup("heap").WriteTo(memoryProfileFile, 1); err != nil { - logger.Fatal("could not write memory profile %s: %v", globalFlagResults.MemoryProfile, err) + logger.Fatal( + "could not write memory profile %s: %v", + globalFlagResults.MemoryProfile, + err, + ) } } for i := range postRunHooks { @@ -324,11 +384,15 @@ func maybeSetupProfiler() (err error) { if globalFlagResults.CPUProfile != "" { cpuProfileFile, err := os.Create(globalFlagResults.CPUProfile) if err != nil { - return fmt.Errorf("could not create CPU profile %s: %v", globalFlagResults.CPUProfile, err) + return fmt.Errorf( + "could not create CPU profile %s: %w", + globalFlagResults.CPUProfile, + err, + ) } globalFlagResults.cpuProfileFile = cpuProfileFile if err = pprof.StartCPUProfile(globalFlagResults.cpuProfileFile); err != nil { - return fmt.Errorf("error starting CPU profiling: %v", err) + return fmt.Errorf("error starting CPU profiling: %w", err) } } return nil diff --git a/lifecycle/pkg/buildah/common.go b/lifecycle/pkg/buildah/common.go index a046c72a8160..ff5417eeaecc 100644 --- a/lifecycle/pkg/buildah/common.go +++ b/lifecycle/pkg/buildah/common.go @@ -28,22 +28,17 @@ import ( "github.com/containers/image/v5/types" "github.com/containers/storage" "github.com/containers/storage/pkg/unshare" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/system" wrapunshare "github.com/labring/sealos/pkg/unshare" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) -var ( - // configuration, including customizations made in containers.conf - needToShutdownStore = false -) +// configuration, including customizations made in containers.conf +var needToShutdownStore = false -var ( - IsRootless = wrapunshare.IsRootless -) +var IsRootless = wrapunshare.IsRootless func flagChanged(c *cobra.Command, name string) bool { if fs := c.Flag(name); fs != nil && fs.Changed { @@ -63,9 +58,21 @@ func setDefaultFlagsWithSetters(c *cobra.Command, setters ...func(*cobra.Command func getPlatformFlags() *pflag.FlagSet { fs := &pflag.FlagSet{} - fs.String("arch", runtime.GOARCH, "set the ARCH of the image to the provided value instead of the architecture of the host") - fs.String("os", runtime.GOOS, "set the OS to the provided value instead of the current operating system of the host") - fs.StringSlice("platform", []string{parse.DefaultPlatform()}, "set the OS/ARCH/VARIANT of the image to the provided value instead of the current operating system and architecture of the host (for example `linux/arm`)") + fs.String( + "arch", + runtime.GOARCH, + "set the ARCH of the image to the provided value instead of the architecture of the host", + ) + fs.String( + "os", + runtime.GOOS, + "set the OS to the provided value instead of the current operating system of the host", + ) + fs.StringSlice( + "platform", + []string{parse.DefaultPlatform()}, + "set the OS/ARCH/VARIANT of the image to the provided value instead of the current operating system and architecture of the host (for example `linux/arm`)", + ) fs.String("variant", "", "override the `variant` of the specified image") return fs } @@ -149,7 +156,7 @@ func getTagsFromFlags(c *cobra.Command) []string { func setDefaultFlagIfNotChanged(c *cobra.Command, k, v string) error { if fs := c.Flag(k); fs != nil && !fs.Changed { if err := c.Flags().Set(k, v); err != nil { - return fmt.Errorf("failed to set --%s default to %s: %v", k, v, err) + return fmt.Errorf("failed to set --%s default to %s: %w", k, v, err) } } return nil @@ -162,10 +169,10 @@ func setDefaultSystemContext(sc *types.SystemContext) { sc.DockerDaemonInsecureSkipTLSVerify = true } -func bailOnError(err error, format string, a ...interface{}) { // nolint: golint,goprintffuncname +func bailOnError(err error, format string) { if err != nil { if format != "" { - logger.Error("%s: %v", fmt.Sprintf(format, a...), err) + logger.Error("%s: %v", format, err) } else { logger.Error("%v", err) } @@ -177,7 +184,10 @@ func getStore(c *cobra.Command) (storage.Store, error) { if err := setXDGRuntimeDir(); err != nil { return nil, err } - options, err := storage.DefaultStoreOptions(unshare.GetRootlessUID() > 0, unshare.GetRootlessUID()) + options, err := storage.DefaultStoreOptions( + unshare.GetRootlessUID() > 0, + unshare.GetRootlessUID(), + ) if err != nil { return nil, err } @@ -201,7 +211,11 @@ func getStore(c *cobra.Command) (storage.Store, error) { // Differently, allow the mount if we are already in a userns, as the mount point will still // be accessible once "buildah mount" exits. if os.Geteuid() != 0 && options.GraphDriverName != "vfs" { - return nil, fmt.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `%s unshare` session", options.GraphDriverName, c.Root().Name()) + return nil, fmt.Errorf( + "cannot mount using driver %s in rootless mode. You need to run it in a `%s unshare` session", + options.GraphDriverName, + c.Root().Name(), + ) } if len(globalFlagResults.UserNSUID) > 0 { @@ -218,10 +232,8 @@ func getStore(c *cobra.Command) (storage.Store, error) { } options.UIDMap = uidmap options.GIDMap = gidmap - } else { - if len(globalFlagResults.UserNSGID) > 0 { - return nil, errors.New("option --userns-gid-map can not be used without --userns-uid-map") - } + } else if len(globalFlagResults.UserNSGID) > 0 { + return nil, errors.New("option --userns-gid-map can not be used without --userns-uid-map") } // If a subcommand has the flags, check if they are set; if so, override the global values @@ -237,10 +249,8 @@ func getStore(c *cobra.Command) (storage.Store, error) { } options.UIDMap = uidmap options.GIDMap = gidmap - } else { - if flagChanged(c, "userns-gid-map") { - return nil, errors.New("option --userns-gid-map can not be used without --userns-uid-map") - } + } else if flagChanged(c, "userns-gid-map") { + return nil, errors.New("option --userns-gid-map can not be used without --userns-uid-map") } umask.Check() @@ -267,7 +277,11 @@ func setXDGRuntimeDir() error { return nil } -func openBuilder(ctx context.Context, store storage.Store, name string) (builder *buildah.Builder, err error) { +func openBuilder( + ctx context.Context, + store storage.Store, + name string, +) (builder *buildah.Builder, err error) { if name != "" { builder, err = buildah.OpenBuilder(store, name) if errors.Is(err, os.ErrNotExist) { diff --git a/lifecycle/pkg/buildah/constants.go b/lifecycle/pkg/buildah/constants.go index c23d2f529ef7..168a68258d31 100644 --- a/lifecycle/pkg/buildah/constants.go +++ b/lifecycle/pkg/buildah/constants.go @@ -43,7 +43,11 @@ func ValidateTransport(s string) error { switch s { case OCIArchive, DockerArchive: default: - return fmt.Errorf("unsupported transport %s, available options are %s", s, strings.Join([]string{OCIArchive, DockerArchive}, ", ")) + return fmt.Errorf( + "unsupported transport %s, available options are %s", + s, + strings.Join([]string{OCIArchive, DockerArchive}, ", "), + ) } return nil } @@ -71,9 +75,16 @@ var ( TransportContainersStorage = storage.Transport.Name() ) -func FormatReferenceWithTransportName(tr string, ref string) string { +func FormatReferenceWithTransportName(tr, ref string) string { switch tr { - case TransportAtomic, TransportContainersStorage, TransportDir, TransportDockerArchive, TransportOCI, TransportOCIArchive, TransportTarball, TransportSif: + case TransportAtomic, + TransportContainersStorage, + TransportDir, + TransportDockerArchive, + TransportOCI, + TransportOCIArchive, + TransportTarball, + TransportSif: return tr + ":" + ref case TransportDocker: return tr + "://" + ref diff --git a/lifecycle/pkg/buildah/containers.go b/lifecycle/pkg/buildah/containers.go index 3445c45e0d44..0a994de017f9 100644 --- a/lifecycle/pkg/buildah/containers.go +++ b/lifecycle/pkg/buildah/containers.go @@ -84,7 +84,13 @@ func newDefaultContainerResults() *containersResults { func (opts *containersResults) RegisterFlags(fs *pflag.FlagSet) { fs.BoolVarP(&opts.all, "all", "a", opts.all, "also list non-buildah containers") - fs.StringVarP(&opts.filter, "filter", "f", opts.filter, "filter output based on conditions provided") + fs.StringVarP( + &opts.filter, + "filter", + "f", + opts.filter, + "filter output based on conditions provided", + ) fs.StringVar(&opts.format, "format", opts.format, "pretty-print containers using a Go template") fs.BoolVar(&opts.json, "json", opts.json, "output in JSON format") fs.BoolVarP(&opts.noheading, "noheading", "n", opts.noheading, "do not print column headings") @@ -104,7 +110,7 @@ func newContainersCommand() *cobra.Command { Short: "List working containers and their base images", Long: containersDescription, Args: cobra.ExactArgs(0), - //Flags: sortFlags(containersFlags), + // Flags: sortFlags(containersFlags), RunE: func(cmd *cobra.Command, args []string) error { return containersCmd(cmd, args, opts) }, @@ -155,7 +161,11 @@ func containersCmd(c *cobra.Command, args []string, iopts *containersResults) er return outputContainers(store, opts, params) } -func readContainers(store storage.Store, opts containerOptions, params *containerFilterParams) ([]containerOutputParams, []JSONContainer, error) { +func readContainers( + store storage.Store, + opts containerOptions, + params *containerFilterParams, +) ([]containerOutputParams, []JSONContainer, error) { seenImages := make(map[string]string) imageNameForID := func(id string) string { if id == "" { @@ -184,15 +194,23 @@ func readContainers(store storage.Store, opts containerOptions, params *containe // only output containers created by buildah for _, builder := range builders { image := imageNameForID(builder.FromImageID) - if !matchesCtrFilter(builder.ContainerID, builder.Container, builder.FromImageID, image, params) { + if !matchesCtrFilter( + builder.ContainerID, + builder.Container, + builder.FromImageID, + image, + params, + ) { continue } if opts.json { - JSONContainers = append(JSONContainers, JSONContainer{ID: builder.ContainerID, + JSONContainers = append(JSONContainers, JSONContainer{ + ID: builder.ContainerID, Builder: true, ImageID: builder.FromImageID, ImageName: image, - ContainerName: builder.Container}) + ContainerName: builder.Container, + }) continue } output := containerOutputParams{ @@ -228,11 +246,13 @@ func readContainers(store storage.Store, opts containerOptions, params *containe continue } if opts.json { - JSONContainers = append(JSONContainers, JSONContainer{ID: container.ID, + JSONContainers = append(JSONContainers, JSONContainer{ + ID: container.ID, Builder: ours, ImageID: container.ImageID, ImageName: imageNameForID(container.ImageID), - ContainerName: name}) + ContainerName: name, + }) continue } output := containerOutputParams{ @@ -248,7 +268,11 @@ func readContainers(store storage.Store, opts containerOptions, params *containe return containerOutput, JSONContainers, nil } -func outputContainers(store storage.Store, opts containerOptions, params *containerFilterParams) error { +func outputContainers( + store storage.Store, + opts containerOptions, + params *containerFilterParams, +) error { containerOutput, JSONContainers, err := readContainers(store, opts, params) if err != nil { return err @@ -263,7 +287,11 @@ func outputContainers(store storage.Store, opts containerOptions, params *contai } if opts.format != "" { - out := formats.StdoutTemplateArray{Output: containersToGeneric(containerOutput), Template: opts.format, Fields: containersHeader} + out := formats.StdoutTemplateArray{ + Output: containersToGeneric(containerOutput), + Template: opts.format, + Fields: containersHeader, + } return formats.Writer(out).Out() } @@ -277,10 +305,10 @@ func outputContainers(store storage.Store, opts containerOptions, params *contai return nil } -func containersToGeneric(templParams []containerOutputParams) (genericParams []interface{}) { +func containersToGeneric(templParams []containerOutputParams) (genericParams []any) { if len(templParams) > 0 { for _, v := range templParams { - genericParams = append(genericParams, interface{}(v)) + genericParams = append(genericParams, any(v)) } } return genericParams @@ -288,7 +316,14 @@ func containersToGeneric(templParams []containerOutputParams) (genericParams []i func containerOutputUsingFormatString(truncate bool, params containerOutputParams) { if truncate { - fmt.Printf("%-12.12s %-8s %-12.12s %-32s %s\n", params.ContainerID, params.Builder, params.ImageID, util.TruncateString(params.ImageName, 32), params.ContainerName) + fmt.Printf( + "%-12.12s %-8s %-12.12s %-32s %s\n", + params.ContainerID, + params.Builder, + params.ImageID, + util.TruncateString(params.ImageName, 32), + params.ContainerName, + ) } else { fmt.Printf("%-64s %-8s %-64s %-32s %s\n", params.ContainerID, params.Builder, params.ImageID, params.ImageName, params.ContainerName) } @@ -296,7 +331,14 @@ func containerOutputUsingFormatString(truncate bool, params containerOutputParam func containerOutputHeader(truncate bool) { if truncate { - fmt.Printf("%-12s %-8s %-12s %-32s %s\n", "CONTAINER ID", "BUILDER", "IMAGE ID", "IMAGE NAME", "CONTAINER NAME") + fmt.Printf( + "%-12s %-8s %-12s %-32s %s\n", + "CONTAINER ID", + "BUILDER", + "IMAGE ID", + "IMAGE NAME", + "CONTAINER NAME", + ) } else { fmt.Printf("%-64s %-8s %-64s %-32s %s\n", "CONTAINER ID", "BUILDER", "IMAGE ID", "IMAGE NAME", "CONTAINER NAME") } @@ -308,7 +350,10 @@ func parseCtrFilter(filter string) (*containerFilterParams, error) { for _, param := range filters { pair := strings.SplitN(param, "=", 2) if len(pair) != 2 { - return nil, fmt.Errorf("incorrect filter value %q, should be of form filter=value", param) + return nil, fmt.Errorf( + "incorrect filter value %q, should be of form filter=value", + param, + ) } switch strings.TrimSpace(pair[0]) { case "id": diff --git a/lifecycle/pkg/buildah/create.go b/lifecycle/pkg/buildah/create.go index adfdf6b87f2d..df491c1b709f 100644 --- a/lifecycle/pkg/buildah/create.go +++ b/lifecycle/pkg/buildah/create.go @@ -21,26 +21,20 @@ import ( "os/exec" "strings" - "github.com/labring/sealos/pkg/guest" - "github.com/containers/buildah" - + "github.com/containers/buildah/pkg/parse" + "github.com/containers/storage/pkg/unshare" "github.com/labring/sealos/fork/golang/expansion" + "github.com/labring/sealos/pkg/guest" v2 "github.com/labring/sealos/pkg/types/v1beta1" - + "github.com/labring/sealos/pkg/utils/file" + "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/pkg/utils/maps" stringsutil "github.com/labring/sealos/pkg/utils/strings" - - "github.com/containers/buildah/pkg/parse" - "github.com/containers/storage/pkg/unshare" v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "github.com/spf13/pflag" "golang.org/x/sync/errgroup" - - "github.com/labring/sealos/pkg/utils/file" - - "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/pkg/utils/maps" ) type createOptions struct { @@ -58,15 +52,32 @@ func newDefaultCreateOptions() *createOptions { } func (opts *createOptions) RegisterFlags(fs *pflag.FlagSet) { - fs.StringVarP(&opts.name, "cluster", "c", opts.name, "name of cluster to be created but not actually run") - fs.StringVar(&opts.platform, "platform", opts.platform, "set the OS/ARCH/VARIANT of the image to the provided value instead of the current operating system and architecture of the host (for example `linux/arm`)") + fs.StringVarP( + &opts.name, + "cluster", + "c", + opts.name, + "name of cluster to be created but not actually run", + ) + fs.StringVar( + &opts.platform, + "platform", + opts.platform, + "set the OS/ARCH/VARIANT of the image to the provided value instead of the current operating system and architecture of the host (for example `linux/arm`)", + ) fs.BoolVar(&opts.short, "short", false, "if true, print just the mount path.") - fs.StringSliceVarP(&opts.env, "env", "e", opts.env, "set environment variables for template files") + fs.StringSliceVarP( + &opts.env, + "env", + "e", + opts.env, + "set environment variables for template files", + ) } func newCreateCmd() *cobra.Command { opts := newDefaultCreateOptions() - var createCmd = &cobra.Command{ + createCmd := &cobra.Command{ Use: "create", Short: "Create a cluster without running the CMD, for inspecting image", Args: cobra.MinimumNArgs(1), @@ -81,7 +92,10 @@ func newCreateCmd() *cobra.Command { if err != nil { return err } - flagSetters = append(flagSetters, WithPlatformOption(v1.Platform{OS: oss, Architecture: arch, Variant: variant})) + flagSetters = append( + flagSetters, + WithPlatformOption(v1.Platform{OS: oss, Architecture: arch, Variant: variant}), + ) } info, err := bder.Create(opts.name, args[0], flagSetters...) if err != nil { @@ -114,12 +128,19 @@ func newCreateCmd() *cobra.Command { args = []string{shell} } // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command - cmd := exec.Command(args[0], args[1:]...) + cmd := exec.Command(args[0], args[1:]...) // #nosec G204 cmd.Env = unshare.RootlessEnv() cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - mountEnvs, unmountMounts, err := unshareMount(bder.(*realImpl).store, []string{info.ContainerID}) + impl, ok := bder.(*realImpl) + if !ok { + return fmt.Errorf("unexpected buildah implementation type %T", bder) + } + mountEnvs, unmountMounts, err := unshareMount( + impl.store, + []string{info.ContainerID}, + ) if err != nil { return err } @@ -133,18 +154,17 @@ func newCreateCmd() *cobra.Command { return createCmd } -func runRender(mountPoints []string, env []string) error { +func runRender(mountPoints, env []string) error { eg, _ := errgroup.WithContext(context.Background()) envs := maps.FromSlice(env) for _, mountPoint := range mountPoints { - mp := mountPoint eg.Go(func() error { - if !file.IsExist(mp) { - logger.Debug("MountPoint %s does not exist, skipping", mp) + if !file.IsExist(mountPoint) { + logger.Debug("MountPoint %s does not exist, skipping", mountPoint) return nil } - return stringsutil.RenderTemplatesWithEnv(mp, envs) + return stringsutil.RenderTemplatesWithEnv(mountPoint, envs) }) } @@ -159,11 +179,27 @@ func printCommands(name string, env []string, info buildah.BuilderInfo) { cmds := make([]string, 0) for i := range info.OCIv1.Config.Entrypoint { - cmds = append(cmds, guest.FormalizeWorkingCommand(name, info.Container, v2.ImageType(typeKey), expansion.Expand(info.OCIv1.Config.Entrypoint[i], mapping))) + cmds = append( + cmds, + guest.FormalizeWorkingCommand( + name, + info.Container, + v2.ImageType(typeKey), + expansion.Expand(info.OCIv1.Config.Entrypoint[i], mapping), + ), + ) } for i := range info.OCIv1.Config.Cmd { - cmds = append(cmds, guest.FormalizeWorkingCommand(name, info.Container, v2.ImageType(typeKey), expansion.Expand(info.OCIv1.Config.Cmd[i], mapping))) + cmds = append( + cmds, + guest.FormalizeWorkingCommand( + name, + info.Container, + v2.ImageType(typeKey), + expansion.Expand(info.OCIv1.Config.Cmd[i], mapping), + ), + ) } logger.Info("Shell command: %s", stringsutil.RenderShellWithEnv(strings.Join(cmds, "; "), envs)) diff --git a/lifecycle/pkg/buildah/diff.go b/lifecycle/pkg/buildah/diff.go index 2d3987e0310b..009c2b6e06bf 100644 --- a/lifecycle/pkg/buildah/diff.go +++ b/lifecycle/pkg/buildah/diff.go @@ -25,10 +25,9 @@ import ( "github.com/containers/common/libimage" "github.com/containers/storage/pkg/archive" + "github.com/labring/sealos/pkg/utils/logger" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/labring/sealos/pkg/utils/logger" ) func parseDiffType(v string) (DiffType, error) { @@ -96,7 +95,9 @@ func changesToJSON(out io.Writer, diffs []archive.Change) error { func changesToTable(out io.Writer, diffs []archive.Change) error { for _, row := range diffs { - fmt.Fprintln(out, row.String()) + if _, err := fmt.Fprintln(out, row.String()); err != nil { + return err + } } return nil } @@ -108,9 +109,25 @@ type patchOption struct { } func (o *patchOption) RegisterFlags(fs *pflag.FlagSet) { - fs.BoolVar(&o.enabled, "patch", false, "if enabled then create a named archive file contains changes that we interested") - fs.StringVarP(&o.tag, "tag", "t", "", `tag name, diff will auto build a new image with the generated changes if flag specified and --patch=true`) - fs.BoolVar(&o.save, "save", false, `if enabled and --tag flag is specified, diff will save the built image into a oci-archive file with the name of --output`) + fs.BoolVar( + &o.enabled, + "patch", + false, + "if enabled then create a named archive file contains changes that we interested", + ) + fs.StringVarP( + &o.tag, + "tag", + "t", + "", + `tag name, diff will auto build a new image with the generated changes if flag specified and --patch=true`, + ) + fs.BoolVar( + &o.save, + "save", + false, + `if enabled and --tag flag is specified, diff will save the built image into a oci-archive file with the name of --output`, + ) } type diffOption struct { @@ -148,12 +165,23 @@ func newDiffCommand() *cobra.Command { } func (o *diffOption) RegisterFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.diffType, "diff-type", DiffImage.String(), "type of object to diff, available options are [container, image, all]") - fs.StringVarP(&o.output, "output", "o", "json", "change the output format, available options are [json, table]") + fs.StringVar( + &o.diffType, + "diff-type", + DiffImage.String(), + "type of object to diff, available options are [container, image, all]", + ) + fs.StringVarP( + &o.output, + "output", + "o", + "json", + "change the output format, available options are [json, table]", + ) } func (o *diffOption) ValidateAndSetDefaults() error { - if o.patchOption.enabled { + if o.enabled { tmpFn := o.filterFn o.filterFn = func(c archive.Change) bool { return tmpFn(c) && filterPatchFileRequired(c) @@ -215,7 +243,7 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { if err != nil { return err } - changes, err := r.Store.Changes(from, to) + changes, err := r.Changes(from, to) if err != nil { return err } @@ -225,7 +253,7 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { diffs = append(diffs, c) } } - if !opts.patchOption.enabled { + if !opts.enabled { switch opts.output { case "json": return changesToJSON(os.Stdout, diffs) @@ -235,13 +263,13 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { return fmt.Errorf("unknown output format %s", opts.output) } } - img, err := r.Store.Image(args[1]) + img, err := r.Image(args[1]) if err != nil { return err } - target, err := r.Store.DifferTarget(img.TopLayer) + target, err := r.DifferTarget(img.TopLayer) if err != nil { - return fmt.Errorf("failed to get diff target: %v", err) + return fmt.Errorf("failed to get diff target: %w", err) } rc, err := archive.ExportChanges(target, diffs, nil, nil) if err != nil { @@ -249,12 +277,12 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { } defer rc.Close() // save into archive file only - if opts.patchOption.tag == "" { + if opts.tag == "" { compression := guessCompression(opts.output) extension := "." + compression.Extension() out := opts.output if !strings.HasSuffix(out, extension) { - out = out + extension + out += extension } file, err := os.Create(out) if err != nil { @@ -283,17 +311,17 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { } debug, _ := c.Flags().GetBool("debug") if err = rerun("build", - fmt.Sprintf("--debug=%s", strconv.FormatBool(debug)), - fmt.Sprintf("-t=%s", opts.patchOption.tag), + "--debug="+strconv.FormatBool(debug), + "-t="+opts.tag, "--save-image=false", tmpDir, ); err != nil { return err } // save new image - if opts.patchOption.save { + if opts.save { if err = rerun("save", - fmt.Sprintf("--debug=%s", strconv.FormatBool(debug)), - fmt.Sprintf("--output=%s", opts.output), opts.patchOption.tag, + "--debug="+strconv.FormatBool(debug), + "--output="+opts.output, opts.tag, ); err != nil { return err } @@ -302,7 +330,7 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { } func rerun(command string, args ...string) error { - cmd := exec.Command("/proc/self/exe", append([]string{command}, args...)...) + cmd := exec.Command("/proc/self/exe", append([]string{command}, args...)...) // #nosec G204 cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout diff --git a/lifecycle/pkg/buildah/from.go b/lifecycle/pkg/buildah/from.go index d744f6700d51..1b5b166fb697 100644 --- a/lifecycle/pkg/buildah/from.go +++ b/lifecycle/pkg/buildah/from.go @@ -30,11 +30,10 @@ import ( "github.com/containers/common/pkg/config" "github.com/containers/image/v5/types" "github.com/containers/storage" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/buildah/internal/util" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type fromReply struct { @@ -85,25 +84,91 @@ func newDefaultFromReply() *fromReply { func (opts *fromReply) RegisterFlags(fs *pflag.FlagSet) { fs.SetInterspersed(false) - fs.StringVar(&opts.authfile, "authfile", opts.authfile, "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") - fs.StringVar(&opts.certDir, "cert-dir", opts.certDir, "use certificates at the specified path to access the registry") + fs.StringVar( + &opts.authfile, + "authfile", + opts.authfile, + "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override", + ) + fs.StringVar( + &opts.certDir, + "cert-dir", + opts.certDir, + "use certificates at the specified path to access the registry", + ) fs.StringVar(&opts.cidfile, "cidfile", opts.cidfile, "write the container ID to the file") - fs.StringVar(&opts.creds, "creds", opts.creds, "use `[username[:password]]` for accessing the registry") - fs.StringVarP(&opts.format, "format", "f", opts.format, "`format` of the image manifest and metadata") + fs.StringVar( + &opts.creds, + "creds", + opts.creds, + "use `[username[:password]]` for accessing the registry", + ) + fs.StringVarP( + &opts.format, + "format", + "f", + opts.format, + "`format` of the image manifest and metadata", + ) fs.StringVar(&opts.name, "name", opts.name, "`name` for the working container") - fs.StringVar(&opts.pull, "pull", opts.pull, "pull the image from the registry if newer or not present in store, if false, only pull the image if not present, if always, pull the image even if the named image is present in store, if never, only use the image present in store if available") - fs.Lookup("pull").NoOptDefVal = "true" //allow `--pull ` to be set to `true` as expected. + fs.StringVar( + &opts.pull, + "pull", + opts.pull, + "pull the image from the registry if newer or not present in store, if false, only pull the image if not present, if always, pull the image even if the named image is present in store, if never, only use the image present in store if available", + ) + fs.Lookup("pull").NoOptDefVal = "true" // allow `--pull ` to be set to `true` as expected. - fs.BoolVar(&opts.pullAlways, "pull-always", opts.pullAlways, "pull the image even if the named image is present in store") - fs.BoolVar(&opts.pullNever, "pull-never", opts.pullNever, "do not pull the image, use the image present in store if available") - fs.BoolVarP(&opts.quiet, "quiet", "q", opts.quiet, "don't output progress information when pulling images") - fs.StringVar(&opts.signaturePolicy, "signature-policy", opts.signaturePolicy, "`pathname` of signature policy file (not usually used)") + fs.BoolVar( + &opts.pullAlways, + "pull-always", + opts.pullAlways, + "pull the image even if the named image is present in store", + ) + fs.BoolVar( + &opts.pullNever, + "pull-never", + opts.pullNever, + "do not pull the image, use the image present in store if available", + ) + fs.BoolVarP( + &opts.quiet, + "quiet", + "q", + opts.quiet, + "don't output progress information when pulling images", + ) + fs.StringVar( + &opts.signaturePolicy, + "signature-policy", + opts.signaturePolicy, + "`pathname` of signature policy file (not usually used)", + ) fs.StringVar(&suffix, "suffix", "", "suffix to add to intermediate containers") - fs.BoolVar(&opts.tlsVerify, "tls-verify", opts.tlsVerify, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - bailOnError(markFlagsHidden(fs, "pull-always", "pull-never", "suffix", "signature-policy", "tls-verify"), "") + fs.BoolVar( + &opts.tlsVerify, + "tls-verify", + opts.tlsVerify, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + bailOnError( + markFlagsHidden( + fs, + "pull-always", + "pull-never", + "suffix", + "signature-policy", + "tls-verify", + ), + "", + ) // Add in the common flags - fromAndBudFlags, err := buildahcli.GetFromAndBudFlags(opts.FromAndBudResults, opts.UserNSResults, opts.NameSpaceResults) + fromAndBudFlags, err := buildahcli.GetFromAndBudFlags( + opts.FromAndBudResults, + opts.UserNSResults, + opts.NameSpaceResults, + ) bailOnError(err, "failed to setup From and Bud flags") fs.AddFlagSet(&fromAndBudFlags) @@ -139,7 +204,7 @@ func newFromCommand() *cobra.Command { func onBuild(builder *buildah.Builder, quiet bool) error { ctr := 0 for _, onBuildSpec := range builder.OnBuild() { - ctr = ctr + 1 + ctr++ commands := strings.Split(onBuildSpec, " ") command := strings.ToUpper(commands[0]) args := commands[1:] @@ -270,7 +335,7 @@ func fromCmd(c *cobra.Command, args []string, iopts *fromReply) error { } if iopts.cidfile != "" { filePath := iopts.cidfile - if err := os.WriteFile(filePath, []byte(builder.ContainerID), 0644); err != nil { + if err := os.WriteFile(filePath, []byte(builder.ContainerID), 0o644); err != nil { return fmt.Errorf("failed to write container ID file %q: %w", filePath, err) } } @@ -345,7 +410,9 @@ func doFrom(c *cobra.Command, image string, iopts *fromReply, return nil, err } - commonOpts.Ulimit = append(defaultContainerConfig.Containers.DefaultUlimits, commonOpts.Ulimit...) + commonOpts.Ulimit = append( + defaultContainerConfig.Containers.DefaultUlimits, + commonOpts.Ulimit...) decConfig, err := util.DecryptConfig(iopts.DecryptionKeys) if err != nil { @@ -355,7 +422,11 @@ func doFrom(c *cobra.Command, image string, iopts *fromReply, var pullPushRetryDelay time.Duration pullPushRetryDelay, err = time.ParseDuration(iopts.RetryDelay) if err != nil { - return nil, fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.RetryDelay, err) + return nil, fmt.Errorf( + "unable to parse value provided %q as --retry-delay: %w", + iopts.RetryDelay, + err, + ) } options := buildah.BuilderOptions{ diff --git a/lifecycle/pkg/buildah/images.go b/lifecycle/pkg/buildah/images.go index 7a5a4552bebc..a3c6288894f5 100644 --- a/lifecycle/pkg/buildah/images.go +++ b/lifecycle/pkg/buildah/images.go @@ -81,9 +81,21 @@ func newDefaultImageResults() *imageResults { func (opts *imageResults) RegisterFlags(fs *pflag.FlagSet) { fs.SetInterspersed(false) - fs.BoolVarP(&opts.all, "all", "a", opts.all, "show all images, including intermediate images from a build") + fs.BoolVarP( + &opts.all, + "all", + "a", + opts.all, + "show all images, including intermediate images from a build", + ) fs.BoolVar(&opts.digests, "digests", opts.digests, "show digests") - fs.StringSliceVarP(&opts.filter, "filter", "f", opts.filter, "filter output based on conditions provided") + fs.StringSliceVarP( + &opts.filter, + "filter", + "f", + opts.filter, + "filter output based on conditions provided", + ) fs.StringVar(&opts.format, "format", opts.format, "pretty-print images using a Go template") fs.BoolVar(&opts.json, "json", opts.json, "output in JSON format") fs.BoolVarP(&opts.noHeading, "noheading", "n", opts.noHeading, "do not print column headings") @@ -133,7 +145,9 @@ func newImagesCommand() *cobra.Command { func imagesCmd(c *cobra.Command, args []string, iopts *imageResults) error { if len(args) > 0 { if iopts.all { - return errors.New("when using the --all switch, you may not pass any images names or IDs") + return errors.New( + "when using the --all switch, you may not pass any images names or IDs", + ) } if err := buildahcli.VerifyFlagsArgsOrder(args); err != nil { @@ -179,8 +193,16 @@ func imagesCmd(c *cobra.Command, args []string, iopts *imageResults) error { return formatImages(images, opts) } -func readImages(store storage.Store, systemContext *types.SystemContext, names []string, iopts *imageResults) ([]*libimage.Image, error) { - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) +func readImages( + store storage.Store, + systemContext *types.SystemContext, + names []string, + iopts *imageResults, +) ([]*libimage.Image, error) { + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return nil, err } @@ -200,7 +222,7 @@ func readImages(store storage.Store, systemContext *types.SystemContext, names [ func outputHeader(opts imageOptions) string { if opts.format != "" { - return strings.Replace(opts.format, `\t`, "\t", -1) + return strings.ReplaceAll(opts.format, `\t`, "\t") } if opts.quiet { return formats.IDString @@ -309,7 +331,11 @@ func formatImages(images []*libimage.Image, opts imageOptions) error { } sort.Sort(outputData) - out := formats.StdoutTemplateArray{Output: imagesToGeneric(outputData), Template: outputHeader(opts), Fields: imagesHeader} + out := formats.StdoutTemplateArray{ + Output: imagesToGeneric(outputData), + Template: outputHeader(opts), + Fields: imagesHeader, + } return formats.Writer(out).Out() } @@ -318,7 +344,7 @@ func formatHistory(history []string, name, tag string) string { return none } // Skip the first history entry if already existing as name - if fmt.Sprintf("%s:%s", name, tag) == history[0] { + if name+":"+tag == history[0] { if len(history) == 1 { return none } @@ -338,10 +364,10 @@ func truncateID(id string, truncate bool) string { return id } -func imagesToGeneric(templParams []imageOutputParams) (genericParams []interface{}) { +func imagesToGeneric(templParams []imageOutputParams) (genericParams []any) { if len(templParams) > 0 { for _, v := range templParams { - genericParams = append(genericParams, interface{}(v)) + genericParams = append(genericParams, any(v)) } } return genericParams diff --git a/lifecycle/pkg/buildah/imagesaver.go b/lifecycle/pkg/buildah/imagesaver.go index aaf49e856c46..1a937d8a8815 100644 --- a/lifecycle/pkg/buildah/imagesaver.go +++ b/lifecycle/pkg/buildah/imagesaver.go @@ -20,20 +20,17 @@ import ( "runtime" "strings" - "github.com/labring/sreg/pkg/registry/crane" - "github.com/labring/sreg/pkg/registry/save" - "github.com/containerd/containerd/platforms" "github.com/containers/buildah/pkg/parse" "github.com/containers/image/v5/types" + "github.com/labring/sealos/pkg/constants" + "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sreg/pkg/buildimage" + "github.com/labring/sreg/pkg/registry/crane" + "github.com/labring/sreg/pkg/registry/save" v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/labring/sreg/pkg/buildimage" - - "github.com/labring/sealos/pkg/constants" - "github.com/labring/sealos/pkg/utils/logger" ) type saverOptions struct { @@ -43,10 +40,20 @@ type saverOptions struct { func (opts *saverOptions) RegisterFlags(fs *pflag.FlagSet) { fs.IntVar(&opts.maxPullProcs, "max-pull-procs", 5, "maximum number of goroutines for pulling") - fs.BoolVar(&opts.enabled, "save-image", true, "store images that parsed from the specific directories") + fs.BoolVar( + &opts.enabled, + "save-image", + true, + "store images that parsed from the specific directories", + ) } -func runSaveImages(contextDir string, platforms []v1.Platform, sys *types.SystemContext, opts *saverOptions) error { +func runSaveImages( + contextDir string, + platforms []v1.Platform, + sys *types.SystemContext, + opts *saverOptions, +) error { if !opts.enabled { logger.Warn("save-image is disabled, skip pulling images") return nil @@ -118,7 +125,7 @@ func parsePlatforms(c *cobra.Command) ([]v1.Platform, error) { return []v1.Platform{platform}, nil } - var ret []v1.Platform + ret := make([]v1.Platform, 0, len(parsedPlatforms)) for _, pf := range parsedPlatforms { ret = append(ret, v1.Platform{Architecture: pf.Arch, OS: pf.OS, Variant: pf.Variant}) } diff --git a/lifecycle/pkg/buildah/inspect.go b/lifecycle/pkg/buildah/inspect.go index f45769df6e69..5c4f503bc2a6 100644 --- a/lifecycle/pkg/buildah/inspect.go +++ b/lifecycle/pkg/buildah/inspect.go @@ -34,13 +34,12 @@ import ( imagestorage "github.com/containers/image/v5/storage" "github.com/containers/image/v5/types" "github.com/containers/storage" + "github.com/labring/sealos/pkg/utils/logger" "github.com/opencontainers/go-digest" ociv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "github.com/spf13/pflag" "golang.org/x/term" - - "github.com/labring/sealos/pkg/utils/logger" ) const ( @@ -63,8 +62,20 @@ func newDefaultInspectResults() *inspectResults { func (opts *inspectResults) RegisterFlags(fs *pflag.FlagSet) { fs.SetInterspersed(false) - fs.StringVarP(&opts.format, "format", "f", opts.format, "use `format` as a Go template to format the output") - fs.StringVarP(&opts.inspectType, "type", "t", opts.inspectType, "look at the item of the specified `type` (container or image) and name") + fs.StringVarP( + &opts.format, + "format", + "f", + opts.format, + "use `format` as a Go template to format the output", + ) + fs.StringVarP( + &opts.inspectType, + "type", + "t", + opts.inspectType, + "look at the item of the specified `type` (container or image) and name", + ) } func newInspectCommand() *cobra.Command { @@ -155,9 +166,16 @@ func inspectCmd(c *cobra.Command, args []string, iopts *inspectResults) error { return manifestInspect(ctx, store, systemContext, name) default: return fmt.Errorf("available type options are %s", strings.Join( - []string{inspectTypeContainer, inspectTypeApp, inspectTypeImage, inspectTypeManifest}, ", ")) + []string{ + inspectTypeContainer, + inspectTypeApp, + inspectTypeImage, + inspectTypeManifest, + }, + ", ", + )) } - var out interface{} + var out any if builder != nil { out = buildah.GetBuildInfo(builder) } else if output != nil { @@ -198,7 +216,13 @@ type InspectOutput struct { OCIv1 *ociv1.Image `json:"OCIv1,omitempty"` } -func openImage(ctx context.Context, sc *types.SystemContext, store storage.Store, transport types.ImageTransport, imgRef string) (*InspectOutput, error) { +func openImage( + ctx context.Context, + sc *types.SystemContext, + store storage.Store, + transport types.ImageTransport, + imgRef string, +) (*InspectOutput, error) { var ( rawManifest []byte config *ociv1.Image @@ -255,18 +279,30 @@ func openImage(ctx context.Context, sc *types.SystemContext, store storage.Store }, nil } -func parseTransportAndReference(defaultTransport types.ImageTransport, ref string) (types.ImageTransport, string, error) { +func parseTransportAndReference( + defaultTransport types.ImageTransport, + ref string, +) (types.ImageTransport, string, error) { transport, imgRef := finalizeReference(defaultTransport, ref) parts := strings.SplitN(imgRef, ":", 2) // should never happened if len(parts) != 2 { - return nil, "", fmt.Errorf(`invalid image name "%s", expected colon-separated transport:reference`, imgRef) + return nil, "", fmt.Errorf( + `invalid image name "%s", expected colon-separated transport:reference`, + imgRef, + ) } return transport, parts[1], nil } // inspectImage return image id if found -func inspectImage(ctx context.Context, sc *types.SystemContext, store storage.Store, transport types.ImageTransport, imgName string) (string, types.Image, types.ImageSource, error) { +func inspectImage( + ctx context.Context, + sc *types.SystemContext, + store storage.Store, + transport types.ImageTransport, + imgName string, +) (string, types.Image, types.ImageSource, error) { parseSource := func(s string) (types.ImageSource, error) { logger.Debug("parse reference %s with transport %s", s, transport.Name()) ref, err := transport.ParseReference(s) diff --git a/lifecycle/pkg/buildah/interface.go b/lifecycle/pkg/buildah/interface.go index fb502d50125f..31510877d9ae 100644 --- a/lifecycle/pkg/buildah/interface.go +++ b/lifecycle/pkg/buildah/interface.go @@ -15,6 +15,7 @@ package buildah import ( + "errors" "fmt" "strings" @@ -25,18 +26,17 @@ import ( "github.com/containers/image/v5/types" "github.com/containers/storage" storagetypes "github.com/containers/storage/types" + "github.com/labring/sealos/pkg/utils/logger" v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/labring/sealos/pkg/utils/logger" ) type Interface interface { Pull(imageNames []string, opts ...FlagSetter) error - Load(input string, ociType string) (string, error) + Load(input, ociType string) (string, error) InspectImage(name string, opts ...string) (*InspectOutput, error) - Create(name string, image string, opts ...FlagSetter) (buildah.BuilderInfo, error) + Create(name, image string, opts ...FlagSetter) (buildah.BuilderInfo, error) Delete(name string) error InspectContainer(name string) (buildah.BuilderInfo, error) ListContainers() ([]JSONContainer, error) @@ -78,7 +78,7 @@ func (impl *realImpl) Runtime() *Runtime { type FlagSetter func(*pflag.FlagSet) error -func newFlagSetter(k string, v string) FlagSetter { +func newFlagSetter(k, v string) FlagSetter { return func(fs *pflag.FlagSet) error { if f := fs.Lookup(k); f != nil { return fs.Set(k, v) @@ -128,7 +128,10 @@ func (impl *realImpl) Pull(imageNames []string, opts ...FlagSetter) error { return nil } -func finalizeReference(transport types.ImageTransport, imgName string) (types.ImageTransport, string) { +func finalizeReference( + transport types.ImageTransport, + imgName string, +) (types.ImageTransport, string) { parts := strings.SplitN(imgName, ":", 2) if len(parts) == 2 { if transport := transports.Get(parts[0]); transport != nil { @@ -151,15 +154,15 @@ func (impl *realImpl) InspectImage(name string, opts ...string) (*InspectOutput, return openImage(ctx, impl.systemContext, impl.store, transport, name) } -func (impl *realImpl) Create(name string, image string, opts ...FlagSetter) (buildah.BuilderInfo, error) { +func (impl *realImpl) Create(name, image string, opts ...FlagSetter) (buildah.BuilderInfo, error) { if err := impl.Delete(impl.finalizeName(name)); err != nil { - return buildah.BuilderInfo{}, fmt.Errorf("failed to delete: %v", err) + return buildah.BuilderInfo{}, fmt.Errorf("failed to delete: %w", err) } if _, err := impl.from(impl.finalizeName(name), image, opts...); err != nil { - return buildah.BuilderInfo{}, fmt.Errorf("failed to from: %v", err) + return buildah.BuilderInfo{}, fmt.Errorf("failed to from: %w", err) } if _, err := impl.mount(impl.finalizeName(name)); err != nil { - return buildah.BuilderInfo{}, fmt.Errorf("failed to mount: %v", err) + return buildah.BuilderInfo{}, fmt.Errorf("failed to mount: %w", err) } return impl.InspectContainer(name) } @@ -167,7 +170,7 @@ func (impl *realImpl) Create(name string, image string, opts ...FlagSetter) (bui func (impl *realImpl) Delete(name string) error { builder, err := openBuilder(getContext(), impl.store, impl.finalizeName(name)) if err != nil { - if err == storagetypes.ErrContainerUnknown { + if errors.Is(err, storagetypes.ErrContainerUnknown) { return nil } return err @@ -226,7 +229,7 @@ func (impl *realImpl) ListContainers() ([]JSONContainer, error) { json: true, noHeading: true, } - params, err := parseCtrFilter(fmt.Sprintf("name=%s", impl.id)) + params, err := parseCtrFilter("name=" + impl.id) if err != nil { return nil, fmt.Errorf("parsing filter: %w", err) } @@ -234,7 +237,7 @@ func (impl *realImpl) ListContainers() ([]JSONContainer, error) { return jsonContainers, err } -func (impl *realImpl) Load(input string, transport string) (string, error) { +func (impl *realImpl) Load(input, transport string) (string, error) { ref := FormatReferenceWithTransportName(transport, input) names, err := impl.runtime.PullOrLoadImages(getContext(), []string{ref}, libimage.CopyOptions{}) if err != nil { diff --git a/lifecycle/pkg/buildah/interface_test.go b/lifecycle/pkg/buildah/interface_test.go index 47fbe51e75cb..78d2b12fed4b 100644 --- a/lifecycle/pkg/buildah/interface_test.go +++ b/lifecycle/pkg/buildah/interface_test.go @@ -111,7 +111,7 @@ func Test_realImpl_InspectImage(t *testing.T) { Use: "test", Short: "test", } - //storage-driver + // storage-driver RegisterRootCommand(root) impl, err := New("") if err != nil { diff --git a/lifecycle/pkg/buildah/internal/util/util.go b/lifecycle/pkg/buildah/internal/util/util.go index 3cdda1d09fc8..fe385c4dc619 100644 --- a/lifecycle/pkg/buildah/internal/util/util.go +++ b/lifecycle/pkg/buildah/internal/util/util.go @@ -38,7 +38,10 @@ func DecryptConfig(decryptionKeys []string) (*encconfig.DecryptConfig, error) { } // EncryptConfig translates encryptionKeys into a EncriptionsConfig structure -func EncryptConfig(encryptionKeys []string, encryptLayers []int) (*encconfig.EncryptConfig, *[]int, error) { +func EncryptConfig( + encryptionKeys []string, + encryptLayers []int, +) (*encconfig.EncryptConfig, *[]int, error) { var encLayers *[]int var encConfig *encconfig.EncryptConfig diff --git a/lifecycle/pkg/buildah/load.go b/lifecycle/pkg/buildah/load.go index c2ce2d54d6ed..43656c1d22c1 100644 --- a/lifecycle/pkg/buildah/load.go +++ b/lifecycle/pkg/buildah/load.go @@ -37,12 +37,18 @@ type loadOptions struct { } func (o *loadOptions) RegisterFlags(fs *pflag.FlagSet) { - fs.StringVarP(&o.input, "input", "i", "", "load images from specified tar archive file, default(stdin)") + fs.StringVarP( + &o.input, + "input", + "i", + "", + "load images from specified tar archive file, default(stdin)", + ) fs.BoolVarP(&o.quiet, "quiet", "q", false, "suppress the output") } func newLoadCommand() *cobra.Command { - var opts = &loadOptions{} + opts := &loadOptions{} loadCommand := &cobra.Command{ Use: "load", @@ -50,7 +56,7 @@ func newLoadCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return load(cmd, args, opts) }, - Example: fmt.Sprintf(`%[1]s load -i kubernetes.tar`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " load -i kubernetes.tar", } loadCommand.SetUsageTemplate(UsageTemplate()) opts.RegisterFlags(loadCommand.Flags()) @@ -61,7 +67,8 @@ func newLoadCommand() *cobra.Command { func load(cmd *cobra.Command, _ []string, loadOpts *loadOptions) error { if len(loadOpts.input) > 0 { // Download the input file if needed. - if strings.HasPrefix(loadOpts.input, "https://") || strings.HasPrefix(loadOpts.input, "http://") { + if strings.HasPrefix(loadOpts.input, "https://") || + strings.HasPrefix(loadOpts.input, "http://") { containerConfig, err := config.Default() if err != nil { return err @@ -87,14 +94,14 @@ func load(cmd *cobra.Command, _ []string, loadOpts *loadOptions) error { } outFile, err := os.CreateTemp("", rootCmd.Name()) if err != nil { - return fmt.Errorf("creating file %v", err) + return fmt.Errorf("creating file %w", err) } defer os.Remove(outFile.Name()) defer outFile.Close() _, err = io.Copy(outFile, os.Stdin) if err != nil { - return fmt.Errorf("copying file %v", err) + return fmt.Errorf("copying file %w", err) } loadOpts.input = outFile.Name() } diff --git a/lifecycle/pkg/buildah/login.go b/lifecycle/pkg/buildah/login.go index 7700a401970e..1c20bf7f00cf 100644 --- a/lifecycle/pkg/buildah/login.go +++ b/lifecycle/pkg/buildah/login.go @@ -22,11 +22,10 @@ import ( "github.com/containers/buildah/pkg/parse" "github.com/containers/common/pkg/auth" + fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/spf13/cobra" "github.com/spf13/pflag" "k8s.io/client-go/tools/clientcmd" - - fileutil "github.com/labring/sealos/pkg/utils/file" ) type loginReply struct { @@ -50,11 +49,27 @@ func newDefaultLoginReply() loginReply { func (opts *loginReply) RegisterFlags(fs *pflag.FlagSet) { fs.SetInterspersed(false) - fs.BoolVar(&opts.tlsVerify, "tls-verify", opts.tlsVerify, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - fs.BoolVar(&opts.getLogin, "get-login", opts.getLogin, "return the current login user for the registry") + fs.BoolVar( + &opts.tlsVerify, + "tls-verify", + opts.tlsVerify, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + fs.BoolVar( + &opts.getLogin, + "get-login", + opts.getLogin, + "return the current login user for the registry", + ) fs.AddFlagSet(auth.GetLoginFlags(&opts.loginOpts)) // e.g sealos login --kubeconfig /root/.kube/config hub.sealos.io - fs.StringVarP(&opts.kubeconfig, "kubeconfig", "k", opts.kubeconfig, "Login to sealos registry: hub.sealos.io by kubeconfig") + fs.StringVarP( + &opts.kubeconfig, + "kubeconfig", + "k", + opts.kubeconfig, + "Login to sealos registry: hub.sealos.io by kubeconfig", + ) bailOnError(markFlagsHidden(fs, "tls-verify"), "") } @@ -98,7 +113,13 @@ func newLoginCommand() *cobra.Command { if err != nil { return err } - sealosKubeConfdir := fmt.Sprintf("%s/%s/%s/%s", os.Getenv("HOME"), ".sealos", registryHost, opts.loginOpts.Username) + sealosKubeConfdir := fmt.Sprintf( + "%s/%s/%s/%s", + os.Getenv("HOME"), + ".sealos", + registryHost, + opts.loginOpts.Username, + ) if err := fileutil.MkDirs(sealosKubeConfdir); err != nil { return err } @@ -106,7 +127,7 @@ func newLoginCommand() *cobra.Command { // copy file, will overwrite the original file return fileutil.Copy(opts.kubeconfig, sealosKubeconfPath) }, - Example: fmt.Sprintf(`%s login quay.io`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " login quay.io", } loginCommand.SetUsageTemplate(UsageTemplate()) opts.RegisterFlags(loginCommand.Flags()) @@ -157,12 +178,12 @@ func parseRawURL(rawurl string) (domain string, err error) { u, repErr := url.ParseRequestURI("https://" + rawurl) if repErr != nil { fmt.Printf("Could not parse raw url: %s, error: %v", rawurl, err) - return + return domain, err } domain = u.Host err = nil - return + return domain, err } domain = u.Host - return + return domain, err } diff --git a/lifecycle/pkg/buildah/logout.go b/lifecycle/pkg/buildah/logout.go index 4c128a969d28..25b325d25530 100644 --- a/lifecycle/pkg/buildah/logout.go +++ b/lifecycle/pkg/buildah/logout.go @@ -39,7 +39,7 @@ func newLogoutCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return logoutCmd(cmd, args, &opts) }, - Example: fmt.Sprintf(`%s logout quay.io`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " logout quay.io", } logoutCommand.SetUsageTemplate(UsageTemplate()) diff --git a/lifecycle/pkg/buildah/manifest.go b/lifecycle/pkg/buildah/manifest.go index 4d964b74ae52..578ce9beb828 100644 --- a/lifecycle/pkg/buildah/manifest.go +++ b/lifecycle/pkg/buildah/manifest.go @@ -36,12 +36,11 @@ import ( "github.com/containers/image/v5/types" "github.com/containers/storage" "github.com/hashicorp/go-multierror" + "github.com/labring/sealos/pkg/utils/logger" digest "github.com/opencontainers/go-digest" imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/labring/sealos/pkg/utils/logger" ) type manifestCreateOpts struct { @@ -50,12 +49,42 @@ type manifestCreateOpts struct { } func (opts *manifestCreateOpts) RegisterFlags(fs *pflag.FlagSet) error { - fs.BoolVar(&opts.all, "all", false, "add all of the lists' images if the images to add are lists") - fs.BoolVar(&opts.amend, "amend", false, "modify an existing list if one with the desired name already exists") - fs.StringVar(&opts.os, "os", "", "if any of the specified images is a list, choose the one for `os`") - fs.StringVar(&opts.arch, "arch", "", "if any of the specified images is a list, choose the one for `arch`") - fs.BoolVar(&opts.insecure, "insecure", false, "neither require HTTPS nor verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - fs.BoolVar(&opts.tlsVerify, "tls-verify", false, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") + fs.BoolVar( + &opts.all, + "all", + false, + "add all of the lists' images if the images to add are lists", + ) + fs.BoolVar( + &opts.amend, + "amend", + false, + "modify an existing list if one with the desired name already exists", + ) + fs.StringVar( + &opts.os, + "os", + "", + "if any of the specified images is a list, choose the one for `os`", + ) + fs.StringVar( + &opts.arch, + "arch", + "", + "if any of the specified images is a list, choose the one for `arch`", + ) + fs.BoolVar( + &opts.insecure, + "insecure", + false, + "neither require HTTPS nor verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + fs.BoolVar( + &opts.tlsVerify, + "tls-verify", + false, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) fs.SetNormalizeFunc(cli.AliasFlags) return markFlagsHidden(fs, []string{"os", "arch", "insecure", "tls-verify"}...) } @@ -67,18 +96,58 @@ type manifestAddOpts struct { } func (opts *manifestAddOpts) RegisterFlags(fs *pflag.FlagSet) error { - fs.StringVar(&opts.authfile, "authfile", auth.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") - fs.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry") + fs.StringVar( + &opts.authfile, + "authfile", + auth.GetDefaultAuthFile(), + "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override", + ) + fs.StringVar( + &opts.certDir, + "cert-dir", + "", + "use certificates at the specified path to access the registry", + ) fs.StringVar(&opts.creds, "creds", "", "use `[username[:password]]` for accessing the registry") fs.StringVar(&opts.os, "os", "", "override the `OS` of the specified image") fs.StringVar(&opts.arch, "arch", "", "override the `architecture` of the specified image") fs.StringVar(&opts.variant, "variant", "", "override the `variant` of the specified image") - fs.StringVar(&opts.osVersion, "os-version", "", "override the OS `version` of the specified image") - fs.StringSliceVar(&opts.features, "features", nil, "override the `features` of the specified image") - fs.StringSliceVar(&opts.osFeatures, "os-features", nil, "override the OS `features` of the specified image") - fs.StringSliceVar(&opts.annotations, "annotation", nil, "set an `annotation` for the specified image") - fs.BoolVar(&opts.insecure, "insecure", false, "neither require HTTPS nor verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - fs.BoolVar(&opts.tlsVerify, "tls-verify", false, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") + fs.StringVar( + &opts.osVersion, + "os-version", + "", + "override the OS `version` of the specified image", + ) + fs.StringSliceVar( + &opts.features, + "features", + nil, + "override the `features` of the specified image", + ) + fs.StringSliceVar( + &opts.osFeatures, + "os-features", + nil, + "override the OS `features` of the specified image", + ) + fs.StringSliceVar( + &opts.annotations, + "annotation", + nil, + "set an `annotation` for the specified image", + ) + fs.BoolVar( + &opts.insecure, + "insecure", + false, + "neither require HTTPS nor verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + fs.BoolVar( + &opts.tlsVerify, + "tls-verify", + false, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) fs.BoolVar(&opts.all, "all", false, "add all of the list's images if the image is a list") fs.SetNormalizeFunc(cli.AliasFlags) return markFlagsHidden(fs, []string{"insecure", "tls-verify"}...) @@ -95,10 +164,30 @@ func (opts *manifestAnnotateOpts) RegisterFlags(fs *pflag.FlagSet) error { fs.StringVar(&opts.os, "os", "", "override the `OS` of the specified image") fs.StringVar(&opts.arch, "arch", "", "override the `Architecture` of the specified image") fs.StringVar(&opts.variant, "variant", "", "override the `Variant` of the specified image") - fs.StringVar(&opts.osVersion, "os-version", "", "override the os `version` of the specified image") - fs.StringSliceVar(&opts.features, "features", nil, "override the `features` of the specified image") - fs.StringSliceVar(&opts.osFeatures, "os-features", nil, "override the os `features` of the specified image") - fs.StringSliceVar(&opts.annotations, "annotation", nil, "set an `annotation` for the specified image") + fs.StringVar( + &opts.osVersion, + "os-version", + "", + "override the os `version` of the specified image", + ) + fs.StringSliceVar( + &opts.features, + "features", + nil, + "override the `features` of the specified image", + ) + fs.StringSliceVar( + &opts.osFeatures, + "os-features", + nil, + "override the os `features` of the specified image", + ) + fs.StringSliceVar( + &opts.annotations, + "annotation", + nil, + "set an `annotation` for the specified image", + ) return nil } @@ -177,21 +266,19 @@ func newManifestCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return manifestRemoveCmd(cmd, args, manifestRemoveOpts) }, - Example: fmt.Sprintf(`%s manifest remove mylist:v1.11 sha256:15352d97781ffdf357bf3459c037be3efac4133dc9070c2dce7eca7c05c3e736`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " manifest remove mylist:v1.11 sha256:15352d97781ffdf357bf3459c037be3efac4133dc9070c2dce7eca7c05c3e736", Args: cobra.MinimumNArgs(2), } manifestRemoveCommand.SetUsageTemplate(UsageTemplate()) manifestCommand.AddCommand(manifestRemoveCommand) manifestExistsCommand := &cobra.Command{ - Use: "exists", - Short: "Check if a manifest list exists in local storage", - Long: manifestExistsDescription, - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - return manifestExistsCmd(cmd, args) - }, - Example: fmt.Sprintf(`%s manifest exists mylist`, rootCmd.CommandPath()), + Use: "exists", + Short: "Check if a manifest list exists in local storage", + Long: manifestExistsDescription, + Args: cobra.ExactArgs(1), + RunE: manifestExistsCmd, + Example: rootCmd.CommandPath() + " manifest exists mylist", } manifestExistsCommand.SetUsageTemplate(UsageTemplate()) manifestCommand.AddCommand(manifestExistsCommand) @@ -203,7 +290,7 @@ func newManifestCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return manifestAnnotateCmd(cmd, args, manifestAnnotateOpts) }, - Example: fmt.Sprintf(`%s manifest annotate --annotation left=right mylist:v1.11 image:v1.11-amd64`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " manifest annotate --annotation left=right mylist:v1.11 image:v1.11-amd64", Args: cobra.MinimumNArgs(2), } manifestAnnotateCommand.SetUsageTemplate(UsageTemplate()) @@ -218,7 +305,7 @@ func newManifestCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return manifestInspectCmd(cmd, args, manifestInspectOpts) }, - Example: fmt.Sprintf(`%s manifest inspect mylist:v1.11`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " manifest inspect mylist:v1.11", Args: cobra.MinimumNArgs(1), } manifestInspectCommand.SetUsageTemplate(UsageTemplate()) @@ -231,36 +318,92 @@ func newManifestCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return manifestPushCmd(cmd, args, manifestPushOpts) }, - Example: fmt.Sprintf(`%s manifest push mylist:v1.11 transport:imageName`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " manifest push mylist:v1.11 transport:imageName", Args: cobra.MinimumNArgs(2), } manifestPushCommand.SetUsageTemplate(UsageTemplate()) fs := manifestPushCommand.Flags() fs.BoolVar(&manifestPushOpts.rm, "rm", false, "remove the manifest list if push succeeds") fs.BoolVar(&manifestPushOpts.all, "all", false, "also push the images in the list") - fs.StringVar(&manifestPushOpts.authfile, "authfile", auth.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") - fs.StringVar(&manifestPushOpts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry") - fs.StringVar(&manifestPushOpts.creds, "creds", "", "use `[username[:password]]` for accessing the registry") - fs.StringVar(&manifestPushOpts.digestfile, "digestfile", "", "after copying the image, write the digest of the resulting digest to the file") - fs.StringVarP(&manifestPushOpts.format, "format", "f", "", "manifest type (oci or v2s2) to attempt to use when pushing the manifest list (default is manifest type of source)") - fs.BoolVarP(&manifestPushOpts.removeSignatures, "remove-signatures", "", false, "don't copy signatures when pushing images") - fs.StringVar(&manifestPushOpts.signBy, "sign-by", "", "sign the image using a GPG key with the specified `FINGERPRINT`") - fs.StringVar(&manifestPushOpts.signaturePolicy, "signature-policy", "", "`pathname` of signature policy file (not usually used)") - fs.BoolVar(&manifestPushOpts.insecure, "insecure", false, "neither require HTTPS nor verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - fs.BoolVar(&manifestPushOpts.tlsVerify, "tls-verify", false, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - fs.BoolVarP(&manifestPushOpts.quiet, "quiet", "q", false, "don't output progress information when pushing lists") + fs.StringVar( + &manifestPushOpts.authfile, + "authfile", + auth.GetDefaultAuthFile(), + "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override", + ) + fs.StringVar( + &manifestPushOpts.certDir, + "cert-dir", + "", + "use certificates at the specified path to access the registry", + ) + fs.StringVar( + &manifestPushOpts.creds, + "creds", + "", + "use `[username[:password]]` for accessing the registry", + ) + fs.StringVar( + &manifestPushOpts.digestfile, + "digestfile", + "", + "after copying the image, write the digest of the resulting digest to the file", + ) + fs.StringVarP( + &manifestPushOpts.format, + "format", + "f", + "", + "manifest type (oci or v2s2) to attempt to use when pushing the manifest list (default is manifest type of source)", + ) + fs.BoolVarP( + &manifestPushOpts.removeSignatures, + "remove-signatures", + "", + false, + "don't copy signatures when pushing images", + ) + fs.StringVar( + &manifestPushOpts.signBy, + "sign-by", + "", + "sign the image using a GPG key with the specified `FINGERPRINT`", + ) + fs.StringVar( + &manifestPushOpts.signaturePolicy, + "signature-policy", + "", + "`pathname` of signature policy file (not usually used)", + ) + fs.BoolVar( + &manifestPushOpts.insecure, + "insecure", + false, + "neither require HTTPS nor verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + fs.BoolVar( + &manifestPushOpts.tlsVerify, + "tls-verify", + false, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + fs.BoolVarP( + &manifestPushOpts.quiet, + "quiet", + "q", + false, + "don't output progress information when pushing lists", + ) fs.SetNormalizeFunc(cli.AliasFlags) bailOnError(markFlagsHidden(fs, "signature-policy", "insecure", "tls-verify"), "") manifestCommand.AddCommand(manifestPushCommand) manifestRmCommand := &cobra.Command{ - Use: "rm", - Short: "Remove manifest list or image index", - Long: manifestRmDescription, - RunE: func(cmd *cobra.Command, args []string) error { - return manifestRmCmd(cmd, args) - }, - Example: fmt.Sprintf(`%s manifest rm mylist:v1.11`, rootCmd.CommandPath()), + Use: "rm", + Short: "Remove manifest list or image index", + Long: manifestRmDescription, + RunE: manifestRmCmd, + Example: rootCmd.CommandPath() + " manifest rm mylist:v1.11", Args: cobra.MinimumNArgs(1), } manifestRmCommand.SetUsageTemplate(UsageTemplate()) @@ -283,7 +426,10 @@ func manifestExistsCmd(c *cobra.Command, args []string) error { if err != nil { return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -316,7 +462,10 @@ func manifestCreateCmd(c *cobra.Command, args []string, opts manifestCreateOpts) if err != nil { return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -344,7 +493,10 @@ func manifestCreateCmd(c *cobra.Command, args []string, opts manifestCreateOpts) break } if list == nil { - return fmt.Errorf("--amend specified but no matching manifest list found with name %q", listImageSpec) + return fmt.Errorf( + "--amend specified but no matching manifest list found with name %q", + listImageSpec, + ) } } else { return err @@ -387,8 +539,10 @@ func manifestAddCmd(c *cobra.Command, args []string, opts manifestAddOpts) error return err } - listImageSpec := "" - imageSpec := "" + var ( + listImageSpec string + imageSpec string + ) switch len(args) { case 0, 1: return errors.New("at least a list image and an image to add must be specified") @@ -414,7 +568,10 @@ func manifestAddCmd(c *cobra.Command, args []string, opts manifestAddOpts) error if err != nil { return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -507,11 +664,13 @@ func manifestAddCmd(c *cobra.Command, args []string, opts manifestAddOpts) error } func manifestRemoveCmd(c *cobra.Command, args []string, _ manifestRemoveOpts) error { - listImageSpec := "" + var listImageSpec string var instanceDigest digest.Digest switch len(args) { case 0, 1: - return errors.New("at least a list image and one or more instance digests must be specified") + return errors.New( + "at least a list image and one or more instance digests must be specified", + ) case 2: listImageSpec = args[0] if listImageSpec == "" { @@ -523,11 +682,13 @@ func manifestRemoveCmd(c *cobra.Command, args []string, _ manifestRemoveOpts) er } d, err := digest.Parse(instanceSpec) if err != nil { - return fmt.Errorf(`invalid instance "%s": %v`, args[1], err) + return fmt.Errorf(`invalid instance "%s": %w`, args[1], err) } instanceDigest = d default: - return errors.New("at least two arguments are necessary: list and digest of instance to remove from list") + return errors.New( + "at least two arguments are necessary: list and digest of instance to remove from list", + ) } store, err := getStore(c) @@ -540,7 +701,10 @@ func manifestRemoveCmd(c *cobra.Command, args []string, _ manifestRemoveOpts) er return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -569,7 +733,10 @@ func manifestRmCmd(c *cobra.Command, args []string) error { return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -596,7 +763,7 @@ func manifestRmCmd(c *cobra.Command, args []string) error { } func manifestAnnotateCmd(c *cobra.Command, args []string, opts manifestAnnotateOpts) error { - listImageSpec := "" + var listImageSpec string imageSpec := "" switch len(args) { case 0: @@ -628,7 +795,10 @@ func manifestAnnotateCmd(c *cobra.Command, args []string, opts manifestAnnotateO if err != nil { return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -718,7 +888,7 @@ func manifestAnnotateCmd(c *cobra.Command, args []string, opts manifestAnnotateO } func manifestInspectCmd(c *cobra.Command, args []string, _ manifestInspectOpts) error { - imageSpec := "" + var imageSpec string switch len(args) { case 0: return errors.New("at least a source list ID must be specified") @@ -744,8 +914,16 @@ func manifestInspectCmd(c *cobra.Command, args []string, _ manifestInspectOpts) return manifestInspect(getContext(), store, systemContext, imageSpec) } -func manifestInspect(ctx context.Context, store storage.Store, systemContext *types.SystemContext, imageSpec string) error { - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) +func manifestInspect( + ctx context.Context, + store storage.Store, + systemContext *types.SystemContext, + imageSpec string, +) error { + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -807,7 +985,7 @@ func manifestInspect(ctx context.Context, store storage.Store, systemContext *ty if latestErr == nil { latestErr = e } else { - latestErr = fmt.Errorf("tried %v: %w", e, latestErr) + latestErr = fmt.Errorf("tried %w: %w", e, latestErr) } } @@ -849,8 +1027,10 @@ func manifestPushCmd(c *cobra.Command, args []string, opts pushOptions) error { return err } - listImageSpec := "" - destSpec := "" + var ( + listImageSpec string + destSpec string + ) switch len(args) { case 0: return errors.New("at least a source list ID must be specified") @@ -881,8 +1061,16 @@ func manifestPushCmd(c *cobra.Command, args []string, opts pushOptions) error { return manifestPush(systemContext, store, listImageSpec, destSpec, opts) } -func manifestPush(systemContext *types.SystemContext, store storage.Store, listImageSpec, destSpec string, opts pushOptions) error { - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) +func manifestPush( + systemContext *types.SystemContext, + store storage.Store, + listImageSpec, destSpec string, + opts pushOptions, +) error { + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } @@ -910,7 +1098,10 @@ func manifestPush(systemContext *types.SystemContext, store storage.Store, listI case "v2s2", "docker": manifestType = manifest.DockerV2Schema2MediaType default: - return fmt.Errorf("unknown format %q. Choose on of the supported formats: 'oci' or 'v2s2'", opts.format) + return fmt.Errorf( + "unknown format %q. Choose on of the supported formats: 'oci' or 'v2s2'", + opts.format, + ) } } @@ -937,8 +1128,11 @@ func manifestPush(systemContext *types.SystemContext, store storage.Store, listI } if opts.digestfile != "" { - if err = os.WriteFile(opts.digestfile, []byte(digest.String()), 0644); err != nil { - return util.GetFailureCause(err, fmt.Errorf("failed to write digest to file %q: %w", opts.digestfile, err)) + if err = os.WriteFile(opts.digestfile, []byte(digest.String()), 0o644); err != nil { + return util.GetFailureCause( + err, + fmt.Errorf("failed to write digest to file %q: %w", opts.digestfile, err), + ) } } diff --git a/lifecycle/pkg/buildah/merge.go b/lifecycle/pkg/buildah/merge.go index 75878b90d051..5f24f71d4c8f 100644 --- a/lifecycle/pkg/buildah/merge.go +++ b/lifecycle/pkg/buildah/merge.go @@ -17,23 +17,21 @@ limitations under the License. package buildah import ( + "errors" "fmt" "os" "path" "strings" - "github.com/labring/sealos/pkg/image" - - "github.com/containers/buildah/pkg/parse" - "github.com/containers/buildah" buildahcli "github.com/containers/buildah/pkg/cli" + "github.com/containers/buildah/pkg/parse" "github.com/containers/buildah/util" - v1 "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/spf13/cobra" - + "github.com/labring/sealos/pkg/image" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/rand" + v1 "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/spf13/cobra" ) func newMergeCommand() *cobra.Command { @@ -65,7 +63,7 @@ func newMergeCommand() *cobra.Command { if flagChanged(cmd, "platform") { platformFlags := getPlatformFromFlags(cmd) if len(platformFlags) != 1 { - return fmt.Errorf("only one platform is allowed") + return errors.New("only one platform is allowed") } oss, arch, variant, err := parse.Platform(platformFlags[0]) if err != nil { @@ -111,9 +109,18 @@ func newMergeCommand() *cobra.Command { // build is a all common flags buildFlags := buildahcli.GetBudFlags(&buildFlagResults) - buildFlags.StringVar(&buildFlagResults.Runtime, "runtime", util.Runtime(), "`path` to an alternate runtime. Use BUILDAH_RUNTIME environment variable to override.") + buildFlags.StringVar( + &buildFlagResults.Runtime, + "runtime", + util.Runtime(), + "`path` to an alternate runtime. Use BUILDAH_RUNTIME environment variable to override.", + ) layerFlags := buildahcli.GetLayerFlags(&layerFlagsResults) - fromAndBudFlags, err := buildahcli.GetFromAndBudFlags(&fromAndBudResults, &userNSResults, &namespaceResults) + fromAndBudFlags, err := buildahcli.GetFromAndBudFlags( + &fromAndBudResults, + &userNSResults, + &namespaceResults, + ) bailOnError(err, "failed to setup From and Build flags") sopts.RegisterFlags(flags) @@ -123,11 +130,20 @@ func newMergeCommand() *cobra.Command { flags.SetNormalizeFunc(buildahcli.AliasFlags) bailOnError(markFlagsHidden(flags, "save-image"), "") bailOnError(markFlagsHidden(flags, "tls-verify"), "") - bailOnError(markFlagsHidden(flags, append(flagsInBuildCommandToBeHidden(), flagsAssociatedWithPlatform()...)...), "") + bailOnError( + markFlagsHidden( + flags, + append(flagsInBuildCommandToBeHidden(), flagsAssociatedWithPlatform()...)...), + "", + ) return mergeCommand } -func mergeImagesWithScratchContainer(newImageName string, images []string, setters []FlagSetter) (*buildah.BuilderInfo, error) { +func mergeImagesWithScratchContainer( + newImageName string, + images []string, + setters []FlagSetter, +) (*buildah.BuilderInfo, error) { b, err := New("") if err != nil { return nil, err @@ -158,7 +174,7 @@ func mergeImagesWithScratchContainer(newImageName string, images []string, sette return nil, err } mergeDir := bInfo.MountPoint - err = os.WriteFile(path.Join(mergeDir, "Sealfile"), []byte(dockerfile), 0755) + err = os.WriteFile(path.Join(mergeDir, "Sealfile"), []byte(dockerfile), 0o755) if err != nil { return nil, err } diff --git a/lifecycle/pkg/buildah/mount.go b/lifecycle/pkg/buildah/mount.go index b53eb6f2c579..46d09bb3c46f 100644 --- a/lifecycle/pkg/buildah/mount.go +++ b/lifecycle/pkg/buildah/mount.go @@ -35,11 +35,9 @@ type mountOptions struct { func newMountCommand() *cobra.Command { var ( - mountDescription = fmt.Sprintf(`%[1]s mount - mounts a working container's root filesystem for manipulation. -`, rootCmd.CommandPath()) - opts mountOptions - noTruncate bool + mountDescription = rootCmd.CommandPath() + " mount\n mounts a working container's root filesystem for manipulation.\n" + opts mountOptions + noTruncate bool ) mountCommand := &cobra.Command{ Use: "mount", @@ -100,7 +98,10 @@ func doMounts(store storage.Store, args []string) ([]jsonMount, error) { // Differently, allow the mount if we are already in a userns, as the mount point will still // be accessible once "buildah mount" exits. if os.Geteuid() != 0 && store.GraphDriverName() != "vfs" { - return nil, fmt.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", store.GraphDriverName()) + return nil, fmt.Errorf( + "cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", + store.GraphDriverName(), + ) } for _, name := range args { diff --git a/lifecycle/pkg/buildah/pull.go b/lifecycle/pkg/buildah/pull.go index 2f1b90460092..305328f4b729 100644 --- a/lifecycle/pkg/buildah/pull.go +++ b/lifecycle/pkg/buildah/pull.go @@ -29,11 +29,10 @@ import ( "github.com/containers/common/pkg/auth" "github.com/containers/image/v5/types" "github.com/containers/storage" - "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/buildah/internal/util" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type pullOptions struct { @@ -75,23 +74,105 @@ func newDefaultPullOptions() *pullOptions { func (opts *pullOptions) RegisterFlags(fs *pflag.FlagSet) error { fs.SetInterspersed(false) - fs.BoolVarP(&opts.allTags, "all-tags", "a", opts.allTags, "download all tagged images in the repository") - fs.StringVar(&opts.authfile, "authfile", opts.authfile, "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") - fs.StringVar(&opts.blobCache, "blob-cache", opts.blobCache, "store copies of pulled image blobs in the specified directory") - fs.StringVar(&opts.certDir, "cert-dir", opts.certDir, "use certificates at the specified path to access the registry") - fs.StringVar(&opts.creds, "creds", opts.creds, "use `[username[:password]]` for accessing the registry") + fs.BoolVarP( + &opts.allTags, + "all-tags", + "a", + opts.allTags, + "download all tagged images in the repository", + ) + fs.StringVar( + &opts.authfile, + "authfile", + opts.authfile, + "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override", + ) + fs.StringVar( + &opts.blobCache, + "blob-cache", + opts.blobCache, + "store copies of pulled image blobs in the specified directory", + ) + fs.StringVar( + &opts.certDir, + "cert-dir", + opts.certDir, + "use certificates at the specified path to access the registry", + ) + fs.StringVar( + &opts.creds, + "creds", + opts.creds, + "use `[username[:password]]` for accessing the registry", + ) fs.StringVar(&opts.pullPolicy, "policy", opts.pullPolicy, "missing, always, or never.") - fs.BoolVar(&opts.removeSignatures, "remove-signatures", opts.removeSignatures, "don't copy signatures when pulling image") - fs.StringVar(&opts.signaturePolicy, "signature-policy", opts.signaturePolicy, "`pathname` of signature policy file (not usually used)") - fs.StringSliceVar(&opts.decryptionKeys, "decryption-key", opts.decryptionKeys, "key needed to decrypt the image") - fs.BoolVarP(&opts.quiet, "quiet", "q", opts.quiet, "don't output progress information when pulling images") - fs.StringVar(&opts.os, "os", opts.os, "prefer `OS` instead of the running OS for choosing images") - fs.StringVar(&opts.arch, "arch", opts.arch, "prefer `ARCH` instead of the architecture of the machine for choosing images") - fs.StringSliceVar(&opts.platform, "platform", opts.platform, "prefer OS/ARCH instead of the current operating system and architecture for choosing images") - fs.StringVar(&opts.variant, "variant", opts.variant, "override the `variant` of the specified image") - fs.BoolVar(&opts.tlsVerify, "tls-verify", opts.tlsVerify, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") - fs.IntVar(&opts.retry, "retry", opts.retry, "number of times to retry in case of failure when performing pull") - fs.DurationVar(&opts.retryDelay, "retry-delay", opts.retryDelay, "delay between retries in case of pull failures") + fs.BoolVar( + &opts.removeSignatures, + "remove-signatures", + opts.removeSignatures, + "don't copy signatures when pulling image", + ) + fs.StringVar( + &opts.signaturePolicy, + "signature-policy", + opts.signaturePolicy, + "`pathname` of signature policy file (not usually used)", + ) + fs.StringSliceVar( + &opts.decryptionKeys, + "decryption-key", + opts.decryptionKeys, + "key needed to decrypt the image", + ) + fs.BoolVarP( + &opts.quiet, + "quiet", + "q", + opts.quiet, + "don't output progress information when pulling images", + ) + fs.StringVar( + &opts.os, + "os", + opts.os, + "prefer `OS` instead of the running OS for choosing images", + ) + fs.StringVar( + &opts.arch, + "arch", + opts.arch, + "prefer `ARCH` instead of the architecture of the machine for choosing images", + ) + fs.StringSliceVar( + &opts.platform, + "platform", + opts.platform, + "prefer OS/ARCH instead of the current operating system and architecture for choosing images", + ) + fs.StringVar( + &opts.variant, + "variant", + opts.variant, + "override the `variant` of the specified image", + ) + fs.BoolVar( + &opts.tlsVerify, + "tls-verify", + opts.tlsVerify, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) + fs.IntVar( + &opts.retry, + "retry", + opts.retry, + "number of times to retry in case of failure when performing pull", + ) + fs.DurationVar( + &opts.retryDelay, + "retry-delay", + opts.retryDelay, + "delay between retries in case of pull failures", + ) return markFlagsHidden(fs, opts.HiddenFlags()...) } @@ -157,7 +238,13 @@ func pullCmd(c *cobra.Command, args []string, iopts *pullOptions) error { return nil } -func doPull(c *cobra.Command, store storage.Store, systemContext *types.SystemContext, imageNames []string, iopts *pullOptions) ([]string, error) { +func doPull( + c *cobra.Command, + store storage.Store, + systemContext *types.SystemContext, + imageNames []string, + iopts *pullOptions, +) ([]string, error) { var err error if systemContext == nil { systemContext, err = parse.SystemContextFromOptions(c) @@ -195,7 +282,7 @@ func doPull(c *cobra.Command, store storage.Store, systemContext *types.SystemCo if iopts.quiet { options.ReportWriter = nil // Turns off logging output } - var ids []string + ids := make([]string, 0, len(imageNames)) for _, imageName := range imageNames { id, err := buildah.Pull(getContext(), imageName, options) if err != nil { diff --git a/lifecycle/pkg/buildah/push.go b/lifecycle/pkg/buildah/push.go index ff15f4889b56..41288d0a4873 100644 --- a/lifecycle/pkg/buildah/push.go +++ b/lifecycle/pkg/buildah/push.go @@ -32,12 +32,11 @@ import ( "github.com/containers/image/v5/transports" "github.com/containers/image/v5/transports/alltransports" "github.com/containers/storage" + iutil "github.com/labring/sealos/pkg/buildah/internal/util" + "github.com/labring/sealos/pkg/utils/logger" imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "github.com/spf13/pflag" - - iutil "github.com/labring/sealos/pkg/buildah/internal/util" - "github.com/labring/sealos/pkg/utils/logger" ) type pushOptions struct { @@ -75,25 +74,119 @@ func newDefaultPushOptions() *pushOptions { func (opts *pushOptions) RegisterFlags(fs *pflag.FlagSet) error { fs.SetInterspersed(false) fs.BoolVar(&opts.all, "all", opts.all, "push all of the images referenced by the manifest list") - fs.StringVar(&opts.authfile, "authfile", opts.authfile, "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") - fs.StringVar(&opts.blobCache, "blob-cache", opts.blobCache, "assume image blobs in the specified directory will be available for pushing") - fs.StringVar(&opts.certDir, "cert-dir", opts.certDir, "use certificates at the specified path to access the registry") - fs.StringVar(&opts.creds, "creds", opts.creds, "use `[username[:password]]` for accessing the registry") - fs.StringVar(&opts.digestfile, "digestfile", opts.digestfile, "after copying the image, write the digest of the resulting image to the file") - fs.BoolVarP(&opts.disableCompression, "disable-compression", "D", false, "don't compress layers") - fs.StringVarP(&opts.format, "format", "f", opts.format, "manifest type (oci, v2s1, or v2s2) to use in the destination (default is manifest type of source, with fallbacks)") - fs.StringVar(&opts.compressionFormat, "compression-format", opts.compressionFormat, "compression format to use") - fs.IntVar(&opts.compressionLevel, "compression-level", opts.compressionLevel, "compression level to use") - fs.BoolVarP(&opts.quiet, "quiet", "q", opts.quiet, "don't output progress information when pushing images") - fs.IntVar(&opts.retry, "retry", opts.retry, "number of times to retry in case of failure when performing push/pull") - fs.DurationVar(&opts.retryDelay, "retry-delay", opts.retryDelay, "delay between retries in case of push/pull failures") + fs.StringVar( + &opts.authfile, + "authfile", + opts.authfile, + "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override", + ) + fs.StringVar( + &opts.blobCache, + "blob-cache", + opts.blobCache, + "assume image blobs in the specified directory will be available for pushing", + ) + fs.StringVar( + &opts.certDir, + "cert-dir", + opts.certDir, + "use certificates at the specified path to access the registry", + ) + fs.StringVar( + &opts.creds, + "creds", + opts.creds, + "use `[username[:password]]` for accessing the registry", + ) + fs.StringVar( + &opts.digestfile, + "digestfile", + opts.digestfile, + "after copying the image, write the digest of the resulting image to the file", + ) + fs.BoolVarP( + &opts.disableCompression, + "disable-compression", + "D", + false, + "don't compress layers", + ) + fs.StringVarP( + &opts.format, + "format", + "f", + opts.format, + "manifest type (oci, v2s1, or v2s2) to use in the destination (default is manifest type of source, with fallbacks)", + ) + fs.StringVar( + &opts.compressionFormat, + "compression-format", + opts.compressionFormat, + "compression format to use", + ) + fs.IntVar( + &opts.compressionLevel, + "compression-level", + opts.compressionLevel, + "compression level to use", + ) + fs.BoolVarP( + &opts.quiet, + "quiet", + "q", + opts.quiet, + "don't output progress information when pushing images", + ) + fs.IntVar( + &opts.retry, + "retry", + opts.retry, + "number of times to retry in case of failure when performing push/pull", + ) + fs.DurationVar( + &opts.retryDelay, + "retry-delay", + opts.retryDelay, + "delay between retries in case of push/pull failures", + ) fs.BoolVar(&opts.rm, "rm", opts.rm, "remove the manifest list if push succeeds") - fs.BoolVarP(&opts.removeSignatures, "remove-signatures", "", opts.removeSignatures, "don't copy signatures when pushing image") - fs.StringVar(&opts.signBy, "sign-by", opts.signBy, "sign the image using a GPG key with the specified `FINGERPRINT`") - fs.StringVar(&opts.signaturePolicy, "signature-policy", opts.signaturePolicy, "`pathname` of signature policy file (not usually used)") - fs.StringSliceVar(&opts.encryptionKeys, "encryption-key", opts.encryptionKeys, "key with the encryption protocol to use needed to encrypt the image (e.g. jwe:/path/to/key.pem)") - fs.IntSliceVar(&opts.encryptLayers, "encrypt-layer", opts.encryptLayers, "layers to encrypt, 0-indexed layer indices with support for negative indexing (e.g. 0 is the first layer, -1 is the last layer). If not defined, will encrypt all layers if encryption-key flag is specified") - fs.BoolVar(&opts.tlsVerify, "tls-verify", opts.tlsVerify, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.") + fs.BoolVarP( + &opts.removeSignatures, + "remove-signatures", + "", + opts.removeSignatures, + "don't copy signatures when pushing image", + ) + fs.StringVar( + &opts.signBy, + "sign-by", + opts.signBy, + "sign the image using a GPG key with the specified `FINGERPRINT`", + ) + fs.StringVar( + &opts.signaturePolicy, + "signature-policy", + opts.signaturePolicy, + "`pathname` of signature policy file (not usually used)", + ) + fs.StringSliceVar( + &opts.encryptionKeys, + "encryption-key", + opts.encryptionKeys, + "key with the encryption protocol to use needed to encrypt the image (e.g. jwe:/path/to/key.pem)", + ) + fs.IntSliceVar( + &opts.encryptLayers, + "encrypt-layer", + opts.encryptLayers, + "layers to encrypt, 0-indexed layer indices with support for negative indexing (e.g. 0 is the first layer, -1 is the last layer). If not defined, will encrypt all layers if encryption-key flag is specified", + ) + fs.BoolVar( + &opts.tlsVerify, + "tls-verify", + opts.tlsVerify, + "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.", + ) return markFlagsHidden(fs, []string{"signature-policy", "blob-cache", "tls-verify"}...) } @@ -144,7 +237,10 @@ func pushCmd(c *cobra.Command, args []string, iopts *pushOptions) error { case 1: src = args[0] destSpec = src - logger.Debug("Destination argument not specified, assuming the same as the source: %s", destSpec) + logger.Debug( + "Destination argument not specified, assuming the same as the source: %s", + destSpec, + ) case 2: src = args[0] destSpec = args[1] @@ -203,7 +299,10 @@ func pushCmd(c *cobra.Command, args []string, iopts *pushOptions) error { case "v2s2", "docker": manifestType = manifest.DockerV2Schema2MediaType default: - return fmt.Errorf("unknown format %q. Choose on of the supported formats: 'oci', 'v2s1', or 'v2s2'", iopts.format) + return fmt.Errorf( + "unknown format %q. Choose on of the supported formats: 'oci', 'v2s1', or 'v2s2'", + iopts.format, + ) } } @@ -248,7 +347,10 @@ func pushCmd(c *cobra.Command, args []string, iopts *pushOptions) error { return nil } } - return util.GetFailureCause(err, fmt.Errorf("pushing image %q to %q: %w", src, destSpec, err)) + return util.GetFailureCause( + err, + fmt.Errorf("pushing image %q to %q: %w", src, destSpec, err), + ) } if ref != nil { logger.Debug("pushed image %q with digest %s", ref, digest.String()) @@ -256,11 +358,18 @@ func pushCmd(c *cobra.Command, args []string, iopts *pushOptions) error { logger.Debug("pushed image with digest %s", digest.String()) } - logger.Debug("Successfully pushed %s with digest %s", transports.ImageName(dest), digest.String()) + logger.Debug( + "Successfully pushed %s with digest %s", + transports.ImageName(dest), + digest.String(), + ) if iopts.digestfile != "" { - if err = os.WriteFile(iopts.digestfile, []byte(digest.String()), 0644); err != nil { - return util.GetFailureCause(err, fmt.Errorf("failed to write digest to file %q: %w", iopts.digestfile, err)) + if err = os.WriteFile(iopts.digestfile, []byte(digest.String()), 0o644); err != nil { + return util.GetFailureCause( + err, + fmt.Errorf("failed to write digest to file %q: %w", iopts.digestfile, err), + ) } } diff --git a/lifecycle/pkg/buildah/rm.go b/lifecycle/pkg/buildah/rm.go index 58c2cef89a74..b32d4a6ae438 100644 --- a/lifecycle/pkg/buildah/rm.go +++ b/lifecycle/pkg/buildah/rm.go @@ -59,7 +59,9 @@ func rmCmd(c *cobra.Command, args []string, iopts rmResults) error { return errors.New("container ID must be specified") } if len(args) > 0 && iopts.all { - return errors.New("when using the --all switch, you may not pass any containers names or IDs") + return errors.New( + "when using the --all switch, you may not pass any containers names or IDs", + ) } if err := buildahcli.VerifyFlagsArgsOrder(args); err != nil { @@ -81,7 +83,11 @@ func rmCmd(c *cobra.Command, args []string, iopts rmResults) error { for _, builder := range builders { id := builder.ContainerID if err = builder.Delete(); err != nil { - lastError = util.WriteError(os.Stderr, fmt.Errorf("%s %q: %w", delContainerErrStr, builder.Container, err), lastError) + lastError = util.WriteError( + os.Stderr, + fmt.Errorf("%s %q: %w", delContainerErrStr, builder.Container, err), + lastError, + ) continue } fmt.Printf("%s\n", id) diff --git a/lifecycle/pkg/buildah/rmi.go b/lifecycle/pkg/buildah/rmi.go index d647780b9eed..62b58e9358f7 100644 --- a/lifecycle/pkg/buildah/rmi.go +++ b/lifecycle/pkg/buildah/rmi.go @@ -55,7 +55,13 @@ func newRMICommand() *cobra.Command { flags.SetInterspersed(false) flags.BoolVarP(&opts.all, "all", "a", false, "remove all images") flags.BoolVarP(&opts.prune, "prune", "p", false, "prune dangling images") - flags.BoolVarP(&opts.force, "force", "f", false, "force removal of the image and any containers using the image") + flags.BoolVarP( + &opts.force, + "force", + "f", + false, + "force removal of the image and any containers using the image", + ) return rmiCommand } @@ -87,7 +93,10 @@ func rmiCmd(c *cobra.Command, args []string, iopts rmiOptions) error { if err != nil { return err } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } diff --git a/lifecycle/pkg/buildah/runtime.go b/lifecycle/pkg/buildah/runtime.go index 2cbf5338c658..221f42831107 100644 --- a/lifecycle/pkg/buildah/runtime.go +++ b/lifecycle/pkg/buildah/runtime.go @@ -35,8 +35,14 @@ type Runtime struct { *libimage.Runtime } -func getRuntimeWithStoreAndSystemContext(store storage.Store, sc *types.SystemContext) (*Runtime, error) { - libimageRuntime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: sc}) +func getRuntimeWithStoreAndSystemContext( + store storage.Store, + sc *types.SystemContext, +) (*Runtime, error) { + libimageRuntime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: sc}, + ) if err != nil { return nil, err } @@ -64,7 +70,7 @@ func getRuntime(c *cobra.Command) (*Runtime, error) { func (r *Runtime) getLayerID(id string, diffType DiffType) (string, error) { var lastErr error if diffType&DiffImage == DiffImage { - toImage, _, err := r.Runtime.LookupImage(id, nil) + toImage, _, err := r.LookupImage(id, nil) if err == nil { return toImage.TopLayer(), nil } @@ -72,7 +78,7 @@ func (r *Runtime) getLayerID(id string, diffType DiffType) (string, error) { } if diffType&DiffContainer == DiffContainer { - toCtr, err := r.Store.Container(id) + toCtr, err := r.Container(id) if err == nil { return toCtr.LayerID, nil } @@ -80,7 +86,7 @@ func (r *Runtime) getLayerID(id string, diffType DiffType) (string, error) { } if diffType == DiffAll { - toLayer, err := r.Store.Layer(id) + toLayer, err := r.Layer(id) if err == nil { return toLayer.ID, nil } @@ -89,12 +95,16 @@ func (r *Runtime) getLayerID(id string, diffType DiffType) (string, error) { return "", fmt.Errorf("%s not found: %w", id, lastErr) } -func (r *Runtime) PullOrLoadImages(ctx context.Context, args []string, options libimage.CopyOptions) ([]string, error) { +func (r *Runtime) PullOrLoadImages( + ctx context.Context, + args []string, + options libimage.CopyOptions, +) ([]string, error) { copyOpts := options if copyOpts.Writer == nil { copyOpts.Writer = os.Stderr } - var result []string + result := make([]string, 0, len(args)) for i := range args { name := args[i] tr, ref, err := parseTransportAndReference(imagestorage.Transport, name) @@ -106,9 +116,14 @@ func (r *Runtime) PullOrLoadImages(ctx context.Context, args []string, options l if tr.Name() == TransportDocker { ref = strings.TrimPrefix(ref, "//") } - pullImages, err := r.Runtime.Pull(ctx, ref, config.PullPolicyMissing, &libimage.PullOptions{ - CopyOptions: copyOpts, - }) + pullImages, err := r.Pull( + ctx, + ref, + config.PullPolicyMissing, + &libimage.PullOptions{ + CopyOptions: copyOpts, + }, + ) if err != nil { return nil, err } @@ -124,7 +139,7 @@ func (r *Runtime) PullOrLoadImages(ctx context.Context, args []string, options l } ref = filepath.Join(cwd, ref) } - images, err := r.Runtime.Load(ctx, ref, &libimage.LoadOptions{ + images, err := r.Load(ctx, ref, &libimage.LoadOptions{ CopyOptions: copyOpts, }) if err != nil { diff --git a/lifecycle/pkg/buildah/save.go b/lifecycle/pkg/buildah/save.go index 4fefda496d29..8f7c224af3aa 100644 --- a/lifecycle/pkg/buildah/save.go +++ b/lifecycle/pkg/buildah/save.go @@ -37,13 +37,40 @@ type saveOptions struct { } func (o *saveOptions) RegisterFlags(fs *pflag.FlagSet) { - fs.BoolVar(&o.compress, "compress", false, "compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)") + fs.BoolVar( + &o.compress, + "compress", + false, + "compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)", + ) fs.BoolVarP(&o.quiet, "quiet", "q", false, "suppress the output") - fs.BoolVarP(&o.multiImageArchive, "multi-image-archive", "m", false, "interpret additional arguments as images not tags and create a multi-image-archive (only for docker-archive)") - fs.BoolVar(&o.ociAcceptUncompressedLayers, "uncompressed", false, "Accept uncompressed layers when copying OCI images") - fs.StringVar(&o.format, "format", OCIArchive, "save image to oci-archive, oci-dir (directory with oci manifest type), "+ - "docker-archive, docker-dir (directory with v2s2 manifest type)") - fs.StringVarP(&o.output, "output", "o", "", "write to a specified file (default: stdout, which must be redirected)") + fs.BoolVarP( + &o.multiImageArchive, + "multi-image-archive", + "m", + false, + "interpret additional arguments as images not tags and create a multi-image-archive (only for docker-archive)", + ) + fs.BoolVar( + &o.ociAcceptUncompressedLayers, + "uncompressed", + false, + "Accept uncompressed layers when copying OCI images", + ) + fs.StringVar( + &o.format, + "format", + OCIArchive, + "save image to oci-archive, oci-dir (directory with oci manifest type), "+ + "docker-archive, docker-dir (directory with v2s2 manifest type)", + ) + fs.StringVarP( + &o.output, + "output", + "o", + "", + "write to a specified file (default: stdout, which must be redirected)", + ) } func (o *saveOptions) Validate() error { @@ -54,7 +81,7 @@ func (o *saveOptions) Validate() error { } func newSaveCommand() *cobra.Command { - var opts = &saveOptions{} + opts := &saveOptions{} saveCommand := &cobra.Command{ Use: "save", @@ -63,7 +90,7 @@ func newSaveCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runSave(cmd, args, opts) }, - Example: fmt.Sprintf(`%[1]s save -o kubernetes.tar labring/kubernetes:latest`, rootCmd.CommandPath()), + Example: rootCmd.CommandPath() + " save -o kubernetes.tar labring/kubernetes:latest", } saveCommand.SetUsageTemplate(UsageTemplate()) opts.RegisterFlags(saveCommand.Flags()) @@ -73,9 +100,7 @@ func newSaveCommand() *cobra.Command { } func runSave(cmd *cobra.Command, args []string, saveOpts *saveOptions) error { - var ( - tags []string - ) + var tags []string if flagChanged(cmd, "compress") && saveOpts.format != DockerManifestDir { return errors.New("--compress can only be set when --format is 'docker-dir'") } diff --git a/lifecycle/pkg/buildah/setup.go b/lifecycle/pkg/buildah/setup.go index bd01f8c09ce8..ebedd06d0d13 100644 --- a/lifecycle/pkg/buildah/setup.go +++ b/lifecycle/pkg/buildah/setup.go @@ -21,14 +21,12 @@ import ( "os/exec" "path/filepath" - "github.com/labring/sealos/pkg/system" - "github.com/containers/buildah/pkg/parse" "github.com/containers/common/pkg/config" "github.com/containers/storage/pkg/homedir" "github.com/containers/storage/pkg/unshare" "github.com/containers/storage/types" - + "github.com/labring/sealos/pkg/system" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" ) @@ -140,7 +138,7 @@ func setupStorageConfigFile() error { var content string if unshare.IsRootless() { runRoot := fmt.Sprintf("/run/user/%d", unshare.GetRootlessUID()) - if err := os.MkdirAll(runRoot, 0755); err != nil && errors.Is(err, os.ErrPermission) { + if err := os.MkdirAll(runRoot, 0o755); err != nil && errors.Is(err, os.ErrPermission) { // has not permission, then use cache home cacheHome, err := homedir.GetCacheHome() if err != nil { @@ -168,7 +166,10 @@ func writeFileIfNotExists(filename string, data []byte) error { } func determineIfRootlessPackagePresent() error { - deps := map[string][]string{"uidmap": {"newuidmap", "newgidmap"}, "fuse-overlayfs": {"fuse-overlayfs"}} + deps := map[string][]string{ + "uidmap": {"newuidmap", "newgidmap"}, + "fuse-overlayfs": {"fuse-overlayfs"}, + } if err := determineIfPackagePresent(deps); err != nil { return fmt.Errorf("%s or consider running in root mode", err.Error()) } @@ -179,7 +180,11 @@ func determineIfPackagePresent(deps map[string][]string) error { for pkg, executables := range deps { for i := range executables { if _, err := exec.LookPath(executables[i]); err != nil { - return fmt.Errorf("executable file '%s' not found in $PATH, install package '%s' first", executables[i], pkg) + return fmt.Errorf( + "executable file '%s' not found in $PATH, install package '%s' first", + executables[i], + pkg, + ) } } } diff --git a/lifecycle/pkg/buildah/tag.go b/lifecycle/pkg/buildah/tag.go index ecfd7e72e238..69b014ec470a 100644 --- a/lifecycle/pkg/buildah/tag.go +++ b/lifecycle/pkg/buildah/tag.go @@ -23,9 +23,7 @@ import ( ) func newTagCommand() *cobra.Command { - var ( - tagDescription = "\n Adds one or more additional names to locally-stored image." - ) + tagDescription := "\n Adds one or more additional names to locally-stored image." tagCommand := &cobra.Command{ Use: "tag", Short: "Add an additional name to a local image", @@ -49,7 +47,10 @@ func tagCmd(c *cobra.Command, args []string) error { if err != nil { return fmt.Errorf("building system context: %w", err) } - runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext}) + runtime, err := libimage.RuntimeFromStore( + store, + &libimage.RuntimeOptions{SystemContext: systemContext}, + ) if err != nil { return err } diff --git a/lifecycle/pkg/buildah/umount.go b/lifecycle/pkg/buildah/umount.go index a355bbfd42b8..c8e81b85d336 100644 --- a/lifecycle/pkg/buildah/umount.go +++ b/lifecycle/pkg/buildah/umount.go @@ -45,10 +45,8 @@ func newUmountCommand() *cobra.Command { } func umountCmd(c *cobra.Command, args []string) error { - umountAll := false - if flagChanged(c, "all") { - umountAll = true - } + umountAll := flagChanged(c, "all") + if len(args) == 0 && !umountAll { return errors.New("at least one container ID must be specified") } diff --git a/lifecycle/pkg/buildah/unshare.go b/lifecycle/pkg/buildah/unshare.go index f3d815e0c269..f64a34bd7090 100644 --- a/lifecycle/pkg/buildah/unshare.go +++ b/lifecycle/pkg/buildah/unshare.go @@ -25,9 +25,8 @@ import ( "github.com/containers/storage" "github.com/containers/storage/pkg/unshare" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func newUnshareCommand() *cobra.Command { @@ -48,7 +47,13 @@ func newUnshareCommand() *cobra.Command { unshareCommand.SetUsageTemplate(UsageTemplate()) flags := unshareCommand.Flags() flags.SetInterspersed(false) - flags.StringSliceVarP(&unshareMounts, "mount", "m", []string{}, "mount the specified containers (default [])") + flags.StringSliceVarP( + &unshareMounts, + "mount", + "m", + []string{}, + "mount the specified containers (default [])", + ) return unshareCommand } @@ -56,33 +61,45 @@ func unshareMount(store storage.Store, mounts []string) ([]string, func(), error if len(mounts) == 0 { return nil, nil, nil } - var mountedContainers, env []string + mountedContainers := make([]string, 0, len(mounts)) + env := make([]string, 0, len(mounts)) unmount := func() { for _, mounted := range mountedContainers { builder, err := openBuilder(getContext(), store, mounted) if err != nil { - fmt.Fprintln(os.Stderr, fmt.Errorf("loading information about build container %q: %w", mounted, err)) + fmt.Fprintln( + os.Stderr, + fmt.Errorf("loading information about build container %q: %w", mounted, err), + ) continue } err = builder.Unmount() if err != nil { - fmt.Fprintln(os.Stderr, fmt.Errorf("unmounting build container %q: %w", mounted, err)) + fmt.Fprintln( + os.Stderr, + fmt.Errorf("unmounting build container %q: %w", mounted, err), + ) continue } } } for _, mountSpec := range mounts { - mount := strings.SplitN(mountSpec, "=", 2) - container := mountSpec - envVar := container - if len(mount) == 2 { - envVar = mount[0] - container = mount[1] + parts := strings.SplitN(mountSpec, "=", 2) + var envVar, container string + if len(parts) == 2 { + envVar, container = parts[0], parts[1] + } else { + envVar = mountSpec + container = mountSpec } builder, err := openBuilder(getContext(), store, container) if err != nil { unmount() - return nil, nil, fmt.Errorf("loading information about build container %q: %w", container, err) + return nil, nil, fmt.Errorf( + "loading information about build container %q: %w", + container, + err, + ) } mountPoint, err := builder.Mount(builder.MountLabel) if err != nil { @@ -127,7 +144,7 @@ func unshareCmd(c *cobra.Command, args []string) error { bailOnError(err, "cannot get store") // Temporary ignore it... // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command - cmd := exec.Command(args[0], args[1:]...) + cmd := exec.Command(args[0], args[1:]...) // #nosec G204 cmd.Env = unshare.RootlessEnv() cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout diff --git a/lifecycle/pkg/buildah/util.go b/lifecycle/pkg/buildah/util.go index 2832e74376f9..e7b2f1a3bc9f 100644 --- a/lifecycle/pkg/buildah/util.go +++ b/lifecycle/pkg/buildah/util.go @@ -16,7 +16,6 @@ package buildah import ( "github.com/containers/common/libimage" - "github.com/labring/sealos/pkg/utils/file" ) diff --git a/lifecycle/pkg/cert/cert.go b/lifecycle/pkg/cert/cert.go index 0cc3bb01c423..80db7f50afd6 100644 --- a/lifecycle/pkg/cert/cert.go +++ b/lifecycle/pkg/cert/cert.go @@ -57,7 +57,7 @@ type Config struct { CAName string // root ca map key CommonName string Organization []string - Year time.Duration + Year int AltNames AltNames Usages []x509.ExtKeyUsage } @@ -80,7 +80,12 @@ func NewPrivateKey(keyType x509.PublicKeyAlgorithm) (crypto.Signer, error) { } // NewSelfSignedCACert creates a CA certificate -func NewSelfSignedCACert(key crypto.Signer, commonName string, organization []string, year time.Duration) (*x509.Certificate, error) { +func NewSelfSignedCACert( + key crypto.Signer, + commonName string, + organization []string, + years int, +) (*x509.Certificate, error) { now := time.Now() tmpl := x509.Certificate{ SerialNumber: new(big.Int).SetInt64(0), @@ -89,7 +94,7 @@ func NewSelfSignedCACert(key crypto.Signer, commonName string, organization []st Organization: organization, }, NotBefore: now.UTC(), - NotAfter: now.Add(duration365d * year).UTC(), + NotAfter: now.Add(duration365d * time.Duration(years)).UTC(), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, BasicConstraintsValid: true, IsCA: true, @@ -111,11 +116,14 @@ func NewCaCertAndKey(cfg Config) (*x509.Certificate, crypto.Signer, error) { key, err := NewPrivateKey(x509.UnknownPublicKeyAlgorithm) if err != nil { - return nil, nil, fmt.Errorf("unable to create private key while generating CA certificate %s", err) + return nil, nil, fmt.Errorf( + "unable to create private key while generating CA certificate %w", + err, + ) } cert, err := NewSelfSignedCACert(key, cfg.CommonName, cfg.Organization, cfg.Year) if err != nil { - return nil, nil, fmt.Errorf("unable to create ca cert %s", err) + return nil, nil, fmt.Errorf("unable to create ca cert %w", err) } return cert, key, nil } @@ -140,7 +148,7 @@ func TryLoadKeyFromDisk(pkiPath string) (crypto.Signer, error) { // Parse the private key from a file privKey, err := keyutil.PrivateKeyFromFile(pkiPath) if err != nil { - return nil, fmt.Errorf("couldn't load the private key file %s", err) + return nil, fmt.Errorf("couldn't load the private key file %w", err) } // Allow RSA and ECDSA formats only @@ -151,28 +159,40 @@ func TryLoadKeyFromDisk(pkiPath string) (crypto.Signer, error) { case *ecdsa.PrivateKey: key = k default: - return nil, fmt.Errorf("couldn't convert the private key file %s", err) + return nil, fmt.Errorf("couldn't convert the private key file %w", err) } return key, nil } // NewCaCertAndKeyFromRoot cmd/kubeadm/app/util/pkiutil/pki_helpers.go NewCertAndKey -func NewCaCertAndKeyFromRoot(cfg Config, caCert *x509.Certificate, caKey crypto.Signer) (*x509.Certificate, crypto.Signer, error) { +func NewCaCertAndKeyFromRoot( + cfg Config, + caCert *x509.Certificate, + caKey crypto.Signer, +) (*x509.Certificate, crypto.Signer, error) { key, err := NewPrivateKey(x509.UnknownPublicKeyAlgorithm) if err != nil { - return nil, nil, fmt.Errorf("unable to create private key while generating CA certificate %s", err) + return nil, nil, fmt.Errorf( + "unable to create private key while generating CA certificate %w", + err, + ) } cert, err := NewSignedCert(cfg, key, caCert, caKey) if err != nil { - return nil, nil, fmt.Errorf("new signed cert failed %s", err) + return nil, nil, fmt.Errorf("new signed cert failed %w", err) } return cert, key, nil } // NewSignedCert creates a signed certificate using the given CA certificate and key -func NewSignedCert(cfg Config, key crypto.Signer, caCert *x509.Certificate, caKey crypto.Signer) (*x509.Certificate, error) { +func NewSignedCert( + cfg Config, + key crypto.Signer, + caCert *x509.Certificate, + caKey crypto.Signer, +) (*x509.Certificate, error) { serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64)) if err != nil { return nil, err @@ -184,8 +204,8 @@ func NewSignedCert(cfg Config, key crypto.Signer, caCert *x509.Certificate, caKe return nil, errors.New("must specify at least one ExtKeyUsage") } - var dnsNames []string - var ips []net.IP + dnsNames := make([]string, 0, len(cfg.AltNames.DNSNames)) + ips := make([]net.IP, 0, len(cfg.AltNames.IPs)) for _, v := range cfg.AltNames.DNSNames { dnsNames = append(dnsNames, v) @@ -202,7 +222,7 @@ func NewSignedCert(cfg Config, key crypto.Signer, caCert *x509.Certificate, caKe IPAddresses: ips, SerialNumber: serial, NotBefore: caCert.NotBefore, - NotAfter: time.Now().Add(duration365d * cfg.Year).UTC(), + NotAfter: time.Now().Add(duration365d * time.Duration(cfg.Year)).UTC(), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, ExtKeyUsage: cfg.Usages, } @@ -215,7 +235,7 @@ func NewSignedCert(cfg Config, key crypto.Signer, caCert *x509.Certificate, caKe // WriteTofile // WriteCertAndKey stores certificate and key at the specified location -func WriteCertAndKey(pkiPath string, name string, cert *x509.Certificate, key crypto.Signer) error { +func WriteCertAndKey(pkiPath, name string, cert *x509.Certificate, key crypto.Signer) error { if err := WriteKey(pkiPath, name, key); err != nil { return err } @@ -231,7 +251,7 @@ func WriteCert(pkiPath, name string, cert *x509.Certificate) error { certificatePath := pathForCert(pkiPath, name) if err := certutil.WriteCert(certificatePath, EncodeCertPEM(cert)); err != nil { - return fmt.Errorf("unable to write certificate to file %s %s", certificatePath, err) + return fmt.Errorf("unable to write certificate to file %s %w", certificatePath, err) } return nil @@ -255,10 +275,10 @@ func WriteKey(pkiPath, name string, key crypto.Signer) error { privateKeyPath := pathForKey(pkiPath, name) encoded, err := keyutil.MarshalPrivateKeyToPEM(key) if err != nil { - return fmt.Errorf("unable to marshal private key to PEM %s", err) + return fmt.Errorf("unable to marshal private key to PEM %w", err) } if err := keyutil.WriteKey(privateKeyPath, encoded); err != nil { - return fmt.Errorf("unable to write private key to file %s %s", privateKeyPath, err) + return fmt.Errorf("unable to write private key to file %s %w", privateKeyPath, err) } return nil @@ -276,14 +296,14 @@ func WritePublicKey(pkiPath, name string, key crypto.PublicKey) error { } publicKeyPath := pathForPublicKey(pkiPath, name) if err := keyutil.WriteKey(publicKeyPath, publicKeyBytes); err != nil { - return fmt.Errorf("unable to write public key to file %s %s", publicKeyPath, err) + return fmt.Errorf("unable to write public key to file %s %w", publicKeyPath, err) } return nil } func pathForPublicKey(pkiPath, name string) string { - return filepath.Join(pkiPath, fmt.Sprintf("%s.pub", name)) + return filepath.Join(pkiPath, name+".pub") } // EncodePublicKeyPEM returns PEM-encoded public data @@ -300,9 +320,9 @@ func EncodePublicKeyPEM(key crypto.PublicKey) ([]byte, error) { } func pathForCert(pkiPath, name string) string { - return filepath.Join(pkiPath, fmt.Sprintf("%s.crt", name)) + return filepath.Join(pkiPath, name+".crt") } func pathForKey(pkiPath, name string) string { - return filepath.Join(pkiPath, fmt.Sprintf("%s.key", name)) + return filepath.Join(pkiPath, name+".key") } diff --git a/lifecycle/pkg/cert/cert_cmd.go b/lifecycle/pkg/cert/cert_cmd.go index e876d7239f55..6e9f57433cd3 100644 --- a/lifecycle/pkg/cert/cert_cmd.go +++ b/lifecycle/pkg/cert/cert_cmd.go @@ -19,19 +19,31 @@ package cert import "fmt" // GenerateCert generate all cert. -func GenerateCert(certPATH, certEtcdPATH string, altNames []string, hostIP, hostName, serviceCIRD, DNSDomain string) error { - certConfig, err := NewSealosCertMetaData(certPATH, certEtcdPATH, altNames, serviceCIRD, hostName, hostIP, DNSDomain) +func GenerateCert( + certPath, certEtcdPath string, + altNames []string, + hostIP, hostName, serviceCIRD, dnsDomain string, +) error { + certConfig, err := NewSealosCertMetaData( + certPath, + certEtcdPath, + altNames, + serviceCIRD, + hostName, + hostIP, + dnsDomain, + ) if err != nil { - return fmt.Errorf("generator cert config failed %v", err) + return fmt.Errorf("generator cert config failed %w", err) } return certConfig.GenerateAll() } -func GenerateRegistryCert(registryCertPath string, BaseName string) error { +func GenerateRegistryCert(registryCertPath, baseName string) error { regCertConfig := Config{ Path: registryCertPath, - BaseName: BaseName, - CommonName: BaseName, + BaseName: baseName, + CommonName: baseName, Organization: []string{"labring"}, Year: 100, } diff --git a/lifecycle/pkg/cert/kube_certs.go b/lifecycle/pkg/cert/kube_certs.go index 867879d2ae4e..6cf6d4959b6e 100644 --- a/lifecycle/pkg/cert/kube_certs.go +++ b/lifecycle/pkg/cert/kube_certs.go @@ -23,9 +23,8 @@ import ( "path" "strings" - utilnet "k8s.io/utils/net" - "github.com/labring/sealos/pkg/utils/logger" + utilnet "k8s.io/utils/net" ) var ( @@ -33,10 +32,10 @@ var ( kubeDefaultCertEtcdPath = "/etc/kubernetes/pki/etcd" ) -func CaList(CertPath, CertEtcdPath string) []Config { +func CaList(certPath, certEtcdPath string) []Config { return []Config{ { - Path: CertPath, + Path: certPath, DefaultPath: KubeDefaultCertPath, BaseName: "ca", CommonName: "kubernetes", @@ -46,7 +45,7 @@ func CaList(CertPath, CertEtcdPath string) []Config { Usages: nil, }, { - Path: CertPath, + Path: certPath, DefaultPath: KubeDefaultCertPath, BaseName: "front-proxy-ca", CommonName: "front-proxy-ca", @@ -56,7 +55,7 @@ func CaList(CertPath, CertEtcdPath string) []Config { Usages: nil, }, { - Path: CertEtcdPath, + Path: certEtcdPath, DefaultPath: kubeDefaultCertEtcdPath, BaseName: "ca", CommonName: "etcd-ca", @@ -68,10 +67,10 @@ func CaList(CertPath, CertEtcdPath string) []Config { } } -func List(CertPath, CertEtcdPath string) []Config { +func List(certPath, certEtcdPath string) []Config { return []Config{ { - Path: CertPath, + Path: certPath, DefaultPath: KubeDefaultCertPath, BaseName: "apiserver", CAName: "kubernetes", @@ -92,7 +91,7 @@ func List(CertPath, CertEtcdPath string) []Config { Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, }, { - Path: CertPath, + Path: certPath, DefaultPath: KubeDefaultCertPath, BaseName: "apiserver-kubelet-client", CAName: "kubernetes", @@ -103,7 +102,7 @@ func List(CertPath, CertEtcdPath string) []Config { Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, }, { - Path: CertPath, + Path: certPath, DefaultPath: KubeDefaultCertPath, BaseName: "front-proxy-client", CAName: "front-proxy-ca", @@ -114,7 +113,7 @@ func List(CertPath, CertEtcdPath string) []Config { Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, }, { - Path: CertPath, + Path: certPath, DefaultPath: KubeDefaultCertPath, BaseName: "apiserver-etcd-client", CAName: "etcd-ca", @@ -125,7 +124,7 @@ func List(CertPath, CertEtcdPath string) []Config { Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, }, { - Path: CertEtcdPath, + Path: certEtcdPath, DefaultPath: kubeDefaultCertEtcdPath, BaseName: "server", CAName: "etcd-ca", @@ -133,10 +132,13 @@ func List(CertPath, CertEtcdPath string) []Config { Organization: nil, Year: 100, AltNames: AltNames{}, // need set altNames - Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, + Usages: []x509.ExtKeyUsage{ + x509.ExtKeyUsageServerAuth, + x509.ExtKeyUsageClientAuth, + }, }, { - Path: CertEtcdPath, + Path: certEtcdPath, DefaultPath: kubeDefaultCertEtcdPath, BaseName: "peer", CAName: "etcd-ca", @@ -144,10 +146,13 @@ func List(CertPath, CertEtcdPath string) []Config { Organization: nil, Year: 100, AltNames: AltNames{}, // change this in filter - Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, + Usages: []x509.ExtKeyUsage{ + x509.ExtKeyUsageServerAuth, + x509.ExtKeyUsageClientAuth, + }, }, { - Path: CertEtcdPath, + Path: certEtcdPath, DefaultPath: kubeDefaultCertEtcdPath, BaseName: "healthcheck-client", CAName: "etcd-ca", @@ -166,7 +171,7 @@ type SealosCertMetaData struct { NodeName string NodeIP string DNSDomain string - //证书生成的位置 + // 证书生成的位置 CertPath string CertEtcdPath string } @@ -182,15 +187,19 @@ const ( ) // apiServerIPAndDomains = MasterIP + VIP + CertSANS 暂时只有apiserver, 记得把cluster.local后缀加到apiServerIPAndDOmas里先 -func NewSealosCertMetaData(certPATH, certEtcdPATH string, apiServerIPAndDomains []string, SvcCIDR, nodeName, nodeIP, DNSDomain string) (*SealosCertMetaData, error) { +func NewSealosCertMetaData( + certPath, certEtcdPath string, + apiServerIPAndDomains []string, + svcCIDR, nodeName, nodeIP, dnsDomain string, +) (*SealosCertMetaData, error) { data := &SealosCertMetaData{} - data.CertPath = certPATH - data.CertEtcdPath = certEtcdPATH - data.DNSDomain = DNSDomain + data.CertPath = certPath + data.CertEtcdPath = certEtcdPath + data.DNSDomain = dnsDomain data.APIServer.IPs = make(map[string]net.IP) data.APIServer.DNSNames = make(map[string]string) - for _, svcCidr := range strings.Split(SvcCIDR, ",") { + for _, svcCidr := range strings.Split(svcCIDR, ",") { _, svcNet, err := net.ParseCIDR(svcCidr) if err != nil { return nil, err @@ -224,7 +233,7 @@ func (meta *SealosCertMetaData) apiServerAltName(certList *[]Config) { (*certList)[APIserverCert].AltNames.DNSNames[dns] = dns } - svcDNS := fmt.Sprintf("kubernetes.default.svc.%s", meta.DNSDomain) + svcDNS := "kubernetes.default.svc." + meta.DNSDomain (*certList)[APIserverCert].AltNames.DNSNames[svcDNS] = svcDNS (*certList)[APIserverCert].AltNames.DNSNames[meta.NodeName] = meta.NodeName @@ -251,7 +260,11 @@ func (meta *SealosCertMetaData) etcdAltAndCommonName(certList *[]Config) { (*certList)[EtcdPeerCert].CommonName = meta.NodeName (*certList)[EtcdPeerCert].AltNames = altname - logger.Info("Etcd altnames : %v, commonName : %s", (*certList)[EtcdPeerCert].AltNames, (*certList)[EtcdPeerCert].CommonName) + logger.Info( + "Etcd altnames : %v, commonName : %s", + (*certList)[EtcdPeerCert].AltNames, + (*certList)[EtcdPeerCert].CommonName, + ) } // create sa.key sa.pub for service Account diff --git a/lifecycle/pkg/cert/kube_certs_test.go b/lifecycle/pkg/cert/kube_certs_test.go index 9224d09ed6e1..b88ad2abec25 100644 --- a/lifecycle/pkg/cert/kube_certs_test.go +++ b/lifecycle/pkg/cert/kube_certs_test.go @@ -30,7 +30,15 @@ func TestGenerateAll(t *testing.T) { false, }, } - certMeta, err := NewSealosCertMetaData(BasePath, EtcdBasePath, []string{"test.com", "192.168.1.2", "kubernetes.default.svc.sealos"}, "10.64.0.0/10", "master1", "172.27.139.11", "cluster.local") + certMeta, err := NewSealosCertMetaData( + BasePath, + EtcdBasePath, + []string{"test.com", "192.168.1.2", "kubernetes.default.svc.sealos"}, + "10.64.0.0/10", + "master1", + "172.27.139.11", + "cluster.local", + ) if err != nil { t.Error(err) } diff --git a/lifecycle/pkg/cert/kubeconfig.go b/lifecycle/pkg/cert/kubeconfig.go index 1626fb509349..0a36405760f4 100644 --- a/lifecycle/pkg/cert/kubeconfig.go +++ b/lifecycle/pkg/cert/kubeconfig.go @@ -23,11 +23,10 @@ import ( "os" "path/filepath" + "github.com/labring/sealos/pkg/utils/logger" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/util/keyutil" - - "github.com/labring/sealos/pkg/utils/logger" ) // clientCertAuth struct holds info required to build a client certificate to provide authentication info in a kubeconfig object @@ -54,7 +53,11 @@ type kubeConfigSpec struct { // join --control-plane workflow, plus the admin kubeconfig file used by the administrator and kubeadm itself; the // kubelet.conf file must not be created because it will be created and signed by the kubelet TLS bootstrap process. // If any kubeconfig files already exists, it used only if evaluated equal; otherwise an error is returned. -func CreateJoinControlPlaneKubeConfigFiles(outDir string, cfg Config, nodeName, controlPlaneEndpoint, clusterName string) error { +func CreateJoinControlPlaneKubeConfigFiles( + outDir string, + cfg Config, + nodeName, controlPlaneEndpoint, clusterName string, +) error { return createKubeConfigFiles( outDir, cfg, @@ -64,20 +67,36 @@ func CreateJoinControlPlaneKubeConfigFiles(outDir string, cfg Config, nodeName, "admin.conf", "controller-manager.conf", "scheduler.conf", - "kubelet.conf", //master1上的kubeconfig跟随三个组件一起生成 + "kubelet.conf", // master1上的kubeconfig跟随三个组件一起生成 ) } // 方法没有被 ↑ 的方法调用,而是在cmd/kubeadm/app/cmd/phases/init/kubeconfig.go里调用 // cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go -func CreateKubeConfigFile(kubeConfigFileName string, outDir string, cfg Config, nodeName, controlPlaneEndpoint, clusterName string) error { +func CreateKubeConfigFile( + kubeConfigFileName, outDir string, + cfg Config, + nodeName, controlPlaneEndpoint, clusterName string, +) error { logger.Info("creating kubeconfig file for %s", kubeConfigFileName) - return createKubeConfigFiles(outDir, cfg, kubeConfigFileName, nodeName, controlPlaneEndpoint, clusterName) + return createKubeConfigFiles( + outDir, + cfg, + kubeConfigFileName, + nodeName, + controlPlaneEndpoint, + clusterName, + ) } // createKubeConfigFiles creates all the requested kubeconfig files. // If kubeconfig files already exists, they are used only if evaluated equal; otherwise an error is returned. -func createKubeConfigFiles(outDir string, cfg Config, nodeName, controlPlaneEndpoint, clusterName string, kubeConfigFileNames ...string) error { // gets the KubeConfigSpecs, actualized for the current InitConfiguration +func createKubeConfigFiles( + outDir string, + cfg Config, + nodeName, controlPlaneEndpoint, clusterName string, + kubeConfigFileNames ...string, +) error { // gets the KubeConfigSpecs, actualized for the current InitConfiguration specs, err := getKubeConfigSpecs(cfg, nodeName, controlPlaneEndpoint) if err != nil { return err @@ -107,10 +126,16 @@ func createKubeConfigFiles(outDir string, cfg Config, nodeName, controlPlaneEndp // getKubeConfigSpecs returns all KubeConfigSpecs actualized to the context of the current InitConfiguration // NB. this methods holds the information about how kubeadm creates kubeconfig files. -func getKubeConfigSpecs(cfg Config, nodeName, controlPlaneEndpoint string) (map[string]*kubeConfigSpec, error) { +func getKubeConfigSpecs( + cfg Config, + nodeName, controlPlaneEndpoint string, +) (map[string]*kubeConfigSpec, error) { caCert, caKey, err := LoadCaCertAndKeyFromDisk(cfg) if err != nil { - return nil, fmt.Errorf("couldn't create a kubeconfig; the CA files couldn't be loaded: %w", err) + return nil, fmt.Errorf( + "couldn't create a kubeconfig; the CA files couldn't be loaded: %w", + err, + ) } if len(nodeName) == 0 { @@ -121,7 +146,7 @@ func getKubeConfigSpecs(cfg Config, nodeName, controlPlaneEndpoint string) (map[ return nil, errors.New("controlPlaneEndpoint can not be empty") } - var kubeConfigSpec = map[string]*kubeConfigSpec{ + kubeConfigSpec := map[string]*kubeConfigSpec{ "admin.conf": { CACert: caCert, APIServer: controlPlaneEndpoint, @@ -162,7 +187,10 @@ func getKubeConfigSpecs(cfg Config, nodeName, controlPlaneEndpoint string) (map[ } // buildKubeConfigFromSpec creates a kubeconfig object for the given kubeConfigSpec -func buildKubeConfigFromSpec(spec *kubeConfigSpec, clustername string) (*clientcmdapi.Config, error) { +func buildKubeConfigFromSpec( + spec *kubeConfigSpec, + clustername string, +) (*clientcmdapi.Config, error) { // If this kubeconfig should use token if spec.TokenAuth != nil { // create a kubeconfig with a token @@ -183,9 +211,17 @@ func buildKubeConfigFromSpec(spec *kubeConfigSpec, clustername string) (*clientc Year: 100, } - clientCert, clientKey, err := NewCaCertAndKeyFromRoot(clientCertConfig, spec.CACert, spec.ClientCertAuth.CAKey) + clientCert, clientKey, err := NewCaCertAndKeyFromRoot( + clientCertConfig, + spec.CACert, + spec.ClientCertAuth.CAKey, + ) if err != nil { - return nil, fmt.Errorf("failure while creating %s client certificate: %w", spec.ClientName, err) + return nil, fmt.Errorf( + "failure while creating %s client certificate: %w", + spec.ClientName, + err, + ) } encodedClientKey, err := keyutil.MarshalPrivateKeyToPEM(clientKey) @@ -214,7 +250,11 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er // The kubeconfig already exists, let's check if it has got the same CA and server URL currentConfig, err := clientcmd.LoadFromFile(kubeConfigFilePath) if err != nil { - return fmt.Errorf("failed to load kubeconfig file %s that already exists on disk: %w", kubeConfigFilePath, err) + return fmt.Errorf( + "failed to load kubeconfig file %s that already exists on disk: %w", + kubeConfigFilePath, + err, + ) } expectedCtx, exists := config.Contexts[config.CurrentContext] @@ -224,11 +264,17 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er expectedCluster := expectedCtx.Cluster currentCtx, exists := currentConfig.Contexts[currentConfig.CurrentContext] if !exists { - return fmt.Errorf("failed to find CurrentContext in Contexts of the kubeconfig file %s", kubeConfigFilePath) + return fmt.Errorf( + "failed to find CurrentContext in Contexts of the kubeconfig file %s", + kubeConfigFilePath, + ) } currentCluster := currentCtx.Cluster if currentConfig.Clusters[currentCluster] == nil { - return fmt.Errorf("failed to find the given CurrentContext Infra in Clusters of the kubeconfig file %s", kubeConfigFilePath) + return fmt.Errorf( + "failed to find the given CurrentContext Infra in Clusters of the kubeconfig file %s", + kubeConfigFilePath, + ) } // Make sure the compared CAs are whitespace-trimmed. The function clientcmd.LoadFromFile() just decodes @@ -239,11 +285,17 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er // If the current CA cert on disk doesn't match the expected CA cert, error out because we have a file, but it's stale if !bytes.Equal(caCurrent, caExpected) { - return fmt.Errorf("a kubeconfig file %q exists already but has got the wrong CA cert", kubeConfigFilePath) + return fmt.Errorf( + "a kubeconfig file %q exists already but has got the wrong CA cert", + kubeConfigFilePath, + ) } // If the current API Server location on disk doesn't match the expected API server, error out because we have a file, but it's stale if currentConfig.Clusters[currentCluster].Server != config.Clusters[expectedCluster].Server { - return fmt.Errorf("a kubeconfig file %q exists already but has got the wrong API Server URL", kubeConfigFilePath) + return fmt.Errorf( + "a kubeconfig file %q exists already but has got the wrong API Server URL", + kubeConfigFilePath, + ) } return nil @@ -265,7 +317,11 @@ func createKubeConfigFileIfNotExists(outDir, filename string, config *clientcmda logger.Debug("[kubeconfig] Writing %q kubeconfig file\n", filename) err = WriteToDisk(kubeConfigFilePath, config) if err != nil { - return fmt.Errorf("failed to save kubeconfig file %q on disk: %w", kubeConfigFilePath, err) + return fmt.Errorf( + "failed to save kubeconfig file %q on disk: %w", + kubeConfigFilePath, + err, + ) } return nil } @@ -303,7 +359,11 @@ func CreateBasic(serverURL, clusterName, userName string, caCert []byte) *client // cmd/kubeadm/app/util/kubeconfig/kubeconfig.go // CreateWithToken creates a KubeConfig object with access to the API server with a token -func CreateWithToken(serverURL, clusterName, userName string, caCert []byte, token string) *clientcmdapi.Config { +func CreateWithToken( + serverURL, clusterName, userName string, + caCert []byte, + token string, +) *clientcmdapi.Config { config := CreateBasic(serverURL, clusterName, userName, caCert) config.AuthInfos[userName] = &clientcmdapi.AuthInfo{ Token: token, @@ -313,7 +373,10 @@ func CreateWithToken(serverURL, clusterName, userName string, caCert []byte, tok // cmd/kubeadm/app/util/kubeconfig/kubeconfig.go // CreateWithCerts creates a KubeConfig object with access to the API server with client certificates -func CreateWithCerts(serverURL, clusterName, userName string, caCert []byte, clientKey []byte, clientCert []byte) *clientcmdapi.Config { +func CreateWithCerts( + serverURL, clusterName, userName string, + caCert, clientKey, clientCert []byte, +) *clientcmdapi.Config { config := CreateBasic(serverURL, clusterName, userName, caCert) config.AuthInfos[userName] = &clientcmdapi.AuthInfo{ ClientKeyData: clientKey, diff --git a/lifecycle/pkg/checker/check_list.go b/lifecycle/pkg/checker/check_list.go index 789dcb00e87e..befcfec666b6 100644 --- a/lifecycle/pkg/checker/check_list.go +++ b/lifecycle/pkg/checker/check_list.go @@ -33,7 +33,7 @@ type Interface interface { func RunCheckList(list []Interface, cluster *v2.Cluster, phase string) error { for _, l := range list { if err := l.Check(cluster, phase); err != nil { - return fmt.Errorf("failed to run checker: %v", err) + return fmt.Errorf("failed to run checker: %w", err) } } return nil diff --git a/lifecycle/pkg/checker/cluster_checker.go b/lifecycle/pkg/checker/cluster_checker.go index 401183c521a6..ca31db673850 100644 --- a/lifecycle/pkg/checker/cluster_checker.go +++ b/lifecycle/pkg/checker/cluster_checker.go @@ -21,18 +21,16 @@ import ( "os" "time" - corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/template" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type ClusterChecker struct { -} +type ClusterChecker struct{} type ClusterStatus struct { IP string @@ -60,7 +58,7 @@ func (n *ClusterChecker) Check(cluster *v2.Cluster, phase string) error { return err } healthyClient := kubernetes.NewKubeHealthy(c.Kubernetes(), 30*time.Second) - var NodeList []ClusterStatus + NodeList := make([]ClusterStatus, 0, len(nodes.Items)) ctx := context.Background() for _, node := range nodes.Items { ip, _ := getNodeStatus(node) @@ -75,7 +73,11 @@ func (n *ClusterChecker) Check(cluster *v2.Cluster, phase string) error { } cStatus.KubeAPIServer = healthyClient.ForHealthyPod(apiPod) - controllerPod, err := ke.FetchStaticPod(ctx, node.Name, kubernetes.KubeControllerManager) + controllerPod, err := ke.FetchStaticPod( + ctx, + node.Name, + kubernetes.KubeControllerManager, + ) if err != nil { return err } diff --git a/lifecycle/pkg/checker/cri_shim_checker.go b/lifecycle/pkg/checker/cri_shim_checker.go index 729d451639b0..a7c24e1f11dc 100644 --- a/lifecycle/pkg/checker/cri_shim_checker.go +++ b/lifecycle/pkg/checker/cri_shim_checker.go @@ -22,16 +22,13 @@ import ( "os" "github.com/labring/image-cri-shim/pkg/types" - - "github.com/labring/sreg/pkg/buildimage" - "github.com/labring/sealos/pkg/template" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sreg/pkg/buildimage" ) -type CRIShimChecker struct { -} +type CRIShimChecker struct{} type CRIShimStatus struct { Config map[string]string diff --git a/lifecycle/pkg/checker/crictl_checker.go b/lifecycle/pkg/checker/crictl_checker.go index e85b2b54c247..1d4b74a69606 100644 --- a/lifecycle/pkg/checker/crictl_checker.go +++ b/lifecycle/pkg/checker/crictl_checker.go @@ -20,12 +20,10 @@ import ( "encoding/json" "errors" "fmt" + "net" "os" "strings" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - executils "k8s.io/utils/exec" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/registry/helpers" @@ -36,10 +34,11 @@ import ( fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + executils "k8s.io/utils/exec" ) -type CRICtlChecker struct { -} +type CRICtlChecker struct{} type Container struct { Container string @@ -115,7 +114,11 @@ func (n *CRICtlChecker) Check(cluster *v2.Cluster, phase string) error { root := constants.NewPathResolver(cluster.Name).RootFSPath() regInfo := helpers.GetRegistryInfo(sshCtx, root, cluster.GetRegistryIPAndPort()) - regStatus, err := n.getRegistryStatus(crictlPath, pauseImage, fmt.Sprintf("%s:%s", regInfo.Domain, regInfo.Port)) + regStatus, err := n.getRegistryStatus( + crictlPath, + pauseImage, + net.JoinHostPort(regInfo.Domain, regInfo.Port), + ) if err != nil { status.Error = fmt.Errorf("pull registry image error: %w", err).Error() } @@ -186,16 +189,16 @@ func (n *CRICtlChecker) getCRICtlContainerList(crictlPath string) ([]Container, ID string `json:"id"` PodSandboxID string `json:"podSandboxId"` Metadata struct { - Name string - Attempt int - } + Name string `json:"name"` + Attempt int `json:"attempt"` + } `json:"metadata"` Image struct { - Image string - } - State string //CONTAINER_RUNNING - Labels map[string]string - Annotations map[string]string - } + Image string `json:"image"` + } `json:"image"` + State string `json:"state"` + Labels map[string]string `json:"labels"` + Annotations map[string]string `json:"annotations"` + } `json:"containers"` } ps := &psStruct{} psCmd := `%s ps -a -o json` @@ -220,14 +223,16 @@ func (n *CRICtlChecker) getCRICtlContainerList(crictlPath string) ([]Container, return containerList, nil } -func (n *CRICtlChecker) getRegistryStatus(crictlPath string, pauseImage string, registry string) (status string, err error) { +func (n *CRICtlChecker) getRegistryStatus( + crictlPath, pauseImage, registry string, +) (status string, err error) { pullCmd := `%s pull %s/%s` regStatus, err := exec2.RunBashCmd(fmt.Sprintf(pullCmd, crictlPath, registry, pauseImage)) regStatus = strings.ReplaceAll(regStatus, "\n", "") if err != nil { status = "unknown:" + regStatus - return + return status, err } status = "ok:" + regStatus - return + return status, err } diff --git a/lifecycle/pkg/checker/host_checker.go b/lifecycle/pkg/checker/host_checker.go index 903131998b1c..f84c35c6d3d1 100644 --- a/lifecycle/pkg/checker/host_checker.go +++ b/lifecycle/pkg/checker/host_checker.go @@ -60,11 +60,11 @@ func checkHostnameUnique(s exec.Interface, ipList []string) error { for _, ip := range ipList { hostname, err := s.CmdToString(ip, "hostname", "") if err != nil { - return fmt.Errorf("failed to get host %s hostname, %v", ip, err) + return fmt.Errorf("failed to get host %s hostname, %w", ip, err) } if hostnameList[hostname] { - return fmt.Errorf("hostname cannot be repeated, please set different hostname") + return errors.New("hostname cannot be repeated, please set different hostname") } hostnameList[hostname] = true } @@ -77,15 +77,19 @@ func checkTimeSync(s exec.Interface, ipList []string) error { for _, ip := range ipList { timestamp, err := s.CmdToString(ip, "date +%s", "") if err != nil { - return fmt.Errorf("failed to get %s timestamp, %v", ip, err) + return fmt.Errorf("failed to get %s timestamp, %w", ip, err) } ts, err := strconv.Atoi(timestamp) if err != nil { - return fmt.Errorf("failed to reverse timestamp %s, %v", timestamp, err) + return fmt.Errorf("failed to reverse timestamp %s, %w", timestamp, err) } timeDiff := time.Since(time.Unix(int64(ts), 0)).Seconds() if timeDiff <= -5 || timeDiff >= 5 { - return fmt.Errorf("the time of %s node is not synchronized, the time difference is %.2f seconds", ip, timeDiff) + return fmt.Errorf( + "the time of %s node is not synchronized, the time difference is %.2f seconds", + ip, + timeDiff, + ) } } return nil diff --git a/lifecycle/pkg/checker/initsystem_check.go b/lifecycle/pkg/checker/initsystem_check.go index 60d07a0178e3..b956e8644821 100644 --- a/lifecycle/pkg/checker/initsystem_check.go +++ b/lifecycle/pkg/checker/initsystem_check.go @@ -27,8 +27,7 @@ import ( "github.com/labring/sealos/pkg/utils/logger" ) -type InitSystemChecker struct { -} +type InitSystemChecker struct{} type systemStatus struct { Name string @@ -58,7 +57,14 @@ func (n *InitSystemChecker) Check(_ *v2.Cluster, phase string) error { return nil } - serviceNames := []string{"kubelet", "containerd", "cri-docker", "docker", "registry", "image-cri-shim"} + serviceNames := []string{ + "kubelet", + "containerd", + "cri-docker", + "docker", + "registry", + "image-cri-shim", + } status.ServiceList = make([]systemStatus, 0) for _, sn := range serviceNames { status.ServiceList = append(status.ServiceList, systemStatus{ @@ -84,7 +90,10 @@ Systemd Service Status {{ end }}`) if err != nil || !isOk { if err != nil { - logger.Error("failed to render system service checkers template. error: %s", err.Error()) + logger.Error( + "failed to render system service checkers template. error: %s", + err.Error(), + ) return err } return errors.New("convert system service template failed") @@ -96,7 +105,10 @@ func NewInitSystemChecker() Interface { return &InitSystemChecker{} } -func (n *InitSystemChecker) checkInitSystem(system initsystem.InitSystem, name string) (status string) { +func (n *InitSystemChecker) checkInitSystem( + system initsystem.InitSystem, + name string, +) (status string) { if !system.ServiceExists(name) { status = "NotExists" } else { @@ -114,5 +126,5 @@ func (n *InitSystemChecker) checkInitSystem(system initsystem.InitSystem, name s status = fmt.Sprintf("%s && %s", enable, subStatus) } - return + return status } diff --git a/lifecycle/pkg/checker/node_checker.go b/lifecycle/pkg/checker/node_checker.go index 72734d3b439a..4c907c2edf45 100644 --- a/lifecycle/pkg/checker/node_checker.go +++ b/lifecycle/pkg/checker/node_checker.go @@ -19,14 +19,13 @@ import ( "errors" "os" - corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/template" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -34,8 +33,7 @@ const ( NotReadyNodeStatus = "NotReady" ) -type NodeChecker struct { -} +type NodeChecker struct{} type NodeClusterStatus struct { ReadyCount uint32 @@ -102,29 +100,29 @@ Cluster Node Status return tpl.Execute(os.Stdout, nodeCLusterStatus) } -func getNodeStatus(node corev1.Node) (IP string, Phase string) { +func getNodeStatus(node corev1.Node) (ip, phase string) { if len(node.Status.Addresses) < 1 { return "", "" } for _, address := range node.Status.Addresses { if address.Type == "InternalIP" { - IP = address.Address + ip = address.Address } } - if IP == "" { - IP = node.Status.Addresses[0].Address + if ip == "" { + ip = node.Status.Addresses[0].Address } - Phase = NotReadyNodeStatus + phase = NotReadyNodeStatus for _, condition := range node.Status.Conditions { if condition.Type == ReadyNodeStatus { if condition.Status == "True" { - Phase = ReadyNodeStatus + phase = ReadyNodeStatus } else { - Phase = NotReadyNodeStatus + phase = NotReadyNodeStatus } } } - return IP, Phase + return ip, phase } func NewNodeChecker() Interface { diff --git a/lifecycle/pkg/checker/pod_checker.go b/lifecycle/pkg/checker/pod_checker.go index fa048f7546d7..99bcb2e50b27 100644 --- a/lifecycle/pkg/checker/pod_checker.go +++ b/lifecycle/pkg/checker/pod_checker.go @@ -19,14 +19,13 @@ import ( "errors" "os" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/template" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type PodChecker struct { @@ -56,7 +55,10 @@ func (n *PodChecker) Check(cluster *v2.Cluster, phase string) error { n.client = c - nsList, err := n.client.Kubernetes().CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) + nsList, err := n.client.Kubernetes(). + CoreV1(). + Namespaces(). + List(context.TODO(), metav1.ListOptions{}) if err != nil { return err } @@ -66,7 +68,10 @@ func (n *PodChecker) Check(cluster *v2.Cluster, phase string) error { var notRunningCount uint32 var podCount uint32 var notRunningPodList []*corev1.Pod - namespacePodList, err := n.client.Kubernetes().CoreV1().Pods(podNamespace.Name).List(context.TODO(), metav1.ListOptions{}) + namespacePodList, err := n.client.Kubernetes(). + CoreV1(). + Pods(podNamespace.Name). + List(context.TODO(), metav1.ListOptions{}) if err != nil { return err } diff --git a/lifecycle/pkg/checker/registry_checker.go b/lifecycle/pkg/checker/registry_checker.go index d1c2aac5641c..779be509b037 100644 --- a/lifecycle/pkg/checker/registry_checker.go +++ b/lifecycle/pkg/checker/registry_checker.go @@ -19,18 +19,13 @@ package checker import ( "errors" "fmt" + "net" "os" "github.com/docker/docker/api/types/registry" - - "github.com/labring/sreg/pkg/registry/crane" - + "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/registry/helpers" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/template" v2 "github.com/labring/sealos/pkg/types/v1beta1" @@ -38,10 +33,11 @@ import ( "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + "github.com/labring/sreg/pkg/registry/crane" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) -type RegistryChecker struct { -} +type RegistryChecker struct{} type RegistryStatus struct { Port string @@ -95,8 +91,8 @@ func (n *RegistryChecker) Check(cluster *v2.Cluster, phase string) error { } root := constants.NewPathResolver(cluster.Name).RootFSPath() regInfo := helpers.GetRegistryInfo(execer, root, cluster.GetRegistryIPAndPort()) - status.Auth = fmt.Sprintf("%s:%s", regInfo.Username, regInfo.Password) - status.RegistryDomain = fmt.Sprintf("%s:%s", regInfo.Domain, regInfo.Port) + status.Auth = regInfo.Username + ":" + regInfo.Password + status.RegistryDomain = net.JoinHostPort(regInfo.Domain, regInfo.Port) cfg := registry.AuthConfig{ Username: regInfo.Username, Password: regInfo.Password, diff --git a/lifecycle/pkg/checker/svc_checker.go b/lifecycle/pkg/checker/svc_checker.go index 6e06d6c928ed..a8301fad7105 100644 --- a/lifecycle/pkg/checker/svc_checker.go +++ b/lifecycle/pkg/checker/svc_checker.go @@ -19,16 +19,13 @@ import ( "errors" "os" - "github.com/labring/sealos/pkg/template" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/constants" + "github.com/labring/sealos/pkg/template" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" - corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type SvcChecker struct { @@ -59,31 +56,40 @@ func (n *SvcChecker) Check(cluster *v2.Cluster, phase string) error { n.client = c - //namespaceSvcList, err := n.client.ListAllNamespacesSvcs() + // namespaceSvcList, err := n.client.ListAllNamespacesSvcs() - nsList, err := n.client.Kubernetes().CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) + nsList, err := n.client.Kubernetes(). + CoreV1(). + Namespaces(). + List(context.TODO(), metav1.ListOptions{}) if err != nil { return err } - var svcNamespaceStatusList []*SvcNamespaceStatus + svcNamespaceStatusList := make([]*SvcNamespaceStatus, 0, len(nsList.Items)) if err != nil { return err } for _, svcNamespace := range nsList.Items { - namespaceSVCList, err := n.client.Kubernetes().CoreV1().Services(svcNamespace.Name).List(context.TODO(), metav1.ListOptions{}) + namespaceSVCList, err := n.client.Kubernetes(). + CoreV1(). + Services(svcNamespace.Name). + List(context.TODO(), metav1.ListOptions{}) if err != nil { break } - namespaceEPList, err := n.client.Kubernetes().CoreV1().Endpoints(svcNamespace.Name).List(context.TODO(), metav1.ListOptions{}) + namespaceEPList, err := n.client.Kubernetes(). + CoreV1(). + Endpoints(svcNamespace.Name). + List(context.TODO(), metav1.ListOptions{}) if err != nil { break } serviceCount := len(namespaceSVCList.Items) var unhaelthService []string - var endpointCount = 0 + endpointCount := 0 for _, service := range namespaceSVCList.Items { if IsExistEndpoint(namespaceEPList, service.Name) { diff --git a/lifecycle/pkg/client-go/kubernetes/client.go b/lifecycle/pkg/client-go/kubernetes/client.go index 5f5eec5ad476..f64a6f1c6472 100644 --- a/lifecycle/pkg/client-go/kubernetes/client.go +++ b/lifecycle/pkg/client-go/kubernetes/client.go @@ -18,7 +18,6 @@ import ( "path/filepath" fileutil "github.com/labring/sealos/pkg/utils/file" - "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -60,9 +59,9 @@ func newKubernetesClient(kubeconfig, apiserver string, insecure bool) (Client, e config.QPS = 1e6 config.Burst = 1e6 if insecure { - config.TLSClientConfig.CAFile = "" - config.TLSClientConfig.CAData = nil - config.TLSClientConfig.Insecure = true + config.CAFile = "" + config.CAData = nil + config.Insecure = true } var k kubernetesClient diff --git a/lifecycle/pkg/client-go/kubernetes/expansion.go b/lifecycle/pkg/client-go/kubernetes/expansion.go index 3ad97ef0922c..1b63a84368da 100644 --- a/lifecycle/pkg/client-go/kubernetes/expansion.go +++ b/lifecycle/pkg/client-go/kubernetes/expansion.go @@ -21,23 +21,20 @@ import ( "fmt" str "strings" + "github.com/labring/sealos/pkg/utils/iputils" + "github.com/labring/sealos/pkg/utils/logger" v1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clientset "k8s.io/client-go/kubernetes" ckubeadm "k8s.io/kubernetes/cmd/kubeadm/app/constants" - - "github.com/labring/sealos/pkg/utils/iputils" - "github.com/labring/sealos/pkg/utils/logger" ) -var ( - // ControlPlaneComponents defines the control-plane component names - ControlPlaneComponents = []string{KubeAPIServer, KubeControllerManager, KubeScheduler} -) +// ControlPlaneComponents defines the control-plane component names +var ControlPlaneComponents = []string{KubeAPIServer, KubeControllerManager, KubeScheduler} type Expansion interface { - FetchStaticPod(ctx context.Context, nodeName string, component string) (*v1.Pod, error) + FetchStaticPod(ctx context.Context, nodeName, component string) (*v1.Pod, error) FetchKubeadmConfig(ctx context.Context) (string, error) UpdateKubeadmConfig(ctx context.Context, clusterConfig string) error FetchKubeletConfig(ctx context.Context) (string, error) @@ -55,31 +52,50 @@ func NewKubeExpansion(client clientset.Interface) Expansion { } } -func (ke *kubeExpansion) FetchStaticPod(ctx context.Context, nodeName string, component string) (*v1.Pod, error) { +func (ke *kubeExpansion) FetchStaticPod( + ctx context.Context, + nodeName, component string, +) (*v1.Pod, error) { staticPodName := fmt.Sprintf("%s-%s", component, nodeName) - return ke.client.CoreV1().Pods(metav1.NamespaceSystem).Get(ctx, staticPodName, metav1.GetOptions{}) + return ke.client.CoreV1(). + Pods(metav1.NamespaceSystem). + Get(ctx, staticPodName, metav1.GetOptions{}) } + func (ke *kubeExpansion) FetchKubeadmConfig(ctx context.Context) (string, error) { - cm, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, ckubeadm.KubeadmConfigConfigMap, metav1.GetOptions{}) + cm, err := ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Get(ctx, ckubeadm.KubeadmConfigConfigMap, metav1.GetOptions{}) if err != nil { return "", err } return cm.Data[ckubeadm.ClusterConfigurationConfigMapKey], nil } + func (ke *kubeExpansion) UpdateKubeadmConfig(ctx context.Context, clusterConfig string) error { - cm, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, ckubeadm.KubeadmConfigConfigMap, metav1.GetOptions{}) + cm, err := ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Get(ctx, ckubeadm.KubeadmConfigConfigMap, metav1.GetOptions{}) if err != nil { return err } cm.Data[ckubeadm.ClusterConfigurationConfigMapKey] = clusterConfig - _, err = ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Update(ctx, cm, metav1.UpdateOptions{}) + _, err = ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Update(ctx, cm, metav1.UpdateOptions{}) return err } + func (ke *kubeExpansion) FetchKubeletConfig(ctx context.Context) (string, error) { - cm, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, ckubeadm.KubeletBaseConfigurationConfigMap, metav1.GetOptions{}) + cm, err := ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Get(ctx, ckubeadm.KubeletBaseConfigurationConfigMap, metav1.GetOptions{}) if err != nil { if kerrors.IsNotFound(err) { - logger.Info("cannot find configMap %q, try to detect older versions", ckubeadm.KubeletBaseConfigurationConfigMap) + logger.Info( + "cannot find configMap %q, try to detect older versions", + ckubeadm.KubeletBaseConfigurationConfigMap, + ) data, err := ke.fetchOldKubeletConfig(ctx) if err != nil { return "", err @@ -93,20 +109,29 @@ func (ke *kubeExpansion) FetchKubeletConfig(ctx context.Context) (string, error) } return cm.Data[ckubeadm.KubeletBaseConfigurationConfigMapKey], nil } + func (ke *kubeExpansion) UpdateKubeletConfig(ctx context.Context, kubeletConfig string) error { - cm, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, ckubeadm.KubeletBaseConfigurationConfigMap, metav1.GetOptions{}) + cm, err := ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Get(ctx, ckubeadm.KubeletBaseConfigurationConfigMap, metav1.GetOptions{}) if err != nil { return err } cm.Data[ckubeadm.KubeletBaseConfigurationConfigMap] = kubeletConfig - _, err = ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Update(ctx, cm, metav1.UpdateOptions{}) + _, err = ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Update(ctx, cm, metav1.UpdateOptions{}) return err } -func (ke *kubeExpansion) FetchHostNameFromInternalIP(ctx context.Context, nodeIP string) (string, error) { + +func (ke *kubeExpansion) FetchHostNameFromInternalIP( + ctx context.Context, + nodeIP string, +) (string, error) { ip := iputils.GetHostIP(nodeIP) nodeList, err := ke.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) if err != nil { - return "", fmt.Errorf("get node list failed: %v", err) + return "", fmt.Errorf("get node list failed: %w", err) } for _, node := range nodeList.Items { for _, address := range node.Status.Addresses { @@ -121,8 +146,10 @@ func (ke *kubeExpansion) FetchHostNameFromInternalIP(ctx context.Context, nodeIP } func (ke *kubeExpansion) fetchOldKubeletConfig(ctx context.Context) (string, error) { - kubeletBaseConfigurationConfigMapPrefix := fmt.Sprintf("%s-", ckubeadm.KubeletBaseConfigurationConfigMap) - cms, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).List(ctx, metav1.ListOptions{}) + kubeletBaseConfigurationConfigMapPrefix := ckubeadm.KubeletBaseConfigurationConfigMap + "-" + cms, err := ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + List(ctx, metav1.ListOptions{}) if err != nil { return "", err } @@ -131,14 +158,19 @@ func (ke *kubeExpansion) fetchOldKubeletConfig(ctx context.Context) (string, err return cm.Data[ckubeadm.KubeletBaseConfigurationConfigMapKey], nil } } - return "", fmt.Errorf("cannot find config map with prefix %q", kubeletBaseConfigurationConfigMapPrefix) + return "", fmt.Errorf( + "cannot find config map with prefix %q", + kubeletBaseConfigurationConfigMapPrefix, + ) } func (ke *kubeExpansion) cloneOldKubeletConfig(ctx context.Context, data string) error { cm := &v1.ConfigMap{Data: map[string]string{}} cm.Name = ckubeadm.KubeletBaseConfigurationConfigMap cm.Data[ckubeadm.KubeletBaseConfigurationConfigMapKey] = data - _, err := ke.client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(ctx, cm, metav1.CreateOptions{}) + _, err := ke.client.CoreV1(). + ConfigMaps(metav1.NamespaceSystem). + Create(ctx, cm, metav1.CreateOptions{}) if err != nil { return err } diff --git a/lifecycle/pkg/client-go/kubernetes/healthy.go b/lifecycle/pkg/client-go/kubernetes/healthy.go index 4cae39020bc4..c7f2d04bf65e 100644 --- a/lifecycle/pkg/client-go/kubernetes/healthy.go +++ b/lifecycle/pkg/client-go/kubernetes/healthy.go @@ -21,17 +21,18 @@ import ( "errors" "fmt" "io" + "net" "net/http" "net/url" + "strconv" "time" + "github.com/labring/sealos/pkg/utils/logger" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" netutil "k8s.io/apimachinery/pkg/util/net" "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" - - "github.com/labring/sealos/pkg/utils/logger" ) // Healthy is an interface for waiting for criteria in Kubernetes to happen @@ -65,16 +66,29 @@ func (w *kubeHealthy) ForAPI() error { start := time.Now() ctx, cancel := context.WithTimeout(context.Background(), w.timeout) defer cancel() - return wait.PollUntilContextCancel(ctx, APICallRetryInterval, true, func(ctx context.Context) (done bool, err error) { - healthStatus := 0 - w.client.Discovery().RESTClient().Get().AbsPath("/healthz").Do(context.TODO()).StatusCode(&healthStatus) - if healthStatus != http.StatusOK { - return false, nil - } + return wait.PollUntilContextCancel( + ctx, + APICallRetryInterval, + true, + func(ctx context.Context) (done bool, err error) { + healthStatus := 0 + w.client.Discovery(). + RESTClient(). + Get(). + AbsPath("/healthz"). + Do(context.TODO()). + StatusCode(&healthStatus) + if healthStatus != http.StatusOK { + return false, nil + } - logger.Debug("[apiclient] All control plane components are healthy after %f seconds\n", time.Since(start).Seconds()) - return true, nil - }) + logger.Debug( + "[apiclient] All control plane components are healthy after %f seconds\n", + time.Since(start).Seconds(), + ) + return true, nil + }, + ) } // ForHealthyKubelet blocks until the kubelet /healthz endpoint returns 'ok',port is 10248 @@ -85,28 +99,47 @@ func (w *kubeHealthy) ForHealthyKubelet(initialTimeout time.Duration, host strin trans := netutil.SetOldTransportDefaults(&http.Transport{}) client := &http.Client{Transport: trans} - healthzEndpoint, _ := url.JoinPath(fmt.Sprintf("http://%s:%d", host, KubeletHealthzPort), "healthz") + healthzEndpoint, _ := url.JoinPath( + "http://"+net.JoinHostPort(host, strconv.Itoa(KubeletHealthzPort)), + "healthz", + ) resp, err := client.Get(healthzEndpoint) if err != nil { logger.Warn("[kubelet-check] It seems like the kubelet isn't running or healthy.") - logger.Warn("[kubelet-check] The HTTP call equal to 'curl -sSL %s' failed with error: %v.\n", healthzEndpoint, err) + logger.Warn( + "[kubelet-check] The HTTP call equal to 'curl -sSL %s' failed with error: %v.\n", + healthzEndpoint, + err, + ) return err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { logger.Warn("[kubelet-check] It seems like the kubelet isn't running or healthy.") - logger.Warn("[kubelet-check] The HTTP call equal to 'curl -sSL %s' returned HTTP code %d\n", healthzEndpoint, resp.StatusCode) + logger.Warn( + "[kubelet-check] The HTTP call equal to 'curl -sSL %s' returned HTTP code %d\n", + healthzEndpoint, + resp.StatusCode, + ) return errors.New("the kubelet healthz endpoint is unhealthy") } b, err := io.ReadAll(resp.Body) if err != nil { logger.Warn("[kubelet-check] It seems like the kubelet isn't running or healthy.") - logger.Warn("[kubelet-check] The HTTP call equal to 'curl -sSL %s' failed with error: %v.\n", healthzEndpoint, err) + logger.Warn( + "[kubelet-check] The HTTP call equal to 'curl -sSL %s' failed with error: %v.\n", + healthzEndpoint, + err, + ) return err } if string(b) != "ok" { logger.Warn("[kubelet-check] It seems like the kubelet isn't running or healthy.") - logger.Warn("[kubelet-check] The HTTP call equal to 'curl -sSL %s' returned HTTP code %d\n", healthzEndpoint, resp.StatusCode) + logger.Warn( + "[kubelet-check] The HTTP call equal to 'curl -sSL %s' returned HTTP code %d\n", + healthzEndpoint, + resp.StatusCode, + ) return errors.New("the kubelet healthz endpoint is unhealthy: resp is " + string(b)) } @@ -180,17 +213,18 @@ func printPod(pod *v1.Pod) string { lastRestartDate = terminatedDate } } - if container.State.Waiting != nil && container.State.Waiting.Reason != "" { + switch { + case container.State.Waiting != nil && container.State.Waiting.Reason != "": reason = container.State.Waiting.Reason - } else if container.State.Terminated != nil && container.State.Terminated.Reason != "" { + case container.State.Terminated != nil && container.State.Terminated.Reason != "": reason = container.State.Terminated.Reason - } else if container.State.Terminated != nil && container.State.Terminated.Reason == "" { + case container.State.Terminated != nil: if container.State.Terminated.Signal != 0 { reason = fmt.Sprintf("Signal:%d", container.State.Terminated.Signal) } else { reason = fmt.Sprintf("ExitCode:%d", container.State.Terminated.ExitCode) } - } else if container.Ready && container.State.Running != nil { + case container.Ready && container.State.Running != nil: hasRunning = true readyContainers++ } diff --git a/lifecycle/pkg/client-go/kubernetes/idempotency.go b/lifecycle/pkg/client-go/kubernetes/idempotency.go index ead5ee6b0679..c204bccf3767 100644 --- a/lifecycle/pkg/client-go/kubernetes/idempotency.go +++ b/lifecycle/pkg/client-go/kubernetes/idempotency.go @@ -22,6 +22,7 @@ import ( "fmt" "time" + "github.com/labring/sealos/pkg/utils/logger" apps "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" rbac "k8s.io/api/rbac/v1" @@ -31,8 +32,6 @@ import ( "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" - - "github.com/labring/sealos/pkg/utils/logger" ) const ( @@ -146,13 +145,17 @@ func (ki *kubeIdempotency) CreateOrUpdateDaemonSet(ds *apps.DaemonSet) error { // DeleteDaemonSetForeground deletes the specified DaemonSet in foreground mode; i.e. it blocks until/makes sure all the managed Pods are deleted func (ki *kubeIdempotency) DeleteDaemonSetForeground(namespace, name string) error { foregroundDelete := metav1.DeletePropagationForeground - return ki.client.AppsV1().DaemonSets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{PropagationPolicy: &foregroundDelete}) + return ki.client.AppsV1(). + DaemonSets(namespace). + Delete(context.TODO(), name, metav1.DeleteOptions{PropagationPolicy: &foregroundDelete}) } // DeleteDeploymentForeground deletes the specified Deployment in foreground mode; i.e. it blocks until/makes sure all the managed Pods are deleted func (ki *kubeIdempotency) DeleteDeploymentForeground(namespace, name string) error { foregroundDelete := metav1.DeletePropagationForeground - return ki.client.AppsV1().Deployments(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{PropagationPolicy: &foregroundDelete}) + return ki.client.AppsV1(). + Deployments(namespace). + Delete(context.TODO(), name, metav1.DeleteOptions{PropagationPolicy: &foregroundDelete}) } // CreateOrUpdateRole creates a Role if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. @@ -198,7 +201,9 @@ func (ki *kubeIdempotency) CreateOrUpdateClusterRole(clusterRole *rbac.ClusterRo } // CreateOrUpdateClusterRoleBinding creates a ClusterRoleBinding if the target resource doesn't exist. If the resource exists already, this function will update the resource instead. -func (ki *kubeIdempotency) CreateOrUpdateClusterRoleBinding(clusterRoleBinding *rbac.ClusterRoleBinding) error { +func (ki *kubeIdempotency) CreateOrUpdateClusterRoleBinding( + clusterRoleBinding *rbac.ClusterRoleBinding, +) error { if _, err := ki.client.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRoleBinding, metav1.CreateOptions{}); err != nil { if !apierrors.IsAlreadyExists(err) { return fmt.Errorf("unable to create RBAC clusterrolebinding: %w", err) @@ -215,7 +220,10 @@ func (ki *kubeIdempotency) CreateOrUpdateClusterRoleBinding(clusterRoleBinding * // This is a condition function meant to be used with wait.Poll. false, nil // implies it is safe to try again, an error indicates no more tries should be // made and true indicates success. -func (ki *kubeIdempotency) patchNodeOnce(nodeName string, patchFn func(*v1.Node)) func(ctx context.Context) (bool, error) { +func (ki *kubeIdempotency) patchNodeOnce( + nodeName string, + patchFn func(*v1.Node), +) func(ctx context.Context) (bool, error) { return func(ctx context.Context) (bool, error) { // First get the node object n, err := ki.client.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) @@ -225,13 +233,17 @@ func (ki *kubeIdempotency) patchNodeOnce(nodeName string, patchFn func(*v1.Node) // The node may appear to have no labels at first, // so we wait for it to get hostname label. - if _, found := n.ObjectMeta.Labels[v1.LabelHostname]; !found { + if _, found := n.Labels[v1.LabelHostname]; !found { return false, nil } oldData, err := json.Marshal(n) if err != nil { - return false, fmt.Errorf("failed to marshal unmodified node %q into JSON: %w", n.Name, err) + return false, fmt.Errorf( + "failed to marshal unmodified node %q into JSON: %w", + n.Name, + err, + ) } // Execute the mutating function @@ -239,7 +251,11 @@ func (ki *kubeIdempotency) patchNodeOnce(nodeName string, patchFn func(*v1.Node) newData, err := json.Marshal(n) if err != nil { - return false, fmt.Errorf("failed to marshal modified node %q into JSON: %w", n.Name, err) + return false, fmt.Errorf( + "failed to marshal modified node %q into JSON: %w", + n.Name, + err, + ) } patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{}) @@ -249,7 +265,9 @@ func (ki *kubeIdempotency) patchNodeOnce(nodeName string, patchFn func(*v1.Node) if _, err := ki.client.CoreV1().Nodes().Patch(ctx, n.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}); err != nil { if apierrors.IsConflict(err) { - logger.Debug("Temporarily unable to update node metadata due to conflict (will retry)") + logger.Debug( + "Temporarily unable to update node metadata due to conflict (will retry)", + ) return false, nil } return false, fmt.Errorf("error patching node %q through apiserver: %w", n.Name, err) @@ -264,9 +282,14 @@ func (ki *kubeIdempotency) patchNodeOnce(nodeName string, patchFn func(*v1.Node) func (ki *kubeIdempotency) PatchNode(nodeName string, patchFn func(*v1.Node)) error { ctx, cancel := context.WithTimeout(context.Background(), PatchNodeTimeout) defer cancel() - return wait.PollUntilContextCancel(ctx, APICallRetryInterval, true, ki.patchNodeOnce(nodeName, patchFn)) + return wait.PollUntilContextCancel( + ctx, + APICallRetryInterval, + true, + ki.patchNodeOnce(nodeName, patchFn), + ) // wait.Poll will rerun the condition function every interval function if // the function returns false. If the condition function returns an error // then the retries end and the error is returned. - //return wait.Poll(APICallRetryInterval, PatchNodeTimeout, ki.patchNodeOnce(nodeName, patchFn)) + // return wait.Poll(APICallRetryInterval, PatchNodeTimeout, ki.patchNodeOnce(nodeName, patchFn)) } diff --git a/lifecycle/pkg/clusterfile/clusterfile_test.go b/lifecycle/pkg/clusterfile/clusterfile_test.go index 7dbdbe166f63..c01cd5fac818 100644 --- a/lifecycle/pkg/clusterfile/clusterfile_test.go +++ b/lifecycle/pkg/clusterfile/clusterfile_test.go @@ -18,12 +18,11 @@ import ( "reflect" "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" - "github.com/labring/sealos/pkg/runtime" "github.com/labring/sealos/pkg/runtime/kubernetes/types" v2 "github.com/labring/sealos/pkg/types/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" ) func Test_NewClusterFile(t *testing.T) { @@ -64,7 +63,11 @@ func Test_NewClusterFile(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"10.74.16.27:22", "10.74.16.140:22", "10.74.16.101:22"}, + IPS: []string{ + "10.74.16.27:22", + "10.74.16.140:22", + "10.74.16.101:22", + }, Roles: []string{v2.MASTER, string(v2.AMD64)}, }, }, @@ -107,7 +110,11 @@ func Test_NewClusterFile(t *testing.T) { err := cf.Process() if (err != nil) != tt.wantErr { - t.Errorf("newClusterfile(string, ...OptionFunc) error = %v, wantErr %v", err, tt.wantErr) + t.Errorf( + "newClusterfile(string, ...OptionFunc) error = %v, wantErr %v", + err, + tt.wantErr, + ) } equal := reflect.DeepEqual(cf.GetCluster(), tt.args.cluster) if !equal { @@ -148,7 +155,11 @@ func Test_NoClusterFileWithSingleSchedule(t *testing.T) { cf := NewClusterFile("") err := cf.Process() if (err != nil) != tt.wantErr { - t.Errorf("newClusterfile(string, ...OptionFunc) error = %v, wantErr %v", err, tt.wantErr) + t.Errorf( + "newClusterfile(string, ...OptionFunc) error = %v, wantErr %v", + err, + tt.wantErr, + ) } if cf.GetCluster() != nil { @@ -204,7 +215,11 @@ func Test_NewClusterFileWithSingleSchedule(t *testing.T) { }, Hosts: []v2.Host{ { - IPS: []string{"10.74.16.27:22", "10.74.16.140:22", "10.74.16.101:22"}, + IPS: []string{ + "10.74.16.27:22", + "10.74.16.140:22", + "10.74.16.101:22", + }, Roles: []string{v2.MASTER, string(v2.AMD64)}, }, }, @@ -260,7 +275,11 @@ func Test_NewClusterFileWithSingleSchedule(t *testing.T) { ) err := cf.Process() if (err != nil) != tt.wantErr { - t.Errorf("newClusterfile(string, ...OptionFunc) error = %v, wantErr %v", err, tt.wantErr) + t.Errorf( + "newClusterfile(string, ...OptionFunc) error = %v, wantErr %v", + err, + tt.wantErr, + ) } equal := reflect.DeepEqual(cf.GetCluster(), tt.args.cluster) diff --git a/lifecycle/pkg/clusterfile/decode.go b/lifecycle/pkg/clusterfile/decode.go index 7ac756258e98..8c9f4340ed0c 100644 --- a/lifecycle/pkg/clusterfile/decode.go +++ b/lifecycle/pkg/clusterfile/decode.go @@ -16,52 +16,58 @@ package clusterfile import ( "bytes" + "errors" "fmt" "io" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/util/yaml" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/types/v1beta1" fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/yaml" ) func Cluster(filepath string) (clusters []v1beta1.Cluster, err error) { decodeClusters, err := decodeCRD(filepath, constants.Cluster) if err != nil { - return nil, fmt.Errorf("failed to decode cluster from %s, %v", filepath, err) + return nil, fmt.Errorf("failed to decode cluster from %s, %w", filepath, err) } - clusters = decodeClusters.([]v1beta1.Cluster) - return + clustersTyped, ok := decodeClusters.([]v1beta1.Cluster) + if !ok { + return nil, fmt.Errorf("unexpected cluster decode type %T", decodeClusters) + } + return clustersTyped, nil } func Configs(filepath string) (configs []v1beta1.Config, err error) { decodeConfigs, err := decodeCRD(filepath, constants.Config) if err != nil { - return nil, fmt.Errorf("failed to decode config from %s, %v", filepath, err) + return nil, fmt.Errorf("failed to decode config from %s, %w", filepath, err) + } + configsTyped, ok := decodeConfigs.([]v1beta1.Config) + if !ok { + return nil, fmt.Errorf("unexpected config decode type %T", decodeConfigs) } - configs = decodeConfigs.([]v1beta1.Config) - return + return configsTyped, nil } -func decodeCRD(filepath string, kind string) (out interface{}, err error) { +func decodeCRD(filepath, kind string) (out any, err error) { data, err := fileutil.ReadAll(filepath) if err != nil { - return nil, fmt.Errorf("failed to read file %v", err) + return nil, fmt.Errorf("failed to read file %w", err) } return CRDForBytes(data, kind) } -func CRDForBytes(data []byte, kind string) (out interface{}, err error) { - keyFunc := func(v interface{}) string { +func CRDForBytes(data []byte, kind string) (out any, err error) { + keyFunc := func(v any) string { if acc, ok := v.(metav1.ObjectMetaAccessor); ok { return fmt.Sprintf("%s/%s", kind, acc.GetObjectMeta().GetName()) } - return fmt.Sprintf("%s/unknown", kind) + return kind + "/unknown" } var ( @@ -75,7 +81,7 @@ func CRDForBytes(data []byte, kind string) (out interface{}, err error) { for { ext := runtime.RawExtension{} if err = d.Decode(&ext); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } return nil, err @@ -98,7 +104,7 @@ func CRDForBytes(data []byte, kind string) (out interface{}, err error) { cluster := v1beta1.Cluster{} err = yaml.Unmarshal(ext.Raw, &cluster) if err != nil { - return nil, fmt.Errorf("decode cluster failed %v", err) + return nil, fmt.Errorf("decode cluster failed %w", err) } k := keyFunc(&cluster) if idx, ok := tmp[k]; !ok { @@ -113,7 +119,7 @@ func CRDForBytes(data []byte, kind string) (out interface{}, err error) { config := v1beta1.Config{} err = yaml.Unmarshal(ext.Raw, &config) if err != nil { - return nil, fmt.Errorf("decode config failed %v", err) + return nil, fmt.Errorf("decode config failed %w", err) } k := keyFunc(&config) if idx, ok := tmp[k]; !ok { diff --git a/lifecycle/pkg/clusterfile/pre_process.go b/lifecycle/pkg/clusterfile/pre_process.go index cf561d8ca0cf..3659e1238610 100644 --- a/lifecycle/pkg/clusterfile/pre_process.go +++ b/lifecycle/pkg/clusterfile/pre_process.go @@ -17,9 +17,7 @@ package clusterfile import ( "bytes" "errors" - - "helm.sh/helm/v3/pkg/cli/values" - "helm.sh/helm/v3/pkg/getter" + "fmt" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/runtime/decode" @@ -29,6 +27,8 @@ import ( v2 "github.com/labring/sealos/pkg/types/v1beta1" fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "helm.sh/helm/v3/pkg/cli/values" + "helm.sh/helm/v3/pkg/getter" ) var ErrClusterFileNotExists = errors.New("the cluster file is not exist") @@ -51,7 +51,7 @@ func (c *ClusterFile) Process() (err error) { return c.decode(clusterFileData) }() }) - return + return err } func (c *ClusterFile) loadClusterFile() ([]byte, error) { @@ -64,7 +64,7 @@ func (c *ClusterFile) loadClusterFile() ([]byte, error) { return nil, err } logger.Debug("loadClusterFile loadRenderValues: %+v", mergeValues) - data := map[string]interface{}{ + data := map[string]any{ "Values": mergeValues, } out := bytes.NewBuffer(nil) @@ -100,7 +100,7 @@ func (c *ClusterFile) loadClusterFile() ([]byte, error) { return out.Bytes(), nil } -func (c *ClusterFile) loadRenderValues() (map[string]interface{}, error) { +func (c *ClusterFile) loadRenderValues() (map[string]any, error) { valueOpt := &values.Options{ ValueFiles: c.customValues, Values: c.customSets, @@ -115,7 +115,7 @@ func (c *ClusterFile) decode(data []byte) error { for _, fn := range []func([]byte) error{ c.DecodeCluster, c.DecodeConfigs, c.DecodeRuntimeConfig, } { - if err := fn(data); err != nil && err != ErrTypeNotFound { + if err := fn(data); err != nil && !errors.Is(err, ErrTypeNotFound) { return err } } @@ -142,8 +142,11 @@ func (c *ClusterFile) DecodeConfigs(data []byte) error { if configs == nil { return ErrTypeNotFound } - cfgs := configs.([]v2.Config) - c.configs = cfgs + cfgList, ok := configs.([]v2.Config) + if !ok { + return fmt.Errorf("unexpected config list type %T", configs) + } + c.configs = cfgList return nil } diff --git a/lifecycle/pkg/clusterfile/util.go b/lifecycle/pkg/clusterfile/util.go index fabfd7bd3d24..926de76c261f 100644 --- a/lifecycle/pkg/clusterfile/util.go +++ b/lifecycle/pkg/clusterfile/util.go @@ -15,20 +15,20 @@ package clusterfile import ( + "errors" "fmt" "os" "strings" - k8sV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/yaml" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/runtime/decode" v2 "github.com/labring/sealos/pkg/types/v1beta1" yaml2 "github.com/labring/sealos/pkg/utils/yaml" + k8sV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" ) -var ErrClusterNotExist = fmt.Errorf("no cluster exist") +var ErrClusterNotExist = errors.New("no cluster exist") func GetDefaultClusterName() (string, error) { files, err := os.ReadDir(constants.WorkDir()) @@ -44,11 +44,12 @@ func GetDefaultClusterName() (string, error) { if len(clusters) == 1 { return clusters[0], nil } else if len(clusters) > 1 { - return "", fmt.Errorf("select a cluster through the -c parameter: " + strings.Join(clusters, ",")) + return "", errors.New("select a cluster through the -c parameter: " + strings.Join(clusters, ",")) } return "", ErrClusterNotExist } + func GetClusterFromName(clusterName string) (cluster *v2.Cluster, err error) { if clusterName == "" { clusterName, err = GetDefaultClusterName() @@ -58,18 +59,18 @@ func GetClusterFromName(clusterName string) (cluster *v2.Cluster, err error) { } clusterFile := constants.Clusterfile(clusterName) cluster, err = GetClusterFromFile(clusterFile) - return + return cluster, err } + func GetClusterFromFile(filepath string) (cluster *v2.Cluster, err error) { cluster = &v2.Cluster{} if err = yaml2.UnmarshalFile(filepath, cluster); err != nil { - return nil, fmt.Errorf("failed to get cluster from %s, %v", filepath, err) + return nil, fmt.Errorf("failed to get cluster from %s, %w", filepath, err) } return cluster, nil } func GetClusterFromDataCompatV1(data []byte) (*v2.Cluster, error) { - var cluster *v2.Cluster metaType := k8sV1.TypeMeta{} err := yaml.Unmarshal(data, &metaType) if err != nil { @@ -84,6 +85,9 @@ func GetClusterFromDataCompatV1(data []byte) (*v2.Cluster, error) { } else if c == nil { return nil, fmt.Errorf("not found type cluster from: \n%s", data) } - cluster = c.(*v2.Cluster) - return cluster, nil + clusterTyped, ok := c.(*v2.Cluster) + if !ok { + return nil, fmt.Errorf("unexpected cluster type %T", c) + } + return clusterTyped, nil } diff --git a/lifecycle/pkg/config/config.go b/lifecycle/pkg/config/config.go index 5819f5ec1688..2359b3a16234 100644 --- a/lifecycle/pkg/config/config.go +++ b/lifecycle/pkg/config/config.go @@ -21,13 +21,12 @@ import ( "path/filepath" "github.com/imdario/mergo" - "sigs.k8s.io/yaml" - "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "sigs.k8s.io/yaml" ) /* @@ -88,7 +87,7 @@ func (c *Dumper) Dump() error { } if err := c.WriteFiles(); err != nil { - return fmt.Errorf("failed to write config files %v", err) + return fmt.Errorf("failed to write config files %w", err) } return nil } @@ -114,7 +113,7 @@ func (c *Dumper) WriteFiles() (err error) { } err = file.WriteFile(configPath, configData) if err != nil { - return fmt.Errorf("write config file failed %v", err) + return fmt.Errorf("write config file failed %w", err) } } @@ -139,26 +138,26 @@ func getAppendOrInsertConfigData(path string, data []byte, insert bool) ([]byte, // merge the contents of data into the path file func getMergeConfigData(path string, data []byte) ([]byte, error) { - var configs [][]byte + configs := make([][]byte, 0, 8) // reasonable estimate for config chunks context, err := os.ReadFile(filepath.Clean(path)) if err != nil && !os.IsNotExist(err) { return nil, err } - mergeConfigMap := make(map[string]interface{}) + mergeConfigMap := make(map[string]any) err = yaml.Unmarshal(data, &mergeConfigMap) if err != nil { - return nil, fmt.Errorf("failed to unmarshal merge map: %v", err) + return nil, fmt.Errorf("failed to unmarshal merge map: %w", err) } for _, rawCfgData := range bytes.Split(context, []byte("---\n")) { - configMap := make(map[string]interface{}) + configMap := make(map[string]any) err = yaml.Unmarshal(rawCfgData, &configMap) if err != nil { - return nil, fmt.Errorf("failed to unmarshal config: %v", err) + return nil, fmt.Errorf("failed to unmarshal config: %w", err) } if err := mergo.Merge(&configMap, &mergeConfigMap, mergo.WithOverwriteWithEmptyValue, mergo.WithTypeCheck, ); err != nil { - return nil, fmt.Errorf("merge: %v", err) + return nil, fmt.Errorf("merge: %w", err) } cfg, err := yaml.Marshal(&configMap) diff --git a/lifecycle/pkg/config/config_test.go b/lifecycle/pkg/config/config_test.go index 1aab0b48ad07..ee72f20e8cd5 100644 --- a/lifecycle/pkg/config/config_test.go +++ b/lifecycle/pkg/config/config_test.go @@ -19,10 +19,9 @@ import ( "os" "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/file" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestDumper_Dump(t *testing.T) { @@ -100,7 +99,7 @@ func Test_NewConfiguration_Dump(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := file.AtomicWriteFile(filename, []byte(tt.fields.origin), 0644); err != nil { + if err := file.AtomicWriteFile(filename, []byte(tt.fields.origin), 0o644); err != nil { t.Errorf("failed to write file %v", err) } c := NewConfiguration(tt.fields.name, tt.fields.rootPath, tt.fields.configs) @@ -136,7 +135,9 @@ func Test_getMergeConfig(t *testing.T) { }, { name: "test", args: args{ - data: []byte("spec:\n template:\n metadata:\n labels:\n name: tigera-operatorssssss"), + data: []byte( + "spec:\n template:\n metadata:\n labels:\n name: tigera-operatorssssss", + ), path: "tigera-operator.yaml", }, }, @@ -148,7 +149,7 @@ func Test_getMergeConfig(t *testing.T) { t.Error(err) return } - err = os.WriteFile("test_"+tt.args.path, got, 0644) + err = os.WriteFile("test_"+tt.args.path, got, 0o644) if err != nil { t.Error(err) } @@ -209,7 +210,9 @@ func Test_getOverrideConfig(t *testing.T) { }, { name: "test", args: args{ - data: []byte("spec:\n template:\n metadata:\n labels:\n name: tigera-operatorssssss"), + data: []byte( + "spec:\n template:\n metadata:\n labels:\n name: tigera-operatorssssss", + ), path: "tigera-operator.yaml", }, }, @@ -219,7 +222,7 @@ func Test_getOverrideConfig(t *testing.T) { got := tt.args.data output := "test_" + tt.args.path - err := os.WriteFile(output, got, 0644) + err := os.WriteFile(output, got, 0o644) if err != nil { t.Error(err) } diff --git a/lifecycle/pkg/constants/bash.go b/lifecycle/pkg/constants/bash.go index 8cc2cded8a74..e92a41ee44bb 100644 --- a/lifecycle/pkg/constants/bash.go +++ b/lifecycle/pkg/constants/bash.go @@ -40,7 +40,7 @@ type Bash interface { CheckBash(host string) string InitCRIBash(host string) string CleanCRIBash(host string) string - WrapBash(host string, shell string) string + WrapBash(host, shell string) string } type bash struct { @@ -51,14 +51,14 @@ type bash struct { func (b *bash) getFromRenderContextOrDefault(key string) string { if val, ok := b.renderContext[key]; ok { - return fmt.Sprintf("bash %s", val) + return "bash " + val } return fmt.Sprintf("bash %s.sh", key) } func (b *bash) getFromRenderContext(key string) string { if val, ok := b.renderContext[key]; ok { - return fmt.Sprintf("bash %s", val) + return "bash " + val } return "" } @@ -93,10 +93,19 @@ func (b *bash) CleanRegistryBash(host string) string { func (b *bash) InitCRIBash(host string) string { return b.WrapBash(host, b.getFromRenderContext(renderInitCRI)) } + func (b *bash) CleanCRIBash(host string) string { return b.WrapBash(host, b.getFromRenderContext(renderCleanCRI)) } -func NewBash(clusterName string, renderContext map[string]string, shellWrapper func(string, string) string) Bash { - return &bash{pathResolver: NewPathResolver(clusterName), renderContext: renderContext, wrap: shellWrapper} +func NewBash( + clusterName string, + renderContext map[string]string, + shellWrapper func(string, string) string, +) Bash { + return &bash{ + pathResolver: NewPathResolver(clusterName), + renderContext: renderContext, + wrap: shellWrapper, + } } diff --git a/lifecycle/pkg/constants/pathresolver.go b/lifecycle/pkg/constants/pathresolver.go index 6b6fa92841dc..78b8eaf25163 100644 --- a/lifecycle/pkg/constants/pathresolver.go +++ b/lifecycle/pkg/constants/pathresolver.go @@ -17,7 +17,6 @@ limitations under the License. package constants import ( - "fmt" "io/fs" "os" "path" @@ -68,7 +67,7 @@ func GetRuntimeRootDir(name string) string { if v, ok := os.LookupEnv(strings.ToUpper(name) + "_RUNTIME_ROOT"); ok { return v } - return path.Join(homedir.Get(), fmt.Sprintf(".%s", name)) + return path.Join(homedir.Get(), "."+name) } func LogPath() string { diff --git a/lifecycle/pkg/env/env.go b/lifecycle/pkg/env/env.go index 5b8151696272..4c3a583152a0 100644 --- a/lifecycle/pkg/env/env.go +++ b/lifecycle/pkg/env/env.go @@ -109,7 +109,10 @@ func (p *processor) getHostEnv(hostIP string) map[string]string { excludeSysEnv := func(m map[string]string) map[string]string { m, exclude := ExcludeKeysWithPrefix(m, "SEALOS_SYS") if len(exclude) > 0 { - logger.Warn("skip %s cause envs with prefix SEALOS_SYS are sealos system only", strings.Join(exclude, ", ")) + logger.Warn( + "skip %s cause envs with prefix SEALOS_SYS are sealos system only", + strings.Join(exclude, ", "), + ) } return m } diff --git a/lifecycle/pkg/env/env_shim_test.go b/lifecycle/pkg/env/env_shim_test.go index 55e98beeaa8a..0af72f2f26e2 100644 --- a/lifecycle/pkg/env/env_shim_test.go +++ b/lifecycle/pkg/env/env_shim_test.go @@ -23,7 +23,12 @@ import ( func getShimCluster1() *v2.Cluster { return &v2.Cluster{ Spec: v2.ClusterSpec{ - Env: []string{"registryDomain=sealos.hub", "registryPort=5000", "registryUsername=admin", "registryPassword=passw0rd"}, + Env: []string{ + "registryDomain=sealos.hub", + "registryPort=5000", + "registryUsername=admin", + "registryPassword=passw0rd", + }, Hosts: []v2.Host{ { IPS: []string{"192.168.0.2", "192.168.0.3", "192.168.0.4"}, @@ -51,10 +56,16 @@ func getShimCluster1() *v2.Cluster { }, } } + func getShimCluster2() *v2.Cluster { return &v2.Cluster{ Spec: v2.ClusterSpec{ - Env: []string{"registryDomain=sealos.hub", "registryPort=5000", "registryUsername=admin", "registryPassword=passw0rd"}, + Env: []string{ + "registryDomain=sealos.hub", + "registryPort=5000", + "registryUsername=admin", + "registryPassword=passw0rd", + }, Hosts: []v2.Host{ { IPS: []string{"192.168.0.2", "192.168.0.3", "192.168.0.4"}, @@ -82,10 +93,16 @@ func getShimCluster2() *v2.Cluster { }, } } + func getShimCluster3() *v2.Cluster { return &v2.Cluster{ Spec: v2.ClusterSpec{ - Env: []string{"registryDomain=sealos.hub", "registryPort=5000", "registryUsername=admin", "registryPassword=passw0rd"}, + Env: []string{ + "registryDomain=sealos.hub", + "registryPort=5000", + "registryUsername=admin", + "registryPassword=passw0rd", + }, Hosts: []v2.Host{ { IPS: []string{"192.168.0.2", "192.168.0.3", "192.168.0.4"}, diff --git a/lifecycle/pkg/exec/exec.go b/lifecycle/pkg/exec/exec.go index 573402741d72..d6d3adf64028 100644 --- a/lifecycle/pkg/exec/exec.go +++ b/lifecycle/pkg/exec/exec.go @@ -23,14 +23,13 @@ import ( "path/filepath" "strings" - "k8s.io/apimachinery/pkg/util/sets" - "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/unshare" fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" netutil "github.com/labring/sealos/pkg/utils/net" + "k8s.io/apimachinery/pkg/util/sets" ) type Interface ssh.Interface @@ -43,7 +42,7 @@ type wrap struct { func New(inner ssh.Interface) (Interface, error) { addr, err := net.InterfaceAddrs() if err != nil { - return nil, fmt.Errorf("could not get local addresses: %v", err) + return nil, fmt.Errorf("could not get local addresses: %w", err) } return &wrap{ inner: inner, @@ -59,7 +58,7 @@ func (w *wrap) Ping(host string) error { return w.inner.Ping(host) } -func (w *wrap) Cmd(host string, command string) ([]byte, error) { +func (w *wrap) Cmd(host, command string) ([]byte, error) { if w.isLocal(host) { // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command b, err := exec.Command("/bin/bash", "-c", command).CombinedOutput() @@ -72,7 +71,7 @@ func (w *wrap) CmdAsyncWithContext(ctx context.Context, host string, commands .. if w.isLocal(host) { for i := range commands { // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command - cmd := exec.CommandContext(ctx, "/bin/bash", "-c", commands[i]) + cmd := exec.CommandContext(ctx, "/bin/bash", "-c", commands[i]) // #nosec G204 cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -96,7 +95,7 @@ func warnIfNotAbs(path string) { } } -func (w *wrap) Copy(host string, src string, dest string) error { +func (w *wrap) Copy(host, src, dest string) error { if w.isLocal(host) { warnIfNotAbs(src) warnIfNotAbs(dest) @@ -106,7 +105,7 @@ func (w *wrap) Copy(host string, src string, dest string) error { return w.inner.Copy(host, src, dest) } -func (w *wrap) Fetch(host string, src string, dest string) error { +func (w *wrap) Fetch(host, src, dest string) error { if w.isLocal(host) { warnIfNotAbs(src) warnIfNotAbs(dest) @@ -127,7 +126,7 @@ func (w *wrap) CmdToString(host, cmd, sep string) (string, error) { return getOnelineResult(string(output), sep), nil } -func getOnelineResult(output string, sep string) string { +func getOnelineResult(output, sep string) string { return strings.ReplaceAll(strings.ReplaceAll(output, "\r\n", sep), "\n", sep) } diff --git a/lifecycle/pkg/filesystem/filesystem.go b/lifecycle/pkg/filesystem/filesystem.go index 56020f7fc82e..f4bc43543c82 100644 --- a/lifecycle/pkg/filesystem/filesystem.go +++ b/lifecycle/pkg/filesystem/filesystem.go @@ -28,5 +28,5 @@ type Mounter interface { } type RegistrySyncer interface { - Sync(context.Context, ...string) error + Sync(ctx context.Context, hosts ...string) error } diff --git a/lifecycle/pkg/filesystem/registry/sync.go b/lifecycle/pkg/filesystem/registry/sync.go index 285d1366010f..9256b31ade3c 100644 --- a/lifecycle/pkg/filesystem/registry/sync.go +++ b/lifecycle/pkg/filesystem/registry/sync.go @@ -25,11 +25,6 @@ import ( "github.com/containers/image/v5/copy" "github.com/containers/image/v5/types" - "golang.org/x/sync/errgroup" - - "github.com/labring/sreg/pkg/registry/handler" - "github.com/labring/sreg/pkg/registry/sync" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/filesystem" @@ -38,6 +33,9 @@ import ( "github.com/labring/sealos/pkg/utils/file" httputils "github.com/labring/sealos/pkg/utils/http" "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sreg/pkg/registry/handler" + "github.com/labring/sreg/pkg/registry/sync" + "golang.org/x/sync/errgroup" ) const ( @@ -79,7 +77,8 @@ func (s *impl) Sync(ctx context.Context, hosts ...string) error { logger.Debug("running temporary registry on host %s", host) if err := s.execer.CmdAsyncWithContext(ctx, host, getRegistryServeCommand(s.pathResolver, defaultTemporaryPort)); err != nil { // ignore expected signal killed error when context cancel - if !strings.Contains(err.Error(), "signal: killed") && !strings.Contains(err.Error(), "context canceled") { + if !strings.Contains(err.Error(), "signal: killed") && + !strings.Contains(err.Error(), "context canceled") { logger.Error(err) } } @@ -99,7 +98,11 @@ func (s *impl) Sync(ctx context.Context, hosts ...string) error { defer cancel() ep := sync.ParseRegistryAddress(trimPortStr(target), defaultTemporaryPort) if err := httputils.WaitUntilEndpointAlive(probeCtx, "http://"+ep); err != nil { - logger.Warn("cannot connect to remote temporary registry %s: %v, fallback using ssh mode instead", ep, err) + logger.Warn( + "cannot connect to remote temporary registry %s: %v, fallback using ssh mode instead", + ep, + err, + ) syncOptionChan <- &syncOption{target: target, typ: sshMode} } else { syncOptionChan <- &syncOption{target: ep, typ: httpMode} @@ -109,7 +112,7 @@ func (s *impl) Sync(ctx context.Context, hosts ...string) error { }() eg, _ := errgroup.WithContext(ctx) - for i := 0; i < len(hosts); i++ { + for range hosts { opt, ok := <-syncOptionChan if !ok { break @@ -126,7 +129,7 @@ func (s *impl) Sync(ctx context.Context, hosts ...string) error { case sshMode: err = syncViaSSH(ctx, s, opt.target, registryDir) } - return + return err }) } } @@ -146,11 +149,11 @@ func getRegistryServeCommand(pathResolver constants.PathResolver, port string) s ) } -func syncViaSSH(_ context.Context, s *impl, target string, localDir string) error { +func syncViaSSH(_ context.Context, s *impl, target, localDir string) error { return ssh.CopyDir(s.execer, target, localDir, s.pathResolver.RootFSRegistryPath(), nil) } -func syncViaHTTP(ctx context.Context, target string, localDir string) error { +func syncViaHTTP(ctx context.Context, target, localDir string) error { sys := &types.SystemContext{ DockerInsecureSkipTLSVerify: types.OptionalBoolTrue, } @@ -182,12 +185,17 @@ func syncViaHTTP(ctx context.Context, target string, localDir string) error { OmitError: true, } - if err = sync.ToRegistry(ctx, opts); err != nil && !strings.Contains(err.Error(), "manifest unknown") { + if err = sync.ToRegistry(ctx, opts); err != nil && + !strings.Contains(err.Error(), "manifest unknown") { return err } return nil } -func New(pathResolver constants.PathResolver, execer exec.Interface, mounts []v2.MountImage) filesystem.RegistrySyncer { +func New( + pathResolver constants.PathResolver, + execer exec.Interface, + mounts []v2.MountImage, +) filesystem.RegistrySyncer { return &impl{pathResolver, execer, mounts} } diff --git a/lifecycle/pkg/filesystem/rootfs/rootfs_default.go b/lifecycle/pkg/filesystem/rootfs/rootfs_default.go index 3215d2d11b56..74a73236c27c 100644 --- a/lifecycle/pkg/filesystem/rootfs/rootfs_default.go +++ b/lifecycle/pkg/filesystem/rootfs/rootfs_default.go @@ -24,8 +24,6 @@ import ( "path/filepath" "strings" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/env" "github.com/labring/sealos/pkg/exec" @@ -37,6 +35,7 @@ import ( "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/maps" stringsutil "github.com/labring/sealos/pkg/utils/strings" + "golang.org/x/sync/errgroup" ) type defaultRootfs struct { @@ -59,25 +58,26 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error envProcessor := env.NewEnvProcessor(cluster) // TODO: remove this when rendering on client side is GA for _, mount := range f.mounts { - src := mount eg.Go(func() error { - if !file.IsExist(src.MountPoint) { - logger.Debug("Image %s not exist, render env continue", src.ImageName) + if !file.IsExist(mount.MountPoint) { + logger.Debug("Image %s not exist, render env continue", mount.ImageName) return nil } // TODO: if we are planing to support rendering templates for each host, // then move this rendering process before ssh.CopyDir and do it one by one. - envs := v2.MergeEnvWithBuiltinKeys(src.Env, src) - err := renderTemplatesWithEnv(src.MountPoint, ipList, envProcessor, envs) + envs := v2.MergeEnvWithBuiltinKeys(mount.Env, mount) + err := renderTemplatesWithEnv(mount.MountPoint, ipList, envProcessor, envs) if err != nil { return fmt.Errorf("failed to render env: %w", err) } - dirs, err := file.StatDir(src.MountPoint, true) + dirs, err := file.StatDir(mount.MountPoint, true) if err != nil { return fmt.Errorf("failed to stat files: %w", err) } if len(dirs) != 0 { - _, err = executils.RunBashCmd(fmt.Sprintf(constants.DefaultChmodBash, src.MountPoint)) + _, err = executils.RunBashCmd( + fmt.Sprintf(constants.DefaultChmodBash, mount.MountPoint), + ) if err != nil { return fmt.Errorf("run chmod to rootfs failed: %w", err) } @@ -98,7 +98,12 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error notRegistryDirFilter := func(entry fs.DirEntry) bool { return !constants.IsRegistryDir(entry) } copyFn := func(m v2.MountImage, targetHost, targetDir string) error { - logger.Debug("send mount image, target: %s, image: %s, type: %s", targetHost, m.ImageName, m.Type) + logger.Debug( + "send mount image, target: %s, image: %s, type: %s", + targetHost, + m.ImageName, + m.Type, + ) if err := ssh.CopyDir(execer, targetHost, m.MountPoint, targetDir, notRegistryDirFilter); err != nil { logger.Error("error occur while sending mount image %s: %v", m.Name, err) return err @@ -114,8 +119,7 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error } rootfsEnvs := v2.MergeEnvWithBuiltinKeys(rootfs.Env, *rootfs) - for idx := range ipList { - ip := ipList[idx] + for _, ip := range ipList { eg.Go(func() error { var renderingRequired bool for i := range f.mounts { @@ -153,7 +157,10 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error return err } renderCommand := getRenderCommand(pathResolver.RootFSSealctlPath(), targetDir) - return execer.CmdAsync(master0, stringsutil.RenderShellWithEnv(renderCommand, mountInfo.Env)) + return execer.CmdAsync( + master0, + stringsutil.RenderShellWithEnv(renderCommand, mountInfo.Env), + ) } return nil }) @@ -161,7 +168,7 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error return eg.Wait() } -func getRenderCommand(binary string, target string) string { +func getRenderCommand(binary, target string) string { // skip if sealctl doesn't has subcommand render return fmt.Sprintf("%s render --debug=%v --clear %s 2>/dev/null || true", binary, logger.IsDebugMode(), @@ -174,8 +181,8 @@ func getRenderCommand(binary string, target string) string { func (f *defaultRootfs) unmountRootfs(cluster *v2.Cluster, ipList []string) error { clusterRootfsDir := constants.NewPathResolver(cluster.Name).Root() - rmRootfs := fmt.Sprintf("rm -rf %s", clusterRootfsDir) - deleteHomeDirCmd := fmt.Sprintf("rm -rf %s", constants.ClusterDir(cluster.Name)) + rmRootfs := "rm -rf " + clusterRootfsDir + deleteHomeDirCmd := "rm -rf " + constants.ClusterDir(cluster.Name) eg, _ := errgroup.WithContext(context.Background()) sshClient := ssh.NewCacheClientFromCluster(cluster, true) execer, err := exec.New(sshClient) @@ -183,8 +190,7 @@ func (f *defaultRootfs) unmountRootfs(cluster *v2.Cluster, ipList []string) erro return err } - for _, IP := range ipList { - ip := IP + for _, ip := range ipList { eg.Go(func() error { return execer.CmdAsync(ip, rmRootfs, deleteHomeDirCmd) }) @@ -192,7 +198,12 @@ func (f *defaultRootfs) unmountRootfs(cluster *v2.Cluster, ipList []string) erro return eg.Wait() } -func renderTemplatesWithEnv(mountDir string, ipList []string, p env.Interface, envs map[string]string) error { +func renderTemplatesWithEnv( + mountDir string, + ipList []string, + p env.Interface, + envs map[string]string, +) error { // currently only render once return p.RenderAll(ipList[0], mountDir, envs) } diff --git a/lifecycle/pkg/guest/guest.go b/lifecycle/pkg/guest/guest.go index 3a76372e4f3d..722422b23701 100644 --- a/lifecycle/pkg/guest/guest.go +++ b/lifecycle/pkg/guest/guest.go @@ -18,8 +18,6 @@ import ( "context" "strings" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/fork/golang/expansion" "github.com/labring/sealos/pkg/env" "github.com/labring/sealos/pkg/exec" @@ -27,6 +25,7 @@ import ( v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/maps" stringsutil "github.com/labring/sealos/pkg/utils/strings" + "golang.org/x/sync/errgroup" ) type Interface interface { @@ -79,18 +78,39 @@ func (d *Default) Apply(cluster *v2.Cluster, mounts []v2.MountImage, targetHosts return nil } -func formalizeImageCommands(cluster *v2.Cluster, index int, m v2.MountImage, extraEnvs map[string]string) []string { +func formalizeImageCommands( + cluster *v2.Cluster, + index int, + m v2.MountImage, + extraEnvs map[string]string, +) []string { envs := maps.Merge(m.Env, extraEnvs) envs = v2.MergeEnvWithBuiltinKeys(envs, m) mapping := expansion.MappingFuncFor(envs) cmds := make([]string, 0) for i := range m.Entrypoint { - cmds = append(cmds, FormalizeWorkingCommand(cluster.Name, m.Name, m.Type, expansion.Expand(m.Entrypoint[i], mapping))) + cmds = append( + cmds, + FormalizeWorkingCommand( + cluster.Name, + m.Name, + m.Type, + expansion.Expand(m.Entrypoint[i], mapping), + ), + ) } if index == 0 && len(cluster.Spec.Command) > 0 { for i := range cluster.Spec.Command { - cmds = append(cmds, FormalizeWorkingCommand(cluster.Name, m.Name, m.Type, expansion.Expand(cluster.Spec.Command[i], mapping))) + cmds = append( + cmds, + FormalizeWorkingCommand( + cluster.Name, + m.Name, + m.Type, + expansion.Expand(cluster.Spec.Command[i], mapping), + ), + ) } } else { for i := range m.Cmd { diff --git a/lifecycle/pkg/guest/guest_test.go b/lifecycle/pkg/guest/guest_test.go index 638c187891b1..56e551f0099a 100644 --- a/lifecycle/pkg/guest/guest_test.go +++ b/lifecycle/pkg/guest/guest_test.go @@ -22,16 +22,18 @@ import ( "testing" "github.com/labring/sealos/pkg/constants" - v2 "github.com/labring/sealos/pkg/types/v1beta1" ) func TestDefault_getGuestCmd(t *testing.T) { shell := func(cName, containerName, cmd string) string { - return fmt.Sprintf(constants.CdAndExecCmd, constants.GetAppWorkDir(cName, containerName), cmd) - } - type fields struct { + return fmt.Sprintf( + constants.CdAndExecCmd, + constants.GetAppWorkDir(cName, containerName), + cmd, + ) } + type fields struct{} type args struct { envs map[string]string cluster *v2.Cluster @@ -96,8 +98,10 @@ func TestDefault_getGuestCmd(t *testing.T) { name: "default-cmd-override", fields: fields{}, args: args{ - envs: map[string]string{"IFACE": "default"}, - cluster: &v2.Cluster{Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$(IFACE)\" sh ovn-install.sh"}}}, + envs: map[string]string{"IFACE": "default"}, + cluster: &v2.Cluster{ + Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$(IFACE)\" sh ovn-install.sh"}}, + }, mounts: []v2.MountImage{ { Cmd: []string{"IFACE=\"$(IFACE)\" bash ovn-install.sh"}, @@ -112,8 +116,10 @@ func TestDefault_getGuestCmd(t *testing.T) { name: "default-entrypoint-cmd-override", fields: fields{}, args: args{ - envs: map[string]string{"IFACE": "default"}, - cluster: &v2.Cluster{Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$(IFACE)\" sh ovn-install.sh"}}}, + envs: map[string]string{"IFACE": "default"}, + cluster: &v2.Cluster{ + Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$(IFACE)\" sh ovn-install.sh"}}, + }, mounts: []v2.MountImage{ { Cmd: []string{"IFACE=\"$(IFACE)\" bash ovn-install.sh"}, @@ -122,13 +128,19 @@ func TestDefault_getGuestCmd(t *testing.T) { }, }, }, - want: []string{shell("", "", "AA=default"), shell("", "", "IFACE=\"default\" sh ovn-install.sh")}, + want: []string{ + shell("", "", "AA=default"), + shell("", "", "IFACE=\"default\" sh ovn-install.sh"), + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for i := range tt.args.mounts { - if got := formalizeImageCommands(tt.args.cluster, i, tt.args.mounts[i], tt.args.envs); !reflect.DeepEqual(got, tt.want) { + if got := formalizeImageCommands(tt.args.cluster, i, tt.args.mounts[i], tt.args.envs); !reflect.DeepEqual( + got, + tt.want, + ) { t.Errorf("getGuestCmd() = %v, want %v", got, tt.want) } } @@ -138,10 +150,13 @@ func TestDefault_getGuestCmd(t *testing.T) { func TestDefault_getGuestCmd_dollarBraceVariable(t *testing.T) { shell := func(cName, containerName, cmd string) string { - return fmt.Sprintf(constants.CdAndExecCmd, constants.GetAppWorkDir(cName, containerName), cmd) - } - type fields struct { + return fmt.Sprintf( + constants.CdAndExecCmd, + constants.GetAppWorkDir(cName, containerName), + cmd, + ) } + type fields struct{} type args struct { envs map[string]string cluster *v2.Cluster @@ -206,8 +221,10 @@ func TestDefault_getGuestCmd_dollarBraceVariable(t *testing.T) { name: "default-cmd-override", fields: fields{}, args: args{ - envs: map[string]string{"IFACE": "default"}, - cluster: &v2.Cluster{Spec: v2.ClusterSpec{Command: []string{"IFACE=\"${IFACE}\" sh ovn-install.sh"}}}, + envs: map[string]string{"IFACE": "default"}, + cluster: &v2.Cluster{ + Spec: v2.ClusterSpec{Command: []string{"IFACE=\"${IFACE}\" sh ovn-install.sh"}}, + }, mounts: []v2.MountImage{ { Cmd: []string{"IFACE=\"${IFACE}\" bash ovn-install.sh"}, @@ -222,8 +239,10 @@ func TestDefault_getGuestCmd_dollarBraceVariable(t *testing.T) { name: "default-entrypoint-cmd-override", fields: fields{}, args: args{ - envs: map[string]string{"IFACE": "default"}, - cluster: &v2.Cluster{Spec: v2.ClusterSpec{Command: []string{"IFACE=\"${IFACE}\" sh ovn-install.sh"}}}, + envs: map[string]string{"IFACE": "default"}, + cluster: &v2.Cluster{ + Spec: v2.ClusterSpec{Command: []string{"IFACE=\"${IFACE}\" sh ovn-install.sh"}}, + }, mounts: []v2.MountImage{ { Cmd: []string{"IFACE=\"${IFACE}\" bash ovn-install.sh"}, @@ -232,13 +251,19 @@ func TestDefault_getGuestCmd_dollarBraceVariable(t *testing.T) { }, }, }, - want: []string{shell("", "", "AA=default"), shell("", "", "IFACE=\"default\" sh ovn-install.sh")}, + want: []string{ + shell("", "", "AA=default"), + shell("", "", "IFACE=\"default\" sh ovn-install.sh"), + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for i := range tt.args.mounts { - if got := formalizeImageCommands(tt.args.cluster, i, tt.args.mounts[i], tt.args.envs); !reflect.DeepEqual(got, tt.want) { + if got := formalizeImageCommands(tt.args.cluster, i, tt.args.mounts[i], tt.args.envs); !reflect.DeepEqual( + got, + tt.want, + ) { t.Errorf("getGuestCmd() = %v, want %v", got, tt.want) } } @@ -248,10 +273,13 @@ func TestDefault_getGuestCmd_dollarBraceVariable(t *testing.T) { func TestDefault_getGuestCmd_dollarVariable(t *testing.T) { shell := func(cName, containerName, cmd string) string { - return fmt.Sprintf(constants.CdAndExecCmd, constants.GetAppWorkDir(cName, containerName), cmd) - } - type fields struct { + return fmt.Sprintf( + constants.CdAndExecCmd, + constants.GetAppWorkDir(cName, containerName), + cmd, + ) } + type fields struct{} type args struct { envs map[string]string cluster *v2.Cluster @@ -316,8 +344,10 @@ func TestDefault_getGuestCmd_dollarVariable(t *testing.T) { name: "default-cmd-override", fields: fields{}, args: args{ - envs: map[string]string{"IFACE": "default"}, - cluster: &v2.Cluster{Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$IFACE\" sh ovn-install.sh"}}}, + envs: map[string]string{"IFACE": "default"}, + cluster: &v2.Cluster{ + Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$IFACE\" sh ovn-install.sh"}}, + }, mounts: []v2.MountImage{ { Cmd: []string{"IFACE=\"$IFACE\" bash ovn-install.sh"}, @@ -332,8 +362,10 @@ func TestDefault_getGuestCmd_dollarVariable(t *testing.T) { name: "default-entrypoint-cmd-override", fields: fields{}, args: args{ - envs: map[string]string{"IFACE": "default"}, - cluster: &v2.Cluster{Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$IFACE\" sh ovn-install.sh"}}}, + envs: map[string]string{"IFACE": "default"}, + cluster: &v2.Cluster{ + Spec: v2.ClusterSpec{Command: []string{"IFACE=\"$IFACE\" sh ovn-install.sh"}}, + }, mounts: []v2.MountImage{ { Cmd: []string{"IFACE=\"$IFACE\" bash ovn-install.sh"}, @@ -342,13 +374,19 @@ func TestDefault_getGuestCmd_dollarVariable(t *testing.T) { }, }, }, - want: []string{shell("", "", "AA=default"), shell("", "", "IFACE=\"default\" sh ovn-install.sh")}, + want: []string{ + shell("", "", "AA=default"), + shell("", "", "IFACE=\"default\" sh ovn-install.sh"), + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for i := range tt.args.mounts { - if got := formalizeImageCommands(tt.args.cluster, i, tt.args.mounts[i], tt.args.envs); !reflect.DeepEqual(got, tt.want) { + if got := formalizeImageCommands(tt.args.cluster, i, tt.args.mounts[i], tt.args.envs); !reflect.DeepEqual( + got, + tt.want, + ) { t.Errorf("getGuestCmd() = %v, want %v", got, tt.want) } } diff --git a/lifecycle/pkg/guest/util.go b/lifecycle/pkg/guest/util.go index af7ba31242ff..79dde6cabbda 100644 --- a/lifecycle/pkg/guest/util.go +++ b/lifecycle/pkg/guest/util.go @@ -23,7 +23,11 @@ import ( "github.com/labring/sealos/pkg/types/v1beta1" ) -func FormalizeWorkingCommand(clusterName string, imageName string, t v1beta1.ImageType, cmd string) string { +func FormalizeWorkingCommand( + clusterName, imageName string, + t v1beta1.ImageType, + cmd string, +) string { if cmd == "" { return "" } @@ -31,7 +35,11 @@ func FormalizeWorkingCommand(clusterName string, imageName string, t v1beta1.Ima case v1beta1.RootfsImage, v1beta1.PatchImage: return fmt.Sprintf(constants.CdAndExecCmd, constants.GetRootWorkDir(clusterName), cmd) case v1beta1.AppImage, "": - return fmt.Sprintf(constants.CdAndExecCmd, constants.GetAppWorkDir(clusterName, imageName), cmd) + return fmt.Sprintf( + constants.CdAndExecCmd, + constants.GetAppWorkDir(clusterName, imageName), + cmd, + ) } return "" } diff --git a/lifecycle/pkg/image/merge.go b/lifecycle/pkg/image/merge.go index c2d97d6390ed..bb5634066f20 100644 --- a/lifecycle/pkg/image/merge.go +++ b/lifecycle/pkg/image/merge.go @@ -22,10 +22,9 @@ import ( "strings" "text/template" - v1 "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/maps" + v1 "github.com/opencontainers/image-spec/specs-go/v1" ) func escapeDollarSign(s string, cmd bool) string { @@ -33,7 +32,7 @@ func escapeDollarSign(s string, cmd bool) string { return strings.ReplaceAll(s, "$", "\\$") } var buffer bytes.Buffer - for i := 0; i < len(s); i++ { + for i := range len(s) { if s[i] == '$' { if i+1 < len(s) && s[i+1] == '(' { buffer.WriteByte(s[i]) @@ -60,7 +59,9 @@ func MergeDockerfileFromImages(imageObjList []map[string]v1.Image) (string, erro 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) { + if key := maps.GetFromKeys(val.Config.Labels, v1beta1.ImageTypeKeys...); key == string( + v1beta1.RootfsImage, + ) { isRootfs = true } } diff --git a/lifecycle/pkg/image/merge_test.go b/lifecycle/pkg/image/merge_test.go index 8877f69e1285..729b1f29c519 100644 --- a/lifecycle/pkg/image/merge_test.go +++ b/lifecycle/pkg/image/merge_test.go @@ -112,10 +112,8 @@ COPY --from=cccc . .`, t.Run(tt.name, func(t *testing.T) { if data, err := MergeDockerfileFromImages(tt.args.ociList); (err != nil) != tt.wantErr { t.Errorf("MergeDockerfileFromImages() error = %v, wantErr %v", err, tt.wantErr) - } else { - if data != tt.wantData { - t.Errorf("MergeDockerfileFromImages() data = %v, wantData %v", data, tt.wantData) - } + } else if data != tt.wantData { + t.Errorf("MergeDockerfileFromImages() data = %v, wantData %v", data, tt.wantData) } }) } diff --git a/lifecycle/pkg/ipvs/lvscare.go b/lifecycle/pkg/ipvs/lvscare.go index bfcdf541cda3..846e88e84e6a 100644 --- a/lifecycle/pkg/ipvs/lvscare.go +++ b/lifecycle/pkg/ipvs/lvscare.go @@ -15,26 +15,30 @@ package ipvs import ( + "errors" "fmt" + "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/types/v1beta1" - + "github.com/labring/sealos/pkg/utils/hosts" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/scheme" - - "github.com/labring/sealos/pkg/constants" - "github.com/labring/sealos/pkg/utils/hosts" ) const ( LvsCareCommand = "/usr/bin/lvscare" ) -func LvsStaticPodYaml(vip string, masters []string, image, name string, options []string) (string, error) { +func LvsStaticPodYaml( + vip string, + masters []string, + image, name string, + options []string, +) (string, error) { if vip == "" || len(masters) == 0 { - return "", fmt.Errorf("vip and mster not allow empty") + return "", errors.New("vip and mster not allow empty") } if image == "" { image = v1beta1.DefaultLvsCareImage diff --git a/lifecycle/pkg/registry/commands/password.go b/lifecycle/pkg/registry/commands/password.go index 36947f41fdd3..ad345ce87aa9 100644 --- a/lifecycle/pkg/registry/commands/password.go +++ b/lifecycle/pkg/registry/commands/password.go @@ -19,16 +19,15 @@ package commands import ( "fmt" - "github.com/spf13/cobra" - "github.com/labring/sealos/pkg/registry/password" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" ) func NewRegistryPasswdCmd() *cobra.Command { flagsResults := password.RegistryPasswdResults{} - var registryPasswdCmd = &cobra.Command{ + registryPasswdCmd := &cobra.Command{ Use: "passwd", Short: "configure registry password", RunE: func(cmd *cobra.Command, args []string) error { @@ -41,7 +40,7 @@ func NewRegistryPasswdCmd() *cobra.Command { return nil } if err := flagsResults.Apply(cluster); err != nil { - return fmt.Errorf("registry passwd apply error: %v", err) + return fmt.Errorf("registry passwd apply error: %w", err) } logger.Info("registry passwd apply success") return nil diff --git a/lifecycle/pkg/registry/helpers/helpers.go b/lifecycle/pkg/registry/helpers/helpers.go index 207906b5927d..88ab10e71073 100644 --- a/lifecycle/pkg/registry/helpers/helpers.go +++ b/lifecycle/pkg/registry/helpers/helpers.go @@ -15,24 +15,24 @@ package helpers import ( - "fmt" "path" "github.com/labring/image-cri-shim/pkg/types" - - "k8s.io/apimachinery/pkg/util/yaml" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" + "k8s.io/apimachinery/pkg/util/yaml" ) const RegistryCustomConfig = "registry.yml" -func GetRegistryInfo(execer exec.Interface, rootfs, defaultRegistry string) *v1beta1.RegistryConfig { - var DefaultConfig = &v1beta1.RegistryConfig{ +func GetRegistryInfo( + execer exec.Interface, + rootfs, defaultRegistry string, +) *v1beta1.RegistryConfig { + DefaultConfig := &v1beta1.RegistryConfig{ IP: iputils.GetHostIP(defaultRegistry), Domain: constants.DefaultRegistryDomain, Port: "5000", @@ -41,7 +41,7 @@ func GetRegistryInfo(execer exec.Interface, rootfs, defaultRegistry string) *v1b Data: constants.DefaultRegistryData, } etcPath := path.Join(rootfs, constants.EtcDirName, RegistryCustomConfig) - out, err := execer.Cmd(defaultRegistry, fmt.Sprintf("cat %s", etcPath)) + out, err := execer.Cmd(defaultRegistry, "cat "+etcPath) if err != nil { logger.Warn("load registry config error: %+v, using default registry config", err) return DefaultConfig @@ -62,12 +62,17 @@ func GetRegistryInfo(execer exec.Interface, rootfs, defaultRegistry string) *v1b if readConfig.Port == "" { readConfig.Port = DefaultConfig.Port } - logger.Debug("show registry info, IP: %s, Domain: %s, Data: %s", readConfig.IP, readConfig.Domain, readConfig.Data) + logger.Debug( + "show registry info, IP: %s, Domain: %s, Data: %s", + readConfig.IP, + readConfig.Domain, + readConfig.Data, + ) return readConfig } func GetImageCRIShimInfo(execer exec.Interface, config, defaultIP string) *types.Config { - out, _ := execer.Cmd(defaultIP, fmt.Sprintf("cat %s", config)) + out, _ := execer.Cmd(defaultIP, "cat "+config) logger.Debug("image shim data info: %s", string(out)) readConfig := &types.Config{} err := yaml.Unmarshal(out, &readConfig) diff --git a/lifecycle/pkg/registry/password/apply.go b/lifecycle/pkg/registry/password/apply.go index 3fe1c134d55c..932df3d78153 100644 --- a/lifecycle/pkg/registry/password/apply.go +++ b/lifecycle/pkg/registry/password/apply.go @@ -22,9 +22,6 @@ import ( "path" "strings" - "github.com/modood/table" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/clusterfile" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" @@ -34,6 +31,8 @@ import ( "github.com/labring/sealos/pkg/utils/confirm" fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "github.com/modood/table" + "github.com/spf13/pflag" ) type RegistryPasswdResults struct { @@ -50,8 +49,20 @@ type RegistryPasswdResults struct { func (r *RegistryPasswdResults) RegisterFlags(fs *pflag.FlagSet) { fs.SetInterspersed(false) fs.StringVarP(&r.ClusterName, "cluster-name", "c", "default", "cluster name") - fs.StringVarP(&r.HtpasswdPath, "htpasswd-path", "p", "/etc/registry/registry_htpasswd", "registry passwd file path") - fs.StringVarP(&r.ImageCRIShimFilePath, "cri-shim-file-path", "f", "/etc/image-cri-shim.yaml", "image cri shim file path,if empty will not update image cri shim file") + fs.StringVarP( + &r.HtpasswdPath, + "htpasswd-path", + "p", + "/etc/registry/registry_htpasswd", + "registry passwd file path", + ) + fs.StringVarP( + &r.ImageCRIShimFilePath, + "cri-shim-file-path", + "f", + "/etc/image-cri-shim.yaml", + "image cri shim file path,if empty will not update image cri shim file", + ) } type confirmPrint struct { @@ -75,19 +86,30 @@ func (r *RegistryPasswdResults) Validate() (*v1beta1.Cluster, error) { clusterFile := clusterfile.NewClusterFile(clusterPath) err := clusterFile.Process() if err != nil { - return nil, fmt.Errorf("cluster %s process error: %+v", r.ClusterName, err) + return nil, fmt.Errorf("cluster %s process error: %+w", r.ClusterName, err) } cluster := clusterFile.GetCluster() - r.RegistryType = confirm.SelectInput("Please select registry type", []string{string(RegistryTypeRegistry), string(RegistryTypeContainerd), string(RegistryTypeDocker)}) + r.RegistryType = confirm.SelectInput( + "Please select registry type", + []string{ + string(RegistryTypeRegistry), + string(RegistryTypeContainerd), + string(RegistryTypeDocker), + }, + ) if r.RegistryType == "" { return nil, errors.New("invalid registry type") } - r.RegistryUsername = confirm.Input("Please input registry username", "admin", func(input string) error { - if len(input) < 3 { - return errors.New("username must have more than 3 characters") - } - return nil - }) + r.RegistryUsername = confirm.Input( + "Please input registry username", + "admin", + func(input string) error { + if len(input) < 3 { + return errors.New("username must have more than 3 characters") + } + return nil + }, + ) r.RegistryPasswd = confirm.PasswordInput("Please input registry password") if r.RegistryUsername == "" || r.RegistryPasswd == "" { return nil, errors.New("must provide registry username and password") @@ -145,13 +167,17 @@ func (r *RegistryPasswdResults) Apply(cluster *v1beta1.Cluster) error { } root := constants.NewPathResolver(cluster.Name).RootFSPath() registry := helpers.GetRegistryInfo(r.execer, root, cluster.GetRegistryIPAndPort()) - shim := helpers.GetImageCRIShimInfo(r.execer, r.ImageCRIShimFilePath, cluster.GetMaster0IPAndPort()) + shim := helpers.GetImageCRIShimInfo( + r.execer, + r.ImageCRIShimFilePath, + cluster.GetMaster0IPAndPort(), + ) if registry == nil || shim == nil { return errors.New("get registry or shim info error") } registry.Username = r.RegistryUsername registry.Password = r.RegistryPasswd - shim.Auth = fmt.Sprintf("%s:%s", r.RegistryUsername, r.RegistryPasswd) + shim.Auth = r.RegistryUsername + ":" + r.RegistryPasswd passwordErrorIP := make([]string, 0) for _, v := range cluster.GetRegistryIPAndPortList() { if err := r.upgrade.UpdateRegistryPasswd(registry, r.HtpasswdPath, v, RegistryType(r.RegistryType)); err != nil { diff --git a/lifecycle/pkg/registry/password/password.go b/lifecycle/pkg/registry/password/password.go index 7d37c3a105ae..93af0ec81133 100644 --- a/lifecycle/pkg/registry/password/password.go +++ b/lifecycle/pkg/registry/password/password.go @@ -17,13 +17,11 @@ limitations under the License. package password import ( + "errors" "fmt" "path" "github.com/labring/image-cri-shim/pkg/types" - "k8s.io/apimachinery/pkg/util/sets" - "sigs.k8s.io/yaml" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/registry/helpers" @@ -31,6 +29,8 @@ import ( "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/passwd" + "k8s.io/apimachinery/pkg/util/sets" + "sigs.k8s.io/yaml" ) type RegistryType string @@ -42,7 +42,11 @@ const ( ) type Upgrade interface { - UpdateRegistryPasswd(rc *v1beta1.RegistryConfig, target, host string, registryType RegistryType) error + UpdateRegistryPasswd( + rc *v1beta1.RegistryConfig, + target, host string, + registryType RegistryType, + ) error UpdateRegistryConfig(rc *v1beta1.RegistryConfig, target, host string) error UpdateImageShimConfig(rc *types.Config, target, host string) error } @@ -64,12 +68,15 @@ type upgrade struct { } func (m *upgrade) UpdateRegistryConfig(rc *v1beta1.RegistryConfig, target, host string) error { - configPath, err := m.mk.configLocalRegistryConfig(path.Join(constants.ClusterDir(m.Cluster), constants.EtcDirName), rc) + configPath, err := m.mk.configLocalRegistryConfig( + path.Join(constants.ClusterDir(m.Cluster), constants.EtcDirName), + rc, + ) if err != nil { return err } if target == "" { - return fmt.Errorf("update registry target path is empty") + return errors.New("update registry target path is empty") } if len(configPath) > 0 { if err = m.SSHInterface.Copy(host, configPath, target); err != nil { @@ -78,13 +85,17 @@ func (m *upgrade) UpdateRegistryConfig(rc *v1beta1.RegistryConfig, target, host } return nil } + func (m *upgrade) UpdateImageShimConfig(rc *types.Config, target, host string) error { - configPath, err := m.mk.configLocalImageShimConfig(path.Join(constants.ClusterDir(m.Cluster), constants.EtcDirName), rc) + configPath, err := m.mk.configLocalImageShimConfig( + path.Join(constants.ClusterDir(m.Cluster), constants.EtcDirName), + rc, + ) if err != nil { return err } if target == "" { - return fmt.Errorf("update image shim target path is empty") + return errors.New("update image shim target path is empty") } if len(configPath) > 0 { if err = m.SSHInterface.Copy(host, configPath, target); err != nil { @@ -96,13 +107,25 @@ func (m *upgrade) UpdateImageShimConfig(rc *types.Config, target, host string) e } return nil } -func (m *upgrade) UpdateRegistryPasswd(rc *v1beta1.RegistryConfig, target, host string, registryType RegistryType) error { - htpasswdPath, err := m.mk.configLocalHtpasswd(path.Join(constants.ClusterDir(m.Cluster), constants.EtcDirName), rc) + +func (m *upgrade) UpdateRegistryPasswd( + rc *v1beta1.RegistryConfig, + target, host string, + registryType RegistryType, +) error { + htpasswdPath, err := m.mk.configLocalHtpasswd( + path.Join(constants.ClusterDir(m.Cluster), constants.EtcDirName), + rc, + ) if err != nil { return err } if target == "" { - target = path.Join(constants.GetRootWorkDir(m.Cluster), constants.EtcDirName, "registry_htpasswd") + target = path.Join( + constants.GetRootWorkDir(m.Cluster), + constants.EtcDirName, + "registry_htpasswd", + ) } if len(htpasswdPath) > 0 { if err = m.SSHInterface.Copy(host, htpasswdPath, target); err != nil { @@ -137,13 +160,16 @@ func (m *maker) configLocalHtpasswd(cfgBasedir string, rc *v1beta1.RegistryConfi if !m.paths.Has(fp) { pwd := passwd.Htpasswd(rc.Username, rc.Password) if err := file.WriteFile(fp, []byte(pwd)); err != nil { - return "", fmt.Errorf("failed to make htpasswd: %v", err) + return "", fmt.Errorf("failed to make htpasswd: %w", err) } } return fp, nil } -func (m *maker) configLocalRegistryConfig(cfgBasedir string, rc *v1beta1.RegistryConfig) (string, error) { +func (m *maker) configLocalRegistryConfig( + cfgBasedir string, + rc *v1beta1.RegistryConfig, +) (string, error) { data, err := yaml.Marshal(rc) if err != nil { return "", err @@ -151,7 +177,7 @@ func (m *maker) configLocalRegistryConfig(cfgBasedir string, rc *v1beta1.Registr fp := path.Join(cfgBasedir, helpers.RegistryCustomConfig) if !m.paths.Has(fp) { if err = file.WriteFile(fp, data); err != nil { - return "", fmt.Errorf("failed to make registry config: %v", err) + return "", fmt.Errorf("failed to make registry config: %w", err) } } return fp, nil @@ -165,7 +191,7 @@ func (m *maker) configLocalImageShimConfig(cfgBasedir string, rc *types.Config) fp := path.Join(cfgBasedir, "image-cri-shim.yaml") if !m.paths.Has(fp) { if err = file.WriteFile(fp, data); err != nil { - return "", fmt.Errorf("failed to make registry config: %v", err) + return "", fmt.Errorf("failed to make registry config: %w", err) } } return fp, nil diff --git a/lifecycle/pkg/runtime/decode/decode.go b/lifecycle/pkg/runtime/decode/decode.go index 13ab92864821..725cc66a1bec 100644 --- a/lifecycle/pkg/runtime/decode/decode.go +++ b/lifecycle/pkg/runtime/decode/decode.go @@ -16,27 +16,26 @@ package decode import ( "bytes" + "errors" "fmt" "io" "os" "path/filepath" "strings" - "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" - + v2 "github.com/labring/sealos/pkg/types/v1beta1" + "github.com/labring/sealos/pkg/utils/logger" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8sruntime "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/yaml" proxy "k8s.io/kube-proxy/config/v1alpha1" kubelet "k8s.io/kubelet/config/v1beta1" + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmv1beta3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3" kubeadmv1beta4 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4" kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config/v1beta1" proxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config/v1alpha1" - - v2 "github.com/labring/sealos/pkg/types/v1beta1" - "github.com/labring/sealos/pkg/utils/logger" ) const ( @@ -62,13 +61,18 @@ func init() { utilruntime.Must(proxyconfig.AddToScheme(scheme)) } -func overrideKubeletDefaults(obj interface{}) { - kubeletconfig.SetObjectDefaults_KubeletConfiguration(obj.(*kubelet.KubeletConfiguration)) +func overrideKubeletDefaults(obj any) { + cfg, ok := obj.(*kubelet.KubeletConfiguration) + if !ok { + logger.Debug("unexpected type %T when overriding kubelet defaults", obj) + return + } + kubeletconfig.SetObjectDefaults_KubeletConfiguration(cfg) logger.Debug("override defaults of kubelet configuration") - obj.(*kubelet.KubeletConfiguration).ResolverConfig = nil + cfg.ResolverConfig = nil } -func CRDFromFile(filePath string, kind string) (interface{}, error) { +func CRDFromFile(filePath, kind string) (any, error) { file, err := os.Open(filepath.Clean(filePath)) if err != nil { return nil, err @@ -77,13 +81,13 @@ func CRDFromFile(filePath string, kind string) (interface{}, error) { return CRDFromReader(file, kind) } -func CRDFromReader(r io.Reader, kind string) (interface{}, error) { +func CRDFromReader(r io.Reader, kind string) (any, error) { d := yaml.NewYAMLOrJSONDecoder(r, 4096) for { ext := k8sruntime.RawExtension{} if err := d.Decode(&ext); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } return nil, err @@ -96,7 +100,7 @@ func CRDFromReader(r io.Reader, kind string) (interface{}, error) { metaType := &metav1.TypeMeta{} err := yaml.Unmarshal(ext.Raw, metaType) if err != nil { - return nil, fmt.Errorf("decode Cluster failed %v", err) + return nil, fmt.Errorf("decode Cluster failed %w", err) } // ext.Raw if metaType.Kind == kind { @@ -106,59 +110,71 @@ func CRDFromReader(r io.Reader, kind string) (interface{}, error) { return nil, nil } -func CRDFromString(config string, kind string) (interface{}, error) { +func CRDFromString(config, kind string) (any, error) { return CRDFromReader(strings.NewReader(config), kind) } -func typeConversion(raw []byte, metaType *metav1.TypeMeta) (interface{}, error) { +func typeConversion(raw []byte, metaType *metav1.TypeMeta) (any, error) { var obj k8sruntime.Object switch metaType.Kind { case Cluster: obj = &v2.Cluster{} case InitConfiguration: - obj = &kubeadm.InitConfiguration{} + initCfg := &kubeadm.InitConfiguration{} + obj = initCfg if metaType.APIVersion == kubeadmv1beta3.SchemeGroupVersion.String() { newObj := &kubeadmv1beta3.InitConfiguration{} if err := yaml.Unmarshal(raw, newObj); err != nil { - return nil, fmt.Errorf("decode kubeadmv1beta3 InitConfiguration failed %v", err) + return nil, fmt.Errorf("decode kubeadmv1beta3 InitConfiguration failed %w", err) } // Convert to v1beta4 - if err := kubeadmv1beta3.Convert_v1beta3_InitConfiguration_To_kubeadm_InitConfiguration(newObj, obj.(*kubeadm.InitConfiguration), nil); err != nil { - return nil, fmt.Errorf("convert kubeadmv1beta3 InitConfiguration to v1beta4 failed %v", err) + if err := kubeadmv1beta3.Convert_v1beta3_InitConfiguration_To_kubeadm_InitConfiguration(newObj, initCfg, nil); err != nil { + return nil, fmt.Errorf( + "convert kubeadmv1beta3 InitConfiguration to v1beta4 failed %w", + err, + ) } - obj.(*kubeadm.InitConfiguration).APIVersion = kubeadmv1beta3.SchemeGroupVersion.String() - obj.(*kubeadm.InitConfiguration).Kind = metaType.Kind - return obj, nil + initCfg.APIVersion = kubeadmv1beta3.SchemeGroupVersion.String() + initCfg.Kind = metaType.Kind + return initCfg, nil } case JoinConfiguration: - obj = &kubeadm.JoinConfiguration{} + joinCfg := &kubeadm.JoinConfiguration{} + obj = joinCfg if metaType.APIVersion == kubeadmv1beta3.SchemeGroupVersion.String() { newObj := &kubeadmv1beta3.JoinConfiguration{} if err := yaml.Unmarshal(raw, newObj); err != nil { - return nil, fmt.Errorf("decode kubeadmv1beta3 JoinConfiguration failed %v", err) + return nil, fmt.Errorf("decode kubeadmv1beta3 JoinConfiguration failed %w", err) } // Convert to v1beta4 - if err := kubeadmv1beta3.Convert_v1beta3_JoinConfiguration_To_kubeadm_JoinConfiguration(newObj, obj.(*kubeadm.JoinConfiguration), nil); err != nil { - return nil, fmt.Errorf("convert kubeadmv1beta3 JoinConfiguration to v1beta4 failed %v", err) + if err := kubeadmv1beta3.Convert_v1beta3_JoinConfiguration_To_kubeadm_JoinConfiguration(newObj, joinCfg, nil); err != nil { + return nil, fmt.Errorf( + "convert kubeadmv1beta3 JoinConfiguration to v1beta4 failed %w", + err, + ) } - obj.(*kubeadm.JoinConfiguration).APIVersion = kubeadmv1beta3.SchemeGroupVersion.String() - obj.(*kubeadm.JoinConfiguration).Kind = metaType.Kind - return obj, nil + joinCfg.APIVersion = kubeadmv1beta3.SchemeGroupVersion.String() + joinCfg.Kind = metaType.Kind + return joinCfg, nil } case ClusterConfiguration: - obj = &kubeadm.ClusterConfiguration{} + clusterCfg := &kubeadm.ClusterConfiguration{} + obj = clusterCfg if metaType.APIVersion == kubeadmv1beta3.SchemeGroupVersion.String() { newObj := &kubeadmv1beta3.ClusterConfiguration{} if err := yaml.Unmarshal(raw, newObj); err != nil { - return nil, fmt.Errorf("decode kubeadmv1beta3 ClusterConfiguration failed %v", err) + return nil, fmt.Errorf("decode kubeadmv1beta3 ClusterConfiguration failed %w", err) } // Convert to v1beta4 - if err := kubeadmv1beta3.Convert_v1beta3_ClusterConfiguration_To_kubeadm_ClusterConfiguration(newObj, obj.(*kubeadm.ClusterConfiguration), nil); err != nil { - return nil, fmt.Errorf("convert kubeadmv1beta3 ClusterConfiguration to v1beta4 failed %v", err) + if err := kubeadmv1beta3.Convert_v1beta3_ClusterConfiguration_To_kubeadm_ClusterConfiguration(newObj, clusterCfg, nil); err != nil { + return nil, fmt.Errorf( + "convert kubeadmv1beta3 ClusterConfiguration to v1beta4 failed %w", + err, + ) } - obj.(*kubeadm.ClusterConfiguration).APIVersion = kubeadmv1beta3.SchemeGroupVersion.String() - obj.(*kubeadm.ClusterConfiguration).Kind = metaType.Kind - return obj, nil + clusterCfg.APIVersion = kubeadmv1beta3.SchemeGroupVersion.String() + clusterCfg.Kind = metaType.Kind + return clusterCfg, nil } case KubeletConfiguration: obj = &kubelet.KubeletConfiguration{} diff --git a/lifecycle/pkg/runtime/interface.go b/lifecycle/pkg/runtime/interface.go index 9a4fb4ef7341..ffcc62efbf60 100644 --- a/lifecycle/pkg/runtime/interface.go +++ b/lifecycle/pkg/runtime/interface.go @@ -18,8 +18,8 @@ type Interface interface { Ruler Init() error Reset() error - ScaleUp(newMasterIPList []string, newNodeIPList []string) error - ScaleDown(deleteMastersIPList []string, deleteNodesIPList []string) error + ScaleUp(newMasterIPList, newNodeIPList []string) error + ScaleDown(deleteMastersIPList, deleteNodesIPList []string) error Upgrade(version string) error GetRawConfig() ([]byte, error) } diff --git a/lifecycle/pkg/runtime/k3s/bootstrap.go b/lifecycle/pkg/runtime/k3s/bootstrap.go index 21ee5819be95..884f5a59d83d 100644 --- a/lifecycle/pkg/runtime/k3s/bootstrap.go +++ b/lifecycle/pkg/runtime/k3s/bootstrap.go @@ -17,20 +17,19 @@ package k3s import ( "context" "fmt" + "net" "path/filepath" + "strconv" "strings" - "github.com/pkg/errors" - - "golang.org/x/sync/errgroup" - - "github.com/labring/sealos/pkg/utils/iputils" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/utils/file" + "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/rand" "github.com/labring/sealos/pkg/utils/yaml" + "github.com/pkg/errors" + "golang.org/x/sync/errgroup" ) func (k *K3s) initMaster0() error { @@ -42,26 +41,29 @@ func (k *K3s) initMaster0() error { func() error { return k.enableK3sService(master0) }, k.pullKubeConfigFromMaster0, func() error { - return k.remoteUtil.HostsAdd(master0, iputils.GetHostIP(master0), constants.DefaultAPIServerDomain) + return k.remoteUtil.HostsAdd( + master0, + iputils.GetHostIP(master0), + constants.DefaultAPIServerDomain, + ) }, func() error { return k.copyKubeConfigFileToNodes(k.cluster.GetMaster0IPAndPort()) }, ) } func (k *K3s) joinMasters(masters []string) error { - _, err := k.writeJoinConfigWithCallbacks(serverMode) - if err != nil { + if err := k.writeJoinConfigWithCallbacks(serverMode); err != nil { return err } for _, master := range masters { - if err = k.joinMaster(master); err != nil { + if err := k.joinMaster(master); err != nil { return err } } return nil } -func (k *K3s) writeJoinConfigWithCallbacks(runMode string, callbacks ...callback) (string, error) { +func (k *K3s) writeJoinConfigWithCallbacks(runMode string, callbacks ...callback) error { defaultCallbacks := []callback{defaultingConfig, k.merge, k.sealosCfg, k.overrideCertSans} switch runMode { case serverMode: @@ -72,7 +74,11 @@ func (k *K3s) writeJoinConfigWithCallbacks(runMode string, callbacks ...callback defaultCallbacks = append(defaultCallbacks, func(c *Config) *Config { - c.ServerURL = fmt.Sprintf("https://%s:%d", constants.DefaultAPIServerDomain, c.HTTPSPort) + hostPort := net.JoinHostPort( + constants.DefaultAPIServerDomain, + strconv.Itoa(c.HTTPSPort), + ) + c.ServerURL = "https://" + hostPort return c }, ) @@ -80,7 +86,7 @@ func (k *K3s) writeJoinConfigWithCallbacks(runMode string, callbacks ...callback append(defaultCallbacks, callbacks...)..., ) if err != nil { - return "", err + return err } var filename string switch runMode { @@ -90,28 +96,36 @@ func (k *K3s) writeJoinConfigWithCallbacks(runMode string, callbacks ...callback filename = defaultJoinNodesFilename } path := filepath.Join(k.pathResolver.EtcPath(), filename) - return path, file.WriteFile(path, raw) + return file.WriteFile(path, raw) } func (k *K3s) joinMaster(master string) error { - return k.runPipelines(fmt.Sprintf("join master %s", master), + return k.runPipelines("join master "+master, func() error { // the rest masters are also running in agent mode, so agent-token file is needed. return k.generateAndSendTokenFiles(master, "token", "agent-token") }, func() error { - return k.execer.Copy(master, filepath.Join(k.pathResolver.EtcPath(), defaultJoinMastersFilename), defaultK3sConfigPath) + return k.execer.Copy( + master, + filepath.Join(k.pathResolver.EtcPath(), defaultJoinMastersFilename), + defaultK3sConfigPath, + ) }, func() error { return k.enableK3sService(master) }, func() error { - return k.remoteUtil.HostsAdd(master, iputils.GetHostIP(master), constants.DefaultAPIServerDomain) + return k.remoteUtil.HostsAdd( + master, + iputils.GetHostIP(master), + constants.DefaultAPIServerDomain, + ) }, func() error { return k.copyKubeConfigFileToNodes(master) }, ) } func (k *K3s) joinNodes(nodes []string) error { - if _, err := k.writeJoinConfigWithCallbacks(agentMode, removeServerFlagsInAgentConfig); err != nil { + if err := k.writeJoinConfigWithCallbacks(agentMode, removeServerFlagsInAgentConfig); err != nil { return err } for i := range nodes { @@ -136,24 +150,29 @@ func (k *K3s) getAPIServerPort() int { func (k *K3s) getMasterIPListAndHTTPSPort() []string { apiPort := k.getAPIServerPort() masters := make([]string, 0) + port := strconv.Itoa(apiPort) for _, master := range k.cluster.GetMasterIPList() { - masters = append(masters, fmt.Sprintf("%s:%d", master, apiPort)) + masters = append(masters, net.JoinHostPort(master, port)) } return masters } func (k *K3s) getVipAndPort() string { - return fmt.Sprintf("%s:%d", k.cluster.GetVIP(), k.getAPIServerPort()) + return net.JoinHostPort(k.cluster.GetVIP(), strconv.Itoa(k.getAPIServerPort())) } func (k *K3s) joinNode(node string) error { - return k.runPipelines(fmt.Sprintf("join node %s", node), + return k.runPipelines("join node "+node, func() error { return k.remoteUtil.IPVS(node, k.getVipAndPort(), k.getMasterIPListAndHTTPSPort()) }, func() error { return k.generateAndSendTokenFiles(node, "agent-token") }, func() error { - return k.execer.Copy(node, filepath.Join(k.pathResolver.EtcPath(), defaultJoinNodesFilename), defaultK3sConfigPath) + return k.execer.Copy( + node, + filepath.Join(k.pathResolver.EtcPath(), defaultJoinNodesFilename), + defaultK3sConfigPath, + ) }, func() error { return k.enableK3sService(node) }, func() error { return k.copyKubeConfigFileToNodes(node) }, @@ -183,27 +202,31 @@ func (k *K3s) generateAndSendTokenFiles(host string, filenames ...string) error for _, filename := range filenames { src, err := k.generateRandomTokenFileIfNotExists(filename) if err != nil { - return fmt.Errorf("generate token: %v", err) + return fmt.Errorf("generate token: %w", err) } dst := filepath.Join(k.pathResolver.ConfigsPath(), filename) if err = k.execer.Copy(host, src, dst); err != nil { - return fmt.Errorf("copy token file: %v", err) + return fmt.Errorf("copy token file: %w", err) } } return nil } func (k *K3s) getRawInitConfig(callbacks ...callback) ([]byte, error) { - cfg, err := k.getInitConfig(callbacks...) - if err != nil { - return nil, err - } + cfg := k.getInitConfig(callbacks...) return yaml.MarshalConfigs(cfg) } func (k *K3s) generateAndSendInitConfig() error { src := filepath.Join(k.pathResolver.EtcPath(), defaultInitFilename) - defaultCallbacks := []callback{defaultingConfig, k.merge, k.sealosCfg, k.overrideCertSans, k.overrideServerConfig, setClusterInit} + defaultCallbacks := []callback{ + defaultingConfig, + k.merge, + k.sealosCfg, + k.overrideCertSans, + k.overrideServerConfig, + setClusterInit, + } if !file.IsExist(src) { raw, err := k.getRawInitConfig(defaultCallbacks...) if err != nil { @@ -235,13 +258,16 @@ func (k *K3s) copyKubeConfigFileToNodes(hosts ...string) error { if err != nil { return errors.WithMessage(err, "read admin.config file failed") } - newData := strings.ReplaceAll(string(data), "https://0.0.0.0", fmt.Sprintf("https://%s", constants.DefaultAPIServerDomain)) + newData := strings.ReplaceAll( + string(data), + "https://0.0.0.0", + "https://"+constants.DefaultAPIServerDomain, + ) if err = file.WriteFile(src, []byte(newData)); err != nil { return errors.WithMessage(err, "write admin.config file failed") } eg, _ := errgroup.WithContext(context.Background()) for _, node := range hosts { - node := node eg.Go(func() error { home, err := k.execer.CmdToString(node, "echo $HOME", "") if err != nil { diff --git a/lifecycle/pkg/runtime/k3s/config.go b/lifecycle/pkg/runtime/k3s/config.go index c61628aa5959..e6831367acdc 100644 --- a/lifecycle/pkg/runtime/k3s/config.go +++ b/lifecycle/pkg/runtime/k3s/config.go @@ -21,15 +21,13 @@ import ( "path/filepath" "github.com/emirpasic/gods/sets/linkedhashset" - "github.com/imdario/mergo" - netutils "k8s.io/utils/net" - "github.com/labring/sealos/pkg/constants" fileutils "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + netutils "k8s.io/utils/net" ) var defaultMergeOpts = []func(*mergo.Config){ @@ -57,13 +55,13 @@ func defaultingAgentConfig(c *Config) *Config { if c.AgentConfig == nil { c.AgentConfig = &AgentConfig{} } - c.AgentConfig.PreferBundledBin = true - c.AgentConfig.DataDir = defaultDataDir - c.AgentConfig.ExtraKubeProxyArgs = []string{} - c.AgentConfig.ExtraKubeletArgs = []string{} - c.AgentConfig.PauseImage = "docker.io/rancher/pause:3.1" - c.AgentConfig.PrivateRegistry = defaultRegistryConfigPath - c.AgentConfig.Labels = []string{"sealos.io/distribution=k3s"} + c.PreferBundledBin = true + c.DataDir = defaultDataDir + c.ExtraKubeProxyArgs = []string{} + c.ExtraKubeletArgs = []string{} + c.PauseImage = "docker.io/rancher/pause:3.1" + c.PrivateRegistry = defaultRegistryConfigPath + c.Labels = []string{"sealos.io/distribution=k3s"} return c } @@ -129,22 +127,24 @@ func (k *K3s) overrideCertSans(c *Config) *Config { func (k *K3s) sealosCfg(c *Config) *Config { vip := k.cluster.GetVIP() kubeProxy := linkedhashset.New() - for _, v := range c.AgentConfig.ExtraKubeProxyArgs { + for _, v := range c.ExtraKubeProxyArgs { kubeProxy.Add(v) } - kubeProxy.Add(fmt.Sprintf("%s=%s", "ipvs-exclude-cidrs", fmt.Sprintf("%s/32", vip))) + kubeProxy.Add(fmt.Sprintf("%s=%s", "ipvs-exclude-cidrs", vip+"/32")) kubeProxy.Add(fmt.Sprintf("%s=%s", "proxy-mode", "ipvs")) var allArgs []string for _, v := range kubeProxy.Values() { - allArgs = append(allArgs, v.(string)) + if str, ok := v.(string); ok { + allArgs = append(allArgs, str) + } } - c.AgentConfig.ExtraKubeProxyArgs = allArgs + c.ExtraKubeProxyArgs = allArgs return c } func (k *K3s) overrideServerConfig(c *Config) *Config { - c.AgentConfig.TokenFile = filepath.Join(k.pathResolver.ConfigsPath(), "token") + c.TokenFile = filepath.Join(k.pathResolver.ConfigsPath(), "token") c.AgentTokenFile = filepath.Join(k.pathResolver.ConfigsPath(), "agent-token") if len(c.ClusterDNS) == 0 && len(c.ServiceCIDR) > 0 { @@ -160,16 +160,16 @@ func (k *K3s) overrideServerConfig(c *Config) *Config { } func (k *K3s) overrideAgentConfig(c *Config) *Config { - c.AgentConfig.TokenFile = filepath.Join(k.pathResolver.ConfigsPath(), "agent-token") + c.TokenFile = filepath.Join(k.pathResolver.ConfigsPath(), "agent-token") return c } -func (k *K3s) getInitConfig(callbacks ...callback) (*Config, error) { +func (k *K3s) getInitConfig(callbacks ...callback) *Config { cfg := &Config{} for i := range callbacks { cfg = callbacks[i](cfg) } - return cfg, nil + return cfg } // ParseConfig return nil if data structure is not matched diff --git a/lifecycle/pkg/runtime/k3s/k3s.go b/lifecycle/pkg/runtime/k3s/k3s.go index 2cb318a45410..c1a0ca37e3e7 100644 --- a/lifecycle/pkg/runtime/k3s/k3s.go +++ b/lifecycle/pkg/runtime/k3s/k3s.go @@ -17,19 +17,19 @@ package k3s import ( "context" "fmt" - - "golang.org/x/sync/errgroup" - - "github.com/labring/sealos/pkg/utils/iputils" - "github.com/labring/sealos/pkg/utils/strings" + "net" + "strconv" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/env" "github.com/labring/sealos/pkg/exec" "github.com/labring/sealos/pkg/ssh" v2 "github.com/labring/sealos/pkg/types/v1beta1" + "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/pkg/utils/strings" "github.com/labring/sealos/pkg/utils/yaml" + "golang.org/x/sync/errgroup" ) type K3s struct { @@ -75,7 +75,7 @@ func (k *K3s) Reset() error { return nil } -func (k *K3s) ScaleUp(masters []string, nodes []string) error { +func (k *K3s) ScaleUp(masters, nodes []string) error { if len(masters) != 0 { logger.Info("%s will be added as master", masters) if err := k.joinMasters(masters); err != nil { @@ -91,7 +91,7 @@ func (k *K3s) ScaleUp(masters []string, nodes []string) error { return nil } -func (k *K3s) ScaleDown(masters []string, nodes []string) error { +func (k *K3s) ScaleDown(masters, nodes []string) error { if len(masters) != 0 { logger.Info("master %s will be deleted", masters) if err := k.removeNodes(masters); err != nil { @@ -111,11 +111,14 @@ func (k *K3s) Upgrade(version string) error { } func (k *K3s) GetRawConfig() ([]byte, error) { - defaultCallbacks := []callback{defaultingConfig, k.sealosCfg, k.overrideCertSans, k.overrideServerConfig, setClusterInit} - cfg, err := k.getInitConfig(defaultCallbacks...) - if err != nil { - return nil, err + defaultCallbacks := []callback{ + defaultingConfig, + k.sealosCfg, + k.overrideCertSans, + k.overrideServerConfig, + setClusterInit, } + cfg := k.getInitConfig(defaultCallbacks...) cluster := k.cluster.DeepCopy() cluster.Status = v2.ClusterStatus{} return yaml.MarshalConfigs(cluster, cfg) @@ -125,18 +128,27 @@ func (k *K3s) SyncNodeIPVS(mastersIPList, nodeIPList []string) error { apiPort := k.getAPIServerPort() mastersIPList = strings.RemoveDuplicate(mastersIPList) masters := make([]string, 0) + port := strconv.Itoa(apiPort) for _, master := range mastersIPList { - masters = append(masters, fmt.Sprintf("%s:%d", iputils.GetHostIP(master), apiPort)) + masters = append(masters, net.JoinHostPort(iputils.GetHostIP(master), port)) } image := k.cluster.GetLvscareImage() eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodeIPList { - node := node eg.Go(func() error { logger.Info("start to sync lvscare static pod to node: %s master: %+v", node, masters) - err := k.remoteUtil.StaticPod(node, k.getVipAndPort(), constants.LvsCareStaticPodName, image, masters, k3sEtcStaticPod, "--health-status", "401") + err := k.remoteUtil.StaticPod( + node, + k.getVipAndPort(), + constants.LvsCareStaticPodName, + image, + masters, + k3sEtcStaticPod, + "--health-status", + "401", + ) if err != nil { - return fmt.Errorf("update lvscare static pod failed %s %v", node, err) + return fmt.Errorf("update lvscare static pod failed %s %w", node, err) } return nil }) @@ -148,7 +160,7 @@ func (k *K3s) runPipelines(phase string, pipelines ...func() error) error { logger.Info("starting %s", phase) for i := range pipelines { if err := pipelines[i](); err != nil { - return fmt.Errorf("failed to %s: %v", phase, err) + return fmt.Errorf("failed to %s: %w", phase, err) } } return nil diff --git a/lifecycle/pkg/runtime/k3s/lifecycle.go b/lifecycle/pkg/runtime/k3s/lifecycle.go index 7b362cab8790..8f268f2a07f1 100644 --- a/lifecycle/pkg/runtime/k3s/lifecycle.go +++ b/lifecycle/pkg/runtime/k3s/lifecycle.go @@ -17,15 +17,11 @@ package k3s import ( "context" "fmt" + "slices" "github.com/labring/sealos/pkg/utils/iputils" - - "github.com/labring/sealos/pkg/utils/strings" - - "golang.org/x/exp/slices" - "github.com/labring/sealos/pkg/utils/logger" - + "github.com/labring/sealos/pkg/utils/strings" "golang.org/x/sync/errgroup" ) @@ -59,7 +55,11 @@ func (k *K3s) resetNode(host string) error { removeKubeConfig := "rm -rf $HOME/.kube" removeKubeConfigErr := k.execer.CmdAsync(host, removeKubeConfig) if removeKubeConfigErr != nil { - logger.Error("failed to clean node, exec command %s failed, %v", removeKubeConfig, removeKubeConfigErr) + logger.Error( + "failed to clean node, exec command %s failed, %v", + removeKubeConfig, + removeKubeConfigErr, + ) } if slices.Contains(k.cluster.GetNodeIPAndPortList(), host) { ipvsclearErr := k.remoteUtil.IPVSClean(host, k.getVipAndPort()) @@ -72,7 +72,7 @@ func (k *K3s) resetNode(host string) error { // TODO: remove from API func (k *K3s) deleteNode(node string) error { - //remove master + // remove master masterIPs := k.cluster.GetMasterIPList() if slices.Contains(k.cluster.GetMasterIPAndPortList(), node) { masterIPs = strings.RemoveFromSlice(k.cluster.GetMasterIPList(), node) @@ -80,7 +80,7 @@ func (k *K3s) deleteNode(node string) error { if len(masterIPs) > 0 { // TODO: do we need draining first? if err := k.removeNode(node); err != nil { - logger.Warn(fmt.Errorf("delete nodes %s failed %v", node, err)) + logger.Warn(fmt.Errorf("delete nodes %s failed %w", node, err)) } } return nil @@ -88,10 +88,20 @@ func (k *K3s) deleteNode(node string) error { func (k *K3s) removeNode(ip string) error { logger.Info("start to remove node from k3s %s", ip) - nodeName, err := k.execer.CmdToString(k.cluster.GetMaster0IPAndPort(), fmt.Sprintf("kubectl get nodes -o wide | awk '$6==\"%s\" {print $1}'", iputils.GetHostIP(ip)), "") + nodeName, err := k.execer.CmdToString( + k.cluster.GetMaster0IPAndPort(), + fmt.Sprintf( + "kubectl get nodes -o wide | awk '$6==\"%s\" {print $1}'", + iputils.GetHostIP(ip), + ), + "", + ) if err != nil { - return fmt.Errorf("cannot get node with ip address %s: %v", ip, err) + return fmt.Errorf("cannot get node with ip address %s: %w", ip, err) } logger.Debug("found node name is %s, we will delete it", nodeName) - return k.execer.CmdAsync(k.cluster.GetMaster0IPAndPort(), fmt.Sprintf("kubectl delete node %s --ignore-not-found=true", nodeName)) + return k.execer.CmdAsync( + k.cluster.GetMaster0IPAndPort(), + fmt.Sprintf("kubectl delete node %s --ignore-not-found=true", nodeName), + ) } diff --git a/lifecycle/pkg/runtime/kubernetes/certs.go b/lifecycle/pkg/runtime/kubernetes/certs.go index ad9d126a6d03..565b4b64a034 100644 --- a/lifecycle/pkg/runtime/kubernetes/certs.go +++ b/lifecycle/pkg/runtime/kubernetes/certs.go @@ -20,14 +20,13 @@ import ( "fmt" "path" - "golang.org/x/sync/errgroup" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/util/json" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + "golang.org/x/sync/errgroup" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/util/json" ) const ( @@ -66,7 +65,7 @@ func (k *KubeadmRuntime) UpdateCertSANs(certSans []string) error { } for i, f := range pipeline { if err := f(); err != nil { - return fmt.Errorf("failed to generate cert %v in %d", err, i) + return fmt.Errorf("failed to generate cert %w in %d", err, i) } } return nil @@ -82,13 +81,13 @@ func (k *KubeadmRuntime) saveNewKubeadmConfig() error { if err != nil { return err } - //unmarshal data from configmap + // unmarshal data from configmap obj, err := yaml.UnmarshalToMap([]byte(data)) if err != nil { return err } logger.Debug("current cluster config data: %+v", obj) - //set certs to obj interface + // set certs to obj interface err = unstructured.SetNestedStringSlice(obj, k.getCertSANs(), "apiServer", "certSANs") if err != nil { return err @@ -112,10 +111,17 @@ func (k *KubeadmRuntime) uploadConfigFromKubeadm() error { } cmd := k.Command(UpdateCluster) if cmd == "" { - return fmt.Errorf("get config update kubeadm command failed, kubernetes version is %s", k.getKubeVersion()) + return fmt.Errorf( + "get config update kubeadm command failed, kubernetes version is %s", + k.getKubeVersion(), + ) } if err = k.sshCmdAsync(k.getMaster0IPAndPort(), cmd); err != nil { - return fmt.Errorf("failed to exec update kubeadm config %s %v", k.getMaster0IPAndPort(), err) + return fmt.Errorf( + "failed to exec update kubeadm config %s %w", + k.getMaster0IPAndPort(), + err, + ) } return nil } @@ -133,7 +139,13 @@ func (k *KubeadmRuntime) InitCertsAndKubeConfigs() error { if err := k.CreateKubeConfigFiles(); err != nil { return fmt.Errorf("failed to generate kubernetes conf: %w", err) } - return k.SendJoinMasterKubeConfigs(k.getMasterIPAndPortList()[:1], AdminConf, ControllerConf, SchedulerConf, KubeletConf) + return k.SendJoinMasterKubeConfigs( + k.getMasterIPAndPortList()[:1], + AdminConf, + ControllerConf, + SchedulerConf, + KubeletConf, + ) } func (k *KubeadmRuntime) initCert() error { @@ -148,7 +160,7 @@ func (k *KubeadmRuntime) syncCert() error { logger.Debug("start to generate cert for master %s", master) err := k.execCert(master) if err != nil { - return fmt.Errorf("failed to create cert for master %s: %v", master, err) + return fmt.Errorf("failed to create cert for master %s: %w", master, err) } err = k.copyMasterKubeConfig(master) @@ -163,7 +175,10 @@ func (k *KubeadmRuntime) syncCert() error { func (k *KubeadmRuntime) showKubeadmCert() error { certCheck := "kubeadm certs check-expiration" - return k.sshCmdAsync(k.getMaster0IPAndPort(), fmt.Sprintf("%s%s", certCheck, vlogToStr(k.klogLevel))) + return k.sshCmdAsync( + k.getMaster0IPAndPort(), + fmt.Sprintf("%s%s", certCheck, vlogToStr(k.klogLevel)), + ) } func (k *KubeadmRuntime) deleteAPIServer() error { @@ -177,9 +192,8 @@ func (k *KubeadmRuntime) deleteAPIServer() error { logger.Info("delete pod apiserver from crictl") eg, _ := errgroup.WithContext(context.Background()) for _, master := range k.getMasterIPAndPortList() { - m := master eg.Go(func() error { - podIDJson, err := k.sshCmdToString(m, podIDSh) + podIDJson, err := k.sshCmdToString(master, podIDSh) if err != nil { return err } @@ -189,13 +203,13 @@ func (k *KubeadmRuntime) deleteAPIServer() error { } if len(ps.Containers) > 0 { podID := ps.Containers[0].PodSandboxID[:13] - logger.Debug("found podID %s in %s", podID, m) - //crictl stopp - if err = k.sshCmdAsync(m, fmt.Sprintf("crictl --timeout=10s stopp %s", podID)); err != nil { + logger.Debug("found podID %s in %s", podID, master) + // crictl stopp + if err = k.sshCmdAsync(master, "crictl --timeout=10s stopp "+podID); err != nil { return err } - //crictl rmp - return k.sshCmdAsync(m, fmt.Sprintf("crictl rmp %s", podID)) + // crictl rmp + return k.sshCmdAsync(master, "crictl rmp "+podID) } return errors.New("not found apiServer pod running") }) diff --git a/lifecycle/pkg/runtime/kubernetes/commands.go b/lifecycle/pkg/runtime/kubernetes/commands.go index 10278c07c1f5..8c6763c0f6c8 100644 --- a/lifecycle/pkg/runtime/kubernetes/commands.go +++ b/lifecycle/pkg/runtime/kubernetes/commands.go @@ -45,7 +45,10 @@ const ( func (k *KubeadmRuntime) Command(cmdType CommandType) (cmd string) { initConfigPath := k.getInitMasterKubeadmConfigFilePath() - joinMasterConfigPath := path.Join(k.pathResolver.ConfigsPath(), defaultJoinMasterKubeadmFileName) + joinMasterConfigPath := path.Join( + k.pathResolver.ConfigsPath(), + defaultJoinMasterKubeadmFileName, + ) joinNodeConfigPath := path.Join(k.pathResolver.ConfigsPath(), defaultJoinNodeKubeadmFileName) updateClusterConfigPath := path.Join(k.pathResolver.ConfigsPath(), defaultUpdateKubeadmFileName) @@ -63,7 +66,12 @@ func (k *KubeadmRuntime) Command(cmdType CommandType) (cmd string) { } if cmdType == InitMaster || cmdType == JoinMaster { - return fmt.Sprintf("%s%s%s", cmd, vlogToStr(k.klogLevel), " --ignore-preflight-errors=SystemVerification") + return fmt.Sprintf( + "%s%s%s", + cmd, + vlogToStr(k.klogLevel), + " --ignore-preflight-errors=SystemVerification", + ) } return fmt.Sprintf("%s%s", cmd, vlogToStr(k.klogLevel)) } diff --git a/lifecycle/pkg/runtime/kubernetes/init.go b/lifecycle/pkg/runtime/kubernetes/init.go index 4430ec36f4ab..1c7793d45500 100644 --- a/lifecycle/pkg/runtime/kubernetes/init.go +++ b/lifecycle/pkg/runtime/kubernetes/init.go @@ -26,19 +26,19 @@ import ( func (k *KubeadmRuntime) InitKubeadmConfigToMaster0() error { data, err := k.generateInitConfigs() if err != nil { - return fmt.Errorf("generate init config error: %v", err) + return fmt.Errorf("generate init config error: %w", err) } initConfigPath := path.Join(k.pathResolver.TmpPath(), defaultInitKubeadmFileName) outConfigPath := path.Join(k.pathResolver.ConfigsPath(), defaultInitKubeadmFileName) err = file.WriteFile(initConfigPath, data) if err != nil { - return fmt.Errorf("failed to write tmp init kubeadm config: %v", err) + return fmt.Errorf("failed to write tmp init kubeadm config: %w", err) } logger.Info("Copying kubeadm config to master0") err = k.sshCopy(k.getMaster0IPAndPort(), initConfigPath, outConfigPath) if err != nil { - return fmt.Errorf("failed to copy init kubeadm config: %v", err) + return fmt.Errorf("failed to copy init kubeadm config: %w", err) } return nil } @@ -47,7 +47,7 @@ func (k *KubeadmRuntime) GenerateCert() error { logger.Info("start to generate and copy certs to masters...") hostName, err := k.execHostname(k.getMaster0IPAndPort()) if err != nil { - return fmt.Errorf("get hostname failed %v", err) + return fmt.Errorf("get hostname failed %w", err) } logger.Debug("GenerateCert param:", k.pathResolver.PkiPath(), @@ -72,7 +72,7 @@ func (k *KubeadmRuntime) CreateKubeConfigFiles() error { logger.Info("start to create kubeconfig...") hostName, err := k.execHostname(k.getMaster0IPAndPort()) if err != nil { - return fmt.Errorf("get hostname failed %v", err) + return fmt.Errorf("get hostname failed %w", err) } certConfig := cert.Config{ Path: k.pathResolver.PkiPath(), @@ -82,7 +82,7 @@ func (k *KubeadmRuntime) CreateKubeConfigFiles() error { err = cert.CreateJoinControlPlaneKubeConfigFiles(k.pathResolver.EtcPath(), certConfig, hostName, k.getClusterAPIServer(), "kubernetes") if err != nil { - return fmt.Errorf("failed to generate kubeconfig: %v", err) + return fmt.Errorf("failed to generate kubeconfig: %w", err) } return nil } diff --git a/lifecycle/pkg/runtime/kubernetes/kubeadm.go b/lifecycle/pkg/runtime/kubernetes/kubeadm.go index e4ccc37867e0..56a9acc48a75 100644 --- a/lifecycle/pkg/runtime/kubernetes/kubeadm.go +++ b/lifecycle/pkg/runtime/kubernetes/kubeadm.go @@ -16,19 +16,15 @@ package kubernetes import ( "context" + "errors" "fmt" + "net" "path" "path/filepath" + "strconv" "time" - "github.com/labring/sealos/pkg/utils/file" - "github.com/Masterminds/semver/v3" - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/util/json" - "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/runtime/kubernetes/types" fileutil "github.com/labring/sealos/pkg/utils/file" @@ -37,6 +33,10 @@ import ( "github.com/labring/sealos/pkg/utils/rand" stringsutil "github.com/labring/sealos/pkg/utils/strings" "github.com/labring/sealos/pkg/utils/yaml" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/util/json" + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" ) var ( @@ -103,7 +103,7 @@ func (k *KubeadmRuntime) getCGroupDriver(node string) (string, error) { // MergeKubeadmConfig Unsafe, dangerous use of goroutines. func (k *KubeadmRuntime) MergeKubeadmConfig(node string) error { - var mergeErr = func() error { + mergeErr := func() error { for _, fn := range []string{ "", // generate default kubeadm configs k.getDefaultKubeadmConfig(node), // merging from predefined path of file if file exists @@ -116,7 +116,7 @@ func (k *KubeadmRuntime) MergeKubeadmConfig(node string) error { // merge from clusterfile if k.config.KubeadmConfig != nil { if err := k.kubeadmConfig.LoadFromClusterfile(k.config.KubeadmConfig); err != nil { - return fmt.Errorf("failed to load kubeadm config from clusterfile: %v", err) + return fmt.Errorf("failed to load kubeadm config from clusterfile: %w", err) } } k.setKubeadmAPIVersion() @@ -128,8 +128,8 @@ func (k *KubeadmRuntime) MergeKubeadmConfig(node string) error { func (k *KubeadmRuntime) validateVIP(ip string) error { for k, sub := range map[string]string{ - "podSubnet": k.kubeadmConfig.ClusterConfiguration.Networking.PodSubnet, - "serviceSubnet": k.kubeadmConfig.ClusterConfiguration.Networking.ServiceSubnet, + "podSubnet": k.kubeadmConfig.Networking.PodSubnet, + "serviceSubnet": k.kubeadmConfig.Networking.ServiceSubnet, } { if contains, err := iputils.Contains(sub, ip); err != nil { return err @@ -145,15 +145,24 @@ func (k *KubeadmRuntime) getDefaultKubeadmConfig(node string) string { if node == "" { return defaultKubeadm } - out, err := k.execer.Cmd(node, fmt.Sprintf("cat %s", defaultKubeadm)) + out, err := k.execer.Cmd(node, "cat "+defaultKubeadm) if err != nil { - logger.Warn("load rootfs kubeadm config error: %+v, using default rootfs kubeadm config", err) + logger.Warn( + "load rootfs kubeadm config error: %+v, using default rootfs kubeadm config", + err, + ) return filepath.Join(k.pathResolver.RootFSEtcPath(), defaultRootfsKubeadmFileName) } - kubeadmPath := path.Join(k.pathResolver.TmpPath(), fmt.Sprintf("kubeadm-%s.yaml", iputils.GetHostIP(node))) - err = file.WriteFile(kubeadmPath, out) + kubeadmPath := path.Join( + k.pathResolver.TmpPath(), + fmt.Sprintf("kubeadm-%s.yaml", iputils.GetHostIP(node)), + ) + err = fileutil.WriteFile(kubeadmPath, out) if err != nil { - logger.Warn("write temp kubeadm config error: %+v, using default rootfs kubeadm config", err) + logger.Warn( + "write temp kubeadm config error: %+v, using default rootfs kubeadm config", + err, + ) return filepath.Join(k.pathResolver.RootFSEtcPath(), defaultRootfsKubeadmFileName) } return kubeadmPath @@ -164,14 +173,14 @@ func (k *KubeadmRuntime) getVip() string { } func (k *KubeadmRuntime) getAPIServerPort() int32 { - if k.kubeadmConfig.InitConfiguration.LocalAPIEndpoint.BindPort == 0 { - k.kubeadmConfig.InitConfiguration.LocalAPIEndpoint.BindPort = constants.DefaultAPIServerPort + if k.kubeadmConfig.LocalAPIEndpoint.BindPort == 0 { + k.kubeadmConfig.LocalAPIEndpoint.BindPort = constants.DefaultAPIServerPort } - return k.kubeadmConfig.InitConfiguration.LocalAPIEndpoint.BindPort + return k.kubeadmConfig.LocalAPIEndpoint.BindPort } func (k *KubeadmRuntime) getVipAndPort() string { - return fmt.Sprintf("%s:%d", k.getVip(), k.getAPIServerPort()) + return net.JoinHostPort(k.getVip(), strconv.Itoa(int(k.getAPIServerPort()))) } func (k *KubeadmRuntime) getAPIServerDomain() string { @@ -179,11 +188,12 @@ func (k *KubeadmRuntime) getAPIServerDomain() string { } func (k *KubeadmRuntime) getClusterAPIServer() string { - return fmt.Sprintf("https://%s:%d", k.getAPIServerDomain(), k.getAPIServerPort()) + hostPort := net.JoinHostPort(k.getAPIServerDomain(), strconv.Itoa(int(k.getAPIServerPort()))) + return "https://" + hostPort } func (k *KubeadmRuntime) getCertSANs() []string { - return k.kubeadmConfig.ClusterConfiguration.APIServer.CertSANs + return k.kubeadmConfig.APIServer.CertSANs } func (k *KubeadmRuntime) initCertSANS() { @@ -200,7 +210,7 @@ func (k *KubeadmRuntime) setCertSANs(certs []string) { var certSans []string certSans = append(certSans, certs...) certSans = stringsutil.RemoveDuplicate(certSans) - k.kubeadmConfig.ClusterConfiguration.APIServer.CertSANs = certSans + k.kubeadmConfig.APIServer.CertSANs = certSans } func (k *KubeadmRuntime) mergeWithBuiltinKubeadmConfig() error { @@ -213,7 +223,7 @@ func (k *KubeadmRuntime) mergeWithBuiltinKubeadmConfig() error { if err != nil { return err } - //unmarshal data from configmap + // unmarshal data from configmap obj, err := yaml.UnmarshalToMap([]byte(data)) if err != nil { return err @@ -226,20 +236,22 @@ func (k *KubeadmRuntime) mergeWithBuiltinKubeadmConfig() error { if err != nil { return err } - return fmt.Errorf("apiServer certSANs not exist") + return errors.New("apiServer certSANs not exist") } for i := range certsStruct { - certs = append(certs, certsStruct[i].(string)) + if cert, ok := certsStruct[i].(string); ok { + certs = append(certs, cert) + } } logger.Debug("current cluster certSANs: %+v", certs) k.setCertSANs(certs) return k.setNetWorking(obj) } -func (k *KubeadmRuntime) setNetWorking(obj map[string]interface{}) error { +func (k *KubeadmRuntime) setNetWorking(obj map[string]any) error { networkingMap, found, err := unstructured.NestedStringMap(obj, "networking") if !found || err != nil { - return fmt.Errorf("networking section not found or cannot be parsed: %v", err) + return fmt.Errorf("networking section not found or cannot be parsed: %w", err) } requiredKeys := []string{"podSubnet", "serviceSubnet", "dnsDomain"} @@ -249,21 +261,21 @@ func (k *KubeadmRuntime) setNetWorking(obj map[string]interface{}) error { return fmt.Errorf("networking %s not exist", key) } } - k.kubeadmConfig.ClusterConfiguration.Networking.ServiceSubnet = networkingMap["serviceSubnet"] - k.kubeadmConfig.ClusterConfiguration.Networking.DNSDomain = networkingMap["dnsDomain"] - k.kubeadmConfig.ClusterConfiguration.Networking.PodSubnet = networkingMap["podSubnet"] + k.kubeadmConfig.Networking.ServiceSubnet = networkingMap["serviceSubnet"] + k.kubeadmConfig.Networking.DNSDomain = networkingMap["dnsDomain"] + k.kubeadmConfig.Networking.PodSubnet = networkingMap["podSubnet"] return nil } func (k *KubeadmRuntime) getServiceCIDR() string { - return k.kubeadmConfig.ClusterConfiguration.Networking.ServiceSubnet + return k.kubeadmConfig.Networking.ServiceSubnet } func (k *KubeadmRuntime) getDNSDomain() string { - if k.kubeadmConfig.ClusterConfiguration.Networking.DNSDomain == "" { - k.kubeadmConfig.ClusterConfiguration.Networking.DNSDomain = constants.DefaultDNSDomain + if k.kubeadmConfig.Networking.DNSDomain == "" { + k.kubeadmConfig.Networking.DNSDomain = constants.DefaultDNSDomain } - return k.kubeadmConfig.ClusterConfiguration.Networking.DNSDomain + return k.kubeadmConfig.Networking.DNSDomain } func (k *KubeadmRuntime) writeTokenFile(file string) error { @@ -306,7 +318,7 @@ func (k *KubeadmRuntime) setKubernetesToken() error { return err } now := time.Now() - sub := t.Expires.Time.Sub(now) + sub := t.Expires.Sub(now) // regenerate token if it expires in 180 seconds if sub <= 180 { err = k.writeTokenFile(tokenFile) @@ -326,36 +338,36 @@ func (k *KubeadmRuntime) setKubernetesToken() error { } func (k *KubeadmRuntime) setJoinToken(token string) { - if k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken == nil { - k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken = &kubeadm.BootstrapTokenDiscovery{} + if k.kubeadmConfig.Discovery.BootstrapToken == nil { + k.kubeadmConfig.Discovery.BootstrapToken = &kubeadm.BootstrapTokenDiscovery{} } - k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken.Token = token + k.kubeadmConfig.Discovery.BootstrapToken.Token = token } func (k *KubeadmRuntime) setTokenCaCertHash(tokenCaCertHash []string) { - if k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken == nil { - k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken = &kubeadm.BootstrapTokenDiscovery{} + if k.kubeadmConfig.Discovery.BootstrapToken == nil { + k.kubeadmConfig.Discovery.BootstrapToken = &kubeadm.BootstrapTokenDiscovery{} } - k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken.CACertHashes = tokenCaCertHash + k.kubeadmConfig.Discovery.BootstrapToken.CACertHashes = tokenCaCertHash } func (k *KubeadmRuntime) setJoinCertificateKey(certificateKey string) { - if k.kubeadmConfig.JoinConfiguration.ControlPlane == nil { - k.kubeadmConfig.JoinConfiguration.ControlPlane = &kubeadm.JoinControlPlane{} + if k.kubeadmConfig.ControlPlane == nil { + k.kubeadmConfig.ControlPlane = &kubeadm.JoinControlPlane{} } - k.kubeadmConfig.JoinConfiguration.ControlPlane.CertificateKey = certificateKey + k.kubeadmConfig.ControlPlane.CertificateKey = certificateKey } func (k *KubeadmRuntime) setInitCertificateKey(certificateKey string) { - k.kubeadmConfig.InitConfiguration.CertificateKey = certificateKey + k.kubeadmConfig.CertificateKey = certificateKey } func (k *KubeadmRuntime) getInitCertificateKey() string { - return k.kubeadmConfig.InitConfiguration.CertificateKey + return k.kubeadmConfig.CertificateKey } func (k *KubeadmRuntime) setAPIServerEndpoint(endpoint string) { - k.kubeadmConfig.JoinConfiguration.Discovery.BootstrapToken.APIServerEndpoint = endpoint + k.kubeadmConfig.Discovery.BootstrapToken.APIServerEndpoint = endpoint } func (k *KubeadmRuntime) setJoinInternalIP(nodeIP string) { @@ -366,6 +378,7 @@ func (k *KubeadmRuntime) setJoinInternalIP(nodeIP string) { }, } } + func (k *KubeadmRuntime) setInitInternalIP(nodeIP string) { k.kubeadmConfig.InitConfiguration.NodeRegistration.KubeletExtraArgs = []kubeadm.Arg{ { @@ -376,51 +389,53 @@ func (k *KubeadmRuntime) setInitInternalIP(nodeIP string) { } func (k *KubeadmRuntime) setInitAdvertiseAddress(advertiseAddress string) { - k.kubeadmConfig.InitConfiguration.LocalAPIEndpoint.AdvertiseAddress = advertiseAddress + k.kubeadmConfig.LocalAPIEndpoint.AdvertiseAddress = advertiseAddress } func (k *KubeadmRuntime) setJoinAdvertiseAddress(advertiseAddress string) { - if k.kubeadmConfig.JoinConfiguration.ControlPlane == nil { - k.kubeadmConfig.JoinConfiguration.ControlPlane = &kubeadm.JoinControlPlane{} + if k.kubeadmConfig.ControlPlane == nil { + k.kubeadmConfig.ControlPlane = &kubeadm.JoinControlPlane{} } - k.kubeadmConfig.JoinConfiguration.ControlPlane.LocalAPIEndpoint.AdvertiseAddress = advertiseAddress + k.kubeadmConfig.ControlPlane.LocalAPIEndpoint.AdvertiseAddress = advertiseAddress } func (k *KubeadmRuntime) cleanJoinLocalAPIEndPoint() { - k.kubeadmConfig.JoinConfiguration.ControlPlane = nil + k.kubeadmConfig.ControlPlane = nil } func (k *KubeadmRuntime) setControlPlaneEndpoint(endpoint string) { - k.kubeadmConfig.ClusterConfiguration.ControlPlaneEndpoint = endpoint + k.kubeadmConfig.ControlPlaneEndpoint = endpoint } func (k *KubeadmRuntime) setCgroupDriver(cGroup string) { - k.kubeadmConfig.KubeletConfiguration.CgroupDriver = cGroup + k.kubeadmConfig.CgroupDriver = cGroup } func (k *KubeadmRuntime) setInitTaints() { if len(k.cluster.GetAllIPS()) == 1 && k.kubeadmConfig.InitConfiguration.NodeRegistration.Taints == nil { - //set this field to an empty slice avoid to taint control-plane in single host + // set this field to an empty slice avoid to taint control-plane in single host k.kubeadmConfig.InitConfiguration.NodeRegistration.Taints = make([]v1.Taint, 0) } } func (k *KubeadmRuntime) setExcludeCIDRs() { - k.kubeadmConfig.KubeProxyConfiguration.IPVS.ExcludeCIDRs = append( - k.kubeadmConfig.KubeProxyConfiguration.IPVS.ExcludeCIDRs, fmt.Sprintf("%s/32", k.getVip())) - k.kubeadmConfig.KubeProxyConfiguration.IPVS.ExcludeCIDRs = stringsutil.RemoveDuplicate(k.kubeadmConfig.KubeProxyConfiguration.IPVS.ExcludeCIDRs) + k.kubeadmConfig.IPVS.ExcludeCIDRs = append( + k.kubeadmConfig.IPVS.ExcludeCIDRs, k.getVip()+"/32") + k.kubeadmConfig.IPVS.ExcludeCIDRs = stringsutil.RemoveDuplicate( + k.kubeadmConfig.IPVS.ExcludeCIDRs, + ) } func (k *KubeadmRuntime) getEtcdDataDir() string { const defaultEtcdDataDir = "/var/lib/etcd" - if k.kubeadmConfig.ClusterConfiguration.Etcd.Local == nil { + if k.kubeadmConfig.Etcd.Local == nil { return defaultEtcdDataDir } - if k.kubeadmConfig.ClusterConfiguration.Etcd.Local.DataDir == "" { + if k.kubeadmConfig.Etcd.Local.DataDir == "" { return defaultEtcdDataDir } - return k.kubeadmConfig.ClusterConfiguration.Etcd.Local.DataDir + return k.kubeadmConfig.Etcd.Local.DataDir } func (k *KubeadmRuntime) getCRISocket(node string) (string, error) { @@ -435,13 +450,22 @@ func (k *KubeadmRuntime) getCRISocket(node string) (string, error) { //nolint:all func (k *KubeadmRuntime) setCRISocket(criSocket string) { if k.kubeadmConfig.JoinConfiguration.NodeRegistration.CRISocket == "" { - k.kubeadmConfig.JoinConfiguration.NodeRegistration.CRISocket = fmt.Sprintf("unix://%s", criSocket) + k.kubeadmConfig.JoinConfiguration.NodeRegistration.CRISocket = fmt.Sprintf( + "unix://%s", + criSocket, + ) } if k.kubeadmConfig.InitConfiguration.NodeRegistration.CRISocket == "" { - k.kubeadmConfig.InitConfiguration.NodeRegistration.CRISocket = fmt.Sprintf("unix://%s", criSocket) + k.kubeadmConfig.InitConfiguration.NodeRegistration.CRISocket = fmt.Sprintf( + "unix://%s", + criSocket, + ) } if k.kubeadmConfig.KubeletConfiguration.ContainerRuntimeEndpoint == "" { - k.kubeadmConfig.KubeletConfiguration.ContainerRuntimeEndpoint = fmt.Sprintf("unix://%s", criSocket) + k.kubeadmConfig.KubeletConfiguration.ContainerRuntimeEndpoint = fmt.Sprintf( + "unix://%s", + criSocket, + ) } } @@ -494,9 +518,13 @@ func (k *KubeadmRuntime) CompleteKubeadmConfig(fns ...func(*KubeadmRuntime) erro } k.setInitAdvertiseAddress(k.getMaster0IP()) k.setInitInternalIP(k.getMaster0IP()) - k.setControlPlaneEndpoint(fmt.Sprintf("%s:%d", k.getAPIServerDomain(), k.getAPIServerPort())) - if k.kubeadmConfig.ClusterConfiguration.APIServer.ExtraArgs == nil { - k.kubeadmConfig.ClusterConfiguration.APIServer.ExtraArgs = make([]kubeadm.Arg, 0) + controlPlaneEndpoint := net.JoinHostPort( + k.getAPIServerDomain(), + strconv.Itoa(int(k.getAPIServerPort())), + ) + k.setControlPlaneEndpoint(controlPlaneEndpoint) + if k.kubeadmConfig.APIServer.ExtraArgs == nil { + k.kubeadmConfig.APIServer.ExtraArgs = make([]kubeadm.Arg, 0) } k.setExcludeCIDRs() k.initCertSANS() @@ -536,7 +564,9 @@ func (k *KubeadmRuntime) generateJoinMasterConfigs(masterIP string) ([]byte, err } k.setJoinAdvertiseAddress(iputils.GetHostIP(masterIP)) k.setJoinInternalIP(iputils.GetHostIP(masterIP)) - k.setAPIServerEndpoint(fmt.Sprintf("%s:%d", k.getMaster0IP(), k.getAPIServerPort())) + k.setAPIServerEndpoint( + net.JoinHostPort(k.getMaster0IP(), strconv.Itoa(int(k.getAPIServerPort()))), + ) conversion, err := k.kubeadmConfig.ToConvertedKubeadmConfig() if err != nil { @@ -564,7 +594,7 @@ func (k *KubeadmRuntime) setCGroupDriverAndSocket(node string) error { func (k *KubeadmRuntime) setImageSocket() { imageEndpoint := k.cluster.GetImageEndpoint() - k.kubeadmConfig.KubeletConfiguration.ImageServiceEndpoint = fmt.Sprintf("unix://%s", imageEndpoint) + k.kubeadmConfig.ImageServiceEndpoint = "unix://" + imageEndpoint k.kubeadmConfig.InitConfiguration.NodeRegistration.ImagePullPolicy = v1.PullNever k.kubeadmConfig.JoinConfiguration.NodeRegistration.ImagePullPolicy = v1.PullNever } diff --git a/lifecycle/pkg/runtime/kubernetes/kubeconfig.go b/lifecycle/pkg/runtime/kubernetes/kubeconfig.go index 89af8ad0fe7d..253e87c9ca7b 100644 --- a/lifecycle/pkg/runtime/kubernetes/kubeconfig.go +++ b/lifecycle/pkg/runtime/kubernetes/kubeconfig.go @@ -29,7 +29,6 @@ func (k *KubeadmRuntime) copyKubeConfigFileToNodes(hosts ...string) error { src := k.pathResolver.AdminFile() eg, _ := errgroup.WithContext(context.Background()) for _, node := range hosts { - node := node eg.Go(func() error { home, err := k.execer.CmdToString(node, "echo $HOME", "") if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/master.go b/lifecycle/pkg/runtime/kubernetes/master.go index 9dcf3db504d5..c8a1e07c4678 100644 --- a/lifecycle/pkg/runtime/kubernetes/master.go +++ b/lifecycle/pkg/runtime/kubernetes/master.go @@ -17,22 +17,19 @@ package kubernetes import ( "context" "fmt" + "net" "path" "strings" - "github.com/labring/sealos/pkg/utils/iputils" - - "github.com/labring/sreg/pkg/registry/crane" - "k8s.io/apimachinery/pkg/util/json" - "github.com/labring/sealos/pkg/registry/helpers" - "github.com/labring/sealos/pkg/ssh" "github.com/labring/sealos/pkg/utils/file" + "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" str2 "github.com/labring/sealos/pkg/utils/strings" - + "github.com/labring/sreg/pkg/registry/crane" "golang.org/x/sync/errgroup" + "k8s.io/apimachinery/pkg/util/json" ) func (k *KubeadmRuntime) InitMaster0() error { @@ -43,7 +40,10 @@ func (k *KubeadmRuntime) InitMaster0() error { } cmdInit := k.Command(InitMaster) if cmdInit == "" { - return fmt.Errorf("get init master command failed, kubernetes version is %s", k.getKubeVersion()) + return fmt.Errorf( + "get init master command failed, kubernetes version is %s", + k.getKubeVersion(), + ) } err := k.sshCmdAsync(master0, cmdInit) if err != nil { @@ -59,17 +59,28 @@ func (k *KubeadmRuntime) imagePull(hostAndPort, version string) error { type Images struct { Images []string `json:"images"` } - imageList := fmt.Sprintf("kubeadm config images list --kubernetes-version %s -o json 2>/dev/null", version) - listJson, err := k.sshCmdToString(hostAndPort, imageList) + imageList := fmt.Sprintf( + "kubeadm config images list --kubernetes-version %s -o json 2>/dev/null", + version, + ) + listJSON, err := k.sshCmdToString(hostAndPort, imageList) if err != nil { return fmt.Errorf("get kubeadm images list failed, error: %s", err.Error()) } var images Images - if err = json.Unmarshal([]byte(listJson), &images); err != nil { - return fmt.Errorf("unmarshal kubeadm images list failed, json: %s, error: %s", listJson, err.Error()) + if err = json.Unmarshal([]byte(listJSON), &images); err != nil { + return fmt.Errorf( + "unmarshal kubeadm images list failed, json: %s, error: %s", + listJSON, + err.Error(), + ) } var newImageList []string - registry := helpers.GetRegistryInfo(k.execer, k.pathResolver.RootFSPath(), k.cluster.GetRegistryIPAndPort()) + registry := helpers.GetRegistryInfo( + k.execer, + k.pathResolver.RootFSPath(), + k.cluster.GetRegistryIPAndPort(), + ) for _, image := range images.Images { image = strings.TrimSpace(image) if image == "" { @@ -77,14 +88,19 @@ func (k *KubeadmRuntime) imagePull(hostAndPort, version string) error { } regAddr := crane.GetRegistryDomain(image) if regAddr != "" { - image = strings.Replace(image, regAddr, fmt.Sprintf("%s:%s", registry.Domain, registry.Port), 1) + image = strings.Replace( + image, + regAddr, + net.JoinHostPort(registry.Domain, registry.Port), + 1, + ) newImageList = append(newImageList, image) } } logger.Info("start to pull images: %s", strings.Join(newImageList, ", ")) for _, image := range newImageList { - imagePullCmd := fmt.Sprintf("crictl pull %s", image) + imagePullCmd := "crictl pull " + image if err := k.sshCmdAsync(hostAndPort, imagePullCmd); err != nil { return fmt.Errorf("pull image %s failed, error: %s", image, err.Error()) } @@ -98,7 +114,6 @@ func (k *KubeadmRuntime) imagePull(hostAndPort, version string) error { func (k *KubeadmRuntime) sendJoinCPConfig(joinMaster []string) error { eg, _ := errgroup.WithContext(context.Background()) for _, master := range joinMaster { - master := master eg.Go(func() error { k.mu.Lock() defer k.mu.Unlock() @@ -114,7 +129,11 @@ func (k *KubeadmRuntime) ConfigJoinMasterKubeadmToMaster(master string) error { if err != nil { return fmt.Errorf("failed to generate join master kubeadm config: %s", err.Error()) } - joinConfigPath := path.Join(k.pathResolver.TmpPath(), iputils.GetHostIP(master), defaultJoinMasterKubeadmFileName) + joinConfigPath := path.Join( + k.pathResolver.TmpPath(), + iputils.GetHostIP(master), + defaultJoinMasterKubeadmFileName, + ) outConfigPath := path.Join(k.pathResolver.ConfigsPath(), defaultJoinMasterKubeadmFileName) err = file.WriteFile(joinConfigPath, data) if err != nil { @@ -160,7 +179,10 @@ func (k *KubeadmRuntime) joinMasters(masters []string) error { } joinCmd := k.Command(JoinMaster) if joinCmd == "" { - return fmt.Errorf("get join master command failed, kubernetes version is %s", k.getKubeVersion()) + return fmt.Errorf( + "get join master command failed, kubernetes version is %s", + k.getKubeVersion(), + ) } for _, master := range masters { logger.Info("start to join %s as master", master) @@ -170,17 +192,17 @@ func (k *KubeadmRuntime) joinMasters(masters []string) error { logger.Debug("start to generate cert for master %s", master) err = k.execCert(master) if err != nil { - return fmt.Errorf("failed to create cert for master %s: %v", master, err) + return fmt.Errorf("failed to create cert for master %s: %w", master, err) } err = k.sshCmdAsync(master, joinCmd) if err != nil { - return fmt.Errorf("exec kubeadm join in %s failed %v", master, err) + return fmt.Errorf("exec kubeadm join in %s failed %w", master, err) } err = k.execHostsAppend(master, master, k.getAPIServerDomain()) if err != nil { - return fmt.Errorf("add master0 apiserver domain hosts in %s failed %v", master, err) + return fmt.Errorf("add master0 apiserver domain hosts in %s failed %w", master, err) } err = k.copyMasterKubeConfig(master) @@ -202,7 +224,6 @@ func (k *KubeadmRuntime) deleteMasters(masters []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, master := range masters { - master := master eg.Go(func() error { logger.Info("start to delete master %s", master) if err := k.deleteMaster(master); err != nil { @@ -218,12 +239,12 @@ func (k *KubeadmRuntime) deleteMasters(masters []string) error { func (k *KubeadmRuntime) deleteMaster(master string) error { return k.resetNode(master, func() { - //remove master + // remove master masterIPs := str2.RemoveFromSlice(k.getMasterIPList(), master) if len(masterIPs) > 0 { // TODO: do we need draining first? if err := k.removeNode(master); err != nil { - logger.Warn(fmt.Errorf("delete master %s failed %v", master, err)) + logger.Warn(fmt.Errorf("delete master %s failed %w", master, err)) } } }) diff --git a/lifecycle/pkg/runtime/kubernetes/node.go b/lifecycle/pkg/runtime/kubernetes/node.go index ae60698c80cc..0fee4c9769bf 100644 --- a/lifecycle/pkg/runtime/kubernetes/node.go +++ b/lifecycle/pkg/runtime/kubernetes/node.go @@ -23,7 +23,6 @@ import ( "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" - "golang.org/x/sync/errgroup" ) @@ -42,27 +41,29 @@ func (k *KubeadmRuntime) joinNodes(newNodesIPList []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, node := range newNodesIPList { - node := node eg.Go(func() error { logger.Info("start to join %s as worker", node) k.mu.Lock() err = k.copyKubeadmConfigToNode(node) if err != nil { - return fmt.Errorf("failed to copy join node kubeadm config %s %v", node, err) + return fmt.Errorf("failed to copy join node kubeadm config %s %w", node, err) } k.mu.Unlock() logger.Info("run ipvs once module: %s", node) err = k.execIPVS(node, masters) if err != nil { - return fmt.Errorf("run ipvs once failed %v", err) + return fmt.Errorf("run ipvs once failed %w", err) } logger.Info("start join node: %s", node) joinCmd := k.Command(JoinNode) if joinCmd == "" { - return fmt.Errorf("get join node command failed, kubernetes version is %s", k.getKubeVersion()) + return fmt.Errorf( + "get join node command failed, kubernetes version is %s", + k.getKubeVersion(), + ) } if err = k.sshCmdAsync(node, joinCmd); err != nil { - return fmt.Errorf("failed to join node %s %v", node, err) + return fmt.Errorf("failed to join node %s %w", node, err) } logger.Info("succeeded in joining %s as worker", node) return nil @@ -75,9 +76,13 @@ func (k *KubeadmRuntime) copyKubeadmConfigToNode(node string) error { logger.Info("start to copy kubeadm join config to node: %s", node) data, err := k.generateJoinNodeConfigs(node) if err != nil { - return fmt.Errorf("failed to generate join kubeadm config: %v", err) + return fmt.Errorf("failed to generate join kubeadm config: %w", err) } - joinConfigPath := path.Join(k.pathResolver.TmpPath(), iputils.GetHostIP(node), defaultJoinNodeKubeadmFileName) + joinConfigPath := path.Join( + k.pathResolver.TmpPath(), + iputils.GetHostIP(node), + defaultJoinNodeKubeadmFileName, + ) outConfigPath := path.Join(k.pathResolver.ConfigsPath(), defaultJoinNodeKubeadmFileName) err = file.WriteFile(joinConfigPath, data) if err != nil { @@ -96,11 +101,10 @@ func (k *KubeadmRuntime) deleteNodes(nodes []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodes { - node := node eg.Go(func() error { logger.Info("start to delete worker %s", node) if err := k.deleteNode(node); err != nil { - return fmt.Errorf("delete node %s failed %v", node, err) + return fmt.Errorf("delete node %s failed %w", node, err) } logger.Info("succeeded in deleting worker %s", node) return nil @@ -111,10 +115,10 @@ func (k *KubeadmRuntime) deleteNodes(nodes []string) error { func (k *KubeadmRuntime) deleteNode(node string) error { return k.resetNode(node, func() { - //remove node + // remove node if len(k.getMasterIPList()) > 0 { if err := k.removeNode(node); err != nil { - logger.Warn(fmt.Errorf("delete node %s failed %v", node, err)) + logger.Warn(fmt.Errorf("delete node %s failed %w", node, err)) } } }) diff --git a/lifecycle/pkg/runtime/kubernetes/reset.go b/lifecycle/pkg/runtime/kubernetes/reset.go index ebce74b3813c..6e6d175d6ab5 100644 --- a/lifecycle/pkg/runtime/kubernetes/reset.go +++ b/lifecycle/pkg/runtime/kubernetes/reset.go @@ -17,11 +17,10 @@ package kubernetes import ( "context" "fmt" - - "golang.org/x/exp/slices" - "golang.org/x/sync/errgroup" + "slices" "github.com/labring/sealos/pkg/utils/logger" + "golang.org/x/sync/errgroup" ) const ( @@ -43,7 +42,6 @@ func (k *KubeadmRuntime) resetNodes(nodes []string) { logger.Info("start to reset nodes: %v", nodes) eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodes { - node := node eg.Go(func() error { if err := k.resetNode(node, nil); err != nil { logger.Error("delete node %s failed %v", node, err) @@ -79,7 +77,11 @@ func (k *KubeadmRuntime) resetNode(node string, cleanHook func()) error { } removeKubeConfigErr := k.sshCmdAsync(node, removeKubeConfig) if removeKubeConfigErr != nil { - logger.Error("failed to clean node, exec command %s failed, %v", removeKubeConfig, removeKubeConfigErr) + logger.Error( + "failed to clean node, exec command %s failed, %v", + removeKubeConfig, + removeKubeConfigErr, + ) } if slices.Contains(k.cluster.GetNodeIPAndPortList(), node) { ipvscleanErr := k.execIPVSClean(node) diff --git a/lifecycle/pkg/runtime/kubernetes/runtime.go b/lifecycle/pkg/runtime/kubernetes/runtime.go index afd2cd810621..704224c03423 100644 --- a/lifecycle/pkg/runtime/kubernetes/runtime.go +++ b/lifecycle/pkg/runtime/kubernetes/runtime.go @@ -20,7 +20,6 @@ import ( "sync" "github.com/Masterminds/semver/v3" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/exec" @@ -72,7 +71,8 @@ func (k *KubeadmRuntime) GetRawConfig() ([]byte, error) { if err != nil { return nil, err } - objects := []interface{}{cluster, + objects := []any{ + cluster, conversion.InitConfiguration, conversion.ClusterConfiguration, conversion.JoinConfiguration, @@ -87,11 +87,15 @@ func (k *KubeadmRuntime) GetRawConfig() ([]byte, error) { } func (k *KubeadmRuntime) Reset() error { - logger.Info("start to delete Cluster: master %s, node %s", k.getMasterIPList(), k.getNodeIPList()) + logger.Info( + "start to delete Cluster: master %s, node %s", + k.getMasterIPList(), + k.getNodeIPList(), + ) return k.reset() } -func (k *KubeadmRuntime) ScaleUp(newMasterIPList []string, newNodeIPList []string) error { +func (k *KubeadmRuntime) ScaleUp(newMasterIPList, newNodeIPList []string) error { if len(newMasterIPList) != 0 { logger.Info("%s will be added as master", newMasterIPList) if err := k.joinMasters(newMasterIPList); err != nil { @@ -108,7 +112,7 @@ func (k *KubeadmRuntime) ScaleUp(newMasterIPList []string, newNodeIPList []strin return nil } -func (k *KubeadmRuntime) ScaleDown(deleteMastersIPList []string, deleteNodesIPList []string) error { +func (k *KubeadmRuntime) ScaleDown(deleteMastersIPList, deleteNodesIPList []string) error { if len(deleteMastersIPList) != 0 { logger.Info("master %s will be deleted", deleteMastersIPList) if err := k.deleteMasters(deleteMastersIPList); err != nil { @@ -158,13 +162,13 @@ func New(cluster *v2.Cluster, config any) (*KubeadmRuntime, error) { func (k *KubeadmRuntime) Validate() error { if len(k.cluster.Spec.Hosts) == 0 { - return fmt.Errorf("master hosts cannot be empty") + return errors.New("master hosts cannot be empty") } if k.getMaster0IP() == "" { - return fmt.Errorf("master hosts ip cannot be empty") + return errors.New("master hosts ip cannot be empty") } if k.getKubeVersionFromImage() == "" && k.cluster.DeletionTimestamp.IsZero() { - return fmt.Errorf("cluster image kubernetes version cannot be empty") + return errors.New("cluster image kubernetes version cannot be empty") } return nil } @@ -189,7 +193,11 @@ func (k *KubeadmRuntime) Upgrade(version string) error { return fmt.Errorf("cannot apply an older version %s than %s", version, currVersion) } if v0.Minor()+1 < v1.Minor() { - return fmt.Errorf("cannot be upgraded across more than one major releases, %s -> %s", currVersion, version) + return fmt.Errorf( + "cannot be upgraded across more than one major releases, %s -> %s", + currVersion, + version, + ) } return k.upgradeCluster(version) diff --git a/lifecycle/pkg/runtime/kubernetes/runtime_getter.go b/lifecycle/pkg/runtime/kubernetes/runtime_getter.go index 2bc428d5caeb..c32ccdc4f3d6 100644 --- a/lifecycle/pkg/runtime/kubernetes/runtime_getter.go +++ b/lifecycle/pkg/runtime/kubernetes/runtime_getter.go @@ -19,20 +19,21 @@ package kubernetes import ( "context" "fmt" + "net" "path" + "strconv" "strings" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" + "golang.org/x/sync/errgroup" ) func (k *KubeadmRuntime) getKubeVersion() string { - return k.kubeadmConfig.ClusterConfiguration.KubernetesVersion + return k.kubeadmConfig.KubernetesVersion } // old implementation doesn't consider multiple rootfs images; here get the first rootfs image @@ -54,8 +55,9 @@ func (k *KubeadmRuntime) getMasterIPList() []string { func (k *KubeadmRuntime) getMasterIPListAndHTTPSPort() []string { masters := make([]string, 0) + port := strconv.Itoa(int(k.getAPIServerPort())) for _, master := range k.getMasterIPList() { - masters = append(masters, fmt.Sprintf("%s:%d", master, k.getAPIServerPort())) + masters = append(masters, net.JoinHostPort(master, port)) } return masters } @@ -78,7 +80,7 @@ func (k *KubeadmRuntime) getMaster0IPAndPort() string { func (k *KubeadmRuntime) getMaster0IPAPIServer() string { master0 := k.getMaster0IP() - return fmt.Sprintf("https://%s:%d", master0, k.getAPIServerPort()) + return "https://" + net.JoinHostPort(master0, strconv.Itoa(int(k.getAPIServerPort()))) } func (k *KubeadmRuntime) execIPVS(ip string, masters []string) error { @@ -91,18 +93,21 @@ func (k *KubeadmRuntime) execIPVSClean(ip string) error { func (k *KubeadmRuntime) syncNodeIPVSYaml(masterIPs, nodesIPs []string) error { masters := make([]string, 0) + port := strconv.Itoa(int(k.getAPIServerPort())) for _, master := range masterIPs { - masters = append(masters, fmt.Sprintf("%s:%d", iputils.GetHostIP(master), k.getAPIServerPort())) + masters = append( + masters, + net.JoinHostPort(iputils.GetHostIP(master), port), + ) } eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodesIPs { - node := node eg.Go(func() error { logger.Info("start to sync lvscare static pod to node: %s master: %+v", node, masters) err := k.execIPVSPod(node, masters) if err != nil { - return fmt.Errorf("update lvscare static pod failed %s %v", node, err) + return fmt.Errorf("update lvscare static pod failed %s %w", node, err) } return nil }) @@ -112,7 +117,14 @@ func (k *KubeadmRuntime) syncNodeIPVSYaml(masterIPs, nodesIPs []string) error { func (k *KubeadmRuntime) execIPVSPod(ip string, masters []string) error { image := k.cluster.GetLvscareImage() - return k.remoteUtil.StaticPod(ip, k.getVipAndPort(), constants.LvsCareStaticPodName, image, masters, kubernetesEtcStaticPod) + return k.remoteUtil.StaticPod( + ip, + k.getVipAndPort(), + constants.LvsCareStaticPodName, + image, + masters, + kubernetesEtcStaticPod, + ) } func (k *KubeadmRuntime) execToken(ip, certificateKey string) (string, error) { @@ -133,14 +145,21 @@ func (k *KubeadmRuntime) execCert(ip string) error { if err != nil { return err } - return k.remoteUtil.Cert(ip, k.getCertSANs(), iputils.GetHostIP(ip), hostname, k.getServiceCIDR(), k.getDNSDomain()) + return k.remoteUtil.Cert( + ip, + k.getCertSANs(), + iputils.GetHostIP(ip), + hostname, + k.getServiceCIDR(), + k.getDNSDomain(), + ) } func (k *KubeadmRuntime) sshCmdAsync(host string, cmd ...string) error { return k.execer.CmdAsync(host, cmd...) } -func (k *KubeadmRuntime) sshCmdToString(host string, cmd string) (string, error) { +func (k *KubeadmRuntime) sshCmdToString(host, cmd string) (string, error) { return k.execer.CmdToString(host, cmd, "") } @@ -152,7 +171,10 @@ func (k *KubeadmRuntime) getKubeInterface() (kubernetes.Client, error) { if k.cli != nil { return k.cli, nil } - cli, err := kubernetes.NewKubernetesClient(k.pathResolver.AdminFile(), k.getMaster0IPAPIServer()) + cli, err := kubernetes.NewKubernetesClient( + k.pathResolver.AdminFile(), + k.getMaster0IPAPIServer(), + ) if err != nil { return nil, err } diff --git a/lifecycle/pkg/runtime/kubernetes/static_files.go b/lifecycle/pkg/runtime/kubernetes/static_files.go index 06212fbbb91a..183bed12356e 100644 --- a/lifecycle/pkg/runtime/kubernetes/static_files.go +++ b/lifecycle/pkg/runtime/kubernetes/static_files.go @@ -19,9 +19,8 @@ import ( "fmt" "path/filepath" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/utils/logger" + "golang.org/x/sync/errgroup" ) const ( @@ -51,10 +50,14 @@ func (k *KubeadmRuntime) copyStaticFiles(nodes []string) error { logger.Info("start to copy static files to masters") for _, file := range MasterStaticFiles { staticFilePath := filepath.Join(k.pathResolver.RootFSStaticsPath(), file.Name) - cmdLinkStatic := fmt.Sprintf(copyFileToDirCommand, file.DestinationDir, staticFilePath, filepath.Join(file.DestinationDir, file.Name)) + cmdLinkStatic := fmt.Sprintf( + copyFileToDirCommand, + file.DestinationDir, + staticFilePath, + filepath.Join(file.DestinationDir, file.Name), + ) eg, _ := errgroup.WithContext(context.Background()) for _, host := range nodes { - host := host eg.Go(func() error { err := k.sshCmdAsync(host, cmdLinkStatic) if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/types/default_kubeadm_config.go b/lifecycle/pkg/runtime/kubernetes/types/default_kubeadm_config.go index 589b7193eb72..88c5197cf54f 100644 --- a/lifecycle/pkg/runtime/kubernetes/types/default_kubeadm_config.go +++ b/lifecycle/pkg/runtime/kubernetes/types/default_kubeadm_config.go @@ -199,5 +199,14 @@ controlPlane: ) func DefaultKubeadmInitConfiguration() string { - return strings.Join([]string{defaultKubeadmInitConfiguration, defaultKubeadmClusterConfiguration, defaultKubeadmKubeProxyConfiguration, defaultKubeadmKubeletConfiguration, defaultKubeadmJoinConfiguration}, "\n---\n") + return strings.Join( + []string{ + defaultKubeadmInitConfiguration, + defaultKubeadmClusterConfiguration, + defaultKubeadmKubeProxyConfiguration, + defaultKubeadmKubeletConfiguration, + defaultKubeadmJoinConfiguration, + }, + "\n---\n", + ) } diff --git a/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config.go b/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config.go index ad3ab1946329..a5b2c02930bc 100644 --- a/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config.go +++ b/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config.go @@ -17,20 +17,18 @@ package types import ( "fmt" - "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4" - "github.com/imdario/mergo" + "github.com/labring/sealos/pkg/runtime/decode" + "github.com/labring/sealos/pkg/utils/file" + "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/pkg/utils/maps" k8sruntime "k8s.io/apimachinery/pkg/runtime" versionutil "k8s.io/apimachinery/pkg/util/version" kubeproxyconfigv1alpha1 "k8s.io/kube-proxy/config/v1alpha1" kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1" "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" // internal version "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3" - - "github.com/labring/sealos/pkg/runtime/decode" - "github.com/labring/sealos/pkg/utils/file" - "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/pkg/utils/maps" + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4" ) //nolint:all @@ -44,7 +42,7 @@ type KubeadmConfig struct { } type ConvertedKubeadmConfig struct { - InitConfiguration, ClusterConfiguration, JoinConfiguration, KubeProxyConfiguration, KubeletConfiguration interface{} + InitConfiguration, ClusterConfiguration, JoinConfiguration, KubeProxyConfiguration, KubeletConfiguration any } func NewKubeadmConfig() *KubeadmConfig { @@ -108,7 +106,11 @@ func (k *KubeadmConfig) FetchDefaultKubeadmConfig() string { // if defaultKubeadmConfig file not exist, use default raw kubeadm Config to merge k.KubeConfigSpec empty value func (k *KubeadmConfig) Merge(kubeadmYamlPath string) error { if kubeadmYamlPath == "" { - defaultKubeadmConfig, err := LoadKubeadmConfigs(k.FetchDefaultKubeadmConfig(), false, decode.CRDFromString) + defaultKubeadmConfig, err := LoadKubeadmConfigs( + k.FetchDefaultKubeadmConfig(), + false, + decode.CRDFromString, + ) if err != nil { return err } @@ -120,11 +122,11 @@ func (k *KubeadmConfig) Merge(kubeadmYamlPath string) error { logger.Debug("trying to merge kubeadm configs from file %s", kubeadmYamlPath) kc, err := LoadKubeadmConfigs(kubeadmYamlPath, false, decode.CRDFromFile) if err != nil { - return fmt.Errorf("failed to load kubeadm config from %s: %v", kubeadmYamlPath, err) + return fmt.Errorf("failed to load kubeadm config from %s: %w", kubeadmYamlPath, err) } err = mergo.Merge(k, kc, defaultMergeOpts...) if err != nil { - return fmt.Errorf("failed to merge kubeadm config from %s: %v", kubeadmYamlPath, err) + return fmt.Errorf("failed to merge kubeadm config from %s: %w", kubeadmYamlPath, err) } return nil } @@ -213,7 +215,7 @@ func (k *KubeadmConfig) SetDefaults() { } func (k *KubeadmConfig) SetKubeVersion(version string) { - k.ClusterConfiguration.KubernetesVersion = version + k.KubernetesVersion = version } func (k *KubeadmConfig) SetAPIVersion(apiVersion string) { @@ -223,11 +225,16 @@ func (k *KubeadmConfig) SetAPIVersion(apiVersion string) { } func (k *KubeadmConfig) FinalizeFeatureGatesConfiguration() { - if k.ClusterConfiguration.KubernetesVersion == "" { + if k.KubernetesVersion == "" { panic("kubernetesVersion must not been null") } - extraArgs := [][]kubeadm.Arg{k.ClusterConfiguration.ControllerManager.ExtraArgs, k.ClusterConfiguration.APIServer.ExtraArgs, k.ClusterConfiguration.Scheduler.ExtraArgs} - if versionutil.MustParseSemantic(k.ClusterConfiguration.KubernetesVersion).LessThan(versionutil.MustParseSemantic("1.19.0")) { + extraArgs := [][]kubeadm.Arg{ + k.ControllerManager.ExtraArgs, + k.APIServer.ExtraArgs, + k.Scheduler.ExtraArgs, + } + if versionutil.MustParseSemantic(k.ClusterConfiguration.KubernetesVersion). + LessThan(versionutil.MustParseSemantic("1.19.0")) { for i, args := range extraArgs { for j, v := range args { if v.Name == "cluster-signing-duration" { @@ -247,15 +254,18 @@ func (k *KubeadmConfig) FinalizeFeatureGatesConfiguration() { } for j, v := range args { if v.Name == "feature-gates" && v.Value != "" { - newValue := updateFeatureGatesConfiguration(v.Value, k.ClusterConfiguration.KubernetesVersion).(string) - args[j].Value = newValue + if newValue, ok := updateFeatureGatesConfiguration(v.Value, k.KubernetesVersion).(string); ok { + args[j].Value = newValue + } } } extraArgs[i] = args } - k.KubeletConfiguration.FeatureGates = updateFeatureGatesConfiguration( - k.KubeletConfiguration.FeatureGates, k.ClusterConfiguration.KubernetesVersion).(map[string]bool) + if gates, ok := updateFeatureGatesConfiguration( + k.KubeletConfiguration.FeatureGates, k.KubernetesVersion).(map[string]bool); ok { + k.KubeletConfiguration.FeatureGates = gates + } } // https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates-removed/ @@ -265,10 +275,14 @@ var featureGatesUpdate = map[string][]string{ "EphemeralContainers": {"AtLeast", "v1.26.0"}, } -func deleteFeatureMap[T string | bool](currentFeature map[string]T, versionStr string) map[string]T { +func deleteFeatureMap[T string | bool]( + currentFeature map[string]T, + versionStr string, +) map[string]T { for k, v := range featureGatesUpdate { if v[0] == "LessThan" && - versionutil.MustParseSemantic(versionStr).LessThan(versionutil.MustParseSemantic(v[1])) { + versionutil.MustParseSemantic(versionStr). + LessThan(versionutil.MustParseSemantic(v[1])) { delete(currentFeature, k) } if v[0] == "AtLeast" && @@ -293,9 +307,13 @@ func updateFeatureGatesConfiguration(featureGates any, version string) any { return nil } -func LoadKubeadmConfigs(arg string, setDefaults bool, decodeFunc func(arg string, kind string) (interface{}, error)) (*KubeadmConfig, error) { +func LoadKubeadmConfigs( + arg string, + setDefaults bool, + decodeFunc func(arg, kind string) (any, error), +) (*KubeadmConfig, error) { if setDefaults { - decodeFunc = func(arg string, kind string) (interface{}, error) { + decodeFunc = func(arg, kind string) (any, error) { ret, err := decodeFunc(arg, kind) if err != nil { return nil, err @@ -311,31 +329,41 @@ func LoadKubeadmConfigs(arg string, setDefaults bool, decodeFunc func(arg string if err != nil { return nil, err } else if initConfig != nil { - kubeadmConfig.InitConfiguration = *initConfig.(*kubeadm.InitConfiguration) + if initCfg, ok := initConfig.(*kubeadm.InitConfiguration); ok { + kubeadmConfig.InitConfiguration = *initCfg + } } clusterConfig, err := decodeFunc(arg, decode.ClusterConfiguration) if err != nil { return nil, err } else if clusterConfig != nil { - kubeadmConfig.ClusterConfiguration = *clusterConfig.(*kubeadm.ClusterConfiguration) + if clusterCfg, ok := clusterConfig.(*kubeadm.ClusterConfiguration); ok { + kubeadmConfig.ClusterConfiguration = *clusterCfg + } } kubeProxyConfig, err := decodeFunc(arg, decode.KubeProxyConfiguration) if err != nil { return nil, err } else if kubeProxyConfig != nil { - kubeadmConfig.KubeProxyConfiguration = *kubeProxyConfig.(*kubeproxyconfigv1alpha1.KubeProxyConfiguration) + if proxyCfg, ok := kubeProxyConfig.(*kubeproxyconfigv1alpha1.KubeProxyConfiguration); ok { + kubeadmConfig.KubeProxyConfiguration = *proxyCfg + } } kubeletConfig, err := decodeFunc(arg, decode.KubeletConfiguration) if err != nil { return nil, err } else if kubeletConfig != nil { - kubeadmConfig.KubeletConfiguration = *kubeletConfig.(*kubeletconfigv1beta1.KubeletConfiguration) + if kubeletCfg, ok := kubeletConfig.(*kubeletconfigv1beta1.KubeletConfiguration); ok { + kubeadmConfig.KubeletConfiguration = *kubeletCfg + } } joinConfig, err := decodeFunc(arg, decode.JoinConfiguration) if err != nil { return nil, err } else if joinConfig != nil { - kubeadmConfig.JoinConfiguration = *joinConfig.(*kubeadm.JoinConfiguration) + if joinCfg, ok := joinConfig.(*kubeadm.JoinConfiguration); ok { + kubeadmConfig.JoinConfiguration = *joinCfg + } } return kubeadmConfig, nil } diff --git a/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config_test.go b/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config_test.go index a423cc1030ff..6ae27608c3a6 100644 --- a/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config_test.go +++ b/lifecycle/pkg/runtime/kubernetes/types/kubeadm_config_test.go @@ -320,6 +320,7 @@ func TestKubeadmRuntime_setFeatureGatesConfigurationLowerVersion(t *testing.T) { }) } } + func TestKubeadmRuntime_setFeatureGatesConfiguration(t *testing.T) { tests := []struct { name string @@ -344,7 +345,11 @@ func TestKubeadmRuntime_setFeatureGatesConfiguration(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - k, err := LoadKubeadmConfigs(DefaultKubeadmInitConfiguration(), false, decode.CRDFromString) + k, err := LoadKubeadmConfigs( + DefaultKubeadmInitConfiguration(), + false, + decode.CRDFromString, + ) if err != nil { t.Fatalf("error loading default kubeadm config: %v", err) } @@ -364,6 +369,7 @@ func TestKubeadmRuntime_setFeatureGatesConfiguration(t *testing.T) { }) } } + func TestKubeadmRuntime_setFeatureGatesConfiguration4Controller(t *testing.T) { testyaml := `apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration diff --git a/lifecycle/pkg/runtime/kubernetes/types/types.go b/lifecycle/pkg/runtime/kubernetes/types/types.go index d55b83ae080d..0e48ce4d7206 100644 --- a/lifecycle/pkg/runtime/kubernetes/types/types.go +++ b/lifecycle/pkg/runtime/kubernetes/types/types.go @@ -25,7 +25,6 @@ type Token struct { Expires *metav1.Time `json:"expires,omitempty"` } -//nolint:all type Config struct { *KubeadmConfig APIServerDomain string diff --git a/lifecycle/pkg/runtime/kubernetes/upgrade.go b/lifecycle/pkg/runtime/kubernetes/upgrade.go index 51c2de2b1ca9..4fa3bed9c0b4 100644 --- a/lifecycle/pkg/runtime/kubernetes/upgrade.go +++ b/lifecycle/pkg/runtime/kubernetes/upgrade.go @@ -16,26 +16,26 @@ package kubernetes import ( "context" + "errors" "fmt" "path" "strings" "time" "github.com/Masterminds/semver/v3" - v1 "k8s.io/api/core/v1" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" - "github.com/labring/sealos/pkg/runtime/decode" "github.com/labring/sealos/pkg/runtime/kubernetes/types" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/yaml" + v1 "k8s.io/api/core/v1" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" ) const ( upgradeApplyCmd = "kubeadm upgrade apply --certificate-renewal=false --config %s --yes" upradeNodeCmd = "kubeadm upgrade node --certificate-renewal=false --skip-phases preflight" - //drainNodeCmd = "kubectl drain %s --ignore-daemonsets" + // drainNodeCmd = "kubectl drain %s --ignore-daemonsets" cordonNodeCmd = "kubectl cordon %s" uncordonNodeCmd = "kubectl uncordon %s" daemonReload = "systemctl daemon-reload" @@ -56,15 +56,16 @@ func (k *KubeadmRuntime) upgradeCluster(version string) error { if err != nil { return err } - //upgrade master0 + // upgrade master0 logger.Info("start to upgrade master0") err = k.upgradeMaster0(conversion, version) if err != nil { return err } - //upgrade other control-planes and worker nodes - var upgradeNodes []string - for _, node := range append(k.getMasterIPAndPortList(), k.getNodeIPAndPortList()...) { + // upgrade other control-planes and worker nodes + allNodes := append(k.getMasterIPAndPortList(), k.getNodeIPAndPortList()...) + upgradeNodes := make([]string, 0, len(allNodes)-1) // exclude master0 + for _, node := range allNodes { if node == k.getMaster0IPAndPort() { continue } @@ -74,7 +75,10 @@ func (k *KubeadmRuntime) upgradeCluster(version string) error { return k.upgradeOtherNodes(upgradeNodes, version) } -func (k *KubeadmRuntime) upgradeMaster0(conversion *types.ConvertedKubeadmConfig, version string) error { +func (k *KubeadmRuntime) upgradeMaster0( + conversion *types.ConvertedKubeadmConfig, + version string, +) error { master0ip := k.getMaster0IP() sver := semver.MustParse(version) if gte(sver, V1260) { @@ -93,10 +97,10 @@ func (k *KubeadmRuntime) upgradeMaster0(conversion *types.ConvertedKubeadmConfig if err != nil { return err } - //default nodeName in k8s is the lower case of their hostname because of DNS protocol. + // default nodeName in k8s is the lower case of their hostname because of DNS protocol. master0Name = strings.ToLower(master0Name) kubeBinaryPath := k.pathResolver.RootFSBinPath() - //assure the connection to api-server succeed before executing upgrade cmds + // assure the connection to api-server succeed before executing upgrade cmds if err = k.pingAPIServer(); err != nil { return err } @@ -107,7 +111,10 @@ func (k *KubeadmRuntime) upgradeMaster0(conversion *types.ConvertedKubeadmConfig logger.Warn("image pull pre-upgrade failed: %s", err.Error()) } - config, err := yaml.MarshalConfigs(&conversion.InitConfiguration, &conversion.ClusterConfiguration) + config, err := yaml.MarshalConfigs( + &conversion.InitConfiguration, + &conversion.ClusterConfiguration, + ) if err != nil { logger.Error("kubeadm config marshal failed: %s", err.Error()) return err @@ -117,18 +124,18 @@ func (k *KubeadmRuntime) upgradeMaster0(conversion *types.ConvertedKubeadmConfig upgradeConfigPath := path.Join(k.pathResolver.EtcPath(), upgradeConfigName) err = k.sshCmdAsync(master0ip, - //install kubeadm:{version} at master0 + // install kubeadm:{version} at master0 fmt.Sprintf(installKubeadmCmd, kubeBinaryPath), // write kubeadm config to file fmt.Sprintf(writeKubeadmConfig, upgradeConfigPath, string(config)), - //execute kubeadm upgrade apply {version} at master0 + // execute kubeadm upgrade apply {version} at master0 fmt.Sprintf(upgradeApplyCmd, upgradeConfigPath), - //kubectl cordon + // kubectl cordon fmt.Sprintf(cordonNodeCmd, master0Name), - //install kubelet:{version},kubectl{version} at master0 + // install kubelet:{version},kubectl{version} at master0 fmt.Sprintf(installKubectlCmd, kubeBinaryPath), fmt.Sprintf(installKubeletCmd, kubeBinaryPath), - //reload kubelet daemon + // reload kubelet daemon daemonReload, restartKubelet, ) @@ -157,10 +164,10 @@ func (k *KubeadmRuntime) upgradeOtherNodes(ips []string, version string) error { if err != nil { return err } - //default nodeName in k8s is the lower case of their hostname because of DNS protocol. + // default nodeName in k8s is the lower case of their hostname because of DNS protocol. nodename = strings.ToLower(nodename) kubeBinaryPath := k.pathResolver.RootFSBinPath() - //assure the connection to api-server succeed before executing upgrade cmds + // assure the connection to api-server succeed before executing upgrade cmds if err = k.pingAPIServer(); err != nil { return err } @@ -173,16 +180,16 @@ func (k *KubeadmRuntime) upgradeOtherNodes(ips []string, version string) error { logger.Info("upgrade node %s", nodename) err = k.sshCmdAsync(ip, - //install kubeadm:{version} at the node + // install kubeadm:{version} at the node fmt.Sprintf(installKubeadmCmd, kubeBinaryPath), - //upgrade other control-plane and nodes + // upgrade other control-plane and nodes upradeNodeCmd, - //kubectl cordon + // kubectl cordon fmt.Sprintf(cordonNodeCmd, nodename), - //install kubelet:{version},kubectl{version} at the node + // install kubelet:{version},kubectl{version} at the node fmt.Sprintf(installKubectlCmd, kubeBinaryPath), fmt.Sprintf(installKubeletCmd, kubeBinaryPath), - //reload kubelet daemon + // reload kubelet daemon daemonReload, restartKubelet, ) @@ -274,7 +281,7 @@ func (k *KubeadmRuntime) pingAPIServer() error { break } if time.Now().After(timeout) { - return fmt.Errorf("restart api-server timeout within one minute") + return errors.New("restart api-server timeout within one minute") } time.Sleep(5 * time.Second) } @@ -306,7 +313,8 @@ func (k *KubeadmRuntime) changeCRIVersion(ip string) error { } func (k *KubeadmRuntime) changeKubeletExtraArgs(ip string) error { - return k.sshCmdAsync(ip, + return k.sshCmdAsync( + ip, `FILE="/etc/systemd/system/kubelet.service.d/10-kubeadm.conf" && [ -f "$FILE" ] && sed -i 's/\(--container-runtime=\|--pod-infra-container-image=\)\([^ ]*\)\?//g' "$FILE"`, "systemctl daemon-reload", "systemctl restart kubelet", diff --git a/lifecycle/pkg/runtime/kubernetes/utils.go b/lifecycle/pkg/runtime/kubernetes/utils.go index 73bd7159a6dc..77df4f2fde04 100644 --- a/lifecycle/pkg/runtime/kubernetes/utils.go +++ b/lifecycle/pkg/runtime/kubernetes/utils.go @@ -21,18 +21,17 @@ import ( "fmt" "path" + "github.com/labring/sealos/pkg/client-go/kubernetes" + "github.com/labring/sealos/pkg/utils/logger" "golang.org/x/sync/errgroup" "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/labring/sealos/pkg/client-go/kubernetes" - "github.com/labring/sealos/pkg/utils/logger" ) func (k *KubeadmRuntime) runPipelines(phase string, pipelines ...func() error) error { for i := range pipelines { if err := pipelines[i](); err != nil { - return fmt.Errorf("failed to %s: %v", phase, err) + return fmt.Errorf("failed to %s: %w", phase, err) } } return nil @@ -59,13 +58,12 @@ func (k *KubeadmRuntime) sendNewCertAndKey(hosts []string) error { return k.sendFileToHosts(hosts, k.pathResolver.PkiPath(), kubernetesEtcPKI) } -func (k *KubeadmRuntime) sendFileToHosts(Hosts []string, src, dst string) error { +func (k *KubeadmRuntime) sendFileToHosts(hosts []string, src, dst string) error { eg, _ := errgroup.WithContext(context.Background()) - for _, node := range Hosts { - node := node + for _, node := range hosts { eg.Go(func() error { if err := k.sshCopy(node, src, dst); err != nil { - return fmt.Errorf("send file failed %v", err) + return fmt.Errorf("send file failed %w", err) } return nil }) @@ -83,10 +81,13 @@ func (k *KubeadmRuntime) removeNode(ip string) error { exp := kubernetes.NewKubeExpansion(client.Kubernetes()) hostname, err := exp.FetchHostNameFromInternalIP(ctx, ip) if err != nil { - return fmt.Errorf("cannot get node with ip address %s: %v", ip, err) + return fmt.Errorf("cannot get node with ip address %s: %w", ip, err) } deletePropagation := v1.DeletePropagationBackground - err = client.Kubernetes().CoreV1().Nodes().Delete(ctx, hostname, v1.DeleteOptions{PropagationPolicy: &deletePropagation}) + err = client.Kubernetes(). + CoreV1(). + Nodes(). + Delete(ctx, hostname, v1.DeleteOptions{PropagationPolicy: &deletePropagation}) if err != nil && !errors.IsNotFound(err) { return err } diff --git a/lifecycle/pkg/runtime/utils/token.go b/lifecycle/pkg/runtime/utils/token.go index 00bc94f70141..d91855e20994 100644 --- a/lifecycle/pkg/runtime/utils/token.go +++ b/lifecycle/pkg/runtime/utils/token.go @@ -26,18 +26,17 @@ import ( "strings" "time" - "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/client-go/tools/clientcmd" - clientcmdapi "k8s.io/client-go/tools/clientcmd/api" - "k8s.io/client-go/util/cert" - v1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1" - "github.com/labring/sealos/pkg/runtime/kubernetes/types" "github.com/labring/sealos/pkg/utils/exec" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/rand" "github.com/labring/sealos/pkg/utils/yaml" + "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + "k8s.io/client-go/util/cert" + v1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1" ) const defaultAdminConf = "/etc/kubernetes/admin.conf" @@ -50,9 +49,12 @@ func GenerateToken(config, certificateKey string) (*types.Token, error) { key = certificateKey } token.CertificateKey = key - uploadCertTemplate := fmt.Sprintf("kubeadm init phase upload-certs --upload-certs --certificate-key %s", key) + uploadCertTemplate := "kubeadm init phase upload-certs --upload-certs --certificate-key " + key if config != "" { - uploadCertTemplate = fmt.Sprintf("kubeadm init phase upload-certs --config %s --upload-certs", config) + uploadCertTemplate = fmt.Sprintf( + "kubeadm init phase upload-certs --config %s --upload-certs", + config, + ) } logger.Debug("token uploadCertTemplate cmd: %s", uploadCertTemplate) _, _ = exec.RunBashCmd(uploadCertTemplate) @@ -77,10 +79,10 @@ func GenerateToken(config, certificateKey string) (*types.Token, error) { } return token, nil } - return nil, fmt.Errorf("token list found more than one") + return nil, errors.New("token list found more than one") } - return nil, fmt.Errorf("kubeadm command not found or /etc/kubernetes/admin.conf not exist") + return nil, errors.New("kubeadm command not found or /etc/kubernetes/admin.conf not exist") } func ListToken() BootstrapTokens { @@ -99,11 +101,11 @@ func processTokenList(data string) BootstrapTokens { slice = append(slice, to) } } - var result []v1.BootstrapToken + result := make([]v1.BootstrapToken, 0, len(slice)) for _, token := range slice { if token.Expires != nil { t := time.Now().Unix() - ex := token.Expires.Time.Unix() + ex := token.Expires.Unix() if ex < t { continue } @@ -139,17 +141,24 @@ func discoveryTokenCaCertHash(adminPath string) ([]string, error) { // load CA certificates from the kubeconfig (either from PEM data or by file path) var caCerts []*x509.Certificate - if clusterConfig.CertificateAuthorityData != nil { + switch { + case clusterConfig.CertificateAuthorityData != nil: caCerts, err = cert.ParseCertsPEM(clusterConfig.CertificateAuthorityData) if err != nil { - return nil, fmt.Errorf("failed to parse CA certificate from kubeconfig: %w", err) + return nil, fmt.Errorf( + "failed to parse CA certificate from kubeconfig: %w", + err, + ) } - } else if clusterConfig.CertificateAuthority != "" { + case clusterConfig.CertificateAuthority != "": caCerts, err = cert.CertsFromFile(clusterConfig.CertificateAuthority) if err != nil { - return nil, fmt.Errorf("failed to load CA certificate referenced by kubeconfig: %w", err) + return nil, fmt.Errorf( + "failed to load CA certificate referenced by kubeconfig: %w", + err, + ) } - } else { + default: return nil, errors.New("no CA certificates found in kubeconfig") } diff --git a/lifecycle/pkg/ssh/clusterclient.go b/lifecycle/pkg/ssh/clusterclient.go index ecf8b5cf4b0c..e7038ecf0015 100644 --- a/lifecycle/pkg/ssh/clusterclient.go +++ b/lifecycle/pkg/ssh/clusterclient.go @@ -53,12 +53,12 @@ func OverSSHConfig(original, override *v1beta1.SSH) { } } -func (cc *clusterClient) getSSHOptionForHost(host string) (*Option, error) { +func (cc *clusterClient) getSSHOptionForHost(host string) *Option { cc.mutex.RLock() v, ok := cc.configs[host] cc.mutex.RUnlock() if ok { - return v, nil + return v } sshConfig := cc.cluster.Spec.SSH.DeepCopy() for i := range cc.cluster.Spec.Hosts { @@ -73,14 +73,11 @@ func (cc *clusterClient) getSSHOptionForHost(host string) (*Option, error) { cc.mutex.Lock() cc.configs[host] = opt cc.mutex.Unlock() - return opt, nil + return opt } func (cc *clusterClient) getClientForHost(host string) (Interface, error) { - sshConfig, err := cc.getSSHOptionForHost(host) - if err != nil { - return nil, err - } + sshConfig := cc.getSSHOptionForHost(host) cc.mutex.RLock() client := cc.cache[sshConfig] cc.mutex.RUnlock() @@ -121,7 +118,11 @@ func (cc *clusterClient) CmdAsync(host string, cmds ...string) error { return client.CmdAsync(host, cmds...) } -func (cc *clusterClient) CmdAsyncWithContext(ctx context.Context, host string, cmds ...string) error { +func (cc *clusterClient) CmdAsyncWithContext( + ctx context.Context, + host string, + cmds ...string, +) error { client, err := cc.getClientForHost(host) if err != nil { return err diff --git a/lifecycle/pkg/ssh/connect.go b/lifecycle/pkg/ssh/connect.go index 73644db6693d..2fd954fcab55 100644 --- a/lifecycle/pkg/ssh/connect.go +++ b/lifecycle/pkg/ssh/connect.go @@ -17,16 +17,16 @@ package ssh import ( "fmt" "io" + "net" "os" "strings" "sync" "time" - "github.com/pkg/sftp" - "golang.org/x/crypto/ssh" - "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" + "github.com/pkg/sftp" + "golang.org/x/crypto/ssh" ) type HostClientMap struct { @@ -56,7 +56,7 @@ func newSession(client *ssh.Client) (*ssh.Session, error) { return nil, err } modes := ssh.TerminalModes{ - ssh.ECHO: 0, //disable echoing + ssh.ECHO: 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } @@ -73,7 +73,7 @@ func (c *Client) Connect(host string) (sshClient *ssh.Client, session *ssh.Sessi sshClient, session, err = c.newClientAndSession(host) return err }, isErrorWorthRetry) - return + return sshClient, session, err } func isErrorWorthRetry(err error) bool { @@ -83,9 +83,10 @@ func isErrorWorthRetry(err error) bool { func exponentialBackOffRetry(steps int, interval time.Duration, factor int, fn func() error, - retryIfCertainError func(error) bool) error { + retryIfCertainError func(error) bool, +) error { var err error - for i := 0; i < steps; i++ { + for i := range steps { if i > 0 { logger.Debug("retrying %s later due to error occur: %v", interval, err) time.Sleep(interval) @@ -111,24 +112,24 @@ func (c *Client) newClientAndSession(host string) (*ssh.Client, *ssh.Session, er return sshClient, session, err } -func parsePrivateKey(pemBytes []byte, password []byte) (ssh.Signer, error) { +func parsePrivateKey(pemBytes, password []byte) (ssh.Signer, error) { if len(password) == 0 { return ssh.ParsePrivateKey(pemBytes) } return ssh.ParsePrivateKeyWithPassphrase(pemBytes, password) } -func parsePrivateKeyFile(filename string, password string) (ssh.Signer, error) { +func parsePrivateKeyFile(filename, password string) (ssh.Signer, error) { pemBytes, err := os.ReadFile(filename) if err != nil { - return nil, fmt.Errorf("failed to read private key file %v", err) + return nil, fmt.Errorf("failed to read private key file %w", err) } return parsePrivateKey(pemBytes, []byte(password)) } func formalizeAddr(host, port string) string { if !strings.Contains(host, ":") { - host = fmt.Sprintf("%s:%s", host, port) + host = net.JoinHostPort(host, port) } return host } diff --git a/lifecycle/pkg/ssh/option.go b/lifecycle/pkg/ssh/option.go index b78facb32e2c..43ea3915d7dd 100644 --- a/lifecycle/pkg/ssh/option.go +++ b/lifecycle/pkg/ssh/option.go @@ -20,10 +20,9 @@ import ( "time" "github.com/containers/storage/pkg/homedir" + "github.com/labring/sealos/pkg/utils/file" "github.com/spf13/pflag" "golang.org/x/crypto/ssh" - - "github.com/labring/sealos/pkg/utils/file" ) type Option struct { @@ -42,10 +41,26 @@ func (o *Option) BindFlags(fs *pflag.FlagSet) { fs.BoolVar(&o.stdout, "stdout", o.stdout, "print logs to stdout") fs.BoolVar(&o.sudo, "sudo", o.sudo, "enable sudo, user provide must be a superuser or sudoer") fs.StringVarP(&o.user, "user", "u", o.user, "username to authenticate as") - fs.StringVarP(&o.password, "password", "p", o.password, "use given password to authenticate with") - fs.StringVarP(&o.privateKey, "private-key", "i", o.privateKey, - "selects a file from which the identity (private key) for public key authentication is read") - fs.StringVar(&o.passphrase, "passphrase", o.passphrase, "passphrase for decrypting a PEM encoded private key") + fs.StringVarP( + &o.password, + "password", + "p", + o.password, + "use given password to authenticate with", + ) + fs.StringVarP( + &o.privateKey, + "private-key", + "i", + o.privateKey, + "selects a file from which the identity (private key) for public key authentication is read", + ) + fs.StringVar( + &o.passphrase, + "passphrase", + o.passphrase, + "passphrase for decrypting a PEM encoded private key", + ) fs.DurationVar(&o.timeout, "timeout", o.timeout, "ssh connection establish timeout") } diff --git a/lifecycle/pkg/ssh/remote.go b/lifecycle/pkg/ssh/remote.go index ac077bcdc4b1..09372cc49a25 100644 --- a/lifecycle/pkg/ssh/remote.go +++ b/lifecycle/pkg/ssh/remote.go @@ -21,12 +21,9 @@ import ( "fmt" "strconv" - "github.com/labring/sealos/pkg/utils/initsystem" - - "github.com/labring/sealos/pkg/template" - "github.com/labring/sealos/pkg/constants" - + "github.com/labring/sealos/pkg/template" + "github.com/labring/sealos/pkg/utils/initsystem" "github.com/labring/sealos/pkg/utils/iputils" ) @@ -40,7 +37,7 @@ const ( initSystemCommandFmt = "initsystem %s %s" ) -type RenderTemplate func(name, defaultStr string, data map[string]interface{}) (string, error) +type RenderTemplate func(name, defaultStr string, data map[string]any) (string, error) type Remote struct { pathResolver constants.PathResolver @@ -63,7 +60,7 @@ func (s *Remote) Hostname(ip string) (string, error) { func (s *Remote) IPVS(ip, vip string, masters []string) error { ipvsTemplate := `ipvs --vs {{.vip}} {{range $h := .masters}}--rs {{$h}} {{end}} --health-path /healthz --health-schem https --run-once` - data := map[string]interface{}{ + data := map[string]any{ "vip": vip, "masters": masters, } @@ -73,9 +70,10 @@ func (s *Remote) IPVS(ip, vip string, masters []string) error { } return s.executeRemoteUtilSubcommand(ip, out) } + func (s *Remote) IPVSClean(ip, vip string) error { ipvsTemplate := `ipvs --vs {{.vip}} -C` - data := map[string]interface{}{ + data := map[string]any{ "vip": vip, "ip": iputils.GetHostIP(ip), } @@ -86,9 +84,14 @@ func (s *Remote) IPVSClean(ip, vip string) error { return s.executeRemoteUtilSubcommand(ip, out) } -func (s *Remote) StaticPod(ip, vip, name, image string, masters []string, path string, options ...string) error { +func (s *Remote) StaticPod( + ip, vip, name, image string, + masters []string, + path string, + options ...string, +) error { staticPodIPVSTemplate := `static-pod lvscare --path {{.path}} --name {{.name}} --vip {{.vip}} --image {{.image}} {{range $h := .masters}} --masters {{$h}} {{end}} {{range $o := .options}} --options {{$o}} {{end}}` - data := map[string]interface{}{ + data := map[string]any{ "vip": vip, "image": image, "masters": masters, @@ -116,16 +119,20 @@ func (s *Remote) Socket(ip string) (string, error) { return s.outputRemoteUtilSubcommand(ip, socketCommandFmt) } -func (s *Remote) Cert(ip string, altNames []string, nodeIP, nodeName, serviceCIRD, DNSDomain string) error { +func (s *Remote) Cert( + ip string, + altNames []string, + nodeIP, nodeName, serviceCIRD, dnsDomain string, +) error { certTemplate := `cert \ {{if .nodeIP}} --node-ip {{.nodeIP}}{{end}}{{if .nodeName}} --node-name {{.nodeName}}{{end}} \ {{if .serviceCIDR}} --service-cidr {{.serviceCIDR}}{{end}}{{if .dnsDomain}} --dns-domain {{.dnsDomain}}{{end}} \ {{range $h := .altNames}} --alt-names {{$h}} {{end}}` - data := map[string]interface{}{ + data := map[string]any{ "nodeIP": nodeIP, "nodeName": nodeName, "serviceCIDR": serviceCIRD, - "dnsDomain": DNSDomain, + "dnsDomain": dnsDomain, "altNames": altNames, } out, err := template.RenderTemplate("cert", certTemplate, data) @@ -141,7 +148,10 @@ type initSystem struct { } func (s *initSystem) ServiceEnable(service string) error { - return s.remoter.executeRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "enable", service)) + return s.remoter.executeRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "enable", service), + ) } func (s *initSystem) EnableCommand(_ string) string { @@ -149,31 +159,49 @@ func (s *initSystem) EnableCommand(_ string) string { } func (s *initSystem) ServiceStart(service string) error { - return s.remoter.executeRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "start", service)) + return s.remoter.executeRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "start", service), + ) } func (s *initSystem) ServiceStop(service string) error { - return s.remoter.executeRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "stop", service)) + return s.remoter.executeRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "stop", service), + ) } func (s *initSystem) ServiceRestart(service string) error { - return s.remoter.executeRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "restart", service)) + return s.remoter.executeRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "restart", service), + ) } func (s *initSystem) ServiceExists(service string) bool { - out, _ := s.remoter.outputRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "is-exists", service)) + out, _ := s.remoter.outputRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "is-exists", service), + ) result, _ := strconv.ParseBool(out) return result } func (s *initSystem) ServiceIsEnabled(service string) bool { - out, _ := s.remoter.outputRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "is-enabled", service)) + out, _ := s.remoter.outputRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "is-enabled", service), + ) result, _ := strconv.ParseBool(out) return result } func (s *initSystem) ServiceIsActive(service string) bool { - out, _ := s.remoter.outputRemoteUtilSubcommand(s.target, fmt.Sprintf(initSystemCommandFmt, "is-active", service)) + out, _ := s.remoter.outputRemoteUtilSubcommand( + s.target, + fmt.Sprintf(initSystemCommandFmt, "is-active", service), + ) result, _ := strconv.ParseBool(out) return result } @@ -192,7 +220,7 @@ func NewRemoteFromSSH(clusterName string, sshInterface Interface) *Remote { func (s *Remote) executeRemoteUtilSubcommand(ip, cmd string) error { cmd = fmt.Sprintf("%s %s", s.pathResolver.RootFSSealctlPath(), cmd) if err := s.execer.CmdAsync(ip, cmd); err != nil { - return fmt.Errorf("failed to execute remote command `%s`: %v", cmd, err) + return fmt.Errorf("failed to execute remote command `%s`: %w", cmd, err) } return nil } diff --git a/lifecycle/pkg/ssh/scp.go b/lifecycle/pkg/ssh/scp.go index 4c6af22e4c50..3ca883f45b12 100644 --- a/lifecycle/pkg/ssh/scp.go +++ b/lifecycle/pkg/ssh/scp.go @@ -23,13 +23,12 @@ import ( "strings" "time" - "github.com/pkg/sftp" - "github.com/schollz/progressbar/v3" - "golang.org/x/crypto/ssh" - "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/progress" + "github.com/pkg/sftp" + "github.com/schollz/progressbar/v3" + "golang.org/x/crypto/ssh" ) func (c *Client) RemoteSha256Sum(host, remoteFilePath string) string { @@ -41,7 +40,7 @@ func (c *Client) RemoteSha256Sum(host, remoteFilePath string) string { return remoteHash } -func getOnelineResult(output string, sep string) string { +func getOnelineResult(output, sep string) string { return strings.ReplaceAll(strings.ReplaceAll(output, "\r\n", sep), "\n", sep) } @@ -76,7 +75,7 @@ func (c *Client) newClientAndSftpClient(host string) (*ssh.Client, *sftp.Client, return nil, nil, err } // create sftp client - if c.Option.sudo || c.Option.user != defaultUsername { + if c.sudo || c.user != defaultUsername { sftpClient, err = NewSudoSftpClient(sshClient, c.password) } else { sftpClient, err = sftp.NewClient(sshClient) @@ -93,25 +92,29 @@ func (c *Client) newClientAndSftpClient(host string) (*ssh.Client, *sftp.Client, return sshClient, sftpClient, err } -func (c *Client) sftpConnect(host string) (sshClient *ssh.Client, sftpClient *sftp.Client, err error) { +func (c *Client) sftpConnect(host string) (*sftp.Client, error) { + var ( + sftpClient *sftp.Client + err error + ) err = exponentialBackOffRetry(defaultMaxRetry, time.Millisecond*100, 2, func() error { - sshClient, sftpClient, err = c.newClientAndSftpClient(host) + _, sftpClient, err = c.newClientAndSftpClient(host) return err }, isErrorWorthRetry) - return + return sftpClient, err } // Copy is copy file or dir to remotePath, add md5 validate func (c *Client) Copy(host, localPath, remotePath string) error { logger.Debug("remote copy files src %s to dst %s", localPath, remotePath) - _, sftpClient, err := c.sftpConnect(host) + sftpClient, err := c.sftpConnect(host) if err != nil { - return fmt.Errorf("failed to connect: %s", err) + return fmt.Errorf("failed to connect: %w", err) } f, err := os.Stat(localPath) if err != nil { - return fmt.Errorf("get file stat failed %s", err) + return fmt.Errorf("get file stat failed %w", err) } remoteDir := filepath.Dir(remotePath) @@ -121,7 +124,7 @@ func (c *Client) Copy(host, localPath, remotePath string) error { return err } if err = sftpClient.MkdirAll(remoteDir); err != nil { - return fmt.Errorf("failed to Mkdir remote: %v", err) + return fmt.Errorf("failed to Mkdir remote: %w", err) } } else if !rfp.IsDir() { return fmt.Errorf("dir of remote file %s is not a directory", remotePath) @@ -144,23 +147,24 @@ func (c *Client) Copy(host, localPath, remotePath string) error { func (c *Client) Fetch(host, src, dst string) error { logger.Debug("fetch remote file %s to %s", src, dst) - _, sftpClient, err := c.sftpConnect(host) + sftpClient, err := c.sftpConnect(host) if err != nil { - return fmt.Errorf("failed to connect: %s", err) + return fmt.Errorf("failed to connect: %w", err) } rfp, err := sftpClient.Open(src) if err != nil { - return fmt.Errorf("failed to open remote file %s: %v", src, err) + return fmt.Errorf("failed to open remote file %s: %w", src, err) } defer func() { _ = rfp.Close() }() - if file.IsDir(dst) { + switch { + case file.IsDir(dst): dst = filepath.Join(dst, filepath.Base(src)) - } else if file.IsFile(dst) { + case file.IsFile(dst): return fmt.Errorf("local file %s already exists", dst) - } else { + default: if err := file.MkDirs(filepath.Dir(dst)); err != nil { return err } @@ -175,18 +179,23 @@ func (c *Client) Fetch(host, src, dst string) error { return err } -func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progressbar.ProgressBar) error { +func (c *Client) doCopy( + client *sftp.Client, + host, src, dest string, + epu *progressbar.ProgressBar, +) error { + logger.Debug("copying %s to %s on host %s", src, dest, host) lfp, err := os.Stat(src) if err != nil { - return fmt.Errorf("failed to Stat local: %v", err) + return fmt.Errorf("failed to Stat local: %w", err) } if lfp.IsDir() { entries, err := os.ReadDir(src) if err != nil { - return fmt.Errorf("failed to ReadDir: %v", err) + return fmt.Errorf("failed to ReadDir: %w", err) } if err = client.MkdirAll(dest); err != nil { - return fmt.Errorf("failed to Mkdir remote: %v", err) + return fmt.Errorf("failed to Mkdir remote: %w", err) } for _, entry := range entries { if err = c.doCopy(client, host, path.Join(src, entry.Name()), path.Join(dest, entry.Name()), epu); err != nil { @@ -196,7 +205,7 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } else { lf, err := os.Open(filepath.Clean(src)) if err != nil { - return fmt.Errorf("failed to open: %v", err) + return fmt.Errorf("failed to open: %w", err) } defer lf.Close() @@ -204,15 +213,15 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre if err = func(tmpName string) error { dstfp, err := client.Create(tmpName) if err != nil { - return fmt.Errorf("failed to create: %v", err) + return fmt.Errorf("failed to create: %w", err) } defer dstfp.Close() if err = dstfp.Chmod(lfp.Mode()); err != nil { - return fmt.Errorf("failed to Chmod dst: %v", err) + return fmt.Errorf("failed to Chmod dst: %w", err) } if _, err = io.Copy(dstfp, lf); err != nil { - return fmt.Errorf("failed to Copy: %v", err) + return fmt.Errorf("failed to Copy: %w", err) } return nil }(destTmp); err != nil { @@ -220,7 +229,7 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre } if err = client.PosixRename(destTmp, dest); err != nil { - return fmt.Errorf("failed to rename %s to %s: %v", destTmp, dest, err) + return fmt.Errorf("failed to rename %s to %s: %w", destTmp, dest, err) } _ = epu.Add(1) diff --git a/lifecycle/pkg/ssh/ssh.go b/lifecycle/pkg/ssh/ssh.go index 9f72bdcf5451..0c22d3223a3a 100644 --- a/lifecycle/pkg/ssh/ssh.go +++ b/lifecycle/pkg/ssh/ssh.go @@ -18,13 +18,12 @@ import ( "context" "time" - "github.com/spf13/pflag" - "golang.org/x/crypto/ssh" - "golang.org/x/sync/errgroup" - v2 "github.com/labring/sealos/pkg/types/v1beta1" fileutils "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/pflag" + "golang.org/x/crypto/ssh" + "golang.org/x/sync/errgroup" ) var ( @@ -34,7 +33,12 @@ var ( func RegisterFlags(fs *pflag.FlagSet) { fs.IntVar(&defaultMaxRetry, "max-retry", defaultMaxRetry, "define max num of ssh retry times") - fs.DurationVar(&defaultExecutionTimeout, "execution-timeout", defaultExecutionTimeout, "timeout setting of command execution") + fs.DurationVar( + &defaultExecutionTimeout, + "execution-timeout", + defaultExecutionTimeout, + "timeout setting of command execution", + ) } // GetTimeoutContext create a context.Context with default timeout diff --git a/lifecycle/pkg/ssh/sshcmd.go b/lifecycle/pkg/ssh/sshcmd.go index 8c025f5818c2..1bfabfc5b76c 100644 --- a/lifecycle/pkg/ssh/sshcmd.go +++ b/lifecycle/pkg/ssh/sshcmd.go @@ -24,22 +24,21 @@ import ( "strings" "sync" - "golang.org/x/sync/errgroup" - "github.com/labring/sealos/pkg/utils/logger" + "golang.org/x/sync/errgroup" ) func (c *Client) Ping(host string) error { client, _, err := c.Connect(host) if err != nil { - return fmt.Errorf("failed to connect %s: %v", host, err) + return fmt.Errorf("failed to connect %s: %w", host, err) } return client.Close() } func (c *Client) wrapCommands(cmds ...string) string { cmdJoined := strings.Join(cmds, "; ") - if !c.Option.sudo || c.Option.user == defaultUsername { + if !c.sudo || c.user == defaultUsername { return cmdJoined } @@ -54,21 +53,21 @@ func (c *Client) CmdAsyncWithContext(ctx context.Context, host string, cmds ...s logger.Debug("start to exec `%s` on %s", cmd, host) client, session, err := c.Connect(host) if err != nil { - return fmt.Errorf("connect error: %v", err) + return fmt.Errorf("connect error: %w", err) } defer client.Close() defer session.Close() stdout, err := session.StdoutPipe() if err != nil { - return fmt.Errorf("stdout pipe %s: %v", host, err) + return fmt.Errorf("stdout pipe %s: %w", host, err) } stderr, err := session.StderrPipe() if err != nil { - return fmt.Errorf("stderr pipe %s: %v", host, err) + return fmt.Errorf("stderr pipe %s: %w", host, err) } stdin, err := session.StdinPipe() if err != nil { - return fmt.Errorf("stdin pipe %s: %v", host, err) + return fmt.Errorf("stdin pipe %s: %w", host, err) } out := autoAnswerWriter{ in: stdin, @@ -83,13 +82,13 @@ func (c *Client) CmdAsyncWithContext(ctx context.Context, host string, cmds ...s go func() { errCh <- func() error { if err := session.Start(cmd); err != nil { - return fmt.Errorf("start command `%s` on %s: %v", cmd, host, err) + return fmt.Errorf("start command `%s` on %s: %w", cmd, host, err) } if err = eg.Wait(); err != nil { return err } if err = session.Wait(); err != nil { - return fmt.Errorf("run command `%s` on %s, output: %s, error: %v,", cmd, host, out.b.String(), err) + return fmt.Errorf("run command `%s` on %s, output: %s, error: %w,", cmd, host, out.b.String(), err) } return nil }() @@ -114,7 +113,7 @@ func (c *Client) Cmd(host, cmd string) ([]byte, error) { logger.Debug("start to exec `%s` on %s", cmd, host) client, session, err := c.Connect(host) if err != nil { - return nil, fmt.Errorf("failed to create ssh session for %s: %v", host, err) + return nil, fmt.Errorf("failed to create ssh session for %s: %w", host, err) } defer client.Close() defer session.Close() @@ -184,10 +183,13 @@ func (c *Client) handlePipe(host string, pipe io.Reader, out io.Writer, isStdout r := bufio.NewReader(pipe) writers := []io.Writer{out} if isStdout { - writers = append(writers, &withPrefixWriter{prefix: host + "\t", newline: true, w: os.Stdout}) + writers = append( + writers, + &withPrefixWriter{prefix: host + "\t", newline: true, w: os.Stdout}, + ) } w := io.MultiWriter(writers...) - var line []byte + line := make([]byte, 0, 256) // reasonable buffer size for a line for { b, err := r.ReadByte() if err != nil { diff --git a/lifecycle/pkg/ssh/suftp.go b/lifecycle/pkg/ssh/suftp.go index 1ecc0b2038d5..c22d56051957 100644 --- a/lifecycle/pkg/ssh/suftp.go +++ b/lifecycle/pkg/ssh/suftp.go @@ -34,7 +34,11 @@ const ( sshdConfigPath = "/etc/ssh/sshd_config" ) -func NewSudoSftpClient(conn *ssh.Client, sudopwd string, opts ...sftp.ClientOption) (*sftp.Client, error) { +func NewSudoSftpClient( + conn *ssh.Client, + sudopwd string, + opts ...sftp.ClientOption, +) (*sftp.Client, error) { s, err := conn.NewSession() if err != nil { return nil, err @@ -77,7 +81,6 @@ func NewSudoSftpClient(conn *ssh.Client, sudopwd string, opts ...sftp.ClientOpti } err = s.Start(cmd) - if err != nil { return nil, err } @@ -110,7 +113,7 @@ func NewSudoSftpClient(conn *ssh.Client, sudopwd string, opts ...sftp.ClientOpti case prompterror: return nil, errors.New("sudo failed or no sudo rights") case promptpwd: - return nil, fmt.Errorf("wrong sudo password") + return nil, errors.New("wrong sudo password") } return sftp.NewClientPipe(stdout, stdin, opts...) @@ -148,7 +151,7 @@ func getSftpServerPath(conn *ssh.Client, sudopwd string) (string, error) { } defer ses.Close() - cmd := fmt.Sprintf(`sudo -S grep -oP "Subsystem\s+sftp\s+\K.*" %s`, sshdConfigPath) + cmd := "sudo -S grep -oP \"Subsystem\\s+sftp\\s+\\K.*\" " + sshdConfigPath in, err := ses.StdinPipe() if err != nil { return "", err @@ -162,7 +165,11 @@ func getSftpServerPath(conn *ssh.Client, sudopwd string) (string, error) { ses.Stderr = &b err = ses.Run(cmd) if err != nil { - return "", fmt.Errorf("failed to find sftp-server binary path on %s: %v", sshdConfigPath, err) + return "", fmt.Errorf( + "failed to find sftp-server binary path on %s: %w", + sshdConfigPath, + err, + ) } sftpServerPath := strings.ReplaceAll(b.b.String(), "\r", "") return sftpServerPath, nil diff --git a/lifecycle/pkg/ssh/utils.go b/lifecycle/pkg/ssh/utils.go index abe54f2d8d7a..912149cdf857 100644 --- a/lifecycle/pkg/ssh/utils.go +++ b/lifecycle/pkg/ssh/utils.go @@ -26,7 +26,7 @@ import ( func CopyDir(sshClient Interface, host, src, dest string, filter func(fs.DirEntry) bool) error { entries, err := os.ReadDir(src) if err != nil { - return fmt.Errorf("failed to read dir entries %s", err) + return fmt.Errorf("failed to read dir entries %w", err) } // Copy empty dir anyway if len(entries) == 0 { @@ -36,7 +36,13 @@ func CopyDir(sshClient Interface, host, src, dest string, filter func(fs.DirEntr if filter == nil || filter(f) { err = sshClient.Copy(host, filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())) if err != nil { - return fmt.Errorf("failed to copy entry %s -> %s to %s: %v", filepath.Join(src, f.Name()), filepath.Join(dest, f.Name()), host, err) + return fmt.Errorf( + "failed to copy entry %s -> %s to %s: %w", + filepath.Join(src, f.Name()), + filepath.Join(dest, f.Name()), + host, + err, + ) } } } diff --git a/lifecycle/pkg/system/env.go b/lifecycle/pkg/system/env.go index 975c3bee1508..eccbd0e25a74 100644 --- a/lifecycle/pkg/system/env.go +++ b/lifecycle/pkg/system/env.go @@ -24,7 +24,6 @@ import ( "github.com/containers/buildah" "github.com/containers/storage/pkg/homedir" - "github.com/labring/sealos/pkg/constants" ) @@ -64,7 +63,7 @@ var configOptions = []ConfigOption{ { Key: RuntimeRootConfigKey, Description: "root directory for persistent runtime actions/configs.", - DefaultValue: filepath.Join(homedir.Get(), fmt.Sprintf(".%s", constants.AppName)), + DefaultValue: filepath.Join(homedir.Get(), "."+constants.AppName), }, { Key: DataRootConfigKey, @@ -108,7 +107,11 @@ func (*envSystemConfig) getValueOrDefault(key string) (*ConfigOption, error) { for _, option := range configOptions { if option.Key == key { if option.OSEnv == "" { - option.OSEnv = strings.ReplaceAll(strings.ToUpper(constants.AppName+"_"+option.Key), "-", "_") + option.OSEnv = strings.ReplaceAll( + strings.ToUpper(constants.AppName+"_"+option.Key), + "-", + "_", + ) } if value, ok := os.LookupEnv(option.OSEnv); ok { option.DefaultValue = value diff --git a/lifecycle/pkg/system/env_cmd.go b/lifecycle/pkg/system/env_cmd.go index 4dc82f12281a..8674a8368388 100644 --- a/lifecycle/pkg/system/env_cmd.go +++ b/lifecycle/pkg/system/env_cmd.go @@ -25,9 +25,9 @@ import ( func NewEnvCmd(appName string) *cobra.Command { var verbose bool - var cmd = &cobra.Command{ + cmd := &cobra.Command{ Use: "env", - Short: fmt.Sprintf("prints out all the environment information in use by %s", appName), + Short: "prints out all the environment information in use by " + appName, Run: func(cmd *cobra.Command, args []string) { list := ConfigOptions() for _, v := range list { diff --git a/lifecycle/pkg/template/funcmap.go b/lifecycle/pkg/template/funcmap.go index 963a452659f4..c705fe031067 100644 --- a/lifecycle/pkg/template/funcmap.go +++ b/lifecycle/pkg/template/funcmap.go @@ -63,7 +63,7 @@ func funcMap() template.FuncMap { // always return a string, even on marshal error (empty string). // // This is designed to be called from a template. -func toYAML(v interface{}) string { +func toYAML(v any) string { data, err := yaml.Marshal(v) if err != nil { // Swallow errors inside of a template. @@ -78,8 +78,8 @@ func toYAML(v interface{}) string { // YAML documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string into // m["Error"] in the returned map. -func fromYAML(str string) map[string]interface{} { - m := map[string]interface{}{} +func fromYAML(str string) map[string]any { + m := map[string]any{} if err := yaml.Unmarshal([]byte(str), &m); err != nil { m["Error"] = err.Error() @@ -93,11 +93,11 @@ func fromYAML(str string) map[string]interface{} { // YAML documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string as // the first and only item in the returned array. -func fromYAMLArray(str string) []interface{} { - a := []interface{}{} +func fromYAMLArray(str string) []any { + a := []any{} if err := yaml.Unmarshal([]byte(str), &a); err != nil { - a = []interface{}{err.Error()} + a = []any{err.Error()} } return a } @@ -106,7 +106,7 @@ func fromYAMLArray(str string) []interface{} { // always return a string, even on marshal error (empty string). // // This is designed to be called from a template. -func toTOML(v interface{}) string { +func toTOML(v any) string { b := bytes.NewBuffer(nil) e := toml.NewEncoder(b) if err := e.Encode(v); err != nil { @@ -119,7 +119,7 @@ func toTOML(v interface{}) string { // always return a string, even on marshal error (empty string). // // This is designed to be called from a template. -func toJSON(v interface{}) string { +func toJSON(v any) string { data, err := json.Marshal(v) if err != nil { // Swallow errors inside of a template. @@ -134,8 +134,8 @@ func toJSON(v interface{}) string { // JSON documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string into // m["Error"] in the returned map. -func fromJSON(str string) map[string]interface{} { - m := make(map[string]interface{}) +func fromJSON(str string) map[string]any { + m := make(map[string]any) if err := json.Unmarshal([]byte(str), &m); err != nil { m["Error"] = err.Error() @@ -149,11 +149,11 @@ func fromJSON(str string) map[string]interface{} { // JSON documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string as // the first and only item in the returned array. -func fromJSONArray(str string) []interface{} { - a := []interface{}{} +func fromJSONArray(str string) []any { + a := []any{} if err := json.Unmarshal([]byte(str), &a); err != nil { - a = []interface{}{err.Error()} + a = []any{err.Error()} } return a } diff --git a/lifecycle/pkg/template/template.go b/lifecycle/pkg/template/template.go index b01b2ae3d8a1..16b87dfd0636 100644 --- a/lifecycle/pkg/template/template.go +++ b/lifecycle/pkg/template/template.go @@ -43,7 +43,7 @@ func Must(t *template.Template, err error) *template.Template { return template.Must(t, err) } -func RenderTemplate(name, defaultStr string, data map[string]interface{}) (string, error) { +func RenderTemplate(name, defaultStr string, data map[string]any) (string, error) { var out bytes.Buffer tmpl := template.Must(template.New(name).Parse(defaultStr)) err := tmpl.Execute(&out, data) diff --git a/lifecycle/pkg/template/template_test.go b/lifecycle/pkg/template/template_test.go index c8738148f8d4..03be67687696 100644 --- a/lifecycle/pkg/template/template_test.go +++ b/lifecycle/pkg/template/template_test.go @@ -20,7 +20,7 @@ import ( func TestTemplateSemverCompare(t *testing.T) { staticPodIPVSTemplate := `static-pod lvscare --path {{.path}} --name {{.name}} --vip {{.vip}} --image {{.image}} {{range $h := .masters}} --masters {{$h}} {{end}} {{range $o := .options}} --options {{$o}} {{end}} ` - data := map[string]interface{}{ + data := map[string]any{ "vip": "127.0.0.1", "image": "test", "masters": []string{"127.0.0.2"}, diff --git a/lifecycle/pkg/types/v1beta1/cluster.go b/lifecycle/pkg/types/v1beta1/cluster.go index 9ee178c5d1ed..92e75dc89a96 100644 --- a/lifecycle/pkg/types/v1beta1/cluster.go +++ b/lifecycle/pkg/types/v1beta1/cluster.go @@ -17,11 +17,10 @@ package v1beta1 import ( "path" + "github.com/labring/sealos/pkg/version" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/yaml" - - "github.com/labring/sealos/pkg/version" ) // +kubebuilder:object:root=true @@ -81,9 +80,11 @@ var ( imageDistributionKeyV2 = path.Join(GroupName, "distribution") ) -var ImageTypeKeys = []string{imageTypeKey, imageTypeKeyV2} -var ImageVersionKeys = []string{imageVersionKey, imageVersionKeyV2} -var ImageDistributionKeys = []string{imageDistributionKey, imageDistributionKeyV2} +var ( + ImageTypeKeys = []string{imageTypeKey, imageTypeKeyV2} + ImageVersionKeys = []string{imageVersionKey, imageVersionKeyV2} + ImageDistributionKeys = []string{imageDistributionKey, imageDistributionKeyV2} +) type MountImage struct { Name string `json:"name"` diff --git a/lifecycle/pkg/types/v1beta1/config.go b/lifecycle/pkg/types/v1beta1/config.go index d00ac34de4bf..1c3d495b9e10 100644 --- a/lifecycle/pkg/types/v1beta1/config.go +++ b/lifecycle/pkg/types/v1beta1/config.go @@ -96,7 +96,7 @@ type Config struct { // ConfigList contains a list of Config type ConfigList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` + metav1.TypeMeta ` json:",inline"` + metav1.ListMeta ` json:"metadata,omitempty"` Items []Config `json:"items"` } diff --git a/lifecycle/pkg/types/v1beta1/utils.go b/lifecycle/pkg/types/v1beta1/utils.go index d1a767cfb072..de85171baffc 100644 --- a/lifecycle/pkg/types/v1beta1/utils.go +++ b/lifecycle/pkg/types/v1beta1/utils.go @@ -17,14 +17,13 @@ limitations under the License. package v1beta1 import ( - "github.com/Masterminds/semver/v3" - "golang.org/x/exp/slices" - "k8s.io/apimachinery/pkg/util/sets" - - stringsutil "github.com/labring/sealos/pkg/utils/strings" + "slices" + "github.com/Masterminds/semver/v3" "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/maps" + stringsutil "github.com/labring/sealos/pkg/utils/strings" + "k8s.io/apimachinery/pkg/util/sets" ) func (c *Cluster) GetMasterIPList() []string { @@ -142,13 +141,13 @@ func (c *Cluster) ReplaceRootfsImage() { } } } - //if no two rootfsImages, never replace + // if no two rootfsImages, never replace if v1 == "" || v2 == "" { return } sv1 := semver.MustParse(v1) sv2 := semver.MustParse(v2) - //if version format error, never replace + // if version format error, never replace if sv1.LessThan(sv2) { c.Status.Mounts[i1], c.Status.Mounts[i2] = c.Status.Mounts[i2], c.Status.Mounts[i1] c.Status.Mounts = append(c.Status.Mounts[:i2], c.Status.Mounts[i2+1:]...) @@ -233,7 +232,8 @@ func UpdateCondition(conditions []ClusterCondition, condition ClusterCondition) 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 { + if cond.Reason != condition.Reason || cond.Status != condition.Status || + cond.Message != condition.Message { conditions[i] = condition } } @@ -245,7 +245,10 @@ func UpdateCondition(conditions []ClusterCondition, condition ClusterCondition) } // UpdateCommandCondition updates condition in cluster conditions using giving condition, append only -func UpdateCommandCondition(cmdConditions []CommandCondition, cmdCondition CommandCondition) []CommandCondition { +func UpdateCommandCondition( + cmdConditions []CommandCondition, + cmdCondition CommandCondition, +) []CommandCondition { if cmdConditions == nil { cmdConditions = make([]CommandCondition, 0) } diff --git a/lifecycle/pkg/utils/archive/archive.go b/lifecycle/pkg/utils/archive/archive.go index 554f60f3062e..a95472317bc1 100644 --- a/lifecycle/pkg/utils/archive/archive.go +++ b/lifecycle/pkg/utils/archive/archive.go @@ -22,11 +22,10 @@ import ( "strings" "github.com/containers/storage/pkg/archive" - "github.com/opencontainers/go-digest" - "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/flags" "github.com/labring/sealos/pkg/utils/logger" + "github.com/opencontainers/go-digest" ) type Archive interface { @@ -44,9 +43,12 @@ func (opt *Options) UnTarOrGzip(src io.Reader, dst string) (int64, error) { } func (opt *Options) Digest(path string) (digest.Digest, int64, error) { - read, err := compress([]string{path}, Options{Compress: opt.Compress, KeepRootDir: opt.KeepRootDir}) + read, err := compress( + []string{path}, + Options{Compress: opt.Compress, KeepRootDir: opt.KeepRootDir}, + ) if err != nil { - return "", 0, fmt.Errorf("unable to tar on %s, err: %s", path, err) + return "", 0, fmt.Errorf("unable to tar on %s, err: %w", path, err) } return canonicalDigest(read) } @@ -123,5 +125,5 @@ func clean(path string, skip bool) (err error) { if !skip { err = os.RemoveAll(path) } - return + return err } diff --git a/lifecycle/pkg/utils/archive/compress.go b/lifecycle/pkg/utils/archive/compress.go index 20f0088c3181..7fefbde812bf 100644 --- a/lifecycle/pkg/utils/archive/compress.go +++ b/lifecycle/pkg/utils/archive/compress.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "io" + "math" "os" "path/filepath" "strings" @@ -28,12 +29,18 @@ import ( "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" - "golang.org/x/sys/unix" ) const compressionBufSize = 32768 +func sanitizeFileMode(mode int64, name string) (os.FileMode, error) { + if mode < 0 || mode > math.MaxUint32 { + return 0, fmt.Errorf("invalid file mode %d for %s", mode, name) + } + return os.FileMode(mode), nil +} + type Options struct { Compress bool KeepRootDir bool @@ -41,7 +48,7 @@ type Options struct { func validatePath(path string) error { if _, err := os.Stat(path); err != nil { - return fmt.Errorf("dir %s does not exist, err: %s", path, err) + return fmt.Errorf("dir %s does not exist, err: %w", path, err) } return nil } @@ -92,7 +99,7 @@ func writeWhiteout(header *tar.Header, fi os.FileInfo, path string) *tar.Header if fi.Mode()&os.ModeCharDevice != 0 && header.Devminor == 0 && header.Devmajor == 0 { hName := header.Name header.Name = filepath.Join(filepath.Dir(hName), WhiteoutPrefix+filepath.Base(hName)) - header.Mode = 0600 + header.Mode = 0o600 header.Typeflag = tar.TypeReg header.Size = 0 } @@ -101,7 +108,11 @@ func writeWhiteout(header *tar.Header, fi os.FileInfo, path string) *tar.Header if fi.Mode()&os.ModeDir != 0 { opaque, walkErr := file.Lgetxattr(path, "trusted.overlay.opaque") if walkErr != nil { - logger.Debug("failed to get trusted.overlay.opaque for %s at opaque, err: %v", path, walkErr) + logger.Debug( + "failed to get trusted.overlay.opaque for %s at opaque, err: %v", + path, + walkErr, + ) } if len(opaque) == 1 && opaque[0] == 'y' { @@ -153,7 +164,12 @@ func readWhiteout(hdr *tar.Header, path string) (bool, error) { return true, nil } -func writeToTarWriter(path string, tarWriter *tar.Writer, bufWriter *bufio.Writer, options Options) error { +func writeToTarWriter( + path string, + tarWriter *tar.Writer, + bufWriter *bufio.Writer, + options Options, +) error { var newFolder string if options.KeepRootDir { fi, err := os.Stat(path) @@ -196,14 +212,14 @@ func writeToTarWriter(path string, tarWriter *tar.Writer, bufWriter *bufio.Write woh := writeWhiteout(header, fi, file) walkErr = tarWriter.WriteHeader(header) if walkErr != nil { - return fmt.Errorf("failed to write original header, path: %s, err: %v", file, walkErr) + return fmt.Errorf("failed to write original header, path: %s, err: %w", file, walkErr) } // this is a opaque, write the opaque header, in order to set header.PAXRecords with trusted.overlay.opaque:y // when decompress the tar stream. if woh != nil { walkErr = tarWriter.WriteHeader(woh) if walkErr != nil { - return fmt.Errorf("failed to write opaque header, path: %s, err: %v", file, walkErr) + return fmt.Errorf("failed to write opaque header, path: %s, err: %w", file, walkErr) } } // if not a dir && size > 0, write file content @@ -261,7 +277,7 @@ func decompress(src io.Reader, dst string, options Options) (int64, error) { oldMask := syscall.Umask(0) defer syscall.Umask(oldMask) - err := os.MkdirAll(dst, 0755) + err := os.MkdirAll(dst, 0o755) if err != nil { return 0, err } @@ -281,7 +297,7 @@ func decompress(src io.Reader, dst string, options Options) (int64, error) { ) for { header, err := tr.Next() - if err == io.EOF { + if errors.Is(err, io.EOF) { break } if err != nil { @@ -312,7 +328,11 @@ func decompress(src io.Reader, dst string, options Options) (int64, error) { switch header.Typeflag { case tar.TypeDir: if _, err = os.Stat(target); err != nil { - if err = os.MkdirAll(target, os.FileMode(header.Mode)); err != nil { + mode, modeErr := sanitizeFileMode(header.Mode, header.Name) + if modeErr != nil { + return 0, modeErr + } + if err = os.MkdirAll(target, mode); err != nil { return 0, err } dirs = append(dirs, header) @@ -321,12 +341,20 @@ func decompress(src io.Reader, dst string, options Options) (int64, error) { case tar.TypeReg: err = func() error { // regularly won't mkdir, unless add newFolder on compressing - inErr := os.MkdirAll(filepath.Dir(target), 0700|0055) + inErr := os.MkdirAll(filepath.Dir(target), 0o700|0o055) if inErr != nil { return inErr } // #nosec - fileToWrite, inErr := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.FileMode(header.Mode)) + mode, modeErr := sanitizeFileMode(header.Mode, header.Name) + if modeErr != nil { + return modeErr + } + fileToWrite, inErr := os.OpenFile( + target, + os.O_CREATE|os.O_TRUNC|os.O_RDWR, + mode, + ) if inErr != nil { return inErr } @@ -343,7 +371,6 @@ func decompress(src io.Reader, dst string, options Options) (int64, error) { // for not changing return os.Chtimes(target, header.AccessTime, header.ModTime) }() - if err != nil { return 0, err } @@ -364,7 +391,8 @@ func decompress(src io.Reader, dst string, options Options) (int64, error) { // check for path traversal and correct forward slashes func validRelPath(p string) bool { - if p == "" || strings.Contains(p, `\`) || strings.HasPrefix(p, "/") || strings.Contains(p, "../") { + if p == "" || strings.Contains(p, `\`) || strings.HasPrefix(p, "/") || + strings.Contains(p, "../") { return false } return true diff --git a/lifecycle/pkg/utils/archive/compress_test.go b/lifecycle/pkg/utils/archive/compress_test.go index 8210d8fe3f3e..f931b914899f 100644 --- a/lifecycle/pkg/utils/archive/compress_test.go +++ b/lifecycle/pkg/utils/archive/compress_test.go @@ -54,7 +54,7 @@ func TestTarWithRootDir(t *testing.T) { if err != nil { t.Error(err) } - //tmp, err := os.CreateTemp("/tmp", "tar") + // tmp, err := os.CreateTemp("/tmp", "tar") _, err = io.Copy(tmp, reader) if err != nil { t.Error(err) diff --git a/lifecycle/pkg/utils/archive/hash.go b/lifecycle/pkg/utils/archive/hash.go index 80bf72430387..873bc82338b9 100644 --- a/lifecycle/pkg/utils/archive/hash.go +++ b/lifecycle/pkg/utils/archive/hash.go @@ -15,10 +15,9 @@ package archive import ( - "io" - // in some env, there maybe a panic of crypto/sha256 is not imported _ "crypto/sha256" + "io" "github.com/opencontainers/go-digest" ) diff --git a/lifecycle/pkg/utils/confirm/confirm.go b/lifecycle/pkg/utils/confirm/confirm.go index c75b0beb5975..5e392f6c3ea3 100644 --- a/lifecycle/pkg/utils/confirm/confirm.go +++ b/lifecycle/pkg/utils/confirm/confirm.go @@ -18,14 +18,13 @@ import ( "errors" "os" - "github.com/manifoldco/promptui" - "github.com/labring/sealos/pkg/utils/logger" + "github.com/manifoldco/promptui" ) // Confirm is send the prompt and get result func Confirm(prompt, cancel string) (bool, error) { - var hostname, _ = os.Hostname() + hostname, _ := os.Hostname() promptLabel := "Do you want to continue on '" + hostname + "' cluster? Input '" + hostname + "' to continue" logger.Info(prompt) @@ -70,7 +69,6 @@ func PasswordInput(promptInput string) string { } result, err := prompt.Run() - if err != nil { logger.Error("Prompt failed %v\n", err) return "" @@ -93,7 +91,6 @@ func SelectInput(promptInput string, items []string) string { } _, result, err := prompt.Run() - if err != nil { logger.Error("Prompt failed %v\n", err) return "" diff --git a/lifecycle/pkg/utils/exec/bin_path.go b/lifecycle/pkg/utils/exec/bin_path.go index f02edd2113c8..e6f2c462184e 100644 --- a/lifecycle/pkg/utils/exec/bin_path.go +++ b/lifecycle/pkg/utils/exec/bin_path.go @@ -15,7 +15,6 @@ package exec import ( - "fmt" "os" "path/filepath" "strings" @@ -28,7 +27,7 @@ func ExecutableFilePath(name string) string { } func ExecutableFileArch(path string) string { - fileCmd := fmt.Sprintf("file %s", path) + fileCmd := "file " + path out := BashEval(fileCmd) arm64 := []string{"aarch64", "arm64"} amd64 := []string{"x86-64", "x86_64"} diff --git a/lifecycle/pkg/utils/exec/cmd.go b/lifecycle/pkg/utils/exec/cmd.go index 2f893e5299c4..253d61dbbf78 100644 --- a/lifecycle/pkg/utils/exec/cmd.go +++ b/lifecycle/pkg/utils/exec/cmd.go @@ -16,7 +16,6 @@ package exec import ( "context" - "fmt" "os" "os/exec" "strings" @@ -26,7 +25,7 @@ import ( func CmdWithContext(ctx context.Context, name string, args ...string) error { // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command - cmd := exec.CommandContext(ctx, name, args[:]...) // #nosec + cmd := exec.CommandContext(ctx, name, args...) // #nosec cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout @@ -35,7 +34,7 @@ func CmdWithContext(ctx context.Context, name string, args ...string) error { func Output(name string, args ...string) ([]byte, error) { // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command - cmd := exec.Command(name, args[:]...) // #nosec + cmd := exec.Command(name, args...) // #nosec return cmd.CombinedOutput() } @@ -62,7 +61,7 @@ func Eval(cmd string) string { } func CheckCmdIsExist(cmd string) (string, bool) { - cmd = fmt.Sprintf("type %s", cmd) + cmd = "type " + cmd out, err := RunSimpleCmd(cmd) if err != nil { return "", false diff --git a/lifecycle/pkg/utils/file/file.go b/lifecycle/pkg/utils/file/file.go index 38672eb2795b..9f851f679bc0 100644 --- a/lifecycle/pkg/utils/file/file.go +++ b/lifecycle/pkg/utils/file/file.go @@ -117,7 +117,8 @@ func Cmp(src, dest string, chunkSize int) (same bool, err error) { return false, nil } - if (err1 == io.EOF && err2 == io.EOF) || (err1 == io.ErrUnexpectedEOF && err2 == io.ErrUnexpectedEOF) { + if (err1 == io.EOF && err2 == io.EOF) || + (err1 == io.ErrUnexpectedEOF && err2 == io.ErrUnexpectedEOF) { return true, nil } diff --git a/lifecycle/pkg/utils/file/file_v2.go b/lifecycle/pkg/utils/file/file_v2.go index c173fc608de0..f256f0bf23a7 100644 --- a/lifecycle/pkg/utils/file/file_v2.go +++ b/lifecycle/pkg/utils/file/file_v2.go @@ -51,7 +51,8 @@ func IsFile(filePath string) bool { } func IsTarFile(s string) bool { - return strings.HasSuffix(s, ".tar") || strings.HasSuffix(s, ".gz") || strings.HasSuffix(s, ".tgz") + return strings.HasSuffix(s, ".tar") || strings.HasSuffix(s, ".gz") || + strings.HasSuffix(s, ".tgz") } // IsDir returns if the given path is a directory. @@ -67,7 +68,7 @@ func IsDir(path string) bool { func GetFiles(path string) (paths []string, err error) { _, err = os.Stat(path) if err != nil { - return + return paths, err } err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { if info.IsDir() { @@ -81,7 +82,7 @@ func GetFiles(path string) (paths []string, err error) { // ReadLines reads the contents from the file line by line. func ReadLines(fileName string) ([]string, error) { - var lines []string + lines := make([]string, 0, 64) // reasonable starting capacity for file lines if !IsExist(fileName) { return nil, errors.New("no such file") } @@ -121,9 +122,9 @@ func MkDirs(dirs ...string) error { return nil } for _, dir := range dirs { - err := os.MkdirAll(dir, 0755) + err := os.MkdirAll(dir, 0o755) if err != nil { - return fmt.Errorf("failed to create %s, %v", dir, err) + return fmt.Errorf("failed to create %s, %w", dir, err) } } return nil @@ -135,7 +136,7 @@ func MkTmpdir(dir string) (string, error) { if err != nil { return "", err } - return tempDir, os.MkdirAll(tempDir, 0755) + return tempDir, os.MkdirAll(tempDir, 0o755) } // MkTmpFile creates a temporary file. @@ -147,12 +148,12 @@ func MkTmpFile(path string) (*os.File, error) { func WriteFile(fileName string, content []byte) error { dir := filepath.Dir(fileName) if _, err := os.Stat(dir); os.IsNotExist(err) { - if err = os.MkdirAll(dir, 0755); err != nil { + if err = os.MkdirAll(dir, 0o755); err != nil { return err } } - return AtomicWriteFile(fileName, content, 0644) + return AtomicWriteFile(fileName, content, 0o644) } // RecursionCopy equals to `cp -r` @@ -164,9 +165,9 @@ func RecursionCopy(src, dst string) error { return CopyDirV3(src, dst) } - err := os.MkdirAll(filepath.Dir(dst), 0700|0055) + err := os.MkdirAll(filepath.Dir(dst), 0o700|0o055) if err != nil { - return fmt.Errorf("failed to mkdir for recursion copy, err: %v", err) + return fmt.Errorf("failed to mkdir for recursion copy, err: %w", err) } return Copy(src, dst) @@ -179,7 +180,7 @@ func CleanFile(file *os.File) { } // the following operation won't failed regularly, if failed, log it err := file.Close() - if err != nil && err != os.ErrClosed { + if err != nil && !errors.Is(err, os.ErrClosed) { logger.Warn(err) } err = os.Remove(file.Name()) @@ -193,7 +194,7 @@ func CleanFiles(file ...string) error { for _, f := range file { err := os.RemoveAll(f) if err != nil { - return fmt.Errorf("failed to clean file %s, %v", f, err) + return fmt.Errorf("failed to clean file %s, %w", f, err) } } return nil @@ -244,7 +245,7 @@ func CountDirFiles(dirName string) int { func GetFileSize(path string) (size int64, err error) { _, err = os.Stat(path) if err != nil { - return + return size, err } err = filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { if !info.IsDir() { diff --git a/lifecycle/pkg/utils/file/file_v3.go b/lifecycle/pkg/utils/file/file_v3.go index 522a33af97f2..cd0c4da5282c 100644 --- a/lifecycle/pkg/utils/file/file_v3.go +++ b/lifecycle/pkg/utils/file/file_v3.go @@ -23,7 +23,10 @@ import ( "strings" ) -func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool) ([]string, error) { +func statDir( + dirPath, recPath string, + includeDir, isDirOnly, followSymlinks bool, +) ([]string, error) { dir, err := os.Open(dirPath) if err != nil { return nil, err @@ -43,7 +46,8 @@ func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool relPath := path.Join(recPath, fi.Name()) curPath := path.Join(dirPath, fi.Name()) - if fi.IsDir() { + switch { + case fi.IsDir(): if includeDir { statList = append(statList, relPath+"/") } @@ -52,9 +56,9 @@ func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool return nil, err } statList = append(statList, s...) - } else if !isDirOnly { + case !isDirOnly: statList = append(statList, relPath) - } else if followSymlinks && fi.Mode()&os.ModeSymlink != 0 { + case followSymlinks && fi.Mode()&os.ModeSymlink != 0: link, err := os.Readlink(curPath) if err != nil { return nil, err @@ -170,7 +174,7 @@ func GetFileListBySuffix(dirPath, suffix string) ([]string, error) { // It returns error when error occurs in underlying functions. func CopyDirV3(srcPath, destPath string, filters ...func(filePath string) bool) error { // Check if target directory exists. - //if IsExist(destPath) { + // if IsExist(destPath) { // return errors.New("file or directory alreay exists: " + destPath) //} diff --git a/lifecycle/pkg/utils/file/fswriters.go b/lifecycle/pkg/utils/file/fswriters.go index 950dbbd77b7f..c7e0b362a46b 100644 --- a/lifecycle/pkg/utils/file/fswriters.go +++ b/lifecycle/pkg/utils/file/fswriters.go @@ -60,7 +60,7 @@ func AtomicWriteFile(filepath string, data []byte, perm os.FileMode) (err error) err = afw.Close() }() if _, err = afw.Write(data); err != nil { - return + return err } - return + return err } diff --git a/lifecycle/pkg/utils/file/system.go b/lifecycle/pkg/utils/file/system.go index 6228aef2a4cd..b7fc12fb4393 100644 --- a/lifecycle/pkg/utils/file/system.go +++ b/lifecycle/pkg/utils/file/system.go @@ -21,7 +21,7 @@ import "golang.org/x/sys/unix" // Lgetxattr retrieves the value of the extended attribute identified by attr // and associated with the given path in the file system. // It will returns a nil slice and nil error if the xattr is not set. -func Lgetxattr(path string, attr string) ([]byte, error) { +func Lgetxattr(path, attr string) ([]byte, error) { // Start with a 128 length byte array dest := make([]byte, 128) sz, errno := unix.Lgetxattr(path, attr, dest) @@ -49,6 +49,6 @@ func Lgetxattr(path string, attr string) ([]byte, error) { // Lsetxattr sets the value of the extended attribute identified by attr // and associated with the given path in the file system. -func Lsetxattr(path string, attr string, data []byte, flags int) error { +func Lsetxattr(path, attr string, data []byte, flags int) error { return unix.Lsetxattr(path, attr, data, flags) } diff --git a/lifecycle/pkg/utils/flags/flags.go b/lifecycle/pkg/utils/flags/flags.go index 96e273f6a80c..b67950d2e9ac 100644 --- a/lifecycle/pkg/utils/flags/flags.go +++ b/lifecycle/pkg/utils/flags/flags.go @@ -20,9 +20,8 @@ import ( "os" "strings" - "github.com/spf13/pflag" - "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/pflag" ) // PrintFlags logs the flags in the flagset @@ -41,7 +40,7 @@ func SetFlagsFromEnv(prefix string, flags *pflag.FlagSet) { if flag.Changed { return } - envVar := strings.ToUpper(strings.Replace(prefix+flag.Name, "-", "_", -1)) + envVar := strings.ToUpper(strings.ReplaceAll(prefix+flag.Name, "-", "_")) if v := os.Getenv(envVar); v != "" { _ = flags.Set(flag.Name, v) } diff --git a/lifecycle/pkg/utils/hash/hash.go b/lifecycle/pkg/utils/hash/hash.go index 33eec0953f0c..5b81020d0d60 100644 --- a/lifecycle/pkg/utils/hash/hash.go +++ b/lifecycle/pkg/utils/hash/hash.go @@ -17,15 +17,13 @@ package hash import ( "crypto/sha256" "encoding/hex" - "fmt" "hash" "io" "os" "path/filepath" - "github.com/labring/sealos/pkg/utils/logger" - "github.com/davecgh/go-spew/spew" + "github.com/labring/sealos/pkg/utils/logger" ) func Digest(body []byte) string { @@ -47,12 +45,12 @@ func FileDigest(path string) string { return "" } - fileDigest := fmt.Sprintf("%x", h.Sum(nil)) + fileDigest := hex.EncodeToString(h.Sum(nil)) return fileDigest } // ToString gen hash string base on actual values of the nested objects. -func ToString(obj interface{}) string { +func ToString(obj any) string { hasher := sha256.New() DeepHashObject(hasher, obj) return hex.EncodeToString(hasher.Sum(nil)[0:]) @@ -61,7 +59,7 @@ func ToString(obj interface{}) string { // DeepHashObject writes specified object to hash using the spew library // which follows pointers and prints actual values of the nested objects // ensuring the hash does not change when a pointer changes. -func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) { +func DeepHashObject(hasher hash.Hash, objectToWrite any) { hasher.Reset() printer := spew.ConfigState{ Indent: " ", diff --git a/lifecycle/pkg/utils/hosts/hosts.go b/lifecycle/pkg/utils/hosts/hosts.go index 4cc50062b1df..7cce7d322e2d 100644 --- a/lifecycle/pkg/utils/hosts/hosts.go +++ b/lifecycle/pkg/utils/hosts/hosts.go @@ -23,11 +23,10 @@ import ( "os" "strings" + "github.com/emirpasic/gods/maps/linkedhashmap" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" stringsutils "github.com/labring/sealos/pkg/utils/strings" - - "github.com/emirpasic/gods/maps/linkedhashmap" ) type HostFile struct { @@ -40,15 +39,16 @@ type hostname struct { IP string } -func newHostname(comment string, domain string, ip string) *hostname { +func newHostname(comment, domain, ip string) *hostname { return &hostname{comment, domain, ip} } func (h *hostname) toString() string { return h.Comment + h.IP + " " + h.Domain + "\n" } + func appendToFile(filePath string, hostname *hostname) { - fp, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0644) + fp, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0o644) if err != nil { logger.Warn("failed opening file %s : %s", filePath, err) return @@ -94,7 +94,7 @@ func (h *HostFile) ParseHostFile(path string) (*linkedhashmap.Map, error) { } tmpHostnameArr := strings.Fields(str) curDomain := strings.Join(tmpHostnameArr[1:], " ") - //if !iputils.CheckDomain(curDomain) { + // if !iputils.CheckDomain(curDomain) { // return lm, errors.New(" file contain error domain" + curDomain) //} curIP := stringsutils.TrimSpaceWS(tmpHostnameArr[0]) @@ -111,7 +111,7 @@ func (h *HostFile) ParseHostFile(path string) (*linkedhashmap.Map, error) { return lm, nil } -func (h *HostFile) AppendHost(domain string, ip string) { +func (h *HostFile) AppendHost(domain, ip string) { if domain == "" || ip == "" { return } @@ -126,14 +126,14 @@ func (h *HostFile) writeToFile(hostnameMap *linkedhashmap.Map, path string) { return } - fp, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) + fp, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0o644) if err != nil { logger.Warn("open file '%s' failed: %v", path, err) return } defer fp.Close() - hostnameMap.Each(func(key interface{}, value interface{}) { + hostnameMap.Each(func(key, value any) { if v, ok := value.(*hostname); ok { _, writeErr := fp.WriteString(v.toString()) if writeErr != nil { @@ -191,7 +191,7 @@ func (h *HostFile) ListCurrentHosts() { if currHostsMap == nil { return } - currHostsMap.Each(func(key interface{}, value interface{}) { + currHostsMap.Each(func(key, value any) { if v, ok := value.(*hostname); ok { fmt.Print(v.toString()) } diff --git a/lifecycle/pkg/utils/httpserver/cors.go b/lifecycle/pkg/utils/httpserver/cors.go index 0d1b65429ccb..40f444224a76 100644 --- a/lifecycle/pkg/utils/httpserver/cors.go +++ b/lifecycle/pkg/utils/httpserver/cors.go @@ -24,7 +24,8 @@ func Cors(wsContainer *restful.Container) { AllowedHeaders: []string{"Content-Type", "Accept", "Authorization"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, CookiesAllowed: true, - Container: wsContainer} + Container: wsContainer, + } wsContainer.Filter(cors.Filter) // Add container filter to respond to OPTIONS wsContainer.Filter(wsContainer.OPTIONSFilter) diff --git a/lifecycle/pkg/utils/httpserver/go_restful.go b/lifecycle/pkg/utils/httpserver/go_restful.go index 727dd2cd50a1..7abb142db42b 100644 --- a/lifecycle/pkg/utils/httpserver/go_restful.go +++ b/lifecycle/pkg/utils/httpserver/go_restful.go @@ -16,9 +16,9 @@ package httpserver import ( "net/http" + "time" restful "github.com/emicklei/go-restful/v3" - "github.com/labring/sealos/pkg/utils/logger" ) @@ -31,10 +31,14 @@ func GoRestful(registerFunc func(*restful.WebService), addr string) error { registerFunc(webService) container.Add(webService) - //cors + // cors Cors(container) - server := &http.Server{Addr: addr, Handler: container} + server := &http.Server{ + Addr: addr, + Handler: container, + ReadHeaderTimeout: 30 * time.Second, + } logger.Info("start listening on addr", addr) return server.ListenAndServe() diff --git a/lifecycle/pkg/utils/httpserver/response.go b/lifecycle/pkg/utils/httpserver/response.go index 0bb65ae5d174..0ebb542dca2d 100644 --- a/lifecycle/pkg/utils/httpserver/response.go +++ b/lifecycle/pkg/utils/httpserver/response.go @@ -21,9 +21,9 @@ import ( ) type Response struct { - Code int `json:"code"` - Message string `json:"message"` - Data interface{} `json:"data,omitempty"` + Code int `json:"code"` + Message string `json:"message"` + Data any `json:"data,omitempty"` } // RespError write error to response diff --git a/lifecycle/pkg/utils/initsystem/initsystem.go b/lifecycle/pkg/utils/initsystem/initsystem.go index d9a15e091c95..03155b1151ae 100644 --- a/lifecycle/pkg/utils/initsystem/initsystem.go +++ b/lifecycle/pkg/utils/initsystem/initsystem.go @@ -35,16 +35,16 @@ type initSystem struct { } func (s *initSystem) ServiceEnable(service string) error { - cmd := s.InitSystem.EnableCommand(service) + cmd := s.EnableCommand(service) parts := strings.Split(cmd, " ") if parts[0] == "systemctl" { if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil { - return fmt.Errorf("failed to reload init system: %v", err) + return fmt.Errorf("failed to reload init system: %w", err) } } args := parts[1:] // nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command - return exec.Command(parts[0], args...).Run() + return exec.Command(parts[0], args...).Run() // #nosec G204 } func GetInitSystem() (InitSystem, error) { diff --git a/lifecycle/pkg/utils/iputils/iputils_v2.go b/lifecycle/pkg/utils/iputils/iputils_v2.go index 74499bfdfb46..e6ac19e609dd 100644 --- a/lifecycle/pkg/utils/iputils/iputils_v2.go +++ b/lifecycle/pkg/utils/iputils/iputils_v2.go @@ -22,10 +22,9 @@ import ( "sort" "strings" + "github.com/labring/sealos/pkg/utils/logger" "k8s.io/apimachinery/pkg/util/sets" netutils "k8s.io/utils/net" - - "github.com/labring/sealos/pkg/utils/logger" ) // use only one @@ -62,20 +61,20 @@ func GetDiffHosts(hostsOld, hostsNew []string) (add, sub []string) { } } } - return + return add, sub } func GetHostIPs(hosts []string) []string { - var ips []string + ips := make([]string, 0, len(hosts)) for _, name := range hosts { ips = append(ips, GetHostIP(name)) } return ips } -func GetHostIPAndPortOrDefault(host, Default string) (string, string) { +func GetHostIPAndPortOrDefault(host, defaultPort string) (string, string) { if !strings.ContainsRune(host, ':') { - return host, Default + return host, defaultPort } split := strings.Split(host, ":") return split[0], split[1] @@ -85,19 +84,19 @@ func GetSSHHostIPAndPort(host string) (string, string) { return GetHostIPAndPortOrDefault(host, "22") } -func GetHostIPAndPortSlice(hosts []string, Default string) (res []string) { +func GetHostIPAndPortSlice(hosts []string, defaultPort string) (res []string) { for _, ip := range hosts { - _ip, port := GetHostIPAndPortOrDefault(ip, Default) - res = append(res, fmt.Sprintf("%s:%s", _ip, port)) + _ip, port := GetHostIPAndPortOrDefault(ip, defaultPort) + res = append(res, net.JoinHostPort(_ip, port)) } - return + return res } func GetHostIPSlice(hosts []string) (res []string) { for _, ip := range hosts { res = append(res, GetHostIP(ip)) } - return + return res } func ListLocalHostAddrs() (*[]net.Addr, error) { @@ -110,7 +109,7 @@ func ListLocalHostAddrs() (*[]net.Addr, error) { return netInterfaces[i].Index < netInterfaces[j].Index }) var allAddrs []net.Addr - for i := 0; i < len(netInterfaces); i++ { + for i := range netInterfaces { if (netInterfaces[i].Flags & net.FlagUp) == 0 { continue } @@ -118,9 +117,7 @@ func ListLocalHostAddrs() (*[]net.Addr, error) { if err != nil { logger.Warn("failed to get Addrs, %s", err.Error()) } - for j := 0; j < len(addrs); j++ { - allAddrs = append(allAddrs, addrs[j]) - } + allAddrs = append(allAddrs, addrs...) } return &allAddrs, nil } @@ -130,7 +127,9 @@ func IsLocalIP(ip string, addrs *[]net.Addr) bool { ip = defaultIP } for _, address := range *addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil && ipnet.IP.String() == ip { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && + ipnet.IP.To4() != nil && + ipnet.IP.String() == ip { return true } } @@ -139,7 +138,8 @@ func IsLocalIP(ip string, addrs *[]net.Addr) bool { func LocalIP(addrs *[]net.Addr) string { for _, address := range *addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && + ipnet.IP.To4() != nil { return ipnet.IP.String() } } @@ -167,7 +167,8 @@ func ParseIPList(s string) ([]string, error) { return nil, nil } var ret []string - if strings.Contains(s, ",") { + switch { + case strings.Contains(s, ","): ss := strings.Split(s, ",") for i := range ss { ret2, err := ParseIPList(ss[i]) @@ -176,7 +177,7 @@ func ParseIPList(s string) ([]string, error) { } ret = append(ret, ret2...) } - } else if strings.Contains(s, "/") { + case strings.Contains(s, "/"): ip, ipnet, err := net.ParseCIDR(s) if err != nil { return nil, err @@ -185,7 +186,7 @@ func ParseIPList(s string) ([]string, error) { ret = append(ret, ip.String()) } // network address and broadcast address are included - } else if strings.Contains(s, "-") { + case strings.Contains(s, "-"): ips := strings.Split(s, "-") if len(ips) != 2 { return nil, errors.New("ip range format is invalid") @@ -200,7 +201,11 @@ func ParseIPList(s string) ([]string, error) { res, _ := CompareIP(ips[0], ips[1]) if res > 0 { if first { - return nil, fmt.Errorf("start ip %s cannot greater than end ip %s", ips[0], ips[1]) + return nil, fmt.Errorf( + "start ip %s cannot greater than end ip %s", + ips[0], + ips[1], + ) } break } @@ -208,7 +213,7 @@ func ParseIPList(s string) ([]string, error) { ips[0] = NextIP(ips[0]).String() first = false } - } else { + default: ip := net.ParseIP(GetHostIP(s)) if ip == nil { return nil, fmt.Errorf("invalid ip: %v", s) @@ -238,7 +243,7 @@ func CompareIP(v1, v2 string) (int, error) { j := IPToInt(v2) if i == nil || j == nil { - return 2, fmt.Errorf("ip is invalid,check you command args") + return 2, errors.New("ip is invalid,check you command args") } return i.Cmp(j), nil } diff --git a/lifecycle/pkg/utils/logger/logger.go b/lifecycle/pkg/utils/logger/logger.go index 584c0677639f..b8018578c36d 100644 --- a/lifecycle/pkg/utils/logger/logger.go +++ b/lifecycle/pkg/utils/logger/logger.go @@ -25,9 +25,7 @@ import ( "gopkg.in/natefinch/lumberjack.v2" ) -var ( - defaultLogger *zap.Logger -) +var defaultLogger *zap.Logger // init default logger with only console output info above func init() { @@ -37,7 +35,7 @@ func init() { // CfgConsoleLogger config for console logs // cfg donot support concurrent calls (as any package should init cfg at startup once) -func CfgConsoleLogger(debugMode bool, showPath bool) { +func CfgConsoleLogger(debugMode, showPath bool) { level, zos := genConfigs(debugMode, showPath) zc := zapcore.NewTee(newConsoleCore(level)) @@ -58,7 +56,7 @@ func CfgConsoleAndFileLogger(debugMode bool, logDir, name string, showPath bool) defaultLogger = zap.New(zc, zos...) } -func genConfigs(debugMode bool, showPath bool) (zapcore.LevelEnabler, []zap.Option) { +func genConfigs(debugMode, showPath bool) (zapcore.LevelEnabler, []zap.Option) { level := zapcore.InfoLevel if debugMode { level = zapcore.DebugLevel @@ -91,7 +89,7 @@ func newConsoleCore(le zapcore.LevelEnabler) zapcore.Core { } func newFileCore(filename string, le zapcore.LevelEnabler) zapcore.Core { - //TODO: export more rotate configs + // TODO: export more rotate configs fileLogger := zapcore.AddSync(&lumberjack.Logger{ Filename: filename, MaxSize: 10, // megabytes per file @@ -116,36 +114,36 @@ func IsDebugMode() bool { } // Fatal logs a message at emergency level and exit. -func Fatal(f interface{}, v ...interface{}) { +func Fatal(f any, v ...any) { defaultLogger.Sugar().Fatalf(formatLog(zapcore.FatalLevel, f, v...)) } // Panic logs a message at emergency level and exit. -func Panic(f interface{}, v ...interface{}) { +func Panic(f any, v ...any) { defaultLogger.Sugar().Panicf(formatLog(zapcore.PanicLevel, f, v...)) } // Error logs a message at error level. -func Error(f interface{}, v ...interface{}) { +func Error(f any, v ...any) { defaultLogger.Sugar().Errorf(formatLog(zapcore.ErrorLevel, f, v...)) } // Warn logs a message at warning level. -func Warn(f interface{}, v ...interface{}) { +func Warn(f any, v ...any) { defaultLogger.Sugar().Warnf(formatLog(zapcore.WarnLevel, f, v...)) } // Info logs a message at info level. -func Info(f interface{}, v ...interface{}) { +func Info(f any, v ...any) { defaultLogger.Sugar().Infof(formatLog(zapcore.InfoLevel, f, v...)) } // Debug logs a message at debug level. -func Debug(f interface{}, v ...interface{}) { +func Debug(f any, v ...any) { defaultLogger.Sugar().Debugf(formatLog(zapcore.DebugLevel, f, v...)) } -func formatLog(l zapcore.Level, f interface{}, v ...interface{}) string { +func formatLog(l zapcore.Level, f any, v ...any) string { var msg string switch f := f.(type) { case string: diff --git a/lifecycle/pkg/utils/logger/logger_test.go b/lifecycle/pkg/utils/logger/logger_test.go index b337f7620ea2..0c6ac5c4ae4b 100644 --- a/lifecycle/pkg/utils/logger/logger_test.go +++ b/lifecycle/pkg/utils/logger/logger_test.go @@ -48,10 +48,11 @@ func TestFatalLog(t *testing.T) { Fatal("this is fatal") return } - cmd := exec.Command(os.Args[0], "-test.run=TestFatalLog") + cmd := exec.Command(os.Args[0], "-test.run=TestFatalLog") // #nosec G204 cmd.Env = append(os.Environ(), "LOG_FATAL=1") err := cmd.Run() - if e, ok := err.(*exec.ExitError); ok && !e.Success() { + e := &exec.ExitError{} + if errors.As(err, &e) { return } t.Fatalf("process ran with err %v, want exit status 1", err) diff --git a/lifecycle/pkg/utils/maps/maps.go b/lifecycle/pkg/utils/maps/maps.go index 805ed00b27b8..6867c6538f5e 100644 --- a/lifecycle/pkg/utils/maps/maps.go +++ b/lifecycle/pkg/utils/maps/maps.go @@ -31,7 +31,7 @@ func ToString(data map[string]string, sep string) string { return strings.Join(result, sep) } -func FromString(data string, sep string) map[string]string { +func FromString(data, sep string) map[string]string { list := strings.Split(data, sep) return FromSlice(list) } @@ -59,19 +59,19 @@ func Merge(ms ...map[string]string) map[string]string { return res } -func DeepMerge(dst, src *map[string]interface{}) { +func DeepMerge(dst, src *map[string]any) { for srcK, srcV := range *src { dstV, ok := (*dst)[srcK] if !ok { continue } - dV, ok := dstV.(map[string]interface{}) + dV, ok := dstV.(map[string]any) // dstV is string type if !ok { (*dst)[srcK] = srcV continue } - sV, ok := srcV.(map[string]interface{}) + sV, ok := srcV.(map[string]any) if !ok { continue } diff --git a/lifecycle/pkg/utils/net/net.go b/lifecycle/pkg/utils/net/net.go index f9d2eb18c3fd..45fcb2942563 100644 --- a/lifecycle/pkg/utils/net/net.go +++ b/lifecycle/pkg/utils/net/net.go @@ -16,6 +16,7 @@ package net import ( + "errors" "fmt" "net" "os" @@ -27,7 +28,7 @@ import ( ) // WaitForServer waits for a gRPC server to start accepting connections on a socket. -func WaitForServer(socket string, timeout time.Duration, opts ...interface{}) error { +func WaitForServer(socket string, timeout time.Duration, opts ...any) error { var errChecker []func(error) bool var dialOpts []grpc.DialOption var connp **grpc.ClientConn @@ -42,7 +43,7 @@ func WaitForServer(socket string, timeout time.Duration, opts ...interface{}) er dialOpts = append(dialOpts, o...) case **grpc.ClientConn: if connp != nil { - return fmt.Errorf("waitForServer: multiple net.Conn pointer options given") + return errors.New("waitForServer: multiple net.Conn pointer options given") } connp = o default: @@ -143,30 +144,32 @@ func isFatalDialError(err error) bool { } } - switch err.(type) { //nolint:gosimple - case *net.OpError: - err = err.(*net.OpError).Err //nolint:gosimple + var opErr *net.OpError + if errors.As(err, &opErr) { + err = opErr.Err continue - case *os.SyscallError: - ne := err.(*os.SyscallError) //nolint:gosimple + } + + var sysErr *os.SyscallError + if errors.As(err, &sysErr) { switch { - case os.IsPermission(ne): + case os.IsPermission(sysErr): return true - case os.IsNotExist(ne): + case os.IsNotExist(sysErr): return false - case ne.Err == syscall.ECONNREFUSED: + case errors.Is(sysErr.Err, syscall.ECONNREFUSED): return true default: - err = ne - continue - } - default: - if oe, ok := err.(origin); ok { - err = oe.Origin() + err = sysErr continue } } + if oe, ok := err.(origin); ok { + err = oe.Origin() + continue + } + return true } } diff --git a/lifecycle/pkg/utils/progress/progress_download.go b/lifecycle/pkg/utils/progress/progress_download.go index c71d10ce8ece..254076970d46 100644 --- a/lifecycle/pkg/utils/progress/progress_download.go +++ b/lifecycle/pkg/utils/progress/progress_download.go @@ -24,7 +24,6 @@ import ( "strconv" http2 "github.com/labring/sealos/pkg/utils/http" - "github.com/schollz/progressbar/v3" ) @@ -34,9 +33,9 @@ func Download(srcFile, destFile string) error { var sourceSize int64 if _, ok := http2.IsURL(srcFile); ok { // open as url - resp, err := http.Get(sourceName) + resp, err := http.Get(sourceName) // #nosec G107 if err != nil { - return fmt.Errorf("can't get %s: %v", sourceName, err) + return fmt.Errorf("can't get %s: %w", sourceName, err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { @@ -49,13 +48,13 @@ func Download(srcFile, destFile string) error { // open as file s, err := os.Open(sourceName) if err != nil { - return fmt.Errorf("can't open %s: %v", sourceName, err) + return fmt.Errorf("can't open %s: %w", sourceName, err) } defer s.Close() // get source size sourceStat, err := s.Stat() if err != nil { - return fmt.Errorf("can't stat %s: %v", sourceName, err) + return fmt.Errorf("can't stat %s: %w", sourceName, err) } sourceSize = sourceStat.Size() source = s diff --git a/lifecycle/pkg/utils/rand/rand.go b/lifecycle/pkg/utils/rand/rand.go index 827e9b35a1d7..ed3ef1a0cf1d 100644 --- a/lifecycle/pkg/utils/rand/rand.go +++ b/lifecycle/pkg/utils/rand/rand.go @@ -23,13 +23,13 @@ import ( "math/big" ) -func Generator(len int) string { +func Generator(length int) string { var container string - var str = "abcdefghijklmnopqrstuvwxyz1234567890" + str := "abcdefghijklmnopqrstuvwxyz1234567890" b := bytes.NewBufferString(str) - length := b.Len() - bigInt := big.NewInt(int64(length)) - for i := 0; i < len; i++ { + charsetLen := b.Len() + bigInt := big.NewInt(int64(charsetLen)) + for range length { randomInt, _ := rand.Int(rand.Reader, bigInt) container += string(str[randomInt.Int64()]) } diff --git a/lifecycle/pkg/utils/retry/retry.go b/lifecycle/pkg/utils/retry/retry.go index f4edc24c1f53..b225f8d96473 100644 --- a/lifecycle/pkg/utils/retry/retry.go +++ b/lifecycle/pkg/utils/retry/retry.go @@ -21,7 +21,7 @@ import ( func Retry(tryTimes int, trySleepTime time.Duration, action func() error) error { var err error - for i := 0; i < tryTimes; i++ { + for i := range tryTimes { err = action() if err == nil { return nil @@ -29,5 +29,5 @@ func Retry(tryTimes int, trySleepTime time.Duration, action func() error) error time.Sleep(trySleepTime * time.Duration(2*i+1)) } - return fmt.Errorf("retry action timeout: %v", err) + return fmt.Errorf("retry action timeout: %w", err) } diff --git a/lifecycle/pkg/utils/strings/strings.go b/lifecycle/pkg/utils/strings/strings.go index 6615619d2725..8a68081806de 100644 --- a/lifecycle/pkg/utils/strings/strings.go +++ b/lifecycle/pkg/utils/strings/strings.go @@ -22,15 +22,13 @@ import ( "os" "path/filepath" "regexp" + "slices" "strings" "unicode" "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/template" "github.com/labring/sealos/pkg/utils/file" - - "golang.org/x/exp/slices" - "github.com/labring/sealos/pkg/utils/logger" ) @@ -63,7 +61,7 @@ func FilterNonEmptyFromSlice(list []string) (ret []string) { ret = append(ret, list[i]) } } - return + return ret } func FilterNonEmptyFromString(s, sep string) []string { @@ -101,7 +99,7 @@ func RemoveFromSlice(ss []string, s string) (result []string) { result = append(result, v) } } - return + return result } func Merge(ss []string, s string) []string { @@ -115,17 +113,17 @@ func Merge(ss []string, s string) []string { return ret } -func FormatSize(size int64) (Size string) { - if size < 1024 { - Size = fmt.Sprintf("%.2fB", float64(size)/float64(1)) - } else if size < (1024 * 1024) { - Size = fmt.Sprintf("%.2fKB", float64(size)/float64(1024)) - } else if size < (1024 * 1024 * 1024) { - Size = fmt.Sprintf("%.2fMB", float64(size)/float64(1024*1024)) - } else { - Size = fmt.Sprintf("%.2fGB", float64(size)/float64(1024*1024*1024)) +func FormatSize(size int64) string { + switch { + case size < 1024: + return fmt.Sprintf("%.2fB", float64(size)) + case size < 1024*1024: + return fmt.Sprintf("%.2fKB", float64(size)/float64(1024)) + case size < 1024*1024*1024: + return fmt.Sprintf("%.2fMB", float64(size)/float64(1024*1024)) + default: + return fmt.Sprintf("%.2fGB", float64(size)/float64(1024*1024*1024)) } - return } func IsLetterOrNumber(k string) bool { @@ -156,7 +154,7 @@ func RenderTextWithEnv(text string, envs map[string]string) string { for k, v := range envs { replaces[fmt.Sprintf("$(%s)", k)] = v replaces[fmt.Sprintf("${%s}", k)] = v - replaces[fmt.Sprintf("$%s", k)] = v + replaces["$"+k] = v } logger.Debug("renderTextFromEnv: replaces: %+v ; text: %s", replaces, text) for o, n := range replaces { @@ -196,7 +194,7 @@ func RenderTemplatesWithEnv(filePaths string, envs map[string]string) error { writer, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, os.ModePerm) if err != nil { - return fmt.Errorf("failed to open file [%s] for rendering: %v", path, err) + return fmt.Errorf("failed to open file [%s] for rendering: %w", path, err) } defer writer.Close() @@ -208,10 +206,10 @@ func RenderTemplatesWithEnv(filePaths string, envs map[string]string) error { t, isOk, err := template.TryParse(string(body)) if isOk { if err != nil { - return fmt.Errorf("failed to create template: %s %v", path, err) + return fmt.Errorf("failed to create template: %s %w", path, err) } if err := t.Execute(writer, envs); err != nil { - return fmt.Errorf("failed to render env template: %s %v", path, err) + return fmt.Errorf("failed to render env template: %s %w", path, err) } } else { return errors.New("parse template failed") @@ -219,7 +217,7 @@ func RenderTemplatesWithEnv(filePaths string, envs map[string]string) error { return nil }); err != nil { - return fmt.Errorf("failed to render templates in directory %s: %v", dir, err) + return fmt.Errorf("failed to render templates in directory %s: %w", dir, err) } } diff --git a/lifecycle/pkg/utils/toml/toml.go b/lifecycle/pkg/utils/toml/toml.go index b2001e78729f..2aae83e1644c 100644 --- a/lifecycle/pkg/utils/toml/toml.go +++ b/lifecycle/pkg/utils/toml/toml.go @@ -17,12 +17,11 @@ limitations under the License. package toml import ( - "github.com/pelletier/go-toml" - fileutil "github.com/labring/sealos/pkg/utils/file" + "github.com/pelletier/go-toml" ) -func MarshalFile(file string, obj interface{}) error { +func MarshalFile(file string, obj any) error { data, err := toml.Marshal(obj) if err != nil { return err @@ -30,7 +29,7 @@ func MarshalFile(file string, obj interface{}) error { return fileutil.WriteFile(file, data) } -func UnmarshalFile(file string, obj interface{}) error { +func UnmarshalFile(file string, obj any) error { metadata, err := fileutil.ReadAll(file) if err != nil { return err diff --git a/lifecycle/pkg/utils/yaml/yaml.go b/lifecycle/pkg/utils/yaml/yaml.go index 16070199fea9..b8a37b88f419 100644 --- a/lifecycle/pkg/utils/yaml/yaml.go +++ b/lifecycle/pkg/utils/yaml/yaml.go @@ -19,22 +19,22 @@ package yaml import ( "bufio" "bytes" + "errors" "fmt" "io" "os" "reflect" "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" ) const nonStructPointerErrorFmt = "must be a struct pointer, got %T" -func unmarshalStrict(r io.Reader, obj interface{}) (err error) { +func unmarshalStrict(r io.Reader, obj any) (err error) { if obj != nil && reflect.ValueOf(obj).Kind() != reflect.Pointer { return fmt.Errorf(nonStructPointerErrorFmt, obj) } @@ -45,7 +45,7 @@ func unmarshalStrict(r io.Reader, obj interface{}) (err error) { rd := utilyaml.NewYAMLReader(bufio.NewReader(r)) for { buf, rerr := rd.Read() - if rerr == io.EOF { + if errors.Is(rerr, io.EOF) { break } if rerr != nil { @@ -63,11 +63,11 @@ func unmarshalStrict(r io.Reader, obj interface{}) (err error) { err = fmt.Errorf("document do not have corresponding struct %T", obj) } } - return + return err } -func UnmarshalToMap(buf []byte) (map[string]interface{}, error) { - var data map[string]interface{} +func UnmarshalToMap(buf []byte) (map[string]any, error) { + var data map[string]any err := yaml.Unmarshal(buf, &data) if err != nil { return nil, err @@ -81,21 +81,21 @@ func ToJSON(bs []byte) (jsons []string) { d := utilyaml.NewYAMLOrJSONDecoder(reader, 4096) for { if err := d.Decode(&ext); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } break } jsons = append(jsons, string(ext.Raw)) } - return + return jsons } -func Marshal(obj interface{}) ([]byte, error) { +func Marshal(obj any) ([]byte, error) { return yaml.Marshal(obj) } -func MarshalFile(file string, obj ...interface{}) error { +func MarshalFile(file string, obj ...any) error { data, err := MarshalConfigs(obj...) if err != nil { return err @@ -103,7 +103,7 @@ func MarshalFile(file string, obj ...interface{}) error { return fileutil.WriteFile(file, data) } -func Unmarshal(r io.Reader, obj interface{}) error { +func Unmarshal(r io.Reader, obj any) error { return unmarshalStrict(r, obj) } @@ -117,7 +117,7 @@ func IsNil(b []byte) (bool, error) { // UnmarshalFile if there is no content in the file or it contains only spaces, // result will be nil, then the given object is not initialized at this time. -func UnmarshalFile(file string, obj interface{}) error { +func UnmarshalFile(file string, obj any) error { r, err := os.Open(file) if err != nil { return err @@ -126,8 +126,8 @@ func UnmarshalFile(file string, obj interface{}) error { return unmarshalStrict(r, obj) } -func MarshalConfigs(configs ...interface{}) ([]byte, error) { - var cfgs [][]byte +func MarshalConfigs(configs ...any) ([]byte, error) { + cfgs := make([][]byte, 0, len(configs)) for _, cfg := range configs { data, err := yaml.Marshal(cfg) if err != nil { @@ -138,7 +138,7 @@ func MarshalConfigs(configs ...interface{}) ([]byte, error) { return bytes.Join(cfgs, []byte("\n---\n")), nil } -func ShowStructYaml(s interface{}) { +func ShowStructYaml(s any) { data, _ := yaml.Marshal(s) fmt.Println(string(data)) } diff --git a/lifecycle/pkg/version/types.go b/lifecycle/pkg/version/types.go index 9ee80479ba52..4745e15fa1ed 100644 --- a/lifecycle/pkg/version/types.go +++ b/lifecycle/pkg/version/types.go @@ -24,12 +24,12 @@ import ( // TODO: Add []string of api versions supported? It's still unclear // how we'll want to distribute that information. type Info struct { - GitVersion string `json:"gitVersion" yaml:"gitVersion"` + GitVersion string `json:"gitVersion" yaml:"gitVersion"` GitCommit string `json:"gitCommit,omitempty" yaml:"gitCommit,omitempty"` - BuildDate string `json:"buildDate" yaml:"buildDate"` - GoVersion string `json:"goVersion" yaml:"goVersion"` - Compiler string `json:"compiler" yaml:"compiler"` - Platform string `json:"platform" yaml:"platform"` + BuildDate string `json:"buildDate" yaml:"buildDate"` + GoVersion string `json:"goVersion" yaml:"goVersion"` + Compiler string `json:"compiler" yaml:"compiler"` + Platform string `json:"platform" yaml:"platform"` } // String returns info as a human-friendly version string. @@ -38,19 +38,19 @@ func (info Info) String() string { } type Output struct { - SealosVersion Info `json:"SealosVersion,omitempty" yaml:"SealosVersion,omitempty"` - CriRuntimeVersion *CriRuntimeVersion `json:"CriVersionInfo,omitempty" yaml:"CriVersionInfo,omitempty"` + SealosVersion Info `json:"SealosVersion,omitempty" yaml:"SealosVersion,omitempty"` + CriRuntimeVersion *CriRuntimeVersion `json:"CriVersionInfo,omitempty" yaml:"CriVersionInfo,omitempty"` KubernetesVersion *KubernetesVersion `json:"KubernetesVersionInfo,omitempty" yaml:"KubernetesVersionInfo,omitempty"` } type CriRuntimeVersion struct { // Version of the kubelet runtime API. - Version string `json:"Version,omitempty" yaml:"Version,omitempty"` + Version string `json:"Version,omitempty" yaml:"Version,omitempty"` // Name of the container runtime. - RuntimeName string `json:"RuntimeName,omitempty" yaml:"RuntimeName,omitempty"` + RuntimeName string `json:"RuntimeName,omitempty" yaml:"RuntimeName,omitempty"` // Version of the container runtime. The string must be // semver-compatible. - RuntimeVersion string `json:"RuntimeVersion,omitempty" yaml:"RuntimeVersion,omitempty"` + RuntimeVersion string `json:"RuntimeVersion,omitempty" yaml:"RuntimeVersion,omitempty"` // API version of the container runtime. The string must be // semver-compatible. RuntimeAPIVersion string `json:"RuntimeApiVersion,omitempty" yaml:"RuntimeApiVersion,omitempty"` @@ -58,19 +58,19 @@ type CriRuntimeVersion struct { // Version is a struct for version information type KubernetesVersion struct { - ClientVersion *KubectlInfo `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` + ClientVersion *KubectlInfo `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` KustomizeVersion string `json:"kustomizeVersion,omitempty" yaml:"kustomizeVersion,omitempty"` - ServerVersion *KubectlInfo `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` + ServerVersion *KubectlInfo `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` } type KubectlInfo struct { - Major string `json:"major" yaml:"major"` - Minor string `json:"minor" yaml:"minor"` - GitVersion string `json:"gitVersion" yaml:"gitVersion"` - GitCommit string `json:"gitCommit" yaml:"gitCommit"` + Major string `json:"major" yaml:"major"` + Minor string `json:"minor" yaml:"minor"` + GitVersion string `json:"gitVersion" yaml:"gitVersion"` + GitCommit string `json:"gitCommit" yaml:"gitCommit"` GitTreeState string `json:"gitTreeState" yaml:"gitTreeState"` - BuildDate string `json:"buildDate" yaml:"buildDate"` - GoVersion string `json:"goVersion" yaml:"goVersion"` - Compiler string `json:"compiler" yaml:"compiler"` - Platform string `json:"platform" yaml:"platform"` + BuildDate string `json:"buildDate" yaml:"buildDate"` + GoVersion string `json:"goVersion" yaml:"goVersion"` + Compiler string `json:"compiler" yaml:"compiler"` + Platform string `json:"platform" yaml:"platform"` } diff --git a/lifecycle/pkg/version/utils/utils.go b/lifecycle/pkg/version/utils/utils.go index a36b9b0ab094..076d6a7cbe53 100644 --- a/lifecycle/pkg/version/utils/utils.go +++ b/lifecycle/pkg/version/utils/utils.go @@ -19,13 +19,12 @@ package utils import ( "fmt" - "gopkg.in/yaml.v2" - "github.com/labring/sealos/pkg/constants" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/exec" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/version" + "gopkg.in/yaml.v2" ) func GetKubernetesVersion(cluster *v2.Cluster) *version.KubernetesVersion { diff --git a/lifecycle/scripts/make-rules/golang.mk b/lifecycle/scripts/make-rules/golang.mk index 698f6d474516..3e5f45001501 100644 --- a/lifecycle/scripts/make-rules/golang.mk +++ b/lifecycle/scripts/make-rules/golang.mk @@ -13,6 +13,8 @@ # limitations under the License. GO := go +GOLANGCI_CACHE_DIR := $(abspath $(ROOT_DIR)/../.cache/golangci-lint) +GOLANGCI_GOMODCACHE := $(GOLANGCI_CACHE_DIR)/gomodcache GO_LDFLAGS += -X $(VERSION_PACKAGE).gitVersion=${GIT_TAG} \ -X $(VERSION_PACKAGE).gitCommit=${GIT_COMMIT} \ -X $(VERSION_PACKAGE).buildDate=${BUILD_DATE} \ @@ -89,7 +91,12 @@ go.tidy: .PHONY: go.lint go.lint: tools.verify.golangci-lint @echo "===========> Run golangci to lint source codes" - @$(TOOLS_DIR)/golangci-lint run --build-tags=musl -c $(ROOT_DIR)/.golangci.yml + @if [ -d $(ROOT_DIR)/tmp/gomodcache ]; then chmod -R u+w $(ROOT_DIR)/tmp/gomodcache || true; rm -rf $(ROOT_DIR)/tmp/gomodcache; fi + @mkdir -p $(GOLANGCI_GOMODCACHE) + @GOOS=linux GOARCH=$(GOARCH) GOMODCACHE=$(GOLANGCI_GOMODCACHE) $(TOOLS_DIR)/golangci-lint run --color=always --build-tags musl,containers_image_openpgp,netgo,exclude_graphdriver_devicemapper,static,osusergo,exclude_graphdriver_btrfs -c $(ROOT_DIR)/../.golangci.yml --fix -v + @GOOS=linux GOARCH=$(GOARCH) GOMODCACHE=$(GOLANGCI_GOMODCACHE) $(TOOLS_DIR)/golangci-lint run --color=always --build-tags musl,containers_image_openpgp,netgo,exclude_graphdriver_devicemapper,static,osusergo,exclude_graphdriver_btrfs -c $(ROOT_DIR)/../.golangci.yml --fix -v test/e2e/... + @GOOS=linux GOARCH=$(GOARCH) GOMODCACHE=$(GOLANGCI_GOMODCACHE) $(TOOLS_DIR)/golangci-lint run --color=always --build-tags musl,containers_image_openpgp,netgo,exclude_graphdriver_devicemapper,static,osusergo,exclude_graphdriver_btrfs -c $(ROOT_DIR)/../.golangci.yml --fix -v staging/src/github.com/labring/image-cri-shim/... + @GOOS=linux GOARCH=$(GOARCH) GOMODCACHE=$(GOLANGCI_GOMODCACHE) $(TOOLS_DIR)/golangci-lint run --color=always --build-tags musl,containers_image_openpgp,netgo,exclude_graphdriver_devicemapper,static,osusergo,exclude_graphdriver_btrfs -c $(ROOT_DIR)/../.golangci.yml --fix -v staging/src/github.com/labring/lvscare/... .PHONY: go.format go.format: tools.verify.goimports @@ -101,4 +108,3 @@ go.format: tools.verify.goimports .PHONY: go.coverage go.coverage: @$(GO) test -race -failfast -coverprofile=coverage.out -covermode=atomic `go list ./pkg/env ./pkg/apply | grep -v "/test\|/fork"` - diff --git a/lifecycle/scripts/make-rules/tools.mk b/lifecycle/scripts/make-rules/tools.mk index 7ee22d23bd37..c8da82f9f570 100644 --- a/lifecycle/scripts/make-rules/tools.mk +++ b/lifecycle/scripts/make-rules/tools.mk @@ -26,9 +26,18 @@ tools.install.%: tools.verify.%: @if [ ! -f $(TOOLS_DIR)/$* ]; then GOBIN=$(TOOLS_DIR) $(MAKE) tools.install.$*; fi +.PHONY: tools.verify.golangci-lint +tools.verify.golangci-lint: + @if [ ! -f $(TOOLS_DIR)/golangci-lint ]; then \ + $(MAKE) tools.install.golangci-lint; \ + elif ! /usr/bin/env -u GOOS -u GOARCH $(TOOLS_DIR)/golangci-lint version >/dev/null 2>&1; then \ + rm -f $(TOOLS_DIR)/golangci-lint; \ + $(MAKE) tools.install.golangci-lint; \ + fi + .PHONY: install.golangci-lint install.golangci-lint: - @$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + @/usr/bin/env -u GOOS -u GOARCH bash -c "set -euo pipefail; curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b '$(TOOLS_DIR)' v2.5.0" .PHONY: install.goimports install.goimports: diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/cri/cri.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/cri/cri.go index 50a4e74d43b0..2f9947163453 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/cri/cri.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/cri/cri.go @@ -23,11 +23,10 @@ import ( goruntime "runtime" "strings" - toml "github.com/pelletier/go-toml" - utilsexec "k8s.io/utils/exec" - "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + toml "github.com/pelletier/go-toml" + utilsexec "k8s.io/utils/exec" ) // defaultKnownCRISockets holds the set of known CRI endpoints @@ -57,7 +56,7 @@ type ContainerdRuntime struct { // CRIORuntime is a struct that interfaces with the CRI // -//nolint:all + type CRIORuntime struct { ContainerdRuntime } @@ -68,10 +67,17 @@ func (runtime *CRIORuntime) CGroupDriver() (string, error) { } var outBytes []byte var err error - driverCmd := fmt.Sprintf("crio-status -s %s info | grep \"cgroup driver\" | awk '{print $3}'", runtime.criSocket) + driverCmd := fmt.Sprintf( + "crio-status -s %s info | grep \"cgroup driver\" | awk '{print $3}'", + runtime.criSocket, + ) // nosemgrep: trailofbits.go.invalid-usage-of-modified-variable.invalid-usage-of-modified-variable if outBytes, err = runtime.exec.Command("bash", "-c", driverCmd).CombinedOutput(); err != nil { - return DefaultCgroupDriver, fmt.Errorf("container cgroup driver: output: %s, error: %w", string(outBytes), err) + return DefaultCgroupDriver, fmt.Errorf( + "container cgroup driver: output: %s, error: %w", + string(outBytes), + err, + ) } return strings.TrimSpace(string(outBytes)), nil } @@ -82,7 +88,10 @@ type DockerRuntime struct { } // NewContainerRuntime sets up and returns a ContainerRuntime struct -func NewContainerRuntime(execer utilsexec.Interface, criSocket string, config string) (ContainerRuntime, error) { +func NewContainerRuntime( + execer utilsexec.Interface, + criSocket, config string, +) (ContainerRuntime, error) { var toolNames string var runtime ContainerRuntime @@ -120,7 +129,11 @@ func NewContainerRuntime(execer utilsexec.Interface, criSocket string, config st func (runtime *ContainerdRuntime) IsRunning() error { // nosemgrep: trailofbits.go.invalid-usage-of-modified-variable.invalid-usage-of-modified-variable if out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "info").CombinedOutput(); err != nil { - return fmt.Errorf("container runtime is not running: output: %s, error: %w", string(out), err) + return fmt.Errorf( + "container runtime is not running: output: %s, error: %w", + string(out), + err, + ) } return nil } @@ -129,7 +142,11 @@ func (runtime *ContainerdRuntime) IsRunning() error { func (runtime *DockerRuntime) IsRunning() error { // nosemgrep: trailofbits.go.invalid-usage-of-modified-variable.invalid-usage-of-modified-variable if out, err := runtime.exec.Command("docker", "info").CombinedOutput(); err != nil { - return fmt.Errorf("container runtime is not running: output: %s, error: %w", string(out), err) + return fmt.Errorf( + "container runtime is not running: output: %s, error: %w", + string(out), + err, + ) } return nil } @@ -142,7 +159,11 @@ func (runtime *DockerRuntime) CGroupDriver() (string, error) { var out []byte // nosemgrep: trailofbits.go.invalid-usage-of-modified-variable.invalid-usage-of-modified-variable if out, err = runtime.exec.Command("docker", "info", "--format", "{{.CgroupDriver}}").CombinedOutput(); err != nil { - return "", fmt.Errorf("container runtime is not running: output: %s, error: %w", string(out), err) + return "", fmt.Errorf( + "container runtime is not running: output: %s, error: %w", + string(out), + err, + ) } return string(out), nil } @@ -229,7 +250,10 @@ func detectCRISocketImpl(isSocket func(string) bool, knownCRISockets []string) ( return foundCRISockets[0], nil default: // Multiple CRIs installed? - return "", fmt.Errorf("found multiple CRI sockets, please use --cri-socket to select one: %s", strings.Join(foundCRISockets, ", ")) + return "", fmt.Errorf( + "found multiple CRI sockets, please use --cri-socket to select one: %s", + strings.Join(foundCRISockets, ", "), + ) } } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go index 8a073c4bebf6..4e45c274023a 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go @@ -20,9 +20,7 @@ import ( "sync" rtype "github.com/docker/docker/api/types/registry" - "github.com/labring/image-cri-shim/pkg/types" - "github.com/labring/sealos/pkg/utils/logger" ) @@ -51,7 +49,11 @@ func (a *AuthStore) Update(auth *types.ShimAuthConfig) { a.criConfigs = cloneAuthMap(auth.CRIConfigs) a.offlineCRIConfigs = cloneAuthMap(auth.OfflineCRIConfigs) - logger.Info("updated shim auth config, registries: %d, offline: %d", len(a.criConfigs), len(a.offlineCRIConfigs)) + logger.Info( + "updated shim auth config, registries: %d, offline: %d", + len(a.criConfigs), + len(a.offlineCRIConfigs), + ) } func (a *AuthStore) GetCRIConfig(registry string) (rtype.AuthConfig, bool) { diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store_test.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store_test.go index 0ce3e4721d49..5d4008c45128 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store_test.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store_test.go @@ -61,7 +61,7 @@ func TestAuthStoreUpdateAndGet(t *testing.T) { offline := store.GetOfflineConfigs() offline["example.com"] = rtype.AuthConfig{Username: "mutated"} - if cfg, _ := store.GetOfflineConfigs()["example.com"]; cfg.Username != "offline" { + if cfg := store.GetOfflineConfigs()["example.com"]; cfg.Username != "offline" { t.Fatalf("expected offline cache to be immutable, got %#v", cfg) } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/const.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/const.go index 81c089f5fbec..afa1caf272f4 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/const.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/const.go @@ -16,7 +16,7 @@ package server const ( // DirPermissions is the permissions to create the directory for sockets with. - DirPermissions = 0711 + DirPermissions = 0o711 ) // maxMsgSize use 16MB as the default message size limit. diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go index 47343ff9f15a..c70694aed4fe 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//nolint:staticcheck package server import ( @@ -23,11 +22,10 @@ import ( "syscall" "time" + netutil "github.com/labring/sealos/pkg/utils/net" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" - - netutil "github.com/labring/sealos/pkg/utils/net" ) // DialNotifyFn is a function to call after a successful net.Dial[Timeout](). @@ -204,6 +202,6 @@ func (c *client) dialNotify(socket string) { } // Return a formatted client-specific error. -func clientError(format string, args ...interface{}) error { +func clientError(format string, args ...any) error { return fmt.Errorf("cri/client: "+format, args...) } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_server_v1.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_server_v1.go index 30820388382e..f3e37129b863 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_server_v1.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_server_v1.go @@ -20,12 +20,9 @@ import ( "context" rtype "github.com/docker/docker/api/types/registry" - "github.com/google/go-containerregistry/pkg/name" - - api "k8s.io/cri-api/pkg/apis/runtime/v1" - "github.com/labring/sealos/pkg/utils/logger" + api "k8s.io/cri-api/pkg/apis/runtime/v1" ) type v1ImageService struct { @@ -57,10 +54,10 @@ func ToV1AuthConfig(c *rtype.AuthConfig) *api.AuthConfig { } func (s *v1ImageService) ListImages(ctx context.Context, - req *api.ListImagesRequest) (*api.ListImagesResponse, error) { + req *api.ListImagesRequest, +) (*api.ListImagesResponse, error) { logger.Debug("ListImages: %+v", req) rsp, err := s.imageClient.ListImages(ctx, req) - if err != nil { return nil, err } @@ -69,7 +66,8 @@ func (s *v1ImageService) ListImages(ctx context.Context, } func (s *v1ImageService) ImageStatus(ctx context.Context, - req *api.ImageStatusRequest) (*api.ImageStatusResponse, error) { + req *api.ImageStatusRequest, +) (*api.ImageStatusResponse, error) { logger.Debug("ImageStatus: %+v", req) if req.Image != nil { if id, _ := s.GetImageRefByID(ctx, req.Image.Image); id != "" { @@ -81,7 +79,6 @@ func (s *v1ImageService) ImageStatus(ctx context.Context, } } rsp, err := s.imageClient.ImageStatus(ctx, req) - if err != nil { return nil, err } @@ -90,7 +87,8 @@ func (s *v1ImageService) ImageStatus(ctx context.Context, } func (s *v1ImageService) PullImage(ctx context.Context, - req *api.PullImageRequest) (*api.PullImageResponse, error) { + req *api.PullImageRequest, +) (*api.PullImageResponse, error) { logger.Debug("PullImage begin: %+v", req) if req.Image != nil { imageName := req.Image.Image @@ -119,7 +117,8 @@ func (s *v1ImageService) PullImage(ctx context.Context, } func (s *v1ImageService) RemoveImage(ctx context.Context, - req *api.RemoveImageRequest) (*api.RemoveImageResponse, error) { + req *api.RemoveImageRequest, +) (*api.RemoveImageResponse, error) { logger.Debug("RemoveImage: %+v", req) if req.Image != nil { if id, _ := s.GetImageRefByID(ctx, req.Image.Image); id != "" { @@ -131,7 +130,6 @@ func (s *v1ImageService) RemoveImage(ctx context.Context, } } rsp, err := s.imageClient.RemoveImage(ctx, req) - if err != nil { return nil, err } @@ -140,10 +138,10 @@ func (s *v1ImageService) RemoveImage(ctx context.Context, } func (s *v1ImageService) ImageFsInfo(ctx context.Context, - req *api.ImageFsInfoRequest) (*api.ImageFsInfoResponse, error) { + req *api.ImageFsInfoRequest, +) (*api.ImageFsInfoResponse, error) { logger.Debug("ImageFsInfo: %+v", req) rsp, err := s.imageClient.ImageFsInfo(ctx, req) - if err != nil { return nil, err } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go index a150f1109f2d..6fde220b35b8 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go @@ -24,11 +24,10 @@ import ( "strconv" "time" - "google.golang.org/grpc" - k8sv1api "k8s.io/cri-api/pkg/apis/runtime/v1" - "github.com/labring/sealos/pkg/utils/logger" netutil "github.com/labring/sealos/pkg/utils/net" + "google.golang.org/grpc" + k8sv1api "k8s.io/cri-api/pkg/apis/runtime/v1" ) type Options struct { @@ -175,7 +174,12 @@ func (s *server) Chown(uid, gid int) error { return serverError("failed to change ownership of socket %q to %s/%s: %v", s.options.Socket, userName, groupName, err) } - logger.Info("changed ownership of socket %q to %s/%s", s.options.Socket, userName, groupName) + logger.Info( + "changed ownership of socket %q to %s/%s", + s.options.Socket, + userName, + groupName, + ) } s.options.User = uid @@ -191,7 +195,7 @@ func (s *server) Stop() { func NewServer(options Options) (Server, error) { if !filepath.IsAbs(options.Socket) { - return nil, fmt.Errorf("invalid socked") + return nil, errors.New("invalid socked") } s := &server{ @@ -201,7 +205,7 @@ func NewServer(options Options) (Server, error) { } // Return a formatter server error. -func serverError(format string, args ...interface{}) error { +func serverError(format string, args ...any) error { return fmt.Errorf("cri/server: "+format, args...) } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go index 782dd6c7dd73..ffaaf927e4b3 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go @@ -20,17 +20,15 @@ import ( "strings" "github.com/docker/docker/api/types/registry" - - "github.com/labring/sreg/pkg/registry/crane" - - "github.com/labring/sealos/pkg/utils/logger" - name "github.com/google/go-containerregistry/pkg/name" + "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sreg/pkg/registry/crane" ) // replaceImage replaces the image name to a new valid image name with the private registry. func replaceImage(image, action string, authConfig map[string]registry.AuthConfig) (newImage string, - isReplace bool, cfg *registry.AuthConfig) { + isReplace bool, cfg *registry.AuthConfig, +) { if len(authConfig) == 0 { return image, false, nil } @@ -75,7 +73,8 @@ func shouldSkipAuth(domain string, cfg *registry.AuthConfig, skip map[string]boo if cfg == nil { return true } - return cfg.Username == "" && cfg.Password == "" && cfg.Auth == "" && cfg.IdentityToken == "" && cfg.RegistryToken == "" + return cfg.Username == "" && cfg.Password == "" && cfg.Auth == "" && cfg.IdentityToken == "" && + cfg.RegistryToken == "" } func referenceSuffix(ref name.Reference) string { diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go index 81bc134e60d7..7028bf7e9b98 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go @@ -20,13 +20,10 @@ import ( "os" "sync" - "github.com/labring/image-cri-shim/pkg/types" - - "google.golang.org/grpc" - "github.com/labring/image-cri-shim/pkg/server" - + "github.com/labring/image-cri-shim/pkg/types" "github.com/labring/sealos/pkg/utils/logger" + "google.golang.org/grpc" ) const ( @@ -78,7 +75,7 @@ func NewShim(cfg *types.Config, auth *types.ShimAuthConfig) (Shim, error) { Socket: cfg.ImageShimSocket, User: -1, Group: -1, - Mode: 0660, + Mode: 0o660, AuthStore: r.authStore, } srv, err := server.NewServer(srvopts) @@ -127,7 +124,7 @@ func (r *shim) UpdateAuth(auth *types.ShimAuthConfig) { r.authStore.Update(auth) } -func (r *shim) dialNotify(socket string, uid int, gid int, mode os.FileMode, err error) { +func (r *shim) dialNotify(socket string, uid, gid int, mode os.FileMode, err error) { if err != nil { logger.Error("failed to determine permissions/ownership of client socket %q: %v", socket, err) @@ -144,6 +141,6 @@ func (r *shim) dialNotify(socket string, uid int, gid int, mode os.FileMode, err } // shimError creates a formatted shim-specific error. -var shimError = func(format string, args ...interface{}) error { +var shimError = func(format string, args ...any) error { return fmt.Errorf("cri/shim: "+format, args...) } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/config.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/config.go index 6ba4942c8364..cbc459baa4be 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/config.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/config.go @@ -22,16 +22,13 @@ import ( "strings" "time" - registry2 "github.com/labring/sreg/pkg/registry/crane" - types2 "github.com/docker/docker/api/types/registry" - "github.com/labring/image-cri-shim/pkg/cri" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/yaml" - fileutil "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + registry2 "github.com/labring/sreg/pkg/registry/crane" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" ) const ( @@ -43,7 +40,7 @@ const ( type Registry struct { Address string `json:"address" yaml:"address"` - Auth string `json:"auth" yaml:"auth,omitempty"` + Auth string `json:"auth" yaml:"auth,omitempty"` } type Config struct { @@ -55,7 +52,7 @@ type Config struct { Timeout metav1.Duration `json:"timeout"` ReloadInterval metav1.Duration `json:"reloadInterval"` Auth string `json:"auth"` - Registries []Registry `json:"registries" yaml:"registries,omitempty"` + Registries []Registry `json:"registries" yaml:"registries,omitempty"` } type ShimAuthConfig struct { @@ -82,7 +79,7 @@ func (c *Config) PreProcess() (*ShimAuthConfig, error) { logger.Warn("url parse error: %+v", err) } domain := rawURL.Host - if c.Timeout.Duration.Milliseconds() == 0 { + if c.Timeout.Milliseconds() == 0 { c.Timeout = metav1.Duration{} c.Timeout.Duration, _ = time.ParseDuration("15m") } @@ -143,7 +140,7 @@ func (c *Config) PreProcess() (*ShimAuthConfig, error) { { offlineName, offlinePasswd := splitNameAndPasswd(c.Auth) - //offline registry auth + // offline registry auth shimAuth.OfflineCRIConfigs = map[string]types2.AuthConfig{domain: { Username: offlineName, Password: offlinePasswd, diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync.go index 7182cbd8b604..b5b004186dda 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync.go @@ -22,17 +22,15 @@ import ( "strings" "time" + fileutil "github.com/labring/sealos/pkg/utils/file" + "github.com/labring/sealos/pkg/utils/logger" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" - "sigs.k8s.io/yaml" - - fileutil "github.com/labring/sealos/pkg/utils/file" - "github.com/labring/sealos/pkg/utils/logger" ) const ( @@ -81,17 +79,32 @@ func SyncConfigFromConfigMap(ctx context.Context, configPath string) { logger.Warn("you can ignore this if you are not running inside a kubernetes cluster") return } - cm, err := client.CoreV1().ConfigMaps(shimConfigMapNamespace).Get(ctx, shimConfigMapName, metav1.GetOptions{}) + cm, err := client.CoreV1(). + ConfigMaps(shimConfigMapNamespace). + Get(ctx, shimConfigMapName, metav1.GetOptions{}) if apierrors.IsNotFound(err) { - logger.Debug("configmap %s/%s not found; skip syncing", shimConfigMapNamespace, shimConfigMapName) + logger.Debug( + "configmap %s/%s not found; skip syncing", + shimConfigMapNamespace, + shimConfigMapName, + ) return } if err != nil { - logger.Debug("failed to read ConfigMap %s/%s: %v", shimConfigMapNamespace, shimConfigMapName, err) + logger.Debug( + "failed to read ConfigMap %s/%s: %v", + shimConfigMapNamespace, + shimConfigMapName, + err, + ) return } if !applyConfigMapToFile(configPath, cm) { - logger.Debug("ConfigMap %s/%s produced no updates", shimConfigMapNamespace, shimConfigMapName) + logger.Debug( + "ConfigMap %s/%s produced no updates", + shimConfigMapNamespace, + shimConfigMapName, + ) return } logger.Info("syncing image-cri-shim config from ConfigMap completed") @@ -124,7 +137,8 @@ func buildKubeClient() (kubernetes.Interface, error) { return kubernetes.NewForConfig(cfg) } rules := clientcmd.NewDefaultClientConfigLoadingRules() - cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{}).ClientConfig() + cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{}). + ClientConfig() if err != nil { return nil, err } @@ -134,7 +148,12 @@ func buildKubeClient() (kubernetes.Interface, error) { func applyConfigMapToFile(configPath string, cm *corev1.ConfigMap) bool { raw, ok := cm.Data[shimConfigMapDataKey] if !ok || strings.TrimSpace(raw) == "" { - logger.Debug("ConfigMap %s/%s does not contain key %s; skip syncing", shimConfigMapNamespace, shimConfigMapName, shimConfigMapDataKey) + logger.Debug( + "ConfigMap %s/%s does not contain key %s; skip syncing", + shimConfigMapNamespace, + shimConfigMapName, + shimConfigMapDataKey, + ) return false } spec := new(registryConfigSpec) diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync_test.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync_test.go index b5fe25a252ef..64f974d31abf 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync_test.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/types/configmap_sync_test.go @@ -59,7 +59,8 @@ func TestMergeShimConfig(t *testing.T) { if len(cfg.Registries) != 1 { t.Fatalf("expected 1 registry, got %d", len(cfg.Registries)) } - if entry := cfg.Registries[0]; entry.Address != "https://registry-1.docker.io" || entry.Auth != "3:4" { + if entry := cfg.Registries[0]; entry.Address != "https://registry-1.docker.io" || + entry.Auth != "3:4" { t.Fatalf("unexpected registry entry: %+v", entry) } if cfg.ReloadInterval.Duration != 5*time.Second { @@ -118,7 +119,8 @@ func TestSyncConfigFromConfigMapWritesFile(t *testing.T) { if merged.Auth != "1:2" { t.Fatalf("unexpected auth: %s", merged.Auth) } - if len(merged.Registries) != 1 || merged.Registries[0].Address != "https://registry-1.docker.io" { + if len(merged.Registries) != 1 || + merged.Registries[0].Address != "https://registry-1.docker.io" { t.Fatalf("unexpected registries: %+v", merged.Registries) } if merged.ReloadInterval.Duration != 5*time.Second { diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go index cfce00193a0e..fbcdfa824473 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go @@ -19,14 +19,13 @@ import ( "fmt" "time" + "github.com/labring/sealos/pkg/utils/logger" "k8s.io/apimachinery/pkg/util/wait" utilsysctl "k8s.io/component-helpers/node/util/sysctl" proxyipvs "k8s.io/kubernetes/pkg/proxy/ipvs" utilipset "k8s.io/kubernetes/pkg/proxy/ipvs/ipset" utiliptables "k8s.io/kubernetes/pkg/util/iptables" "k8s.io/utils/exec" - - "github.com/labring/sealos/pkg/utils/logger" ) type iptablesImpl struct { @@ -62,9 +61,24 @@ var iptablesJumpChain = []struct { to utiliptables.Chain comment string }{ - {utiliptables.TableNAT, utiliptables.ChainOutput, virtualServicesChain, "virtual service portals"}, - {utiliptables.TableNAT, utiliptables.ChainPrerouting, virtualServicesChain, "virtual service portals"}, - {utiliptables.TableNAT, utiliptables.ChainPostrouting, virtualPostroutingChain, "virtual service postrouting rules"}, + { + utiliptables.TableNAT, + utiliptables.ChainOutput, + virtualServicesChain, + "virtual service portals", + }, + { + utiliptables.TableNAT, + utiliptables.ChainPrerouting, + virtualServicesChain, + "virtual service portals", + }, + { + utiliptables.TableNAT, + utiliptables.ChainPostrouting, + virtualPostroutingChain, + "virtual service postrouting rules", + }, } var iptablesChains = []struct { @@ -184,7 +198,11 @@ func ensureSysctl(sysctl utilsysctl.Interface, name string, newVal int) error { return nil } -func ensureDummyDeviceAndAddresses(nl proxyipvs.NetLinkHandle, ifaceName string, addresses ...string) error { +func ensureDummyDeviceAndAddresses( + nl proxyipvs.NetLinkHandle, + ifaceName string, + addresses ...string, +) error { if _, err := nl.EnsureDummyDevice(ifaceName); err != nil { return err } @@ -238,7 +256,12 @@ func (impl *iptablesImpl) ensureIptablesChains() error { // service chain for _, ch := range iptablesChains { if _, err := impl.iptables.EnsureChain(ch.table, ch.chain); err != nil { - logger.Error("Failed to ensure chain, table: %s, chain: %s, %v", ch.table, ch.chain, err) + logger.Error( + "Failed to ensure chain, table: %s, chain: %s, %v", + ch.table, + ch.chain, + err, + ) return err } } @@ -246,7 +269,13 @@ func (impl *iptablesImpl) ensureIptablesChains() error { for _, jc := range iptablesJumpChain { args := []string{"-m", "comment", "--comment", jc.comment, "-j", string(jc.to)} if _, err := impl.iptables.EnsureRule(utiliptables.Append, jc.table, jc.from, args...); err != nil { - logger.Error("Failed to ensure chain jumps, table: %s, src: %s, dst: %s, %v", jc.table, jc.from, jc.to, err) + logger.Error( + "Failed to ensure chain jumps, table: %s, src: %s, dst: %s, %v", + jc.table, + jc.from, + jc.to, + err, + ) } } @@ -280,7 +309,10 @@ func (impl *iptablesImpl) ensureIptablesChains() error { if impl.iptables.HasRandomFully() { masqArgs = append(masqArgs, "--random-fully") } - rules = append(rules, iptablesRule{utiliptables.Append, utiliptables.TableNAT, virtualPostroutingChain, masqArgs}) + rules = append( + rules, + iptablesRule{utiliptables.Append, utiliptables.TableNAT, virtualPostroutingChain, masqArgs}, + ) for i := range rules { if _, err := impl.iptables.EnsureRule(rules[i].position, rules[i].table, rules[i].chain, rules[i].args...); err != nil { return err @@ -289,7 +321,12 @@ func (impl *iptablesImpl) ensureIptablesChains() error { return nil } -func ensureIPSetWithEntries(handle utilipset.Interface, name, comment string, setType utilipset.Type, entries ...string) error { +func ensureIPSetWithEntries( + handle utilipset.Interface, + name, comment string, + setType utilipset.Type, + entries ...string, +) error { set := utilipset.IPSet{ Name: name, SetType: setType, diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/options.go b/lifecycle/staging/src/github.com/labring/lvscare/care/options.go index 1191a7bf3264..aa68cf395316 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/options.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/options.go @@ -23,11 +23,10 @@ import ( "strconv" "time" - "github.com/spf13/pflag" - "k8s.io/klog/v2" - "github.com/labring/sealos/pkg/constants" "github.com/labring/sealos/pkg/utils/hosts" + "github.com/spf13/pflag" + "k8s.io/klog/v2" ) const ( @@ -54,7 +53,13 @@ func (o *options) RegisterFlags(fs *pflag.FlagSet) { fs.StringVar(&o.VirtualServer, "vs", "", "virtual server address, for example 169.254.0.1:6443") fs.StringSliceVar(&o.RealServer, "rs", []string{}, "real server address like 192.168.0.2:6443") fs.StringVar(&o.scheduler, "scheduler", "rr", "proxier scheduler") - fs.StringVarP(&o.IfaceName, "iface", "i", appName, "name of dummy interface to created, same behavior as kube-proxy") + fs.StringVarP( + &o.IfaceName, + "iface", + "i", + appName, + "name of dummy interface to created, same behavior as kube-proxy", + ) fs.StringVar(&o.Logger, "logger", "INFO", "logger level: DEBG/INFO") fs.StringVar(&o.Mode, "mode", routeMode, fmt.Sprintf("proxy mode: %s/%s", routeMode, linkMode)) fs.BoolVar(&o.RunOnce, "run-once", false, "create proxy rules and exit") diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go b/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go index 1c0246aee145..ba4b73a6c824 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go @@ -53,9 +53,24 @@ func (p *httpProber) RegisterFlags(fs *pflag.FlagSet) { fs.StringVar(&p.HealthScheme, "health-schem", "https", "http scheme for prober") fs.StringVar(&p.Method, "health-req-method", "GET", "http request method") fs.StringVar(&p.Body, "health-req-body", "", "body to send for health checker") - fs.StringToStringVar(&p.Headers, "health-req-headers", map[string]string{}, "http request headers") - fs.IntSliceVar(&p.ValidStatusCodes, "health-status", []int{}, "extra valid status codes greater than 400") - fs.BoolVar(&p.InsecureSkipVerify, "health-insecure-skip-verify", true, "skip verify insecure request") + fs.StringToStringVar( + &p.Headers, + "health-req-headers", + map[string]string{}, + "http request headers", + ) + fs.IntSliceVar( + &p.ValidStatusCodes, + "health-status", + []int{}, + "extra valid status codes greater than 400", + ) + fs.BoolVar( + &p.InsecureSkipVerify, + "health-insecure-skip-verify", + true, + "skip verify insecure request", + ) fs.DurationVar(&p.timeout, "health-timeout", 10*time.Second, "http probe timeout") } diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go index f46cf961143c..7ac17b9de020 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go @@ -22,9 +22,8 @@ import ( "sync" "time" - ipvs "k8s.io/kubernetes/pkg/proxy/ipvs/util" - "github.com/labring/sealos/pkg/utils/logger" + ipvs "k8s.io/kubernetes/pkg/proxy/ipvs/util" ) type Proxier interface { @@ -45,7 +44,12 @@ func (ep *endpoint) String() string { return net.JoinHostPort(ep.IP, strconv.Itoa(int(ep.Port))) } -func NewProxier(scheduler string, interval time.Duration, prober Prober, syncFn func() error) Proxier { +func NewProxier( + scheduler string, + interval time.Duration, + prober Prober, + syncFn func() error, +) Proxier { return &realProxier{ scheduler: scheduler, ipvsHandle: ipvs.New(), @@ -121,7 +125,10 @@ func (p *realProxier) DeleteVirtualServer(vs string) error { return nil } -func (p *realProxier) getRealServer(vs *ipvs.VirtualServer, rs *ipvs.RealServer) (*ipvs.RealServer, error) { +func (p *realProxier) getRealServer( + vs *ipvs.VirtualServer, + rs *ipvs.RealServer, +) (*ipvs.RealServer, error) { applied, err := p.ipvsHandle.GetRealServers(vs) if err != nil { return nil, err @@ -134,7 +141,9 @@ func (p *realProxier) getRealServer(vs *ipvs.VirtualServer, rs *ipvs.RealServer) return nil, nil } -func (p *realProxier) getServersByEndpoint(vs, rs endpoint) (*ipvs.VirtualServer, *ipvs.RealServer, error) { +func (p *realProxier) getServersByEndpoint( + vs, rs endpoint, +) (*ipvs.VirtualServer, *ipvs.RealServer, error) { vSrv, err := p.ensureVirtualServer(p.buildVirtualServer(&vs)) if err != nil { return nil, nil, err diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/route.go b/lifecycle/staging/src/github.com/labring/lvscare/care/route.go index 668347273f6a..771cbee195f0 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/route.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/route.go @@ -16,7 +16,6 @@ package care import ( "github.com/labring/lvscare/pkg/route" - "github.com/labring/sealos/pkg/utils/logger" ) diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go index 47a20b119d83..5f9d9ced6bc2 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go @@ -20,10 +20,9 @@ import ( "strings" "time" + "github.com/labring/sealos/pkg/utils/logger" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/manager/signals" - - "github.com/labring/sealos/pkg/utils/logger" ) var LVS = &runner{ @@ -71,7 +70,7 @@ func (r *runner) Run() (err error) { if r.ruler != nil { r.cleanupFuncs = append(r.cleanupFuncs, r.ruler.Cleanup) } - return + return err } errCh := make(chan error, 1) ctx := signals.SetupSignalHandler() @@ -125,7 +124,7 @@ func (r *runner) cleanup() error { } func (r *runner) RegisterCommandFlags(cmd *cobra.Command) { - for _, iter := range []interface{}{r.options, r.prober} { + for _, iter := range []any{r.options, r.prober} { if registerer, ok := iter.(flagRegisterer); ok { registerer.RegisterFlags(cmd.Flags()) } @@ -138,14 +137,19 @@ func (r *runner) RegisterCommandFlags(cmd *cobra.Command) { } func (r *runner) ValidateAndSetDefaults() error { - for _, iter := range []interface{}{r.options, r.prober} { + for _, iter := range []any{r.options, r.prober} { if validator, ok := iter.(flagValidator); ok { if err := validator.ValidateAndSetDefaults(); err != nil { return err } } } - r.proxier = NewProxier(r.options.scheduler, time.Duration(r.options.Interval), r.prober, r.periodicRun) + r.proxier = NewProxier( + r.options.scheduler, + time.Duration(r.options.Interval), + r.prober, + r.periodicRun, + ) virtualIP, _, err := splitHostPort(r.options.VirtualServer) if err != nil { return err @@ -160,7 +164,11 @@ func (r *runner) ValidateAndSetDefaults() error { } ruler, err = newRouteImpl(virtualIP, r.options.TargetIP.String()) case linkMode: - ruler, err = newIptablesImpl(r.options.IfaceName, r.options.MasqueradeBit, r.options.VirtualServer) + ruler, err = newIptablesImpl( + r.options.IfaceName, + r.options.MasqueradeBit, + r.options.VirtualServer, + ) case "": // do nothing, disable ruler default: diff --git a/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go b/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go index 365df9ea9958..1022cd2c6a84 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go @@ -23,7 +23,6 @@ import ( "github.com/labring/sealos/pkg/utils/iputils" "github.com/labring/sealos/pkg/utils/logger" - "github.com/vishvananda/netlink" ) diff --git a/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go b/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go index c9a127eb97db..051b683224c0 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go @@ -35,7 +35,9 @@ var ( // versionMatchRE splits a version string into numeric and "extra" parts versionMatchRE = regexp.MustCompile(`^\s*v?([0-9]+(?:\.[0-9]+)*)(.*)*$`) // extraMatchRE splits the "extra" part of versionMatchRE into semver pre-release and build metadata; it does not validate the "no leading zeroes" constraint for pre-release - extraMatchRE = regexp.MustCompile(`^(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?\s*$`) + extraMatchRE = regexp.MustCompile( + `^(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?\s*$`, + ) ) func parse(str string, semver bool) (*Version, error) { @@ -60,7 +62,12 @@ func parse(str string, semver bool) (*Version, error) { } num, err := strconv.ParseUint(comp, 10, 0) if err != nil { - return nil, fmt.Errorf("illegal non-numeric version component %q in %q: %v", comp, str, err) + return nil, fmt.Errorf( + "illegal non-numeric version component %q in %q: %v", + comp, + str, + err, + ) } v.components[i] = uint(num) } @@ -68,14 +75,22 @@ func parse(str string, semver bool) (*Version, error) { if semver && extra != "" { extraParts := extraMatchRE.FindStringSubmatch(extra) if extraParts == nil { - return nil, fmt.Errorf("could not parse pre-release/metadata (%s) in version %q", extra, str) + return nil, fmt.Errorf( + "could not parse pre-release/metadata (%s) in version %q", + extra, + str, + ) } v.preRelease, v.buildMetadata = extraParts[1], extraParts[2] for _, comp := range strings.Split(v.preRelease, ".") { if _, err := strconv.ParseUint(comp, 10, 0); err == nil { if strings.HasPrefix(comp, "0") && comp != "0" { - return nil, fmt.Errorf("illegal zero-prefixed version component %q in %q", comp, str) + return nil, fmt.Errorf( + "illegal zero-prefixed version component %q in %q", + comp, + str, + ) } } } diff --git a/lifecycle/test/e2e/applier.go b/lifecycle/test/e2e/applier.go index 72838be74830..e4ae411d7085 100644 --- a/lifecycle/test/e2e/applier.go +++ b/lifecycle/test/e2e/applier.go @@ -21,22 +21,16 @@ import ( "strings" "time" - "github.com/labring/sealos/test/e2e/suites/operators" - - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/labring/sealos/pkg/types/v1beta1" - - cmd2 "github.com/labring/sealos/test/e2e/testhelper/cmd" - "github.com/labring/sealos/test/e2e/testhelper/kube" - - "sigs.k8s.io/yaml" - "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/retry" - + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/terraform" + cmd2 "github.com/labring/sealos/test/e2e/testhelper/cmd" + "github.com/labring/sealos/test/e2e/testhelper/kube" "github.com/labring/sealos/test/e2e/testhelper/settings" + "github.com/labring/sealos/test/e2e/testhelper/utils" + "sigs.k8s.io/yaml" ) type Applier struct { @@ -84,36 +78,36 @@ func NewApplier(infra *terraform.InfraDetail) (*Applier, error) { } } if err := a.WaitSSHReady(); err != nil { - return nil, fmt.Errorf("wait for ssh ready: %v", err) + return nil, fmt.Errorf("wait for ssh ready: %w", err) } a.Infra.Nodes = append([]terraform.Host{*a.Infra.Public}, a.Infra.Nodes...) if err := a.initImage(); err != nil { - return nil, fmt.Errorf("init image: %v", err) + return nil, fmt.Errorf("init image: %w", err) } return a, nil } func (a *Applier) initImage() error { if err := a.RemoteCmd.Copy(settings.E2EConfig.SealosBinPath, settings.E2EConfig.SealosBinPath); err != nil { - return fmt.Errorf("copy sealos bin to remote: %v", err) + return fmt.Errorf("copy sealos bin to remote: %w", err) } if err := a.RemoteCmd.AsyncExec("chmod +x " + settings.E2EConfig.SealosBinPath); err != nil { - return fmt.Errorf("chmod sealos bin: %v", err) + return fmt.Errorf("chmod sealos bin: %w", err) } if a.ImageTar != "" { if err := a.RemoteCmd.Copy(a.ImageTar, a.ImageTar); err != nil { - return fmt.Errorf("copy image tar to remote: %v", err) + return fmt.Errorf("copy image tar to remote: %w", err) } if err := a.RemoteSealosCmd.ImageLoad(a.ImageTar); err != nil { - return fmt.Errorf("load image tar: %v", err) + return fmt.Errorf("load image tar: %w", err) } } else { if err := a.RemoteSealosCmd.ImagePull(&cmd2.PullOptions{ ImageRefs: []string{a.ImageName}, Quiet: true, }); err != nil { - return fmt.Errorf("pull image: %v", err) + return fmt.Errorf("pull image: %w", err) } } if a.PatchImageName == "" { @@ -121,20 +115,20 @@ func (a *Applier) initImage() error { } if a.PatchImageTar != "" { if err := a.RemoteCmd.Copy(a.PatchImageTar, a.PatchImageTar); err != nil { - return fmt.Errorf("copy patch image tar to remote: %v", err) + return fmt.Errorf("copy patch image tar to remote: %w", err) } if strings.HasSuffix(a.PatchImageTar, settings.GzSuffix) { if err := a.RemoteCmd.AsyncExec(fmt.Sprintf("gzip %s -d", a.PatchImageTar)); err != nil { - return fmt.Errorf("unzip patch image tar: %v", err) + return fmt.Errorf("unzip patch image tar: %w", err) } a.PatchImageTar = strings.TrimSuffix(a.PatchImageTar, settings.GzSuffix) } if err := a.RemoteSealosCmd.ImageLoad(a.PatchImageTar); err != nil { - return fmt.Errorf("load patch image tar: %v", err) + return fmt.Errorf("load patch image tar: %w", err) } images, err := operators.NewFakeImage(a.RemoteSealosCmd).ListImages(false) if err != nil { - return fmt.Errorf("list images: %v", err) + return fmt.Errorf("list images: %w", err) } logger.Info("images:", images) patchImageName := "" @@ -155,7 +149,7 @@ func (a *Applier) initImage() error { ImageRefs: []string{a.PatchImageName}, Quiet: true, }); err != nil { - return fmt.Errorf("pull patch image: %v", err) + return fmt.Errorf("pull patch image: %w", err) } } if err := a.RemoteSealosCmd.ImageMerge(&cmd2.MergeOptions{ @@ -163,7 +157,7 @@ func (a *Applier) initImage() error { ImageRefs: []string{a.ImageName, a.PatchImageName}, Tag: []string{a.ImageName}, }); err != nil { - return fmt.Errorf("merge image: %v", err) + return fmt.Errorf("merge image: %w", err) } return nil } @@ -177,15 +171,22 @@ func (a *Applier) FetchRemoteKubeConfig() { content, err := os.ReadFile(localConf) utils.CheckErr(err) - certOpts := &cmd2.CertOptions{Cluster: a.ClusterName, AltName: []string{a.Infra.Public.PublicIP}} + certOpts := &cmd2.CertOptions{ + Cluster: a.ClusterName, + AltName: []string{a.Infra.Public.PublicIP}, + } logger.Info("certOpts: %v", certOpts) /* issue: output: Error: open /root/.sealos/e2e_test/etc/kubeadm-init.yaml: no such file or directory exec not in master0 */ - //pattern := regexp.MustCompile(`(?m)^(\s+server:\s+).+$`) - //newData := pattern.ReplaceAllString(string(content), fmt.Sprintf("${1}%s:6443", a.EIp)) - newData := strings.Replace(string(content), "server: https://apiserver.cluster.local:6443", "server: https://"+a.Infra.Public.PublicIP+":6443", -1) + // pattern := regexp.MustCompile(`(?m)^(\s+server:\s+).+$`) + // newData := pattern.ReplaceAllString(string(content), fmt.Sprintf("${1}%s:6443", a.EIp)) + newData := strings.ReplaceAll( + string(content), + "server: https://apiserver.cluster.local:6443", + "server: https://"+a.Infra.Public.PublicIP+":6443", + ) utils.CheckErr(utils.WriteFile(localConf, []byte(newData))) utils.CheckErr(a.RemoteSealosCmd.Cert(certOpts)) time.Sleep(30 * time.Second) @@ -195,7 +196,10 @@ func (a *Applier) CheckNodeNum(num int) { notReady := make(map[string]struct{}) err := retry.Retry(5, 5*time.Second, func() error { var err error - a.k8sClient, err = kube.NewK8sClient(filepath.Join(a.TestDir, "kube", "admin.conf"), "https://"+a.Infra.Public.PublicIP+":6443") + a.k8sClient, err = kube.NewK8sClient( + filepath.Join(a.TestDir, "kube", "admin.conf"), + "https://"+a.Infra.Public.PublicIP+":6443", + ) if err != nil { return err } @@ -204,8 +208,8 @@ func (a *Applier) CheckNodeNum(num int) { return err } - //kubectl get nodes --no-headers=true | awk '$2 != "Ready" {print}' - //not_ready_nodes=$(kubectl get nodes --no-headers | awk '{ if ($2 != "Ready") print $1 }') + // kubectl get nodes --no-headers=true | awk '$2 != "Ready" {print}' + // not_ready_nodes=$(kubectl get nodes --no-headers | awk '{ if ($2 != "Ready") print $1 }') for _, node := range nodes.Items { for _, condition := range node.Status.Conditions { if condition.Reason != "KubeletReady" { @@ -238,11 +242,11 @@ func (a *Applier) CheckNodeNum(num int) { func (a *Applier) WaitSSHReady() error { var err error - for i := 0; i < 10; i++ { + for i := range 10 { if err = a.RemoteCmd.AsyncExec("date"); err == nil { return nil } time.Sleep(time.Duration(i) * time.Second) } - return fmt.Errorf("wait for host %s ssh ready timeout: %v", a.Infra.Public.PublicIP, err) + return fmt.Errorf("wait for host %s ssh ready timeout: %w", a.Infra.Public.PublicIP, err) } diff --git a/lifecycle/test/e2e/apply_docker_test.go b/lifecycle/test/e2e/apply_docker_test.go index 0008a69a9391..876ab57d021f 100644 --- a/lifecycle/test/e2e/apply_docker_test.go +++ b/lifecycle/test/e2e/apply_docker_test.go @@ -19,17 +19,12 @@ package e2e import ( "fmt" + "github.com/labring/sealos/test/e2e/suites/checkers" + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testdata/kubeadm" - + "github.com/labring/sealos/test/e2e/testhelper/config" "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - . "github.com/onsi/ginkgo/v2" - - "github.com/labring/sealos/test/e2e/testhelper/config" - - "github.com/labring/sealos/test/e2e/suites/checkers" ) var _ = Describe("E2E_sealos_apply_docker_test", func() { @@ -45,7 +40,6 @@ var _ = Describe("E2E_sealos_apply_docker_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to reset cluster for earch cluster: %v", err)) }) It("sealos apply single by docker", func() { - By("generate Clusterfile") clusterfileConfig := config.Clusterfile{ BinData: kubeadm.PackageName + "/docker-svc-sans.yaml", @@ -71,7 +65,6 @@ var _ = Describe("E2E_sealos_apply_docker_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) It("sealos apply single by docker-buildimage", func() { - By("build image from dockerfile") kubeadmVar := ` apiVersion: kubeadm.k8s.io/v1beta2 @@ -87,15 +80,22 @@ networking: var tmpdir string tmpdir, err = dFile.Write() utils.CheckErr(err, fmt.Sprintf("failed to create dockerfile: %v", err)) - err = fakeClient.Image.BuildImage("apply-hack-docker:kubeadm-network", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - SaveImage: true, - }) + err = fakeClient.Image.BuildImage( + "apply-hack-docker:kubeadm-network", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + SaveImage: true, + }, + ) utils.CheckErr(err, fmt.Sprintf("failed to build image: %v", err)) By("generate Clusterfile") clusterfileConfig := config.Clusterfile{ - BinData: kubeadm.PackageName + "/custome-docker-svc.yaml", - Replaces: map[string]string{"127.0.0.1": utils.GetLocalIpv4(), "labring/kubernetes-docker:v1.25.0": "apply-hack-docker:kubeadm-network"}, + BinData: kubeadm.PackageName + "/custome-docker-svc.yaml", + Replaces: map[string]string{ + "127.0.0.1": utils.GetLocalIpv4(), + "labring/kubernetes-docker:v1.25.0": "apply-hack-docker:kubeadm-network", + }, } applyfile, err := clusterfileConfig.Write() utils.CheckErr(err, fmt.Sprintf("failed to write file %s: %v", applyfile, err)) @@ -116,5 +116,4 @@ networking: utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/apply_other_test.go b/lifecycle/test/e2e/apply_other_test.go index ba66c635a1f0..c7d908941612 100644 --- a/lifecycle/test/e2e/apply_other_test.go +++ b/lifecycle/test/e2e/apply_other_test.go @@ -18,19 +18,15 @@ package e2e import ( "fmt" + "net" - "github.com/labring/sealos/test/e2e/testdata/kubeadm" - - "github.com/labring/sealos/test/e2e/testhelper/utils" - + "github.com/labring/sealos/test/e2e/suites/checkers" "github.com/labring/sealos/test/e2e/suites/operators" - + "github.com/labring/sealos/test/e2e/testdata/kubeadm" "github.com/labring/sealos/test/e2e/testhelper/config" "github.com/labring/sealos/test/e2e/testhelper/etcd" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" - - "github.com/labring/sealos/test/e2e/suites/checkers" ) var _ = Describe("E2E_sealos_apply_other_test", func() { @@ -92,7 +88,7 @@ var _ = Describe("E2E_sealos_apply_other_test", func() { PodCIDR: "", ServiceCIDR: "100.56.0.0/16", Images: clusterfileConfig.Cluster.Spec.Image, - Etcd: []string{fmt.Sprintf("http://%s:2379", utils.GetLocalIpv4())}, + Etcd: []string{"http://" + net.JoinHostPort(utils.GetLocalIpv4(), "2379")}, } fakeCheckInterface, err = checkers.NewFakeGroupClient("default", opts) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) @@ -100,5 +96,4 @@ var _ = Describe("E2E_sealos_apply_other_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/apply_test.go b/lifecycle/test/e2e/apply_test.go index 2aa771ec35c0..6348fd50770c 100644 --- a/lifecycle/test/e2e/apply_test.go +++ b/lifecycle/test/e2e/apply_test.go @@ -19,17 +19,12 @@ package e2e import ( "fmt" + "github.com/labring/sealos/test/e2e/suites/checkers" + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testdata/kubeadm" - + "github.com/labring/sealos/test/e2e/testhelper/config" "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - . "github.com/onsi/ginkgo/v2" - - "github.com/labring/sealos/test/e2e/testhelper/config" - - "github.com/labring/sealos/test/e2e/suites/checkers" ) var _ = Describe("E2E_sealos_apply_test", func() { @@ -45,7 +40,6 @@ var _ = Describe("E2E_sealos_apply_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to reset cluster for earch cluster: %v", err)) }) It("sealos apply single by containerd", func() { - By("generate Clusterfile") clusterfileConfig := config.Clusterfile{ BinData: kubeadm.PackageName + "/containerd-svc-sans.yaml", @@ -71,7 +65,6 @@ var _ = Describe("E2E_sealos_apply_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) It("sealos apply single by containerd-buildimage", func() { - By("build image from dockerfile") kubeadmVar := ` apiVersion: kubeadm.k8s.io/v1beta2 @@ -87,16 +80,23 @@ networking: var tmpdir string tmpdir, err = dFile.Write() utils.CheckErr(err, fmt.Sprintf("failed to create dockerfile: %v", err)) - err = fakeClient.Image.BuildImage("apply-hack-containerd:kubeadm-network", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - SaveImage: true, - }) + err = fakeClient.Image.BuildImage( + "apply-hack-containerd:kubeadm-network", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + SaveImage: true, + }, + ) utils.CheckErr(err, fmt.Sprintf("failed to build image: %v", err)) By("generate Clusterfile") clusterfileConfig := config.Clusterfile{ - BinData: kubeadm.PackageName + "/custome-containerd-svc.yaml", - Replaces: map[string]string{"127.0.0.1": utils.GetLocalIpv4(), "labring/kubernetes:v1.25.0": "apply-hack-containerd:kubeadm-network"}, + BinData: kubeadm.PackageName + "/custome-containerd-svc.yaml", + Replaces: map[string]string{ + "127.0.0.1": utils.GetLocalIpv4(), + "labring/kubernetes:v1.25.0": "apply-hack-containerd:kubeadm-network", + }, } applyfile, err := clusterfileConfig.Write() utils.CheckErr(err, fmt.Sprintf("failed to write file %s: %v", applyfile, err)) @@ -116,5 +116,4 @@ networking: utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/cert_test.go b/lifecycle/test/e2e/cert_test.go index c3ccfb6d1695..94dc26861ca7 100644 --- a/lifecycle/test/e2e/cert_test.go +++ b/lifecycle/test/e2e/cert_test.go @@ -20,14 +20,10 @@ import ( "fmt" "time" + "github.com/labring/sealos/test/e2e/suites/checkers" + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/cmd" - "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - - "github.com/labring/sealos/test/e2e/suites/checkers" - . "github.com/onsi/ginkgo/v2" ) @@ -44,37 +40,67 @@ var _ = Describe("E2E_sealos_cert_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to reset cluster for single: %v", err)) }) It("sealos cert suit v1.25", func() { - images := []string{"labring/kubernetes:v1.25.0", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "labring/kubernetes:v1.25.0", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) err = fakeClient.Cert.AddDomain("test.sealoshub.io") - utils.CheckErr(err, fmt.Sprintf("failed to generate new cert for domain(test.sealoshub.io): %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to generate new cert for domain(test.sealoshub.io): %v", err), + ) time.Sleep(10 * time.Second) - fakeCheckInterface, err = checkers.NewFakeGroupClient("", &checkers.FakeOpts{CertDomain: "test.sealoshub.io"}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "", + &checkers.FakeOpts{CertDomain: "test.sealoshub.io"}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) It("sealos cert suit v1.28", func() { - images := []string{"labring/kubernetes:v1.28.0", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "labring/kubernetes:v1.28.0", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) err = fakeClient.Cert.AddDomain("test.sealoshub.io") - utils.CheckErr(err, fmt.Sprintf("failed to generate new cert for domain(test.sealoshub.io): %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to generate new cert for domain(test.sealoshub.io): %v", err), + ) time.Sleep(10 * time.Second) - fakeCheckInterface, err = checkers.NewFakeGroupClient("", &checkers.FakeOpts{CertDomain: "test.sealoshub.io"}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "", + &checkers.FakeOpts{CertDomain: "test.sealoshub.io"}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) It("sealos cert suit v1.31", func() { - images := []string{"labring/kubernetes:v1.31.0", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "labring/kubernetes:v1.31.0", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) err = fakeClient.Cert.AddDomain("test.sealoshub.io") - utils.CheckErr(err, fmt.Sprintf("failed to generate new cert for domain(test.sealoshub.io): %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to generate new cert for domain(test.sealoshub.io): %v", err), + ) time.Sleep(10 * time.Second) - fakeCheckInterface, err = checkers.NewFakeGroupClient("", &checkers.FakeOpts{CertDomain: "test.sealoshub.io"}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "", + &checkers.FakeOpts{CertDomain: "test.sealoshub.io"}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) diff --git a/lifecycle/test/e2e/e2e_test.go b/lifecycle/test/e2e/e2e_test.go index bea858256aee..75a8004da19f 100644 --- a/lifecycle/test/e2e/e2e_test.go +++ b/lifecycle/test/e2e/e2e_test.go @@ -17,10 +17,9 @@ package e2e import ( "testing" + "github.com/labring/sealos/test/e2e/testhelper/settings" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - - "github.com/labring/sealos/test/e2e/testhelper/settings" ) func TestSealosTest(t *testing.T) { @@ -29,8 +28,8 @@ func TestSealosTest(t *testing.T) { } var _ = SynchronizedBeforeSuite(func() []byte { - //check sealos bin exist - //exec.CheckCmdIsExist() + // check sealos bin exist + // exec.CheckCmdIsExist() SetDefaultEventuallyTimeout(settings.E2EConfig.WaitTime) return nil }, func(data []byte) { diff --git a/lifecycle/test/e2e/filesystem_test.go b/lifecycle/test/e2e/filesystem_test.go index 92832f2bdce5..0d6abdc21b03 100644 --- a/lifecycle/test/e2e/filesystem_test.go +++ b/lifecycle/test/e2e/filesystem_test.go @@ -21,14 +21,11 @@ import ( "os" "path" - "github.com/labring/sealos/test/e2e/testhelper/cmd" - + "github.com/labring/sealos/test/e2e/suites/checkers" "github.com/labring/sealos/test/e2e/suites/operators" + "github.com/labring/sealos/test/e2e/testhelper/cmd" "github.com/labring/sealos/test/e2e/testhelper/config" "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/checkers" - . "github.com/onsi/ginkgo/v2" ) @@ -55,9 +52,13 @@ var _ = Describe("E2E_sealos_filesystem_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to copy sealctl to rootfs: %v", err)) By("build image") - err = fakeClient.Image.BuildImage("kubernetes-filesystem:v1.25.0", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - }) + err = fakeClient.Image.BuildImage( + "kubernetes-filesystem:v1.25.0", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + }, + ) utils.CheckErr(err, fmt.Sprintf("failed to build image: %v", err)) }) AfterEach(func() { @@ -94,5 +95,4 @@ var _ = Describe("E2E_sealos_filesystem_test", func() { _ = os.Unsetenv("SEALOS_REGISTRY_SYNC_EXPERIMENTAL") }) }) - }) diff --git a/lifecycle/test/e2e/image_cri_shim_test.go b/lifecycle/test/e2e/image_cri_shim_test.go index 6d50235b8720..7a766c8862f6 100644 --- a/lifecycle/test/e2e/image_cri_shim_test.go +++ b/lifecycle/test/e2e/image_cri_shim_test.go @@ -20,22 +20,18 @@ import ( "sync" "time" - "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/yaml" - - "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - "github.com/labring/image-cri-shim/pkg/server" shimType "github.com/labring/image-cri-shim/pkg/types" - "github.com/onsi/ginkgo/v2" - k8sv1 "k8s.io/cri-api/pkg/apis/runtime/v1" - "github.com/labring/sealos/pkg/utils/exec" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/test/e2e/suites/image" + "github.com/labring/sealos/test/e2e/suites/operators" + "github.com/labring/sealos/test/e2e/testhelper/utils" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + k8sv1 "k8s.io/cri-api/pkg/apis/runtime/v1" + "sigs.k8s.io/yaml" ) const ( @@ -49,6 +45,7 @@ var defaultImageListingBenchmarkImages = []string{ defaultImageListingPrefix + "busybox:1-musl", "docker.io/library/busybox:1.28", } + var defaultImageNotExsitBenchmarkImages = []string{ "docker.io/library/kubernetes:v1.23.8", } @@ -59,7 +56,6 @@ const ( ) var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { - var ( imageShimService image.FakeImageCRIShimInterface clt server.Client @@ -87,7 +83,9 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { shimInitErr = fmt.Errorf("failed to preprocess shim config: %w", err) return } - clt, shimInitErr = server.NewClient(server.CRIClientOptions{ImageSocket: shimConfig.ImageShimSocket}) + clt, shimInitErr = server.NewClient( + server.CRIClientOptions{ImageSocket: shimConfig.ImageShimSocket}, + ) if shimInitErr != nil { shimInitErr = fmt.Errorf("failed to create shim client: %w", shimInitErr) return @@ -97,16 +95,18 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { shimInitErr = fmt.Errorf("failed to connect shim client: %w", err) return } - imageShimService = image.NewFakeImageServiceClientWithV1(k8sv1.NewImageServiceClient(gCon)) + imageShimService = image.NewFakeImageServiceClientWithV1( + k8sv1.NewImageServiceClient(gCon), + ) }) utils.CheckErr(shimInitErr, "failed to initialize image shim client") } - var listTestCases = func() { + listTestCases := func() { images, err := imageShimService.ListImages() utils.CheckErr(err, fmt.Sprintf("failed to list images: %v", err)) logger.Info("list images: %v", images) } - var pullTestCases = func() { + pullTestCases := func() { for _, image := range defaultImageListingBenchmarkImages { id, err := imageShimService.PullImage(image) utils.CheckErr(err, fmt.Sprintf("failed to pull image %s: %v", image, err)) @@ -114,7 +114,7 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { } } - var statusExitImagesTestCases = func() { + statusExitImagesTestCases := func() { for _, imageName := range defaultImageListingBenchmarkImages { img, err := imageShimService.ImageStatus(imageName) utils.CheckErr(err, fmt.Sprintf("failed to get image %s status: %v", imageName, err)) @@ -125,7 +125,7 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { } } - var pullAgainImagesTestCases = func() { + pullAgainImagesTestCases := func() { err = fakeClient.CmdInterface.AsyncExec("crictl", "images") utils.CheckErr(err) for _, imageName := range defaultImageListingBenchmarkImages { @@ -138,7 +138,7 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { err = fakeClient.CmdInterface.AsyncExec("crictl", "images") utils.CheckErr(err) } - var statusNotExitImagesTestCases = func() { + statusNotExitImagesTestCases := func() { for _, imageName := range defaultImageNotExsitBenchmarkImages { img, err := imageShimService.ImageStatus(imageName) utils.CheckErr(err, fmt.Sprintf("failed to get image %s status: %v", imageName, err)) @@ -150,7 +150,7 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { } } - var removeTestCases = func() { + removeTestCases := func() { for _, imageName := range defaultImageListingBenchmarkImages { err := imageShimService.RemoveImage(imageName) utils.CheckErr(err, fmt.Sprintf("failed to remove image %s: %v", imageName, err)) @@ -167,29 +167,54 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { } } - var removeByIDTestCases = func() { + removeByIDTestCases := func() { id, err := imageShimService.PullImage("docker.io/labring/kubernetes-docker:v1.23.8") - utils.CheckErr(err, fmt.Sprintf("failed to pull image %s: %v", "docker.io/labring/kubernetes-docker:v1.23.8", err)) - logger.Info("pull images %s success, return image id %s \n", "docker.io/labring/kubernetes-docker:v1.23.8", id) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to pull image %s: %v", + "docker.io/labring/kubernetes-docker:v1.23.8", + err, + ), + ) + logger.Info( + "pull images %s success, return image id %s \n", + "docker.io/labring/kubernetes-docker:v1.23.8", + id, + ) err = imageShimService.RemoveImage(id) utils.CheckErr(err, fmt.Sprintf("failed to remove image %s: %v", id, err)) logger.Info("remove images %s success \n", id) } - var statusByIDTestCases = func() { + statusByIDTestCases := func() { id, err := imageShimService.PullImage("docker.io/labring/kubernetes-docker:v1.23.8") - utils.CheckErr(err, fmt.Sprintf("failed to pull image %s: %v", "docker.io/labring/kubernetes-docker:v1.23.8", err)) - logger.Info("pull images %s success, return image id %s \n", "docker.io/labring/kubernetes-docker:v1.23.8", id) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to pull image %s: %v", + "docker.io/labring/kubernetes-docker:v1.23.8", + err, + ), + ) + logger.Info( + "pull images %s success, return image id %s \n", + "docker.io/labring/kubernetes-docker:v1.23.8", + id, + ) img, err := imageShimService.ImageStatus(id) utils.CheckErr(err, fmt.Sprintf("failed to get image %s status: %v", id, err)) if img == nil || img.Image == nil || img.Image.Id == "" { - utils.CheckErr(errors.New("image is not found"), fmt.Sprintf("failed to get image %s status: %+v", id, img)) + utils.CheckErr( + errors.New("image is not found"), + fmt.Sprintf("failed to get image %s status: %+v", id, img), + ) } else { logger.Info("test get images by id %s success\n", id) } } - var fsInfoTestCases = func() { + fsInfoTestCases := func() { fss, err := imageShimService.ImageFsInfo() utils.CheckErr(err, fmt.Sprintf("failed to get image fs info: %v", err)) ginkgo.By("success get fs info: ") @@ -199,12 +224,16 @@ var _ = ginkgo.Describe("E2E_image-cri-shim_run_test", func() { } ginkgo.Context("install cluster using hack image shim", func() { ginkgo.It("init cluster", func() { - //checkout image-cri-shim status running + // checkout image-cri-shim status running sealFile := `FROM labring/kubernetes:v1.25.0 COPY image-cri-shim cri` err = utils.WriteFile("Dockerfile", []byte(sealFile)) utils.CheckErr(err) - err = fakeClient.Image.BuildImage("kubernetes-hack:v1.25.0", ".", operators.BuildOptions{}) + err = fakeClient.Image.BuildImage( + "kubernetes-hack:v1.25.0", + ".", + operators.BuildOptions{}, + ) utils.CheckErr(err) err = fakeClient.Cluster.Run("kubernetes-hack:v1.25.0") utils.CheckErr(err) @@ -243,7 +272,15 @@ COPY image-cri-shim cri` waitForShimLog("reloaded shim auth configuration", restoreSince, 60*time.Second) }() - _, _ = fakeClient.CmdInterface.Exec("kubectl", "-n", "kube-system", "delete", "configmap", "image-cri-shim", "--ignore-not-found=true") + _, _ = fakeClient.CmdInterface.Exec( + "kubectl", + "-n", + "kube-system", + "delete", + "configmap", + "image-cri-shim", + "--ignore-not-found=true", + ) configMapManifest := fmt.Sprintf(`apiVersion: v1 kind: ConfigMap @@ -262,13 +299,16 @@ data: defer func() { utils.RemoveTempFile(configMapFile) }() - utils.CheckErr(utils.WriteFile(configMapFile, []byte(configMapManifest)), "failed to write ConfigMap manifest") + utils.CheckErr( + utils.WriteFile(configMapFile, []byte(configMapManifest)), + "failed to write ConfigMap manifest", + ) _, err := fakeClient.CmdInterface.Exec("kubectl", "apply", "-f", configMapFile) utils.CheckErr(err, "failed to apply image-cri-shim ConfigMap") gomega.Eventually(func() string { - out, err := exec.RunSimpleCmd(fmt.Sprintf("sudo cat %s", DefaultImageCRIShimConfig)) + out, err := exec.RunSimpleCmd("sudo cat " + DefaultImageCRIShimConfig) if err != nil { return "" } @@ -276,7 +316,7 @@ data: }, 90*time.Second, 3*time.Second).Should(gomega.ContainSubstring(mirrorAddress)) gomega.Eventually(func() string { - out, err := exec.RunSimpleCmd(fmt.Sprintf("sudo cat %s", DefaultImageCRIShimConfig)) + out, err := exec.RunSimpleCmd("sudo cat " + DefaultImageCRIShimConfig) if err != nil { return "" } @@ -290,7 +330,10 @@ data: utils.CheckErr(err, fmt.Sprintf("failed to pull %s: %v", sourceImage, err)) logger.Info("crictl pull output: %s", string(pullOut)) _, err = fakeClient.CmdInterface.Exec("crictl", "inspecti", rewrittenImage) - utils.CheckErr(err, fmt.Sprintf("rewritten image %s not found in cri store", rewrittenImage)) + utils.CheckErr( + err, + fmt.Sprintf("rewritten image %s not found in cri store", rewrittenImage), + ) }) ginkgo.It("allows pulling through registry mirror", func() { @@ -307,7 +350,10 @@ data: cfgCopy := *cfg cfgCopy.ReloadInterval = metav1.Duration{Duration: time.Second} - cfgCopy.Registries = append(cfgCopy.Registries, shimType.Registry{Address: mirrorAddress}) + cfgCopy.Registries = append( + cfgCopy.Registries, + shimType.Registry{Address: mirrorAddress}, + ) payload, err := yaml.Marshal(cfgCopy) utils.CheckErr(err, "failed to marshal shim config with mirror") @@ -325,22 +371,32 @@ data: utils.CheckErr(err, fmt.Sprintf("failed to pull %s: %v", sourceImage, err)) logger.Info("crictl pull output: %s", string(pullOut)) _, err = fakeClient.CmdInterface.Exec("crictl", "inspecti", rewrittenImage) - utils.CheckErr(err, fmt.Sprintf("rewritten image %s not found in cri store", rewrittenImage)) + utils.CheckErr( + err, + fmt.Sprintf("rewritten image %s not found in cri store", rewrittenImage), + ) }) }) }) -const shimJournalTimeLayout = "2006-01-02 15:04:05" +const shimJournalTimeLayout = time.DateTime func writeShimConfig(data []byte) { - cmd := fmt.Sprintf("cat <<'EOF' | sudo tee %s >/dev/null\n%s\nEOF", DefaultImageCRIShimConfig, string(data)) + cmd := fmt.Sprintf( + "cat <<'EOF' | sudo tee %s >/dev/null\n%s\nEOF", + DefaultImageCRIShimConfig, + string(data), + ) _, err := exec.RunSimpleCmd(cmd) utils.CheckErr(err, "failed to write shim config") } func waitForShimLog(fragment string, since time.Time, timeout time.Duration) { gomega.Eventually(func() string { - cmd := fmt.Sprintf("sudo journalctl -u image-cri-shim --since \"%s\" --no-pager", since.Add(-60*time.Second).Format(shimJournalTimeLayout)) + cmd := fmt.Sprintf( + "sudo journalctl -u image-cri-shim --since \"%s\" --no-pager", + since.Add(-60*time.Second).Format(shimJournalTimeLayout), + ) out, err := exec.RunSimpleCmd(cmd) if err != nil { return "" diff --git a/lifecycle/test/e2e/images_buildrun_feature_test.go b/lifecycle/test/e2e/images_buildrun_feature_test.go index 4ee1fcdbcf71..4272149d8abc 100644 --- a/lifecycle/test/e2e/images_buildrun_feature_test.go +++ b/lifecycle/test/e2e/images_buildrun_feature_test.go @@ -21,14 +21,10 @@ import ( "os" "path" - "github.com/labring/sealos/test/e2e/testhelper/cmd" - - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/labring/sealos/test/e2e/suites/operators" - + "github.com/labring/sealos/test/e2e/testhelper/cmd" "github.com/labring/sealos/test/e2e/testhelper/config" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" ) @@ -39,7 +35,10 @@ var _ = Describe("E2E_sealos_images_buildrun_feature_test", func() { By("write dockerfile") _ = os.Setenv("SEALOS_REGISTRY_SYNC_EXPERIMENTAL", "true") dFile := config.RootfsDockerfile{ - Images: []string{"docker.io/altinity/clickhouse-operator:0.18.4", "docker.io/altinity/metrics-exporter:0.18.4"}, + Images: []string{ + "docker.io/altinity/clickhouse-operator:0.18.4", + "docker.io/altinity/metrics-exporter:0.18.4", + }, BaseImage: "docker.io/labring/kubernetes:v1.25.0", Copys: []string{"sealctl opt/"}, } @@ -51,12 +50,20 @@ var _ = Describe("E2E_sealos_images_buildrun_feature_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to copy sealctl to rootfs: %v", err)) By("build image") - err = fakeClient.Image.BuildImage("test-build-image:rootfs-sealctl", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - }) + err = fakeClient.Image.BuildImage( + "test-build-image:rootfs-sealctl", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + }, + ) utils.CheckErr(err) - images := []string{"test-build-image:rootfs-sealctl", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "test-build-image:rootfs-sealctl", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } defer func() { err = fakeClient.Cluster.Reset() utils.CheckErr(err, fmt.Sprintf("failed to reset cluster run: %v", err)) @@ -66,12 +73,23 @@ var _ = Describe("E2E_sealos_images_buildrun_feature_test", func() { err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to run images %v: %v", images, err)) err = fakeClient.CRI.Pull("docker.io/altinity/clickhouse-operator:0.18.4") - utils.CheckErr(err, fmt.Sprintf("failed to pull image docker.io/altinity/clickhouse-operator:0.18.4: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to pull image docker.io/altinity/clickhouse-operator:0.18.4: %v", + err, + ), + ) _, err = fakeClient.CRI.ImageList() utils.CheckErr(err, fmt.Sprintf("failed to list images: %v", err)) err = fakeClient.CRI.HasImage("sealos.hub:5000/altinity/clickhouse-operator:0.18.4") - utils.CheckErr(err, fmt.Sprintf("failed to validate image sealos.hub:5000/altinity/clickhouse-operator:0.18.4: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to validate image sealos.hub:5000/altinity/clickhouse-operator:0.18.4: %v", + err, + ), + ) }) - }) }) diff --git a/lifecycle/test/e2e/images_buildrun_test.go b/lifecycle/test/e2e/images_buildrun_test.go index b211403e3288..399314c2e44b 100644 --- a/lifecycle/test/e2e/images_buildrun_test.go +++ b/lifecycle/test/e2e/images_buildrun_test.go @@ -20,12 +20,9 @@ import ( "fmt" "path" - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/labring/sealos/test/e2e/suites/operators" - "github.com/labring/sealos/test/e2e/testhelper/config" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" ) @@ -33,10 +30,12 @@ var _ = Describe("E2E_sealos_images_buildrun_test", func() { fakeClient := operators.NewFakeClient("") Context("sealos images build and run suit", func() { It("images build image running cluster", func() { - By("write dockerfile") dFile := config.RootfsDockerfile{ - Images: []string{"docker.io/altinity/clickhouse-operator:0.18.4", "docker.io/altinity/metrics-exporter:0.18.4"}, + Images: []string{ + "docker.io/altinity/clickhouse-operator:0.18.4", + "docker.io/altinity/metrics-exporter:0.18.4", + }, BaseImage: "docker.io/labring/kubernetes:v1.25.0", Copys: []string{"sealctl opt/"}, } @@ -48,12 +47,20 @@ var _ = Describe("E2E_sealos_images_buildrun_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to copy sealctl to rootfs: %v", err)) By("build image") - err = fakeClient.Image.BuildImage("test-build-image:rootfs-sealctl", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - }) + err = fakeClient.Image.BuildImage( + "test-build-image:rootfs-sealctl", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + }, + ) utils.CheckErr(err) - images := []string{"test-build-image:rootfs-sealctl", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "test-build-image:rootfs-sealctl", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } defer func() { err = fakeClient.Cluster.Reset() utils.CheckErr(err, fmt.Sprintf("failed to reset cluster run: %v", err)) @@ -61,12 +68,23 @@ var _ = Describe("E2E_sealos_images_buildrun_test", func() { err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to run images %v: %v", images, err)) err = fakeClient.CRI.Pull("docker.io/altinity/clickhouse-operator:0.18.4") - utils.CheckErr(err, fmt.Sprintf("failed to pull image docker.io/altinity/clickhouse-operator:0.18.4: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to pull image docker.io/altinity/clickhouse-operator:0.18.4: %v", + err, + ), + ) _, err = fakeClient.CRI.ImageList() utils.CheckErr(err, fmt.Sprintf("failed to list images: %v", err)) err = fakeClient.CRI.HasImage("sealos.hub:5000/altinity/clickhouse-operator:0.18.4") - utils.CheckErr(err, fmt.Sprintf("failed to validate image sealos.hub:5000/altinity/clickhouse-operator:0.18.4: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to validate image sealos.hub:5000/altinity/clickhouse-operator:0.18.4: %v", + err, + ), + ) }) - }) }) diff --git a/lifecycle/test/e2e/images_test.go b/lifecycle/test/e2e/images_test.go index 8ab70ee2c108..15b60fd34161 100644 --- a/lifecycle/test/e2e/images_test.go +++ b/lifecycle/test/e2e/images_test.go @@ -19,10 +19,8 @@ package e2e import ( "fmt" - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/labring/sealos/test/e2e/suites/operators" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) @@ -36,34 +34,73 @@ var _ = Describe("E2E_sealos_images_test", func() { Context("sealos images basic suit", func() { It("images pull", func() { err = fakeClient.Image.PullImage("docker.io/labring/kubernetes:v1.20.0") - utils.CheckErr(err, fmt.Sprintf("failed to pull image docker.io/labring/kubernetes:v1.20.0: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to pull image docker.io/labring/kubernetes:v1.20.0: %v", err), + ) id, err := fakeClient.Image.FetchImageID("docker.io/labring/kubernetes:v1.20.0") - utils.CheckErr(err, fmt.Sprintf("failed to fetch image id docker.io/labring/kubernetes:v1.20.0: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to fetch image id docker.io/labring/kubernetes:v1.20.0: %v", + err, + ), + ) Expect(id).NotTo(BeEmpty()) }) It("images create local image", func() { _, err = fakeClient.Image.Create("docker.io/labring/kubernetes:v1.20.0", false) - utils.CheckErr(err, fmt.Sprintf("failed to create image docker.io/labring/kubernetes:v1.20.0: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to create image docker.io/labring/kubernetes:v1.20.0: %v", err), + ) }) It("images create remote image", func() { _, err = fakeClient.Image.Create("docker.io/labring/kubernetes:v1.20.1", false) - utils.CheckErr(err, fmt.Sprintf("failed to create image docker.io/labring/kubernetes:v1.20.1: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to create image docker.io/labring/kubernetes:v1.20.1: %v", err), + ) }) It("images create remote image by short", func() { _, err = fakeClient.Image.Create("docker.io/labring/kubernetes:v1.20.2", true) - utils.CheckErr(err, fmt.Sprintf("failed to create image docker.io/labring/kubernetes:v1.20.2: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to create image docker.io/labring/kubernetes:v1.20.2: %v", err), + ) }) It("images more image", func() { - err = fakeClient.Image.PullImage("docker.io/labring/kubernetes:v1.20.3", "labring/helm:v3.8.2") - utils.CheckErr(err, fmt.Sprintf("failed to pull image docker.io/labring/kubernetes:v1.20.3 labring/helm:v3.8.2: %v", err)) + err = fakeClient.Image.PullImage( + "docker.io/labring/kubernetes:v1.20.3", + "labring/helm:v3.8.2", + ) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to pull image docker.io/labring/kubernetes:v1.20.3 labring/helm:v3.8.2: %v", + err, + ), + ) }) It("images rm more image", func() { - err = fakeClient.Image.RemoveImage("docker.io/labring/kubernetes:v1.20.3", "labring/helm:v3.8.2") - utils.CheckErr(err, fmt.Sprintf("failed to remove image docker.io/labring/kubernetes:v1.20.3 labring/helm:v3.8.2: %v", err)) + err = fakeClient.Image.RemoveImage( + "docker.io/labring/kubernetes:v1.20.3", + "labring/helm:v3.8.2", + ) + utils.CheckErr( + err, + fmt.Sprintf( + "failed to remove image docker.io/labring/kubernetes:v1.20.3 labring/helm:v3.8.2: %v", + err, + ), + ) }) It("images save image", func() { err = fakeClient.Image.SaveImage("docker.io/labring/kubernetes:v1.20.1", "k8s.tar") - utils.CheckErr(err, fmt.Sprintf("failed to save image docker.io/labring/kubernetes:v1.20.1: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to save image docker.io/labring/kubernetes:v1.20.1: %v", err), + ) }) It("images load image", func() { err = fakeClient.Image.LoadImage("k8s.tar") @@ -71,9 +108,16 @@ var _ = Describe("E2E_sealos_images_test", func() { }) It("images SaveMultiImage", func() { - err = fakeClient.Image.PullImage("docker.io/labring/kubernetes:v1.20.1", "labring/helm:v3.8.2") + err = fakeClient.Image.PullImage( + "docker.io/labring/kubernetes:v1.20.1", + "labring/helm:v3.8.2", + ) utils.CheckErr(err, fmt.Sprintf("failed to pull images: %v", err)) - err = fakeClient.Image.SaveMultiImage("k8s-multi.tar", "docker.io/labring/kubernetes:v1.20.1", "labring/helm:v3.8.2") + err = fakeClient.Image.SaveMultiImage( + "k8s-multi.tar", + "docker.io/labring/kubernetes:v1.20.1", + "labring/helm:v3.8.2", + ) utils.CheckErr(err, fmt.Sprintf("failed to SaveMultiImage : %v", err)) }) It("images load multi image", func() { @@ -82,11 +126,13 @@ var _ = Describe("E2E_sealos_images_test", func() { }) It("images merge image", func() { - err = fakeClient.Image.Merge("new:0.1.0", []string{"docker.io/labring/kubernetes:v1.20.1", "labring/helm:v3.8.2"}) + err = fakeClient.Image.Merge( + "new:0.1.0", + []string{"docker.io/labring/kubernetes:v1.20.1", "labring/helm:v3.8.2"}, + ) utils.CheckErr(err, fmt.Sprintf("failed to merge image new:0.1.0: %v", err)) _, err := fakeClient.Image.ListImages(true) utils.CheckErr(err, fmt.Sprintf("failed to list images: %v", err)) }) - }) }) diff --git a/lifecycle/test/e2e/inspect_test.go b/lifecycle/test/e2e/inspect_test.go index 7c33803c1da9..a2ffe8a68cb1 100644 --- a/lifecycle/test/e2e/inspect_test.go +++ b/lifecycle/test/e2e/inspect_test.go @@ -19,10 +19,8 @@ package e2e import ( "fmt" - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/labring/sealos/test/e2e/suites/operators" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" ) @@ -36,18 +34,26 @@ var _ = Describe("E2E_sealos_inspect_test", func() { It("inspect image", func() { By("sealos pull image") err = fakeClient.Image.PullImage("labring/kubernetes:v1.23.8") - utils.CheckErr(err, fmt.Sprintf("failed to pull image labring/kubernetes:v1.23.8: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to pull image labring/kubernetes:v1.23.8: %v", err), + ) By("sealos inspect local image") err = fakeClient.Inspect.LocalImage("labring/kubernetes:v1.23.8") - utils.CheckErr(err, fmt.Sprintf("failed to inspect local image labring/kubernetes:v1.23.8: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to inspect local image labring/kubernetes:v1.23.8: %v", err), + ) }) - }) Context("sealos remote image", func() { It("inspect image", func() { By("sealos inspect remote image") err = fakeClient.Inspect.RemoteImage("labring/kubernetes:v1.25.0") - utils.CheckErr(err, fmt.Sprintf("failed to inspect remote image labring/kubernetes:v1.25.0: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to inspect remote image labring/kubernetes:v1.25.0: %v", err), + ) }) }) @@ -63,9 +69,15 @@ var _ = Describe("E2E_sealos_inspect_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to save oci image alpine:3: %v", err)) By("sealos inspect archive image") err = fakeClient.Inspect.DockerArchiveImage(operators.DockerTarFile) - utils.CheckErr(err, fmt.Sprintf("failed to inspect docker archive image alpine:3: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to inspect docker archive image alpine:3: %v", err), + ) err = fakeClient.Inspect.OCIArchiveImage(operators.OCITarFile) - utils.CheckErr(err, fmt.Sprintf("failed to inspect oci archive image alpine:3: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to inspect oci archive image alpine:3: %v", err), + ) }) }) @@ -73,10 +85,12 @@ var _ = Describe("E2E_sealos_inspect_test", func() { It("inspect image", func() { By("sealos inspect image id") id, err := fakeClient.Image.FetchImageID("labring/kubernetes:v1.23.8") - utils.CheckErr(err, fmt.Sprintf("failed to fetch image id labring/kubernetes:v1.23.8: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to fetch image id labring/kubernetes:v1.23.8: %v", err), + ) err = fakeClient.Inspect.ImageID(id) utils.CheckErr(err, fmt.Sprintf("failed to inspect image id %s: %v", id, err)) }) }) - }) diff --git a/lifecycle/test/e2e/k3s_125_test.go b/lifecycle/test/e2e/k3s_125_test.go index 675042628166..fc89a98fca9e 100644 --- a/lifecycle/test/e2e/k3s_125_test.go +++ b/lifecycle/test/e2e/k3s_125_test.go @@ -24,17 +24,13 @@ import ( "time" "github.com/google/go-containerregistry/pkg/name" - - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/util/yaml" - "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/config" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/yaml" ) var _ = Describe("E2E_sealos_k3s_basic_test", func() { @@ -73,7 +69,16 @@ var _ = Describe("E2E_sealos_k3s_basic_test", func() { err = fakeClient.Cluster.Run("k3s:buildin") utils.CheckErr(err) fn := func() []byte { - data, err := fakeClient.CmdInterface.Exec("kubectl", "get", "pods", "-A", "--kubeconfig", "/etc/rancher/k3s/k3s.yaml", "-o", "yaml") + data, err := fakeClient.CmdInterface.Exec( + "kubectl", + "get", + "pods", + "-A", + "--kubeconfig", + "/etc/rancher/k3s/k3s.yaml", + "-o", + "yaml", + ) utils.CheckErr(err) return data } @@ -99,7 +104,13 @@ var _ = Describe("E2E_sealos_k3s_basic_test", func() { utils.CheckErr(errors.New("k3s pods is empty, for timeout")) } } - err = fakeClient.CmdInterface.AsyncExec("kubectl", "get", "nodes", "--kubeconfig", "/etc/rancher/k3s/k3s.yaml") + err = fakeClient.CmdInterface.AsyncExec( + "kubectl", + "get", + "nodes", + "--kubeconfig", + "/etc/rancher/k3s/k3s.yaml", + ) utils.CheckErr(err) displayImages, err := fakeClient.CRI.ImageList() utils.CheckErr(err) @@ -111,12 +122,16 @@ var _ = Describe("E2E_sealos_k3s_basic_test", func() { utils.CheckErr(err) logger.Info("image registry is %s", ref.Context().RegistryStr()) if ref.Context().RegistryStr() != "sealos.hub:5000" { - utils.CheckErr(fmt.Errorf("crictl image is not sealos.hub, %+v", strings.TrimSpace(tag))) + utils.CheckErr( + fmt.Errorf( + "crictl image is not sealos.hub, %+v", + strings.TrimSpace(tag), + ), + ) } } } } }) }) - }) diff --git a/lifecycle/test/e2e/k8s_122_test.go b/lifecycle/test/e2e/k8s_122_test.go index 08dcd80a9ba3..bc6bead5d018 100644 --- a/lifecycle/test/e2e/k8s_122_test.go +++ b/lifecycle/test/e2e/k8s_122_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_122_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_123_test.go b/lifecycle/test/e2e/k8s_123_test.go index 75b928b9d2ae..53936a4640c2 100644 --- a/lifecycle/test/e2e/k8s_123_test.go +++ b/lifecycle/test/e2e/k8s_123_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_123_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_124_test.go b/lifecycle/test/e2e/k8s_124_test.go index 3ba6f8b95212..5f7d8b620d78 100644 --- a/lifecycle/test/e2e/k8s_124_test.go +++ b/lifecycle/test/e2e/k8s_124_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_124_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_125_test.go b/lifecycle/test/e2e/k8s_125_test.go index 5c84eea60e00..ed0491ff1314 100644 --- a/lifecycle/test/e2e/k8s_125_test.go +++ b/lifecycle/test/e2e/k8s_125_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_125_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_126_test.go b/lifecycle/test/e2e/k8s_126_test.go index 68faf678e481..9244ebd9c4b8 100644 --- a/lifecycle/test/e2e/k8s_126_test.go +++ b/lifecycle/test/e2e/k8s_126_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_126_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_127_test.go b/lifecycle/test/e2e/k8s_127_test.go index bdbb55441de4..15f038f0a9e6 100644 --- a/lifecycle/test/e2e/k8s_127_test.go +++ b/lifecycle/test/e2e/k8s_127_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_127_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_128_test.go b/lifecycle/test/e2e/k8s_128_test.go index 25b31c5c8c23..720b98e77ddf 100644 --- a/lifecycle/test/e2e/k8s_128_test.go +++ b/lifecycle/test/e2e/k8s_128_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_128_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_129_test.go b/lifecycle/test/e2e/k8s_129_test.go index b7b4706f5791..48213dd99cdd 100644 --- a/lifecycle/test/e2e/k8s_129_test.go +++ b/lifecycle/test/e2e/k8s_129_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_129_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_130_test.go b/lifecycle/test/e2e/k8s_130_test.go index edab86e1f88a..4d8d3557d1ac 100644 --- a/lifecycle/test/e2e/k8s_130_test.go +++ b/lifecycle/test/e2e/k8s_130_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_130_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_131_test.go b/lifecycle/test/e2e/k8s_131_test.go index 4b6a6a0796e2..75b0cf5cb7fa 100644 --- a/lifecycle/test/e2e/k8s_131_test.go +++ b/lifecycle/test/e2e/k8s_131_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_131_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_132_test.go b/lifecycle/test/e2e/k8s_132_test.go index bacecd28cb5a..0e5b6d7ff5a1 100644 --- a/lifecycle/test/e2e/k8s_132_test.go +++ b/lifecycle/test/e2e/k8s_132_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_132_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_122_test.go b/lifecycle/test/e2e/k8s_docker_122_test.go index 3cc720e8b376..0e52434ad77c 100644 --- a/lifecycle/test/e2e/k8s_docker_122_test.go +++ b/lifecycle/test/e2e/k8s_docker_122_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_122_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_123_test.go b/lifecycle/test/e2e/k8s_docker_123_test.go index 1f828713fe94..1c6e6a389d35 100644 --- a/lifecycle/test/e2e/k8s_docker_123_test.go +++ b/lifecycle/test/e2e/k8s_docker_123_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_123_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_124_test.go b/lifecycle/test/e2e/k8s_docker_124_test.go index 625f520aabc1..585f3407bcbe 100644 --- a/lifecycle/test/e2e/k8s_docker_124_test.go +++ b/lifecycle/test/e2e/k8s_docker_124_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_124_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_125_test.go b/lifecycle/test/e2e/k8s_docker_125_test.go index 695d82a3955c..fe572be3ddbf 100644 --- a/lifecycle/test/e2e/k8s_docker_125_test.go +++ b/lifecycle/test/e2e/k8s_docker_125_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_125_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_126_test.go b/lifecycle/test/e2e/k8s_docker_126_test.go index cb91f200c097..d658b0864f9a 100644 --- a/lifecycle/test/e2e/k8s_docker_126_test.go +++ b/lifecycle/test/e2e/k8s_docker_126_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_126_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_127_test.go b/lifecycle/test/e2e/k8s_docker_127_test.go index b1322e56a330..838b915e7060 100644 --- a/lifecycle/test/e2e/k8s_docker_127_test.go +++ b/lifecycle/test/e2e/k8s_docker_127_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_127_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_128_test.go b/lifecycle/test/e2e/k8s_docker_128_test.go index 5bec1e0724fb..434b3b931eba 100644 --- a/lifecycle/test/e2e/k8s_docker_128_test.go +++ b/lifecycle/test/e2e/k8s_docker_128_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_128_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_129_test.go b/lifecycle/test/e2e/k8s_docker_129_test.go index 2a4893f8cf95..fd3417ff09d9 100644 --- a/lifecycle/test/e2e/k8s_docker_129_test.go +++ b/lifecycle/test/e2e/k8s_docker_129_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_129_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_130_test.go b/lifecycle/test/e2e/k8s_docker_130_test.go index b1f469774111..ac76ae685233 100644 --- a/lifecycle/test/e2e/k8s_docker_130_test.go +++ b/lifecycle/test/e2e/k8s_docker_130_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_130_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_131_test.go b/lifecycle/test/e2e/k8s_docker_131_test.go index 9000f319e785..62c8430c6ccb 100644 --- a/lifecycle/test/e2e/k8s_docker_131_test.go +++ b/lifecycle/test/e2e/k8s_docker_131_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_131_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/k8s_docker_132_test.go b/lifecycle/test/e2e/k8s_docker_132_test.go index 49e780707646..4c8545d8bacc 100644 --- a/lifecycle/test/e2e/k8s_docker_132_test.go +++ b/lifecycle/test/e2e/k8s_docker_132_test.go @@ -21,7 +21,6 @@ import ( "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" - . "github.com/onsi/ginkgo/v2" ) @@ -45,5 +44,4 @@ var _ = Describe("E2E_sealos_runtime_version_docker_132_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to check version image list: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/multi_node_test.go b/lifecycle/test/e2e/multi_node_test.go index 5d27840821fc..61f3c6f5fb0d 100644 --- a/lifecycle/test/e2e/multi_node_test.go +++ b/lifecycle/test/e2e/multi_node_test.go @@ -18,22 +18,17 @@ import ( "fmt" "os" - "github.com/labring/sealos/test/e2e/testhelper/settings" - + "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/test/e2e/terraform" - "github.com/labring/sealos/test/e2e/testhelper/utils" - cmd2 "github.com/labring/sealos/test/e2e/testhelper/cmd" - - "github.com/labring/sealos/pkg/utils/logger" - + "github.com/labring/sealos/test/e2e/testhelper/settings" + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" ) var _ = Describe("E2E_sealos_multi_node_test", func() { Context("start apply", func() { - - var tf = &terraform.Terraform{ + tf := &terraform.Terraform{ AccessKey: os.Getenv("ALIYUN_ACCESS_KEY_ID"), SecretKey: os.Getenv("ALIYUN_ACCESS_KEY_SECRET"), } @@ -81,53 +76,65 @@ var _ = Describe("E2E_sealos_multi_node_test", func() { }) applier.FetchRemoteKubeConfig() - //check result + // check result applier.CheckNodeNum(2) By("add nodes test", func() { // add ip3, ip4 addOpts := &cmd2.AddOptions{ Cluster: applier.ClusterName, - Nodes: []string{infraDetail.Nodes[2].PrivateIP, infraDetail.Nodes[3].PrivateIP}, + Nodes: []string{ + infraDetail.Nodes[2].PrivateIP, + infraDetail.Nodes[3].PrivateIP, + }, } logger.Info("addOpts: %#+v", addOpts) utils.CheckErr(applier.RemoteSealosCmd.Add(addOpts)) - //check result + // check result applier.CheckNodeNum(4) }) By("delete nodes test", func() { // delete ip2, ip3 deleteOpts := &cmd2.DeleteOptions{ Cluster: applier.ClusterName, - Nodes: []string{infraDetail.Nodes[1].PrivateIP, infraDetail.Nodes[2].PrivateIP}, - Force: true, + Nodes: []string{ + infraDetail.Nodes[1].PrivateIP, + infraDetail.Nodes[2].PrivateIP, + }, + Force: true, } logger.Info("deleteOpts: %#+v", deleteOpts.Args()) utils.CheckErr(applier.RemoteSealosCmd.Delete(deleteOpts)) - //check result + // check result applier.CheckNodeNum(2) }) By("add masters test", func() { // add ip2, ip3 addOpts := &cmd2.AddOptions{ Cluster: applier.ClusterName, - Masters: []string{infraDetail.Nodes[1].PrivateIP, infraDetail.Nodes[2].PrivateIP}, + Masters: []string{ + infraDetail.Nodes[1].PrivateIP, + infraDetail.Nodes[2].PrivateIP, + }, } logger.Info("addOpts: %#+v", addOpts.Args()) utils.CheckErr(applier.RemoteSealosCmd.Add(addOpts)) - //check result + // check result applier.CheckNodeNum(4) }) By("delete masters test", func() { // delete ip2, ip3 deleteOpts := &cmd2.DeleteOptions{ Cluster: applier.ClusterName, - Masters: []string{infraDetail.Nodes[1].PrivateIP, infraDetail.Nodes[2].PrivateIP}, - Force: true, + Masters: []string{ + infraDetail.Nodes[1].PrivateIP, + infraDetail.Nodes[2].PrivateIP, + }, + Force: true, } logger.Info("deleteOpts: %#+v", deleteOpts.Args()) utils.CheckErr(applier.RemoteSealosCmd.Delete(deleteOpts)) - //check result 1master will cause etcd down, skip check - //testApplier.CheckNodeNum(2) + // check result 1master will cause etcd down, skip check + // testApplier.CheckNodeNum(2) }) By("reset test", func() { resetOpts := &cmd2.ResetOptions{ diff --git a/lifecycle/test/e2e/run_docker_test.go b/lifecycle/test/e2e/run_docker_test.go index 4662b2df787a..f9eeaa225112 100644 --- a/lifecycle/test/e2e/run_docker_test.go +++ b/lifecycle/test/e2e/run_docker_test.go @@ -19,13 +19,10 @@ package e2e import ( "fmt" - "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - "github.com/labring/sealos/test/e2e/suites/checkers" + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/config" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" ) @@ -42,17 +39,23 @@ var _ = Describe("E2E_sealos_run_docker_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to reset cluster for earch cluster: %v", err)) }) It("sealos run single by docker", func() { - images := []string{"labring/kubernetes-docker:v1.25.0", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "labring/kubernetes-docker:v1.25.0", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) - fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{Socket: "/var/run/cri-dockerd.sock", Images: images}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "default", + &checkers.FakeOpts{Socket: "/var/run/cri-dockerd.sock", Images: images}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) It("sealos run single by docker-buildimage", func() { - By("build image from dockerfile") kubeadm := ` apiVersion: kubeadm.k8s.io/v1beta2 @@ -68,21 +71,34 @@ networking: tmpdir, err = dFile.Write() utils.CheckErr(err, fmt.Sprintf("failed to create dockerfile: %v", err)) - err = fakeClient.Image.BuildImage("test-build-image:kubeadm-servicecidr", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - SaveImage: true, - }) + err = fakeClient.Image.BuildImage( + "test-build-image:kubeadm-servicecidr", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + SaveImage: true, + }, + ) utils.CheckErr(err) By("running kubernete image using build image") - images := []string{"test-build-image:kubeadm-servicecidr", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "test-build-image:kubeadm-servicecidr", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) - fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{ServiceCIDR: "100.55.0.0/16", Socket: "/var/run/cri-dockerd.sock", Images: images}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "default", + &checkers.FakeOpts{ + ServiceCIDR: "100.55.0.0/16", + Socket: "/var/run/cri-dockerd.sock", + Images: images, + }, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) - }) - }) diff --git a/lifecycle/test/e2e/run_other_test.go b/lifecycle/test/e2e/run_other_test.go index a9ee6d4f9a03..20c8a5b652de 100644 --- a/lifecycle/test/e2e/run_other_test.go +++ b/lifecycle/test/e2e/run_other_test.go @@ -19,13 +19,10 @@ package e2e import ( "fmt" - "github.com/labring/sealos/test/e2e/testhelper/utils" - + "github.com/labring/sealos/test/e2e/suites/checkers" "github.com/labring/sealos/test/e2e/suites/operators" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" - - "github.com/labring/sealos/test/e2e/suites/checkers" ) var _ = Describe("E2E_sealos_run_other_test", func() { @@ -47,11 +44,17 @@ var _ = Describe("E2E_sealos_run_other_test", func() { err = fakeClient.Image.SaveImage("labring/kubernetes:v1.25.0", "/tmp/kube.tar") utils.CheckErr(err, fmt.Sprintf("failed to save image: %v", err)) err = fakeClient.Cluster.Run("/tmp/kube.tar") - utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single using tar: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to Run new cluster for single using tar: %v", err), + ) err = fakeClient.Cluster.Run("labring/helm:v3.8.2") utils.CheckErr(err, fmt.Sprintf("failed to running image for helm: %v", err)) newImages := []string{"docker.io/labring/kubernetes:v1.25.0", "labring/helm:v3.8.2"} - fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{Images: newImages}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "default", + &checkers.FakeOpts{Images: newImages}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) @@ -64,12 +67,17 @@ var _ = Describe("E2E_sealos_run_other_test", func() { err = fakeClient.Image.TagImage("labring/kubernetes:v1.25.0", "k8s:dev") utils.CheckErr(err, fmt.Sprintf("failed to tag image: %v", err)) err = fakeClient.Cluster.Run("k8s:dev") - utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single using short name: %v", err)) - fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{Images: []string{"k8s:dev"}}) + utils.CheckErr( + err, + fmt.Sprintf("failed to Run new cluster for single using short name: %v", err), + ) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "default", + &checkers.FakeOpts{Images: []string{"k8s:dev"}}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) }) - }) diff --git a/lifecycle/test/e2e/run_patch_test.go b/lifecycle/test/e2e/run_patch_test.go index fd3472acae95..dddfa135758a 100644 --- a/lifecycle/test/e2e/run_patch_test.go +++ b/lifecycle/test/e2e/run_patch_test.go @@ -20,14 +20,11 @@ import ( "fmt" "path" + "github.com/labring/sealos/test/e2e/suites/checkers" + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/config" "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - . "github.com/onsi/ginkgo/v2" - - "github.com/labring/sealos/test/e2e/suites/checkers" ) var _ = Describe("E2E_sealos_run_patchimage_test", func() { @@ -47,7 +44,12 @@ var _ = Describe("E2E_sealos_run_patchimage_test", func() { dFile := config.PatchDockerfile{ Images: []string{"nginx"}, Copys: []string{"sealctl opt/sealctl", "image-cri-shim cri/image-cri-shim"}, - Cmds: []string{"systemctl stop image-cri-shim", "cp cri/image-cri-shim /usr/bin/image-cri-shim", "systemctl start image-cri-shim", "image-cri-shim -v"}, + Cmds: []string{ + "systemctl stop image-cri-shim", + "cp cri/image-cri-shim /usr/bin/image-cri-shim", + "systemctl start image-cri-shim", + "image-cri-shim -v", + }, } tmpdir, err := dFile.Write() utils.CheckErr(err, fmt.Sprintf("failed to create dockerfile: %v", err)) @@ -57,29 +59,44 @@ var _ = Describe("E2E_sealos_run_patchimage_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to copy sealctl to rootfs: %v", err)) By("copy image-cri-shim to rootfs") - err = fakeClient.CmdInterface.Copy("/tmp/image-cri-shim", path.Join(tmpdir, "image-cri-shim")) + err = fakeClient.CmdInterface.Copy( + "/tmp/image-cri-shim", + path.Join(tmpdir, "image-cri-shim"), + ) utils.CheckErr(err, fmt.Sprintf("failed to copy image-cri-shim to rootfs: %v", err)) By("build image") - err = fakeClient.Image.BuildImage("test-build-image:patch-upgrade", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - }) + err = fakeClient.Image.BuildImage( + "test-build-image:patch-upgrade", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + }, + ) utils.CheckErr(err) images := []string{"labring/kubernetes:v1.25.0", "labring/helm:v3.8.2"} err = fakeClient.Image.PullImage(images...) utils.CheckErr(err, fmt.Sprintf("failed to pull image: %v", err)) err = fakeClient.Cluster.Run(images...) - utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single using tar: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to Run new cluster for single using tar: %v", err), + ) newImages := []string{"labring/kubernetes:v1.25.0", "labring/helm:v3.8.2"} - fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{Images: newImages}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "default", + &checkers.FakeOpts{Images: newImages}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) patchImage := "test-build-image:patch-upgrade" err = fakeClient.Cluster.Run(patchImage) - utils.CheckErr(err, fmt.Sprintf("failed to Run patch image for single using tar: %v", err)) + utils.CheckErr( + err, + fmt.Sprintf("failed to Run patch image for single using tar: %v", err), + ) }) }) - }) diff --git a/lifecycle/test/e2e/run_test.go b/lifecycle/test/e2e/run_test.go index 1d8ad9ace641..029abcbab2a0 100644 --- a/lifecycle/test/e2e/run_test.go +++ b/lifecycle/test/e2e/run_test.go @@ -19,13 +19,10 @@ package e2e import ( "fmt" - "github.com/labring/sealos/test/e2e/testhelper/utils" - - "github.com/labring/sealos/test/e2e/suites/operators" - "github.com/labring/sealos/test/e2e/suites/checkers" + "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/config" - + "github.com/labring/sealos/test/e2e/testhelper/utils" . "github.com/onsi/ginkgo/v2" ) @@ -42,7 +39,11 @@ var _ = Describe("E2E_sealos_run_test", func() { utils.CheckErr(err, fmt.Sprintf("failed to reset cluster for earch cluster: %v", err)) }) It("sealos run single by containerd", func() { - images := []string{"labring/kubernetes:v1.25.0", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "labring/kubernetes:v1.25.0", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{ @@ -54,7 +55,6 @@ var _ = Describe("E2E_sealos_run_test", func() { }) It("sealos run single by containerd-buildimage", func() { - By("build image from dockerfile") kubeadm := ` apiVersion: kubeadm.k8s.io/v1beta2 @@ -70,21 +70,30 @@ networking: tmpdir, err = dFile.Write() utils.CheckErr(err, fmt.Sprintf("failed to create dockerfile: %v", err)) - err = fakeClient.Image.BuildImage("test-build-image:kubeadm-servicecidr", tmpdir, operators.BuildOptions{ - MaxPullProcs: 5, - SaveImage: true, - }) + err = fakeClient.Image.BuildImage( + "test-build-image:kubeadm-servicecidr", + tmpdir, + operators.BuildOptions{ + MaxPullProcs: 5, + SaveImage: true, + }, + ) utils.CheckErr(err) By("running kubernete image using build image") - images := []string{"test-build-image:kubeadm-servicecidr", "labring/helm:v3.8.2", "labring/calico:v3.24.1"} + images := []string{ + "test-build-image:kubeadm-servicecidr", + "labring/helm:v3.8.2", + "labring/calico:v3.24.1", + } err = fakeClient.Cluster.Run(images...) utils.CheckErr(err, fmt.Sprintf("failed to Run new cluster for single: %v", err)) - fakeCheckInterface, err = checkers.NewFakeGroupClient("default", &checkers.FakeOpts{ServiceCIDR: "100.55.0.0/16", Images: images}) + fakeCheckInterface, err = checkers.NewFakeGroupClient( + "default", + &checkers.FakeOpts{ServiceCIDR: "100.55.0.0/16", Images: images}, + ) utils.CheckErr(err, fmt.Sprintf("failed to get cluster interface: %v", err)) err = fakeCheckInterface.Verify() utils.CheckErr(err, fmt.Sprintf("failed to verify cluster for single: %v", err)) }) - }) - }) diff --git a/lifecycle/test/e2e/suites/checkers/cluster_cert_sans.go b/lifecycle/test/e2e/suites/checkers/cluster_cert_sans.go index 4c9bfe1d8e72..094e02c4639f 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_cert_sans.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_cert_sans.go @@ -20,9 +20,7 @@ import ( "fmt" "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/test/e2e/testhelper/utils" - "k8s.io/apimachinery/pkg/util/sets" ) @@ -37,7 +35,8 @@ func (f *fakeCertSansClient) Verify() error { if !sets.NewString(f.ClusterConfiguration.APIServer.CertSANs...).Has("127.0.0.1") { return fmt.Errorf("cert SANs not match %s", "127.0.0.1") } - if !sets.NewString(f.ClusterConfiguration.APIServer.CertSANs...).Has("apiserver.cluster.local") { + if !sets.NewString(f.ClusterConfiguration.APIServer.CertSANs...). + Has("apiserver.cluster.local") { return fmt.Errorf("cert SANs not match %s", "apiserver.cluster.local") } if !sets.NewString(f.ClusterConfiguration.APIServer.CertSANs...).Has("10.103.97.2") { @@ -52,7 +51,11 @@ func (f *fakeCertSansClient) Verify() error { for _, v := range f.data { if v != "" { if !sets.NewString(f.ClusterConfiguration.APIServer.CertSANs...).Has(v) { - return fmt.Errorf("cert SANs %+v not match %s", f.ClusterConfiguration.APIServer.CertSANs, v) + return fmt.Errorf( + "cert SANs %+v not match %s", + f.ClusterConfiguration.APIServer.CertSANs, + v, + ) } } } diff --git a/lifecycle/test/e2e/suites/checkers/cluster_cgroup.go b/lifecycle/test/e2e/suites/checkers/cluster_cgroup.go index f880f77509b7..d8cd577ff6c8 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_cgroup.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_cgroup.go @@ -35,11 +35,19 @@ func (f *fakeCgroupClient) Verify() error { return err } if strings.TrimSpace(string(cgroup)) != f.data { - return fmt.Errorf("cgroup driver %s not match %s from sealctl", strings.TrimSpace(string(cgroup)), f.data) + return fmt.Errorf( + "cgroup driver %s not match %s from sealctl", + strings.TrimSpace(string(cgroup)), + f.data, + ) } if f.KubeletConfiguration.CgroupDriver != f.data { - return fmt.Errorf("kubelet config cgroup driver %s not match %s", f.KubeletConfiguration.CgroupDriver, f.data) + return fmt.Errorf( + "kubelet config cgroup driver %s not match %s", + f.KubeletConfiguration.CgroupDriver, + f.data, + ) } return nil diff --git a/lifecycle/test/e2e/suites/checkers/cluster_etcd.go b/lifecycle/test/e2e/suites/checkers/cluster_etcd.go index e03e992b1704..e6cbb30c9a61 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_etcd.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_etcd.go @@ -16,7 +16,10 @@ limitations under the License. package checkers -import "fmt" +import ( + "errors" + "fmt" +) var _ FakeInterface = &fakeEtcdClient{} @@ -26,17 +29,24 @@ type fakeEtcdClient struct { } func (f *fakeEtcdClient) Verify() error { - err := f.cmd.AsyncExec("kubectl", "get", "pods", "-A", "--kubeconfig", "/etc/kubernetes/admin.conf") + err := f.cmd.AsyncExec( + "kubectl", + "get", + "pods", + "-A", + "--kubeconfig", + "/etc/kubernetes/admin.conf", + ) if err != nil { return err } if len(f.etcd) == 0 { if f.ClusterConfiguration.Etcd.Local == nil { - return fmt.Errorf("etcd is empty when local etcd") + return errors.New("etcd is empty when local etcd") } } else { if f.ClusterConfiguration.Etcd.External == nil { - return fmt.Errorf("etcd is empty when external etcd") + return errors.New("etcd is empty when external etcd") } if len(f.ClusterConfiguration.Etcd.External.Endpoints) != len(f.etcd) { return fmt.Errorf("etcd endpoints not match, expect %v, got %v", f.etcd, f.ClusterConfiguration.Etcd.External.Endpoints) diff --git a/lifecycle/test/e2e/suites/checkers/cluster_pod_cidr.go b/lifecycle/test/e2e/suites/checkers/cluster_pod_cidr.go index 3c20da2e9c23..7dd029e886fb 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_pod_cidr.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_pod_cidr.go @@ -24,13 +24,17 @@ var _ FakeInterface = &fakePodCIDRClient{} type fakePodCIDRClient struct { *fakeClient - //100.64.0.0/10 + // 100.64.0.0/10 data string } func (f *fakePodCIDRClient) Verify() error { if f.ClusterConfiguration.Networking.PodSubnet != f.data { - return fmt.Errorf("cluster config pod subnet %s not match %s", f.ClusterConfiguration.Networking.PodSubnet, f.data) + return fmt.Errorf( + "cluster config pod subnet %s not match %s", + f.ClusterConfiguration.Networking.PodSubnet, + f.data, + ) } return nil } diff --git a/lifecycle/test/e2e/suites/checkers/cluster_service_cidr.go b/lifecycle/test/e2e/suites/checkers/cluster_service_cidr.go index 62979c16b0de..1721fc77ede0 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_service_cidr.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_service_cidr.go @@ -24,13 +24,17 @@ var _ FakeInterface = &fakeServiceCIDRClient{} type fakeServiceCIDRClient struct { *fakeClient - //10.96.0.0/22 + // 10.96.0.0/22 data string } func (f *fakeServiceCIDRClient) Verify() error { if f.ClusterConfiguration.Networking.ServiceSubnet != f.data { - return fmt.Errorf("cluster config service subnet %s not match %s", f.ClusterConfiguration.Networking.ServiceSubnet, f.data) + return fmt.Errorf( + "cluster config service subnet %s not match %s", + f.ClusterConfiguration.Networking.ServiceSubnet, + f.data, + ) } return nil } diff --git a/lifecycle/test/e2e/suites/checkers/cluster_single_taints.go b/lifecycle/test/e2e/suites/checkers/cluster_single_taints.go index a88eadfaaf50..d7dde08100be 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_single_taints.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_single_taints.go @@ -19,9 +19,8 @@ package checkers import ( "fmt" - "k8s.io/kubernetes/cmd/kubeadm/app/constants" - "github.com/labring/sealos/test/e2e/testhelper/kube" + "k8s.io/kubernetes/cmd/kubeadm/app/constants" ) var _ FakeInterface = &fakeSingleTaintsClient{} diff --git a/lifecycle/test/e2e/suites/checkers/cluster_socket.go b/lifecycle/test/e2e/suites/checkers/cluster_socket.go index 89985149daf3..f20342014d4f 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_socket.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_socket.go @@ -35,11 +35,19 @@ func (c *fakeSocketClient) Verify() error { return err } if strings.TrimSpace(string(socket)) != c.data { - return fmt.Errorf("cri socket %s not match %s from sealctl", strings.TrimSpace(string(socket)), c.data) + return fmt.Errorf( + "cri socket %s not match %s from sealctl", + strings.TrimSpace(string(socket)), + c.data, + ) } - if c.InitConfiguration.NodeRegistration.CRISocket != fmt.Sprintf("unix://%s", c.data) { - return fmt.Errorf("init config cri socket %s not match %s", c.InitConfiguration.NodeRegistration.CRISocket, c.data) + if c.InitConfiguration.NodeRegistration.CRISocket != "unix://"+c.data { + return fmt.Errorf( + "init config cri socket %s not match %s", + c.InitConfiguration.NodeRegistration.CRISocket, + c.data, + ) } return nil } diff --git a/lifecycle/test/e2e/suites/checkers/cluster_taints.go b/lifecycle/test/e2e/suites/checkers/cluster_taints.go index d431c15061dd..519a77dc3c41 100644 --- a/lifecycle/test/e2e/suites/checkers/cluster_taints.go +++ b/lifecycle/test/e2e/suites/checkers/cluster_taints.go @@ -47,7 +47,13 @@ func (f *fakeTaintsClient) Verify() error { for _, node := range nodes.Items { for _, taint := range node.Spec.Taints { if f.data[taint.Key] != taint.Value { - return fmt.Errorf("expect taint %s=%s, but got %s=%s", taint.Key, f.data[taint.Key], taint.Key, taint.Value) + return fmt.Errorf( + "expect taint %s=%s, but got %s=%s", + taint.Key, + f.data[taint.Key], + taint.Key, + taint.Value, + ) } } } diff --git a/lifecycle/test/e2e/suites/checkers/fake.go b/lifecycle/test/e2e/suites/checkers/fake.go index 1f61533c168b..d8ae5cf2059d 100644 --- a/lifecycle/test/e2e/suites/checkers/fake.go +++ b/lifecycle/test/e2e/suites/checkers/fake.go @@ -22,20 +22,16 @@ import ( "os" "path/filepath" - v1 "k8s.io/api/core/v1" - - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/labring/sealos/pkg/types/v1beta1" - + "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/test/e2e/testhelper/cmd" + "github.com/labring/sealos/test/e2e/testhelper/utils" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/util/yaml" proxy "k8s.io/kube-proxy/config/v1alpha1" kubelet "k8s.io/kubelet/config/v1beta1" "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" - - "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/test/e2e/testhelper/cmd" ) type FakeInterface interface { @@ -152,7 +148,16 @@ func (f *fakeClient) loadInitConfig() error { } yamls := utils.ToYalms(string(data)) clusterConfigFn := func() ([]byte, error) { - cfg, err := f.cmd.Exec("kubectl", "get", "cm", "-n", "kube-system", "kubeadm-config", "-o", "yaml") + cfg, err := f.cmd.Exec( + "kubectl", + "get", + "cm", + "-n", + "kube-system", + "kubeadm-config", + "-o", + "yaml", + ) if err != nil { return nil, err } @@ -161,7 +166,16 @@ func (f *fakeClient) loadInitConfig() error { return []byte(cm.Data["ClusterConfiguration"]), nil } kubeproxyConfigFn := func() ([]byte, error) { - cfg, err := f.cmd.Exec("kubectl", "get", "cm", "-n", "kube-system", "kube-proxy", "-o", "yaml") + cfg, err := f.cmd.Exec( + "kubectl", + "get", + "cm", + "-n", + "kube-system", + "kube-proxy", + "-o", + "yaml", + ) if err != nil { return nil, err } @@ -170,7 +184,16 @@ func (f *fakeClient) loadInitConfig() error { return []byte(cm.Data["config.conf"]), nil } kubeletConfigFn := func() ([]byte, error) { - cfg, err := f.cmd.Exec("kubectl", "get", "cm", "-n", "kube-system", "kubelet-config", "-o", "yaml") + cfg, err := f.cmd.Exec( + "kubectl", + "get", + "cm", + "-n", + "kube-system", + "kubelet-config", + "-o", + "yaml", + ) if err != nil { return nil, err } @@ -182,8 +205,7 @@ func (f *fakeClient) loadInitConfig() error { for _, yamlString := range yamls { obj, _ := utils.UnmarshalData([]byte(yamlString)) kind, _, _ := unstructured.NestedString(obj, "kind") - switch kind { - case "InitConfiguration": + if kind == "InitConfiguration" { _ = yaml.Unmarshal([]byte(yamlString), &f.InitConfiguration) } } diff --git a/lifecycle/test/e2e/suites/image/imageServiceClient.go b/lifecycle/test/e2e/suites/image/imageServiceClient.go index 157ee044a9e8..9707f797b5b8 100644 --- a/lifecycle/test/e2e/suites/image/imageServiceClient.go +++ b/lifecycle/test/e2e/suites/image/imageServiceClient.go @@ -16,11 +16,10 @@ package image import ( "context" - "fmt" - - v1api "k8s.io/cri-api/pkg/apis/runtime/v1" + "errors" "github.com/labring/sealos/pkg/utils/logger" + v1api "k8s.io/cri-api/pkg/apis/runtime/v1" ) const V1 = "v1" @@ -71,19 +70,20 @@ func (f FakeImageServiceClient) ListImages() ([]string, error) { } func (f FakeImageServiceClient) ImageStatus(image string) (*v1api.ImageStatusResponse, error) { - var ( - ctx, cancel = context.WithCancel(context.Background()) - ) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() v1ImageSpec := &v1api.ImageSpec{Image: image} if f.Version == V1 { - v1ImageListResp, err := f.V1ImageServiceClient.ImageStatus(ctx, &v1api.ImageStatusRequest{Image: v1ImageSpec, Verbose: true}) + v1ImageListResp, err := f.V1ImageServiceClient.ImageStatus( + ctx, + &v1api.ImageStatusRequest{Image: v1ImageSpec, Verbose: true}, + ) if err != nil { return nil, err } return v1ImageListResp, nil } - return nil, fmt.Errorf("not support cri api v1") + return nil, errors.New("not support cri api v1") } // return image ID @@ -94,35 +94,37 @@ func (f FakeImageServiceClient) PullImage(image string) (string, error) { ) defer cancel() if f.Version == V1 { - v1ImageListResp, err := f.V1ImageServiceClient.PullImage(ctx, &v1api.PullImageRequest{Image: v1ImageSpec}) + v1ImageListResp, err := f.V1ImageServiceClient.PullImage( + ctx, + &v1api.PullImageRequest{Image: v1ImageSpec}, + ) if err != nil { return "", err } return v1ImageListResp.ImageRef, nil } - return "", fmt.Errorf("not support cri api v1") + return "", errors.New("not support cri api v1") } func (f FakeImageServiceClient) RemoveImage(image string) error { - var ( - ctx, cancel = context.WithCancel(context.Background()) - ) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() v1ImageSpec := &v1api.ImageSpec{Image: image} if f.Version == V1 { - _, err := f.V1ImageServiceClient.RemoveImage(ctx, &v1api.RemoveImageRequest{Image: v1ImageSpec}) + _, err := f.V1ImageServiceClient.RemoveImage( + ctx, + &v1api.RemoveImageRequest{Image: v1ImageSpec}, + ) if err != nil { return err } return nil } - return fmt.Errorf("not support cri api v1") + return errors.New("not support cri api v1") } func (f FakeImageServiceClient) ImageFsInfo() ([]*v1api.FilesystemUsage, error) { - var ( - ctx, cancel = context.WithCancel(context.Background()) - ) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() if f.Version == V1 { v1ImageListResp, err := f.V1ImageServiceClient.ImageFsInfo(ctx, &v1api.ImageFsInfoRequest{}) @@ -131,5 +133,5 @@ func (f FakeImageServiceClient) ImageFsInfo() ([]*v1api.FilesystemUsage, error) } return v1ImageListResp.ImageFilesystems, nil } - return nil, fmt.Errorf("not support cri api v1") + return nil, errors.New("not support cri api v1") } diff --git a/lifecycle/test/e2e/suites/operators/cert.go b/lifecycle/test/e2e/suites/operators/cert.go index 8c43a8ca333a..a64c328be095 100644 --- a/lifecycle/test/e2e/suites/operators/cert.go +++ b/lifecycle/test/e2e/suites/operators/cert.go @@ -30,13 +30,13 @@ func newCertClient(sealosCmd *cmd.SealosCmd, clusterName string) FakeCertInterfa var _ FakeCertInterface = &fakeCertClient{} type fakeCertClient struct { - //cmd.Interface + // cmd.Interface *cmd.SealosCmd clusterName string } func (c *fakeCertClient) AddDomain(domain string) error { - return c.SealosCmd.Cert(&cmd.CertOptions{ + return c.Cert(&cmd.CertOptions{ Cluster: c.clusterName, AltName: []string{domain}, }) diff --git a/lifecycle/test/e2e/suites/operators/cri.go b/lifecycle/test/e2e/suites/operators/cri.go index b5c292fe6917..e13f0939cb46 100644 --- a/lifecycle/test/e2e/suites/operators/cri.go +++ b/lifecycle/test/e2e/suites/operators/cri.go @@ -19,9 +19,8 @@ package operators import ( "fmt" - "k8s.io/apimachinery/pkg/util/json" - "github.com/labring/sealos/test/e2e/testhelper/cmd" + "k8s.io/apimachinery/pkg/util/json" ) type fakeCRIClient struct { @@ -44,6 +43,7 @@ func (f *fakeCRIClient) Pull(name string) error { } return f.SealosCmd.CRIImagePull(name) } + func (f *fakeCRIClient) ImageList() (*ImageStruct, error) { if f.SealosCmd.CriBinPath == "" { if err := f.SealosCmd.SetCriBinPath(); err != nil { @@ -61,6 +61,7 @@ func (f *fakeCRIClient) ImageList() (*ImageStruct, error) { } return &image, nil } + func (f *fakeCRIClient) HasImage(name string) error { data, err := f.ImageList() if err != nil { diff --git a/lifecycle/test/e2e/suites/operators/image.go b/lifecycle/test/e2e/suites/operators/image.go index 7119cc60f5cd..d3c60c6d5ba9 100644 --- a/lifecycle/test/e2e/suites/operators/image.go +++ b/lifecycle/test/e2e/suites/operators/image.go @@ -17,9 +17,8 @@ package operators import ( "strings" - "k8s.io/apimachinery/pkg/util/json" - "github.com/labring/sealos/test/e2e/testhelper/cmd" + "k8s.io/apimachinery/pkg/util/json" ) const ( @@ -41,9 +40,9 @@ var _ FakeImageInterface = &fakeImageClient{} func (f *fakeImageClient) ListImages(display bool) ([]DisplayImage, error) { if display { - return nil, f.SealosCmd.ImageList() + return nil, f.ImageList() } - data, err := f.SealosCmd.Executor.Exec(f.SealosCmd.BinPath, "images", "--json") + data, err := f.Executor.Exec(f.BinPath, "images", "--json") if err != nil { return nil, err } @@ -53,34 +52,34 @@ func (f *fakeImageClient) ListImages(display bool) ([]DisplayImage, error) { } func (f *fakeImageClient) PullImage(images ...string) error { - return f.SealosCmd.ImagePull(&cmd.PullOptions{ + return f.ImagePull(&cmd.PullOptions{ ImageRefs: images, Quiet: true, }) } func (f *fakeImageClient) RemoveImage(images ...string) error { - return f.SealosCmd.ImageRemove(images...) + return f.ImageRemove(images...) } func (f *fakeImageClient) DockerArchiveImage(name string) error { - return f.SealosCmd.ImageSave(name, DockerTarFile, "docker-archive") + return f.ImageSave(name, DockerTarFile, "docker-archive") } func (f *fakeImageClient) OCIArchiveImage(name string) error { - return f.SealosCmd.ImageSave(name, OCITarFile, "oci-archive") + return f.ImageSave(name, OCITarFile, "oci-archive") } func (f *fakeImageClient) SaveImage(name, file string) error { - return f.SealosCmd.ImageSave(name, file, "") + return f.ImageSave(name, file, "") } func (f *fakeImageClient) SaveMultiImage(file string, name ...string) error { - return f.SealosCmd.ImageMultiSave(file, name...) + return f.ImageMultiSave(file, name...) } func (f *fakeImageClient) LoadImage(file string) error { - return f.SealosCmd.ImageLoad(file) + return f.ImageLoad(file) } func (f *fakeImageClient) Create(name string, short bool) ([]byte, error) { @@ -91,21 +90,22 @@ func (f *fakeImageClient) Create(name string, short bool) ([]byte, error) { } func (f *fakeImageClient) Merge(newImage string, images []string) error { - return f.SealosCmd.ImageMerge(&cmd.MergeOptions{ + return f.ImageMerge(&cmd.MergeOptions{ Tag: []string{newImage}, ImageRefs: images, }) } func (f *fakeImageClient) FetchImageID(name string) (string, error) { - data, err := f.SealosCmd.Executor.Exec(f.BinPath, "images", "-q", name) + data, err := f.Executor.Exec(f.BinPath, "images", "-q", name) if err != nil { return "", err } return strings.TrimSpace(string(data)), nil } + func (f *fakeImageClient) BuildImage(image, context string, opts BuildOptions) error { - return f.SealosCmd.Build(&cmd.BuildOptions{ + return f.Build(&cmd.BuildOptions{ Tag: image, SaveImage: opts.SaveImage, Context: context, @@ -116,5 +116,5 @@ func (f *fakeImageClient) BuildImage(image, context string, opts BuildOptions) e } func (f *fakeImageClient) TagImage(name, newName string) error { - return f.SealosCmd.ImageTag(name, newName) + return f.ImageTag(name, newName) } diff --git a/lifecycle/test/e2e/suites/operators/inspect.go b/lifecycle/test/e2e/suites/operators/inspect.go index f8e0147104dc..b8fb41ef7cc9 100644 --- a/lifecycle/test/e2e/suites/operators/inspect.go +++ b/lifecycle/test/e2e/suites/operators/inspect.go @@ -17,8 +17,6 @@ limitations under the License. package operators import ( - "fmt" - "github.com/labring/sealos/test/e2e/testhelper/cmd" ) @@ -33,21 +31,21 @@ func newInspectClient(sealosCmd *cmd.SealosCmd) FakeInspectInterface { } func (c *fakeInspectClient) LocalImage(name string) error { - return c.SealosCmd.ImageInspect(name) + return c.ImageInspect(name) } func (c *fakeInspectClient) RemoteImage(name string) error { - return c.SealosCmd.ImageInspect(fmt.Sprintf("docker://%s", name)) + return c.ImageInspect("docker://" + name) } func (c *fakeInspectClient) DockerArchiveImage(name string) error { - return c.SealosCmd.ImageInspect(fmt.Sprintf("docker-archive://%s", name)) + return c.ImageInspect("docker-archive://" + name) } func (c *fakeInspectClient) OCIArchiveImage(name string) error { - return c.SealosCmd.ImageInspect(fmt.Sprintf("oci-archive://%s", name)) + return c.ImageInspect("oci-archive://" + name) } func (c *fakeInspectClient) ImageID(id string) error { - return c.SealosCmd.ImageInspect(id) + return c.ImageInspect(id) } diff --git a/lifecycle/test/e2e/suites/operators/types.go b/lifecycle/test/e2e/suites/operators/types.go index 391cd8a377c1..69eeda7772d1 100644 --- a/lifecycle/test/e2e/suites/operators/types.go +++ b/lifecycle/test/e2e/suites/operators/types.go @@ -34,9 +34,9 @@ type BuildOptions struct { } type PodStruct struct { - //var pod PodStruct - //err = json.Unmarshal(data, &pod) - //if err != nil { + // var pod PodStruct + // err = json.Unmarshal(data, &pod) + // if err != nil { // return nil, err //} //return &pod, nil @@ -64,19 +64,19 @@ type ImageStruct struct { //} // return &image, nil Images []struct { - ID string `json:"id"` - RepoTags []string `json:"repoTags"` - RepoDigests []string `json:"repoDigests"` - Size string `json:"size"` - UID interface{} `json:"uid"` - Username string `json:"username"` - Spec interface{} `json:"spec"` - Pinned bool `json:"pinned"` + ID string `json:"id"` + RepoTags []string `json:"repoTags"` + RepoDigests []string `json:"repoDigests"` + Size string `json:"size"` + UID any `json:"uid"` + Username string `json:"username"` + Spec any `json:"spec"` + Pinned bool `json:"pinned"` } `json:"images"` } type ProcessStruct struct { - //var process ProcessStruct + // var process ProcessStruct // err = json.Unmarshal(data, &process) // if err != nil { // return nil, err diff --git a/lifecycle/test/e2e/terraform/main.go b/lifecycle/test/e2e/terraform/main.go index 3ff3df684420..94682aaa8ec4 100644 --- a/lifecycle/test/e2e/terraform/main.go +++ b/lifecycle/test/e2e/terraform/main.go @@ -18,7 +18,6 @@ package terraform import ( bindata "github.com/go-bindata/go-bindata" - "github.com/labring/sealos/pkg/utils/logger" ) diff --git a/lifecycle/test/e2e/terraform/terraform.go b/lifecycle/test/e2e/terraform/terraform.go index 4e0e4c2b7bb1..35901676660d 100644 --- a/lifecycle/test/e2e/terraform/terraform.go +++ b/lifecycle/test/e2e/terraform/terraform.go @@ -18,19 +18,18 @@ package terraform import ( "context" + "errors" "fmt" "os" "path" "strings" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/util/json" - - "k8s.io/client-go/util/homedir" - executils "github.com/labring/sealos/pkg/utils/exec" "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/util/json" + "k8s.io/client-go/util/homedir" ) //go:generate go run main.go @@ -45,7 +44,9 @@ type Terraform struct { func (tf *Terraform) Apply(architecture string) error { _, ok := executils.CheckCmdIsExist("terraform") if !ok { - return fmt.Errorf("not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ") + return errors.New( + "not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ", + ) } tf.setEnv() defer func() { @@ -63,7 +64,7 @@ func (tf *Terraform) Apply(architecture string) error { if err != nil { return err } - if f == fmt.Sprintf("infra/vars.tf.%s", architecture) { + if f == "infra/vars.tf."+architecture { if err = file.WriteFile(path.Join(HomeDir, "vars.tf"), data); err != nil { return err } @@ -76,11 +77,21 @@ func (tf *Terraform) Apply(architecture string) error { } } _ = file.CleanFiles(path.Join(HomeDir, "vars.tf.amd64"), path.Join(HomeDir, "vars.tf.arm64")) - err := executils.CmdWithContext(context.Background(), "bash", "-c", fmt.Sprintf("cd %s && terraform init", HomeDir)) + err := executils.CmdWithContext( + context.Background(), + "bash", + "-c", + fmt.Sprintf("cd %s && terraform init", HomeDir), + ) if err != nil { return err } - err = executils.CmdWithContext(context.Background(), "bash", "-c", fmt.Sprintf("cd %s && terraform apply -auto-approve", HomeDir)) + err = executils.CmdWithContext( + context.Background(), + "bash", + "-c", + fmt.Sprintf("cd %s && terraform apply -auto-approve", HomeDir), + ) if err != nil { return err } @@ -90,7 +101,9 @@ func (tf *Terraform) Apply(architecture string) error { func (tf *Terraform) Destroy() error { _, ok := executils.CheckCmdIsExist("terraform") if !ok { - return fmt.Errorf("not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ") + return errors.New( + "not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ", + ) } var err error tf.setEnv() @@ -98,7 +111,7 @@ func (tf *Terraform) Destroy() error { tf.unsetEnv() }() if !file.IsExist(HomeDir) { - err = fmt.Errorf("infra terraform home dir is not exist") + err = errors.New("infra terraform home dir is not exist") return err } defer func() { @@ -107,7 +120,12 @@ func (tf *Terraform) Destroy() error { file.CleanDir(HomeDir) } }() - err = executils.CmdWithContext(context.Background(), "bash", "-c", fmt.Sprintf("cd %s && terraform destroy -auto-approve", HomeDir)) + err = executils.CmdWithContext( + context.Background(), + "bash", + "-c", + fmt.Sprintf("cd %s && terraform destroy -auto-approve", HomeDir), + ) if err != nil { return err } @@ -115,21 +133,23 @@ func (tf *Terraform) Destroy() error { } type Host struct { - Name string - Password string - PublicIP string - PrivateIP string + Name string `json:"name"` + Password string `json:"password"` + PublicIP string `json:"public_ip"` + PrivateIP string `json:"private_ip"` } type InfraDetail struct { - Public *Host - Nodes []Host + Public *Host `json:"public"` + Nodes []Host `json:"nodes"` } func (tf *Terraform) Detail() (*InfraDetail, error) { _, ok := executils.CheckCmdIsExist("terraform") if !ok { - return nil, fmt.Errorf("not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ") + return nil, errors.New( + "not install terraform, please install terraform. vist: https://developer.hashicorp.com/terraform/downloads ", + ) } var err error tf.setEnv() @@ -137,32 +157,48 @@ func (tf *Terraform) Detail() (*InfraDetail, error) { tf.unsetEnv() }() if !file.IsExist(path.Join(HomeDir, "terraform.tfstate")) { - err = fmt.Errorf("infra terraform status file is not exist") + err = errors.New("infra terraform status file is not exist") return nil, err } data, err := file.ReadAll(path.Join(HomeDir, "terraform.tfstate")) if err != nil { return nil, err } - var stateMap map[string]interface{} + var stateMap map[string]any if err = json.Unmarshal(data, &stateMap); err != nil { return nil, err } d := &InfraDetail{} resources, _, _ := unstructured.NestedSlice(stateMap, "resources") for _, res := range resources { - if obj, ok := res.(map[string]interface{}); ok { + if obj, ok := res.(map[string]any); ok { resourceType, _, _ := unstructured.NestedString(obj, "type") resourceName, _, _ := unstructured.NestedString(obj, "name") if resourceType == "alicloud_instance" { instances, _, _ := unstructured.NestedSlice(obj, "instances") for i, instance := range instances { - if instanceObj, ok := instance.(map[string]interface{}); ok { + if instanceObj, ok := instance.(map[string]any); ok { h := &Host{} - h.Name, _, _ = unstructured.NestedString(instanceObj, "attributes", "instance_name") - h.Password, _, _ = unstructured.NestedString(instanceObj, "attributes", "password") - h.PublicIP, _, _ = unstructured.NestedString(instanceObj, "attributes", "public_ip") - h.PrivateIP, _, _ = unstructured.NestedString(instanceObj, "attributes", "private_ip") + h.Name, _, _ = unstructured.NestedString( + instanceObj, + "attributes", + "instance_name", + ) + h.Password, _, _ = unstructured.NestedString( + instanceObj, + "attributes", + "password", + ) + h.PublicIP, _, _ = unstructured.NestedString( + instanceObj, + "attributes", + "public_ip", + ) + h.PrivateIP, _, _ = unstructured.NestedString( + instanceObj, + "attributes", + "private_ip", + ) if resourceName == "sealos" { d.Public = h break diff --git a/lifecycle/test/e2e/terraform/terraform_test.go b/lifecycle/test/e2e/terraform/terraform_test.go index 17478ef0aa65..f9981b4d837b 100644 --- a/lifecycle/test/e2e/terraform/terraform_test.go +++ b/lifecycle/test/e2e/terraform/terraform_test.go @@ -50,7 +50,11 @@ func TestTerraform_Detail(t *testing.T) { t.Errorf("%+v", err) return } - data, _ := json.MarshalIndent(d, "", " ") + data, err := json.MarshalIndent(d, "", " ") + if err != nil { + t.Errorf("failed to marshal detail: %v", err) + return + } t.Logf("SUCCESS: %s", string(data)) t.Logf("public: %+v", *d.Public) } diff --git a/lifecycle/test/e2e/testhelper/cmd/cmd.go b/lifecycle/test/e2e/testhelper/cmd/cmd.go index 6bef35e4fcbf..c36c9219229e 100644 --- a/lifecycle/test/e2e/testhelper/cmd/cmd.go +++ b/lifecycle/test/e2e/testhelper/cmd/cmd.go @@ -20,9 +20,8 @@ import ( "os/exec" "strings" - "github.com/labring/sealos/pkg/types/v1beta1" - "github.com/labring/sealos/pkg/ssh" + "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/file" ) @@ -30,12 +29,14 @@ import ( type Interface interface { Exec(cmd string, args ...string) ([]byte, error) AsyncExec(cmd string, args ...string) error - Copy(src string, dst string) error - CopyR(dst string, src string) error + Copy(src, dst string) error + CopyR(dst, src string) error } -var _ = (Interface)(&RemoteCmd{}) -var _ = (Interface)(&LocalCmd{}) +var ( + _ = (Interface)(&RemoteCmd{}) + _ = (Interface)(&LocalCmd{}) +) // Exec executes the given command on the remote machine func (c *RemoteCmd) Exec(cmd string, args ...string) ([]byte, error) { @@ -49,12 +50,12 @@ func (c *RemoteCmd) AsyncExec(cmd string, args ...string) error { return c.CmdAsync(c.Host, strings.Join(append([]string{cmd}, args...), " ")) } -func (c *RemoteCmd) Copy(src string, dst string) error { +func (c *RemoteCmd) Copy(src, dst string) error { return c.Interface.Copy(c.Host, src, dst) } -func (c *RemoteCmd) CopyR(dst string, src string) error { - return c.Interface.Fetch(c.Host, src, dst) +func (c *RemoteCmd) CopyR(dst, src string) error { + return c.Fetch(c.Host, src, dst) } // RemoteCmd implements the Interface for remote command execution using SSH @@ -85,10 +86,10 @@ func (c LocalCmd) AsyncExec(cmd string, arg ...string) error { return cmder.Run() } -func (c LocalCmd) Copy(src string, dst string) error { +func (c LocalCmd) Copy(src, dst string) error { return file.RecursionCopy(src, dst) } -func (c LocalCmd) CopyR(dst string, src string) error { +func (c LocalCmd) CopyR(dst, src string) error { return file.RecursionCopy(src, dst) } diff --git a/lifecycle/test/e2e/testhelper/cmd/imageOpts.go b/lifecycle/test/e2e/testhelper/cmd/imageOpts.go index eac65b433a4e..542f3b190c71 100644 --- a/lifecycle/test/e2e/testhelper/cmd/imageOpts.go +++ b/lifecycle/test/e2e/testhelper/cmd/imageOpts.go @@ -40,33 +40,33 @@ type MergeOptions struct { ImageRefs []string AuthFile string AllPlatforms bool - //BuildArg []string - //BuildContext []string - //CertDir string - //Compress bool - //Creds string - //DisableCompression bool - //Dns []string - //DnsOption []string - //DnsSearch []string - //Env []string - //File []string - //ForceRm bool - //Format string - //From string - //GroupAdd []string - //HttpProxy bool - //IgnoreFile string - //Jobs int - //Label []string - //Manifest string - //MaxPullProcs int - //Platform string - //Pull string - //Retry int - //RetryDelay time.Duration - //Rm bool - //ShmSize string + // BuildArg []string + // BuildContext []string + // CertDir string + // Compress bool + // Creds string + // DisableCompression bool + // Dns []string + // DnsOption []string + // DnsSearch []string + // Env []string + // File []string + // ForceRm bool + // Format string + // From string + // GroupAdd []string + // HttpProxy bool + // IgnoreFile string + // Jobs int + // Label []string + // Manifest string + // MaxPullProcs int + // Platform string + // Pull string + // Retry int + // RetryDelay time.Duration + // Rm bool + // ShmSize string } func (po *PullOptions) Args() []string { diff --git a/lifecycle/test/e2e/testhelper/cmd/sealosCmd.go b/lifecycle/test/e2e/testhelper/cmd/sealosCmd.go index d4a21b7823ab..3f11e953cc01 100644 --- a/lifecycle/test/e2e/testhelper/cmd/sealosCmd.go +++ b/lifecycle/test/e2e/testhelper/cmd/sealosCmd.go @@ -18,7 +18,6 @@ import ( "os" "github.com/labring/sealos/test/e2e/testhelper/settings" - "github.com/labring/sealos/test/e2e/testhelper/utils" ) @@ -48,22 +47,22 @@ func (s *SealosCmd) SetCriBinPath() error { } type ClusterCycle interface { - Apply(*ApplyOptions) error - Build(*BuildOptions) error - Create(*CreateOptions) ([]byte, error) - Add(*AddOptions) error - Delete(*DeleteOptions) error - Run(*RunOptions) error - Reset(*ResetOptions) error - Cert(*CertOptions) error + Apply(opts *ApplyOptions) error + Build(opts *BuildOptions) error + Create(opts *CreateOptions) ([]byte, error) + Add(opts *AddOptions) error + Delete(opts *DeleteOptions) error + Run(opts *RunOptions) error + Reset(opts *ResetOptions) error + Cert(opts *CertOptions) error } type ImageService interface { - ImagePull(*PullOptions) error + ImagePull(opts *PullOptions) error ImagePush(image string) error ImageList() error ImageTag(name, newName string) error - ImageSave(image string, path string, archive string) error + ImageSave(image, path, archive string) error ImageLoad(path string) error ImageMerge(options *MergeOptions) error ImageRemove(images ...string) error @@ -159,7 +158,7 @@ func (s *SealosCmd) ImageList() error { return s.Executor.AsyncExec(s.BinPath, "list") } -func (s *SealosCmd) ImageSave(image string, path string, archive string) error { +func (s *SealosCmd) ImageSave(image, path, archive string) error { if archive == "" { return s.Executor.AsyncExec(s.BinPath, "save", "-o", path, image) } @@ -190,18 +189,21 @@ func (s *SealosCmd) ImageRemove(images ...string) error { func (s *SealosCmd) ImageInspect(image string) error { return s.Executor.AsyncExec(s.BinPath, "inspect", image) } + func (s *SealosCmd) CRIImageList(display bool) ([]byte, error) { if display { return nil, s.Executor.AsyncExec(s.CriBinPath, "images") } return s.Executor.Exec(s.CriBinPath, "images", "-o", "json") } + func (s *SealosCmd) CRIProcessList(display bool) ([]byte, error) { if display { return nil, s.Executor.AsyncExec(s.CriBinPath, "ps", "-a") } return s.Executor.Exec(s.CriBinPath, "ps", "-a", "-o", "json") } + func (s *SealosCmd) CRIPodList(display bool) ([]byte, error) { if display { return nil, s.Executor.AsyncExec(s.CriBinPath, "pods") @@ -212,6 +214,7 @@ func (s *SealosCmd) CRIPodList(display bool) ([]byte, error) { } return data, nil } + func (s *SealosCmd) CRIImagePull(name string) error { return s.Executor.AsyncExec(s.CriBinPath, "pull", name) } diff --git a/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts.go b/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts.go index b8bb24754c99..e340acc81118 100644 --- a/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts.go +++ b/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts.go @@ -240,7 +240,7 @@ func (co *CertOptions) Args() []string { appendFlagsWithValues("--alt-names", strings.Join(co.AltName, ",")) } -func (args Args) appendFlagsWithValues(flagName string, values interface{}) Args { +func (args Args) appendFlagsWithValues(flagName string, values any) Args { switch vv := values.(type) { case []string: if vv == nil { diff --git a/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts_test.go b/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts_test.go index bafc0130813e..d70c60d5a22a 100644 --- a/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts_test.go +++ b/lifecycle/test/e2e/testhelper/cmd/sealosCmdOpts_test.go @@ -18,9 +18,8 @@ import ( "reflect" "testing" - "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/pkg/types/v1beta1" + "github.com/labring/sealos/pkg/utils/logger" ) func TestAddOptions_Args(t *testing.T) { @@ -84,7 +83,7 @@ func TestApplyOptions_Args(t *testing.T) { func TestArgs_appendFlagsWithValues(t *testing.T) { type args struct { flagName string - values interface{} + values any } tests := []struct { name string @@ -96,7 +95,10 @@ func TestArgs_appendFlagsWithValues(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.args1.appendFlagsWithValues(tt.args.flagName, tt.args.values); !reflect.DeepEqual(got, tt.want) { + if got := tt.args1.appendFlagsWithValues(tt.args.flagName, tt.args.values); !reflect.DeepEqual( + got, + tt.want, + ) { t.Errorf("appendFlagsWithValues() = %v, want %v", got, tt.want) } }) diff --git a/lifecycle/test/e2e/testhelper/config/clusterfile.go b/lifecycle/test/e2e/testhelper/config/clusterfile.go index eb022a5ca881..92a03b1e5e34 100644 --- a/lifecycle/test/e2e/testhelper/config/clusterfile.go +++ b/lifecycle/test/e2e/testhelper/config/clusterfile.go @@ -20,15 +20,12 @@ import ( "os" "strings" + "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/test/e2e/testdata/kubeadm" - "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/pkg/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/util/yaml" - - "github.com/labring/sealos/pkg/types/v1beta1" ) type Clusterfile struct { @@ -50,15 +47,14 @@ func (c *Clusterfile) Write() (string, error) { for k, v := range c.Replaces { replaceClusterfile = strings.ReplaceAll(replaceClusterfile, k, v) } - if err = os.WriteFile(tmpdir+"/Clusterfile", []byte(replaceClusterfile), 0644); err != nil { + if err = os.WriteFile(tmpdir+"/Clusterfile", []byte(replaceClusterfile), 0o644); err != nil { return "", errors.WithMessage(err, "write clusterfile failed") } yamls := utils.ToYalms(replaceClusterfile) for _, yamlString := range yamls { obj, _ := utils.UnmarshalData([]byte(yamlString)) kind, _, _ := unstructured.NestedString(obj, "kind") - switch kind { - case "Cluster": + if kind == "Cluster" { err = yaml.Unmarshal([]byte(yamlString), &c.Cluster) if err != nil { return "", errors.WithMessage(err, "unmarshal cluster failed") diff --git a/lifecycle/test/e2e/testhelper/config/clusterfile_test.go b/lifecycle/test/e2e/testhelper/config/clusterfile_test.go index 76864ed934c9..e00806c115ec 100644 --- a/lifecycle/test/e2e/testhelper/config/clusterfile_test.go +++ b/lifecycle/test/e2e/testhelper/config/clusterfile_test.go @@ -21,7 +21,6 @@ import ( "testing" "github.com/labring/sealos/test/e2e/testdata/kubeadm" - "github.com/labring/sealos/test/e2e/testhelper/utils" ) diff --git a/lifecycle/test/e2e/testhelper/config/patchfs.go b/lifecycle/test/e2e/testhelper/config/patchfs.go index 5d48893762f1..6d9215e83682 100644 --- a/lifecycle/test/e2e/testhelper/config/patchfs.go +++ b/lifecycle/test/e2e/testhelper/config/patchfs.go @@ -23,16 +23,14 @@ import ( "path" "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/test/e2e/testhelper/template" "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/pkg/errors" ) const ( // ImageDockerfile = `FROM scratch - //COPY . .` + // COPY . .` PatchfsTemplateDockerfile = `FROM scratch MAINTAINER labring LABEL sealos.io.type="patch" @@ -59,11 +57,11 @@ func (d *PatchDockerfile) Write() (string, error) { return "", errors.WithMessage(err, "create tmpdir failed") } if len(d.Images) != 0 { - if err := os.MkdirAll(path.Join(tmpdir, "images", "shim"), 0755); err != nil { + if err := os.MkdirAll(path.Join(tmpdir, "images", "shim"), 0o755); err != nil { return "", errors.WithMessage(err, "create images dir failed") } for i, image := range d.Images { - if err := os.WriteFile(path.Join(tmpdir, "images", "shim", fmt.Sprintf("image%d", i)), []byte(image), 0644); err != nil { + if err := os.WriteFile(path.Join(tmpdir, "images", "shim", fmt.Sprintf("image%d", i)), []byte(image), 0o644); err != nil { return "", errors.WithMessage(err, "write shim image failed") } } @@ -83,7 +81,7 @@ func (d *PatchDockerfile) Write() (string, error) { return "", errors.New("dockerfile content is not set") } logger.Info("dockerfile content: %s", d.dockerfileContent) - if err := os.WriteFile(tmpdir+"/Dockerfile", []byte(d.dockerfileContent), 0644); err != nil { + if err := os.WriteFile(tmpdir+"/Dockerfile", []byte(d.dockerfileContent), 0o644); err != nil { return "", errors.WithMessage(err, "write RootfsDockerfile failed") } return tmpdir, nil diff --git a/lifecycle/test/e2e/testhelper/config/rootfs.go b/lifecycle/test/e2e/testhelper/config/rootfs.go index 970b835456b1..9c29667247e8 100644 --- a/lifecycle/test/e2e/testhelper/config/rootfs.go +++ b/lifecycle/test/e2e/testhelper/config/rootfs.go @@ -24,13 +24,12 @@ import ( "github.com/labring/sealos/test/e2e/testhelper/template" "github.com/labring/sealos/test/e2e/testhelper/utils" - "github.com/pkg/errors" ) const ( // ImageDockerfile = `FROM scratch - //COPY . .` + // COPY . .` RootfsTemplateDockerfile = `FROM {{ .BaseImage }} MAINTAINER labring {{- if .Copys }} @@ -57,11 +56,11 @@ func (d *RootfsDockerfile) Write() (string, error) { d.BaseImage = "scratch" } if len(d.Images) != 0 { - if err := os.MkdirAll(path.Join(tmpdir, "images", "shim"), 0755); err != nil { + if err := os.MkdirAll(path.Join(tmpdir, "images", "shim"), 0o755); err != nil { return "", errors.WithMessage(err, "create images dir failed") } for i, image := range d.Images { - if err := os.WriteFile(path.Join(tmpdir, "images", "shim", fmt.Sprintf("image%d", i)), []byte(image), 0644); err != nil { + if err := os.WriteFile(path.Join(tmpdir, "images", "shim", fmt.Sprintf("image%d", i)), []byte(image), 0o644); err != nil { return "", errors.WithMessage(err, "write shim image failed") } } @@ -69,7 +68,7 @@ func (d *RootfsDockerfile) Write() (string, error) { } if d.KubeadmYaml != "" { - if err := os.WriteFile(tmpdir+"/kubeadm.yml", []byte(d.KubeadmYaml), 0644); err != nil { + if err := os.WriteFile(tmpdir+"/kubeadm.yml", []byte(d.KubeadmYaml), 0o644); err != nil { return "", errors.WithMessage(err, "write kubeadm.yml failed") } d.Copys = append(d.Copys, "kubeadm.yml etc/") @@ -87,7 +86,7 @@ func (d *RootfsDockerfile) Write() (string, error) { if d.dockerfileContent == "" { return "", errors.New("dockerfile content is not set") } - if err := os.WriteFile(tmpdir+"/Dockerfile", []byte(d.dockerfileContent), 0644); err != nil { + if err := os.WriteFile(tmpdir+"/Dockerfile", []byte(d.dockerfileContent), 0o644); err != nil { return "", errors.WithMessage(err, "write RootfsDockerfile failed") } return tmpdir, nil diff --git a/lifecycle/test/e2e/testhelper/config/rootfs_test.go b/lifecycle/test/e2e/testhelper/config/rootfs_test.go index c2507b989a12..35953bd97bb7 100644 --- a/lifecycle/test/e2e/testhelper/config/rootfs_test.go +++ b/lifecycle/test/e2e/testhelper/config/rootfs_test.go @@ -38,7 +38,10 @@ func TestDockerfile_Write(t *testing.T) { { name: "default", fields: fields{ - Images: []string{"docker.io/altinity/clickhouse-operator:0.18.4", "docker.io/altinity/metrics-exporter:0.18.4"}, + Images: []string{ + "docker.io/altinity/clickhouse-operator:0.18.4", + "docker.io/altinity/metrics-exporter:0.18.4", + }, }, wantErr: false, }, diff --git a/lifecycle/test/e2e/testhelper/consts/consts.go b/lifecycle/test/e2e/testhelper/consts/consts.go index 9828b7b7555e..3ffb3dc98613 100644 --- a/lifecycle/test/e2e/testhelper/consts/consts.go +++ b/lifecycle/test/e2e/testhelper/consts/consts.go @@ -17,8 +17,8 @@ limitations under the License. package consts const ( - FileMode0755 = 0755 - FileMode0644 = 0644 + FileMode0755 = 0o755 + FileMode0644 = 0o644 ) const ( diff --git a/lifecycle/test/e2e/testhelper/etcd/etcd.go b/lifecycle/test/e2e/testhelper/etcd/etcd.go index a0e375588f06..fc03ecd5c657 100644 --- a/lifecycle/test/e2e/testhelper/etcd/etcd.go +++ b/lifecycle/test/e2e/testhelper/etcd/etcd.go @@ -57,9 +57,23 @@ nohup /tmp/etcd-download-test/etcd --listen-client-urls http://0.0.0.0:2379 \ ` func (e *Etcd) Install() error { - err := os.WriteFile("/tmp/install_etcd.sh", []byte(installScript), 0755) + tmpFile, err := os.CreateTemp("", "install_etcd_*.sh") if err != nil { return err } - return e.AsyncExec("bash", "-c", "/tmp/install_etcd.sh") + defer os.Remove(tmpFile.Name()) + + if _, err := tmpFile.WriteString(installScript); err != nil { + tmpFile.Close() + return err + } + if err := tmpFile.Chmod(0o755); err != nil { + tmpFile.Close() + return err + } + if err := tmpFile.Close(); err != nil { + return err + } + + return e.AsyncExec("bash", "-c", tmpFile.Name()) } diff --git a/lifecycle/test/e2e/testhelper/kube/k8s.go b/lifecycle/test/e2e/testhelper/kube/k8s.go index 5f57662617cf..0f199c5453c2 100644 --- a/lifecycle/test/e2e/testhelper/kube/k8s.go +++ b/lifecycle/test/e2e/testhelper/kube/k8s.go @@ -18,39 +18,42 @@ import ( "context" "net" + "github.com/labring/sealos/pkg/client-go/kubernetes" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" - - "github.com/labring/sealos/pkg/client-go/kubernetes" ) type K8s interface { ListNodes() (*v1.NodeList, error) ListNodesByLabel(label string) (*v1.NodeList, error) ListNodeIPByLabel(label string) ([]net.IP, error) - ListResources(gvr schema.GroupVersionResource, namespace string, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) + ListResources( + gvr schema.GroupVersionResource, + namespace string, + opts metav1.ListOptions, + ) (*unstructured.UnstructuredList, error) } type K8sClient struct { - //client *kubernetes.Clientset + // client *kubernetes.Clientset Client kubernetes.Client } -func NewK8sClient(kubeconfig string, apiServer string) (K8s, error) { +func NewK8sClient(kubeconfig, apiServer string) (K8s, error) { client, err := kubernetes.NewKubernetesClientSkipTLSVerify(kubeconfig, apiServer) if err != nil { return nil, err } - //config, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig) - //if err != nil { + // config, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig) + // if err != nil { // return nil, errors.Wrap(err, "failed to build kube config") //} // - //clientSet, err := kubernetes.NewForConfig(config) + // clientSet, err := kubernetes.NewForConfig(config) //if err != nil { // return nil, err //} @@ -69,7 +72,10 @@ func (c *K8sClient) ListNodes() (*v1.NodeList, error) { } func (c *K8sClient) ListNodesByLabel(label string) (*v1.NodeList, error) { - nodes, err := c.Client.Kubernetes().CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: label}) + nodes, err := c.Client.Kubernetes(). + CoreV1(). + Nodes(). + List(context.TODO(), metav1.ListOptions{LabelSelector: label}) if err != nil { return nil, errors.Wrapf(err, "failed to get cluster nodes") } @@ -92,7 +98,11 @@ func (c *K8sClient) ListNodeIPByLabel(label string) ([]net.IP, error) { return ips, nil } -func (c *K8sClient) ListResources(gvr schema.GroupVersionResource, namespace string, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) { +func (c *K8sClient) ListResources( + gvr schema.GroupVersionResource, + namespace string, + opts metav1.ListOptions, +) (*unstructured.UnstructuredList, error) { resourceClient := c.Client.KubernetesDynamic().Resource(gvr).Namespace(namespace) resourceList, err := resourceClient.List(context.TODO(), opts) if err != nil { diff --git a/lifecycle/test/e2e/testhelper/template/funcmap.go b/lifecycle/test/e2e/testhelper/template/funcmap.go index 963a452659f4..c705fe031067 100644 --- a/lifecycle/test/e2e/testhelper/template/funcmap.go +++ b/lifecycle/test/e2e/testhelper/template/funcmap.go @@ -63,7 +63,7 @@ func funcMap() template.FuncMap { // always return a string, even on marshal error (empty string). // // This is designed to be called from a template. -func toYAML(v interface{}) string { +func toYAML(v any) string { data, err := yaml.Marshal(v) if err != nil { // Swallow errors inside of a template. @@ -78,8 +78,8 @@ func toYAML(v interface{}) string { // YAML documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string into // m["Error"] in the returned map. -func fromYAML(str string) map[string]interface{} { - m := map[string]interface{}{} +func fromYAML(str string) map[string]any { + m := map[string]any{} if err := yaml.Unmarshal([]byte(str), &m); err != nil { m["Error"] = err.Error() @@ -93,11 +93,11 @@ func fromYAML(str string) map[string]interface{} { // YAML documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string as // the first and only item in the returned array. -func fromYAMLArray(str string) []interface{} { - a := []interface{}{} +func fromYAMLArray(str string) []any { + a := []any{} if err := yaml.Unmarshal([]byte(str), &a); err != nil { - a = []interface{}{err.Error()} + a = []any{err.Error()} } return a } @@ -106,7 +106,7 @@ func fromYAMLArray(str string) []interface{} { // always return a string, even on marshal error (empty string). // // This is designed to be called from a template. -func toTOML(v interface{}) string { +func toTOML(v any) string { b := bytes.NewBuffer(nil) e := toml.NewEncoder(b) if err := e.Encode(v); err != nil { @@ -119,7 +119,7 @@ func toTOML(v interface{}) string { // always return a string, even on marshal error (empty string). // // This is designed to be called from a template. -func toJSON(v interface{}) string { +func toJSON(v any) string { data, err := json.Marshal(v) if err != nil { // Swallow errors inside of a template. @@ -134,8 +134,8 @@ func toJSON(v interface{}) string { // JSON documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string into // m["Error"] in the returned map. -func fromJSON(str string) map[string]interface{} { - m := make(map[string]interface{}) +func fromJSON(str string) map[string]any { + m := make(map[string]any) if err := json.Unmarshal([]byte(str), &m); err != nil { m["Error"] = err.Error() @@ -149,11 +149,11 @@ func fromJSON(str string) map[string]interface{} { // JSON documents. Additionally, because its intended use is within templates // it tolerates errors. It will insert the returned error message string as // the first and only item in the returned array. -func fromJSONArray(str string) []interface{} { - a := []interface{}{} +func fromJSONArray(str string) []any { + a := []any{} if err := json.Unmarshal([]byte(str), &a); err != nil { - a = []interface{}{err.Error()} + a = []any{err.Error()} } return a } diff --git a/lifecycle/test/e2e/testhelper/template/template.go b/lifecycle/test/e2e/testhelper/template/template.go index 7f819e4b6477..9fc8c5f4d804 100644 --- a/lifecycle/test/e2e/testhelper/template/template.go +++ b/lifecycle/test/e2e/testhelper/template/template.go @@ -43,7 +43,7 @@ func Must(t *template.Template, err error) *template.Template { return template.Must(t, err) } -func RenderTemplate(name, defaultStr string, data map[string]interface{}) (string, error) { +func RenderTemplate(name, defaultStr string, data map[string]any) (string, error) { var out bytes.Buffer tmpl := template.Must(template.New(name).Parse(defaultStr)) err := tmpl.Execute(&out, data) diff --git a/lifecycle/test/e2e/testhelper/template/template_test.go b/lifecycle/test/e2e/testhelper/template/template_test.go index f898596a73d4..eae284666f45 100644 --- a/lifecycle/test/e2e/testhelper/template/template_test.go +++ b/lifecycle/test/e2e/testhelper/template/template_test.go @@ -32,7 +32,7 @@ version: {{if (semverCompare "^1.26.0" (default "" .ENV)) }}v1{{ else }}v1alpha2 } out := bytes.NewBuffer(nil) - execErr := v.Execute(out, map[string]interface{}{ + execErr := v.Execute(out, map[string]any{ // comment out this to test true return // "ENV": "v1.26.1", // comment out this to test false return diff --git a/lifecycle/test/e2e/testhelper/utils/utils.go b/lifecycle/test/e2e/testhelper/utils/utils.go index ebab69e39984..6d960c512aa4 100644 --- a/lifecycle/test/e2e/testhelper/utils/utils.go +++ b/lifecycle/test/e2e/testhelper/utils/utils.go @@ -28,18 +28,13 @@ import ( "strings" "time" - "github.com/labring/sealos/test/e2e/testhelper/consts" - - utilyaml "k8s.io/apimachinery/pkg/util/yaml" - + "github.com/labring/sealos/pkg/utils/exec" "github.com/labring/sealos/pkg/utils/logger" - + "github.com/labring/sealos/test/e2e/testhelper/consts" "github.com/onsi/ginkgo/v2" - - "github.com/labring/sealos/pkg/utils/exec" - "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" + utilyaml "k8s.io/apimachinery/pkg/util/yaml" "sigs.k8s.io/yaml" ) @@ -75,7 +70,7 @@ func WriteFile(fileName string, content []byte) error { func GetBinArch(filepath string) (string, error) { f, err := elf.Open(filepath) if err != nil { - return "", fmt.Errorf("error opening file: %v", err) + return "", fmt.Errorf("error opening file: %w", err) } defer f.Close() switch f.Machine { @@ -97,7 +92,7 @@ func IsFileExist(filename string) bool { return !os.IsNotExist(err) } -func UnmarshalYamlFile(file string, obj interface{}) error { +func UnmarshalYamlFile(file string, obj any) error { data, err := os.ReadFile(filepath.Clean(file)) if err != nil { return err @@ -106,8 +101,8 @@ func UnmarshalYamlFile(file string, obj interface{}) error { return err } -func UnmarshalData(metadata []byte) (map[string]interface{}, error) { - var data map[string]interface{} +func UnmarshalData(metadata []byte) (map[string]any, error) { + var data map[string]any err := yaml.Unmarshal(metadata, &data) if err != nil { return nil, err @@ -115,7 +110,7 @@ func UnmarshalData(metadata []byte) (map[string]interface{}, error) { return data, nil } -func MarshalYamlToFile(file string, obj interface{}) error { +func MarshalYamlToFile(file string, obj any) error { data, err := yaml.Marshal(obj) if err != nil { return err @@ -125,7 +120,7 @@ func MarshalYamlToFile(file string, obj interface{}) error { // GetFileDataLocally get file data for cloud apply func GetFileDataLocally(filePath string) string { - cmd := fmt.Sprintf("sudo -E cat %s", filePath) + cmd := "sudo -E cat " + filePath result, err := exec.RunBashCmd(cmd) CheckErr(err) return result @@ -133,7 +128,7 @@ func GetFileDataLocally(filePath string) string { // DeleteFileLocally delete file for cloud apply func DeleteFileLocally(filePath string) { - cmd := fmt.Sprintf("sudo -E rm -rf %s", filePath) + cmd := "sudo -E rm -rf " + filePath _, err := exec.RunBashCmd(cmd) CheckErr(err) } @@ -160,51 +155,53 @@ func Log(msg string) { Logf(msg) } -func Logf(format string, args ...interface{}) { +func Logf(format string, args ...any) { fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: "+format, args...) } func Failf(msg string) { - ginkgo.Fail(fmt.Sprintf("FAIL: %s", msg), 1) + ginkgo.Fail("FAIL: "+msg, 1) } -func CheckNotNil(obj interface{}) { +func CheckNotNil(obj any) { gomega.Expect(obj).NotTo(gomega.BeNil()) } -func CheckEqual(obj1 interface{}, obj2 interface{}) { +func CheckEqual(obj1, obj2 any) { gomega.Expect(obj1).To(gomega.Equal(obj2)) } -func CheckNotEqual(obj1 interface{}, obj2 interface{}) { +func CheckNotEqual(obj1, obj2 any) { gomega.Expect(obj1).NotTo(gomega.Equal(obj2)) } func CheckExit0(sess *gexec.Session, waitTime time.Duration) { gomega.Eventually(sess, waitTime).Should(gexec.Exit(0)) } + func CheckNotExit0(sess *gexec.Session, waitTime time.Duration) { gomega.Eventually(sess, waitTime).ShouldNot(gexec.Exit(0)) } func CheckFuncBeTrue(f func() bool, t time.Duration) { - gomega.Eventually(f(), t).Should(gomega.BeTrue()) + gomega.Eventually(f, t).Should(gomega.BeTrue()) } func CheckBeTrue(b bool) { gomega.Eventually(b).Should(gomega.BeTrue()) } + func CheckNotBeTrue(b bool) { gomega.Eventually(b).ShouldNot(gomega.BeTrue()) } func ToYalms(bs string) (yamls []string) { - buf := bytes.NewBuffer([]byte(bs)) + buf := bytes.NewBufferString(bs) reader := utilyaml.NewYAMLReader(bufio.NewReader(buf)) for { patch, err := reader.Read() if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } break @@ -215,7 +212,7 @@ func ToYalms(bs string) (yamls []string) { } yamls = append(yamls, string(patch)) } - return + return yamls } func GetAllSubDirs(rootPath string) ([]string, error) { @@ -239,7 +236,7 @@ func ListLocalHostAddrs() (*[]net.Addr, error) { return nil, err } var allAddrs []net.Addr - for i := 0; i < len(netInterfaces); i++ { + for i := range netInterfaces { if (netInterfaces[i].Flags & net.FlagUp) == 0 { continue } @@ -247,16 +244,15 @@ func ListLocalHostAddrs() (*[]net.Addr, error) { if err != nil { logger.Warn("failed to get Addrs, %s", err.Error()) } - for j := 0; j < len(addrs); j++ { - allAddrs = append(allAddrs, addrs[j]) - } + allAddrs = append(allAddrs, addrs...) } return &allAddrs, nil } func LocalIP(addrs *[]net.Addr) string { for _, address := range *addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && + ipnet.IP.To4() != nil { return ipnet.IP.String() } } @@ -275,5 +271,5 @@ func MkTmpdir(dir string) (string, error) { if err != nil { return "", err } - return tempDir, os.MkdirAll(tempDir, 0755) + return tempDir, os.MkdirAll(tempDir, 0o755) } diff --git a/lifecycle/test/e2e/utils.go b/lifecycle/test/e2e/utils.go index 8701f29f9563..0da0add09918 100644 --- a/lifecycle/test/e2e/utils.go +++ b/lifecycle/test/e2e/utils.go @@ -20,11 +20,10 @@ import ( "fmt" "strings" - "github.com/onsi/gomega" - "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/test/e2e/suites/operators" "github.com/labring/sealos/test/e2e/testhelper/utils" + "github.com/onsi/gomega" ) func checkVersionImageList(fakeClient *operators.FakeClient) error { diff --git a/lifecycle/test/testpg/pgstresstest.go b/lifecycle/test/testpg/pgstresstest.go index ecb338ef92b0..ae9a12cb5219 100644 --- a/lifecycle/test/testpg/pgstresstest.go +++ b/lifecycle/test/testpg/pgstresstest.go @@ -71,13 +71,13 @@ spec: memory: 300Mi ` -//you can also copy this file to your project and run it or run pod on sealos cloud. -//and also you can write a go test to run this Stress Test. -//what you need is to specify a kubeconfig path and your namespace -//example -//func main() { -// PostgresStressTest("~/.kube/config", "ns-b47ef502") -//} +// you can also copy this file to your project and run it or run pod on sealos cloud. +// and also you can write a go test to run this Stress Test. +// what you need is to specify a kubeconfig path and your namespace +// example +// func main() { +// PostgresStressTest("~/.kube/config", "ns-b47ef502") +// } func PostgresStressTest(configpath, userNamespace string) { err := RunPostgresTest(configpath, userNamespace) @@ -94,7 +94,8 @@ func RunPostgresTest(config, userNamespace string) error { err error ) obj := &unstructured.Unstructured{} - _, gvk, err = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode([]byte(metaCRD), nil, obj) + _, gvk, err = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme). + Decode([]byte(metaCRD), nil, obj) if err != nil { return err } @@ -124,17 +125,21 @@ func RunPostgresTest(config, userNamespace string) error { } // PostgresClusterRandomCreate Create random PostgresCluster. -func PostgresClusterRandomCreate(ctx context.Context, dr dynamic.ResourceInterface, obj *unstructured.Unstructured) ([]*unstructured.Unstructured, error) { +func PostgresClusterRandomCreate( + ctx context.Context, + dr dynamic.ResourceInterface, + obj *unstructured.Unstructured, +) ([]*unstructured.Unstructured, error) { var err error var bigN *big.Int if bigN, err = rand.Int(rand.Reader, big.NewInt(5)); err != nil { - return nil, fmt.Errorf("unable to read random bytes for jwt id: %s", err) + return nil, fmt.Errorf("unable to read random bytes for jwt id: %w", err) } n := int(bigN.Int64()) - var objSlice []*unstructured.Unstructured - //Create - for i := 0; i < n; i++ { + objSlice := make([]*unstructured.Unstructured, 0, n) + // Create + for i := range n { objTmp := obj.DeepCopy() objTmp.SetName(objTmp.GetName() + strconv.Itoa(i)) _, err = dr.Create(ctx, objTmp, metav1.CreateOptions{}) @@ -149,17 +154,25 @@ func PostgresClusterRandomCreate(ctx context.Context, dr dynamic.ResourceInterfa } // PostgresGet Get PostgresCluster. -func PostgresGet(ctx context.Context, dr dynamic.ResourceInterface, cs *kubernetes.Clientset, obj *unstructured.Unstructured, userNamespace string) error { +func PostgresGet( + ctx context.Context, + dr dynamic.ResourceInterface, + cs *kubernetes.Clientset, + obj *unstructured.Unstructured, + userNamespace string, +) error { objGET, err := dr.Get(ctx, obj.GetName(), metav1.GetOptions{}) if err != nil { log.Printf("select resource ERROR: %v\n", err) return err } fmt.Println(objGET.Object["status"]) - oobj, ismap := objGET.Object["status"].(map[string]interface{}) + oobj, ismap := objGET.Object["status"].(map[string]any) if ismap { if oobj["state"] != "running" { - events, _ := cs.CoreV1().Events(userNamespace).List(context.TODO(), metav1.ListOptions{TypeMeta: metav1.TypeMeta{Kind: "Event"}}) + events, _ := cs.CoreV1(). + Events(userNamespace). + List(context.TODO(), metav1.ListOptions{TypeMeta: metav1.TypeMeta{Kind: "Event"}}) for _, event := range events.Items { fmt.Println(event.Message) } @@ -170,8 +183,12 @@ func PostgresGet(ctx context.Context, dr dynamic.ResourceInterface, cs *kubernet } // PostgresDelete Delete PostgresCluster. -func PostgresDelete(ctx context.Context, dr dynamic.ResourceInterface, obj *unstructured.Unstructured) { - //Delete +func PostgresDelete( + ctx context.Context, + dr dynamic.ResourceInterface, + obj *unstructured.Unstructured, +) { + // Delete err := dr.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) if err != nil { log.Printf("delete resource ERROR : %v\n", err) @@ -192,7 +209,10 @@ func GetKubeconfig(configpath string) (*restclient.Config, error) { return config, nil } -func GetGVRdyClientAndClientSet(gvk *schema.GroupVersionKind, configpath string, namespace string) (dr dynamic.ResourceInterface, cs *kubernetes.Clientset, err error) { +func GetGVRdyClientAndClientSet( + gvk *schema.GroupVersionKind, + configpath, namespace string, +) (dr dynamic.ResourceInterface, cs *kubernetes.Clientset, err error) { config, err := GetKubeconfig(configpath) if err != nil { log.Printf("failed to get kubeconfig: %v", err) @@ -204,7 +224,9 @@ func GetGVRdyClientAndClientSet(gvk *schema.GroupVersionKind, configpath string, log.Printf("failed create discovery client: %v", err) return nil, nil, err } - mapperGVRGVK := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(discoveryClient)) + mapperGVRGVK := restmapper.NewDeferredDiscoveryRESTMapper( + memory.NewMemCacheClient(discoveryClient), + ) resourceMapper, err := mapperGVRGVK.RESTMapping(gvk.GroupKind(), gvk.Version) if err != nil { log.Printf("failed to get resourceMapper: %v", err) From 1696f08a5135e9f54bd409de2f204be1888b368d Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 12:35:59 +0800 Subject: [PATCH 02/11] feat(build): add support for non-Linux platforms with stub implementations Signed-off-by: cuisongliu --- .golangci.yml | 1 + lifecycle/pkg/buildah/buildah.go | 3 + lifecycle/pkg/buildah/common.go | 3 + lifecycle/pkg/buildah/create.go | 3 + lifecycle/pkg/system/env.go | 3 + lifecycle/pkg/system/env_unsupported.go | 125 +++++++++++++ .../labring/lvscare/care/iptables.go | 3 + .../lvscare/care/iptables_unsupported.go | 172 ++++++++++++++++++ .../github.com/labring/lvscare/care/prober.go | 3 + .../labring/lvscare/care/proxier.go | 3 + .../github.com/labring/lvscare/care/runner.go | 3 + 11 files changed, 322 insertions(+) create mode 100644 lifecycle/pkg/system/env_unsupported.go create mode 100644 lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go diff --git a/.golangci.yml b/.golangci.yml index 1255a98cddae..c2de566f65ac 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,7 @@ run: go: "1.23" relative-path-mode: gomod modules-download-mode: readonly + timeout: 5m issues: max-issues-per-linter: 0 diff --git a/lifecycle/pkg/buildah/buildah.go b/lifecycle/pkg/buildah/buildah.go index 80c570a05954..bb75c6982944 100644 --- a/lifecycle/pkg/buildah/buildah.go +++ b/lifecycle/pkg/buildah/buildah.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 buildah. // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/pkg/buildah/common.go b/lifecycle/pkg/buildah/common.go index ff5417eeaecc..6fafec8657bb 100644 --- a/lifecycle/pkg/buildah/common.go +++ b/lifecycle/pkg/buildah/common.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 buildah. // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/pkg/buildah/create.go b/lifecycle/pkg/buildah/create.go index df491c1b709f..600b7b8ed1e9 100644 --- a/lifecycle/pkg/buildah/create.go +++ b/lifecycle/pkg/buildah/create.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/pkg/system/env.go b/lifecycle/pkg/system/env.go index eccbd0e25a74..2577cfbb71be 100644 --- a/lifecycle/pkg/system/env.go +++ b/lifecycle/pkg/system/env.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + /* Copyright 2023 cuisongliu@qq.com. diff --git a/lifecycle/pkg/system/env_unsupported.go b/lifecycle/pkg/system/env_unsupported.go new file mode 100644 index 000000000000..fa26de5aa8a9 --- /dev/null +++ b/lifecycle/pkg/system/env_unsupported.go @@ -0,0 +1,125 @@ +//go:build !linux +// +build !linux + +/* +Copyright 2023 cuisongliu@qq.com. + +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 system + +import ( + "fmt" + "os" + "path/filepath" +) + +const ( + PromptConfigKey = "PROMPT" + RuntimeRootConfigKey = "RUNTIME_ROOT" + DataRootConfigKey = "DATA_ROOT" + BuildahFormatConfigKey = "BUILDAH_FORMAT" + BuildahLogLevelConfigKey = "BUILDAH_LOG_LEVEL" + ContainerStorageConfEnvKey = "CONTAINER_STORAGE_CONF" + SyncWorkDirEnvKey = "SYNC_WORKDIR" +) + +type ConfigOption struct { + Key string + Description string + DefaultValue string + OSEnv string +} + +type envSystemConfig struct{} + +var globalConfig *envSystemConfig + +func init() { + globalConfig = &envSystemConfig{} +} + +func Get(key string) (string, error) { + return "", fmt.Errorf("system functionality is not supported on non-Linux platforms") +} + +func GetConfig(key string) (*ConfigOption, error) { + return nil, fmt.Errorf("system functionality is not supported on non-Linux platforms") +} + +func ConfigOptions() []ConfigOption { + return []ConfigOption{ + { + Key: PromptConfigKey, + Description: "toggle interactive prompting in the terminal.", + DefaultValue: "enabled", + }, + { + Key: RuntimeRootConfigKey, + Description: "root directory for persistent runtime actions/configs.", + DefaultValue: filepath.Join(os.TempDir(), "sealos"), + }, + { + Key: DataRootConfigKey, + Description: "cluster root directory for remote.", + DefaultValue: filepath.Join(os.TempDir(), "sealos-data"), + }, + } +} + +func NewEnvSystemConfig() *envSystemConfig { + return &envSystemConfig{} +} + +func (e *envSystemConfig) Init() error { + return fmt.Errorf("buildah functionality is not supported on non-Linux platforms") +} + +func (e *envSystemConfig) GetEnvFile() string { + return filepath.Join(os.TempDir(), "sealos", "env") +} + +func (e *envSystemConfig) GetHostsFile() string { + return filepath.Join(os.TempDir(), "sealos", "hosts") +} + +func (e *envSystemConfig) Parse() (map[string]string, error) { + return make(map[string]string), fmt.Errorf("buildah functionality is not supported on non-Linux platforms") +} + +func (e *envSystemConfig) UpdateEnvFile(env string) error { + return fmt.Errorf("buildah functionality is not supported on non-Linux platforms") +} + +func (e *envSystemConfig) Mount() error { + return fmt.Errorf("buildah functionality is not supported on non-Linux platforms") +} + +func (e *envSystemConfig) Umount() error { + return fmt.Errorf("buildah functionality is not supported on non-Linux platforms") +} + +func (e *envSystemConfig) CopyEnvToHosts() error { + return fmt.Errorf("buildah functionality is not supported on non-Linux platforms") +} + +func (e *envSystemConfig) ParseHostname() (string, error) { + hostname, _ := os.Hostname() + return hostname, nil +} + +func (e *envSystemConfig) ParseHosts() ([]string, error) { + hostname, _ := os.Hostname() + return []string{hostname}, nil +} \ No newline at end of file diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go index fbcdfa824473..f42c534075db 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go new file mode 100644 index 000000000000..72bbbc04ea5e --- /dev/null +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go @@ -0,0 +1,172 @@ +//go:build !linux +// +build !linux + +// Copyright © 2022 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 care + +import ( + "context" + "fmt" + "net" + "strconv" + "time" + + "github.com/labring/sealos/pkg/utils/logger" + "github.com/spf13/cobra" +) + +// iptablesImpl provides a stub implementation for non-Linux platforms +type iptablesImpl struct { + ifaceName string +} + +// Ruler interface methods for non-Linux platforms +func newIptablesImpl(iface string, masqueradeBit int, virtualIPs ...string) (Ruler, error) { + logger.Info("iptables functionality is not supported on non-Linux platforms") + return &iptablesImpl{ifaceName: iface}, nil +} + +func (impl *iptablesImpl) Setup() error { + logger.Warn("iptables Setup() is a no-op on non-Linux platform: %s", impl.ifaceName) + return nil +} + +func (impl *iptablesImpl) Cleanup() error { + logger.Warn("iptables Cleanup() is a no-op on non-Linux platform: %s", impl.ifaceName) + return nil +} + +// endpoint represents a service endpoint for non-Linux platforms +type endpoint struct { + Host string + Port uint16 +} + +func (ep *endpoint) String() string { + return fmt.Sprintf("%s:%d", ep.Host, ep.Port) +} + +// Proxier interface defines IPVS load balancer operations +type Proxier interface { + EnsureVirtualServer(vs string) error + DeleteVirtualServer(vs string) error + EnsureRealServer(vs, rs string) error + DeleteRealServer(vs, rs string) error + RunLoop(context.Context) error + TryRun() error +} + +// Prober interface for health checking +type Prober interface { + Probe(addr string, path string) error +} + +// httpProber provides a stub implementation for non-Linux platforms +type httpProber struct{} + +func (p *httpProber) Probe(addr string, path string) error { + logger.Info("HTTP probe is a no-op on non-Linux platform: %s%s", addr, path) + return nil +} + +// Proxier interface stub implementation for non-Linux platforms +func NewProxier(scheduler string, interval time.Duration, prober Prober, periodicRun func() error) Proxier { + logger.Info("IPVS proxier functionality is not supported on non-Linux platforms") + return &stubProxier{prober: prober, periodicRun: periodicRun} +} + +// splitHostPort splits a network address of the form "host:port" into host and port. +func splitHostPort(hostport string) (string, uint16, error) { + host, portStr, err := net.SplitHostPort(hostport) + if err != nil { + return "", 0, err + } + port, err := strconv.ParseUint(portStr, 10, 16) + if err != nil { + return "", 0, err + } + return host, uint16(port), nil +} + +// stubProxier provides a no-op implementation for non-Linux platforms +type stubProxier struct { + prober Prober + periodicRun func() error +} + +func (p *stubProxier) EnsureVirtualServer(vs string) error { + logger.Warn("EnsureVirtualServer is a no-op on non-Linux platform: %s", vs) + return nil +} + +func (p *stubProxier) DeleteVirtualServer(vs string) error { + logger.Warn("DeleteVirtualServer is a no-op on non-Linux platform: %s", vs) + return nil +} + +func (p *stubProxier) EnsureRealServer(vs, rs string) error { + logger.Warn("EnsureRealServer is a no-op on non-Linux platform: vs=%s, rs=%s", vs, rs) + return nil +} + +func (p *stubProxier) DeleteRealServer(vs, rs string) error { + logger.Warn("DeleteRealServer is a no-op on non-Linux platform: vs=%s, rs=%s", vs, rs) + return nil +} + +func (p *stubProxier) RunLoop(ctx context.Context) error { + logger.Info("RunLoop is a no-op on non-Linux platform") + <-ctx.Done() + return ctx.Err() +} + +func (p *stubProxier) TryRun() error { + logger.Info("TryRun is a no-op on non-Linux platform") + if p.periodicRun != nil { + return p.periodicRun() + } + return nil +} + +func (p *stubProxier) Stop() { + logger.Info("Stop is a no-op on non-Linux platform") +} + +// stubOptions provides a stub implementation for non-Linux platforms +type stubOptions struct { + Logger string +} + +func (o *stubOptions) ValidateAndSetDefaults() error { + logger.Info("ValidateAndSetDefaults is a no-op on non-Linux platform") + return nil +} + +// LVS provides a stub implementation for non-Linux platforms +var LVS = &stubRunner{} + +type stubRunner struct { + stubOptions +} + +func (r *stubRunner) Run() error { + logger.Info("LVS Run is a no-op on non-Linux platform") + return nil +} + +func (r *stubRunner) RegisterCommandFlags(cmd *cobra.Command) { + logger.Info("RegisterCommandFlags is a no-op on non-Linux platform") +} \ No newline at end of file diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go b/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go index ba4b73a6c824..4fdb1fa7ddb9 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go index 7ac17b9de020..558e29ff3f91 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go index 5f9d9ced6bc2..ba7e7129f468 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); From df0775a2b0e4935f934f15da341c78506581533a Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 12:40:04 +0800 Subject: [PATCH 03/11] feat(build): add build constraints for Linux platform in multiple files Signed-off-by: cuisongliu --- lifecycle/pkg/buildah/containers.go | 3 +++ lifecycle/pkg/buildah/manifest.go | 3 +++ lifecycle/pkg/buildah/pull.go | 3 +++ lifecycle/pkg/buildah/save.go | 3 +++ .../src/github.com/labring/image-cri-shim/pkg/server/server.go | 1 + 5 files changed, 13 insertions(+) diff --git a/lifecycle/pkg/buildah/containers.go b/lifecycle/pkg/buildah/containers.go index 0a994de017f9..a5a17a19e455 100644 --- a/lifecycle/pkg/buildah/containers.go +++ b/lifecycle/pkg/buildah/containers.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 buildah. // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/pkg/buildah/manifest.go b/lifecycle/pkg/buildah/manifest.go index 578ce9beb828..192bab82ec29 100644 --- a/lifecycle/pkg/buildah/manifest.go +++ b/lifecycle/pkg/buildah/manifest.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 buildah. // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/pkg/buildah/pull.go b/lifecycle/pkg/buildah/pull.go index 305328f4b729..58b0aa11e9e4 100644 --- a/lifecycle/pkg/buildah/pull.go +++ b/lifecycle/pkg/buildah/pull.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 buildah. // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/pkg/buildah/save.go b/lifecycle/pkg/buildah/save.go index 8f7c224af3aa..fb9460a75c55 100644 --- a/lifecycle/pkg/buildah/save.go +++ b/lifecycle/pkg/buildah/save.go @@ -1,3 +1,6 @@ +//go:build linux +// +build linux + // Copyright © 2022 buildah. // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go index 6fde220b35b8..81e8cfada1ac 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/server.go @@ -16,6 +16,7 @@ package server import ( + "errors" "fmt" "net" "os" From 8dbd3258415a1971f6586abdc37345ef62804006 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 17:19:11 +0800 Subject: [PATCH 04/11] refactor: standardize parameter naming and improve code readability across multiple files Signed-off-by: cuisongliu --- lifecycle/go.mod | 2 +- lifecycle/pkg/buildah/runtime.go | 64 +++++++++++++++++++ lifecycle/pkg/buildah/save.go | 3 +- .../image-cri-shim/pkg/server/auth_store.go | 1 + .../image-cri-shim/pkg/server/cri_client.go | 12 ++-- .../image-cri-shim/pkg/server/utils.go | 3 + .../labring/image-cri-shim/pkg/shim/shim.go | 6 +- lifecycle/test/e2e/suites/operators/types.go | 2 +- lifecycle/test/e2e/testhelper/kube/k8s.go | 2 +- 9 files changed, 82 insertions(+), 13 deletions(-) diff --git a/lifecycle/go.mod b/lifecycle/go.mod index 9d0c74364772..bc1d3a2fee09 100644 --- a/lifecycle/go.mod +++ b/lifecycle/go.mod @@ -37,7 +37,6 @@ require ( github.com/spf13/pflag v1.0.5 go.uber.org/zap v1.26.0 golang.org/x/crypto v0.25.0 - golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 golang.org/x/sync v0.7.0 golang.org/x/sys v0.22.0 golang.org/x/term v0.22.0 @@ -245,6 +244,7 @@ require ( go.opentelemetry.io/proto/otlp v1.0.0 // indirect go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect golang.org/x/mod v0.17.0 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/oauth2 v0.10.0 // indirect diff --git a/lifecycle/pkg/buildah/runtime.go b/lifecycle/pkg/buildah/runtime.go index 221f42831107..b52d31811a30 100644 --- a/lifecycle/pkg/buildah/runtime.go +++ b/lifecycle/pkg/buildah/runtime.go @@ -27,6 +27,7 @@ import ( imagestorage "github.com/containers/image/v5/storage" "github.com/containers/image/v5/types" "github.com/containers/storage" + "github.com/containers/storage/pkg/archive" "github.com/spf13/cobra" ) @@ -163,3 +164,66 @@ func (r *Runtime) getLayerIDs(from, to string, diffType DiffType) (string, strin } return fromLayer, toLayer, nil } + +// Changes returns a list of changes between two layers. +func (r *Runtime) Changes(from, to string) ([]archive.Change, error) { + return r.Store.Changes(from, to) +} + +// Image returns a specific image from the store. +func (r *Runtime) Image(id string) (*storage.Image, error) { + return r.Store.Image(id) +} + +// DifferTarget gets the path to the differ target for a layer. +func (r *Runtime) DifferTarget(id string) (string, error) { + return r.Store.DifferTarget(id) +} + +// Load loads images from a file into the storage. +func (r *Runtime) Load( + ctx context.Context, + path string, + options *libimage.LoadOptions, +) ([]string, error) { + return r.Runtime.Load(ctx, path, options) +} + +// LookupImage looks up an image by name or ID in the storage. +func (r *Runtime) LookupImage( + name string, + options *libimage.LookupImageOptions, +) (*libimage.Image, string, error) { + return r.Runtime.LookupImage(name, options) +} + +// Container returns a specific container from the storage. +func (r *Runtime) Container(id string) (*storage.Container, error) { + return r.Store.Container(id) +} + +// Layer returns a specific layer from the storage. +func (r *Runtime) Layer(id string) (*storage.Layer, error) { + return r.Store.Layer(id) +} + +// Pull pulls images from a registry into the storage. +func (r *Runtime) Pull( + ctx context.Context, + name string, + pullPolicy config.PullPolicy, + options *libimage.PullOptions, +) ([]*libimage.Image, error) { + return r.Runtime.Pull(ctx, name, pullPolicy, options) +} + +// Save saves images to a file from the storage. +func (r *Runtime) Save( + ctx context.Context, + names []string, + format, output string, + options *libimage.SaveOptions, +) ([]string, error) { + err := r.Runtime.Save(ctx, names, format, output, options) + return names, err +} diff --git a/lifecycle/pkg/buildah/save.go b/lifecycle/pkg/buildah/save.go index fb9460a75c55..c9c0072e7be4 100644 --- a/lifecycle/pkg/buildah/save.go +++ b/lifecycle/pkg/buildah/save.go @@ -133,5 +133,6 @@ func runSave(cmd *cobra.Command, args []string, saveOpts *saveOptions) error { } else { saveOptions.AdditionalTags = tags } - return r.Save(getContext(), names, saveOpts.format, saveOpts.output, saveOptions) + _, err = r.Save(getContext(), names, saveOpts.format, saveOpts.output, saveOptions) + return err } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go index 4e45c274023a..2e5de2872f51 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/auth_store.go @@ -89,6 +89,7 @@ func cloneAuthMap(src map[string]rtype.AuthConfig) map[string]rtype.AuthConfig { return dst } +//nolint:unused // utility function for future use func cloneSkipMap(src map[string]bool) map[string]bool { if len(src) == 0 { return map[string]bool{} diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go index c70694aed4fe..1f1dc59ac7a9 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/cri_client.go @@ -52,11 +52,11 @@ type ConnectOptions struct { // Client is the interface we expose to our CRI client. type Client interface { // Connect tries to connect the client to the specified image and runtime services. - Connect(ConnectOptions) (*grpc.ClientConn, error) + Connect(opts ConnectOptions) (*grpc.ClientConn, error) // Close closes any existing client connections. Close() // CheckConnection checks if we have (un-Close()'d as opposed to working) connections. - CheckConnection(ConnectOptions) error + CheckConnection(opts ConnectOptions) error // HasRuntimeService checks if the client is configured with runtime services. HasRuntimeService() bool // HasImageService checks if the client is configured with image services. @@ -88,10 +88,10 @@ func NewClient(options CRIClientOptions) (Client, error) { } // Connect attempts to establish gRPC client connections to the configured services. -func (c *client) Connect(options ConnectOptions) (*grpc.ClientConn, error) { +func (c *client) Connect(opts ConnectOptions) (*grpc.ClientConn, error) { kind, socket := "image services", c.options.ImageSocket - icc, err := c.connect(kind, socket, options) + icc, err := c.connect(kind, socket, opts) if err != nil { return nil, err } @@ -110,14 +110,14 @@ func (c *client) Close() { } // CheckConnection if the connecton to CRI services is up, try to reconnect if requested. -func (c *client) CheckConnection(options ConnectOptions) error { +func (c *client) CheckConnection(opts ConnectOptions) error { if c.icc == nil || c.icc.GetState() == connectivity.Ready { return nil } c.Close() - if options.Reconnect { + if opts.Reconnect { if _, err := c.Connect(ConnectOptions{Wait: false}); err == nil { return nil } diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go index ffaaf927e4b3..8e252be56cb5 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/server/utils.go @@ -58,6 +58,7 @@ func replaceImage(image, action string, authConfig map[string]registry.AuthConfi return newImage, true, cfg } +//nolint:unused // utility function for future use func registryFromImage(image string) string { parts := strings.SplitN(image, "/", 2) if len(parts) == 0 { @@ -66,6 +67,7 @@ func registryFromImage(image string) string { return parts[0] } +//nolint:unused // utility function for future use func shouldSkipAuth(domain string, cfg *registry.AuthConfig, skip map[string]bool) bool { if skip != nil && skip[domain] { return true @@ -77,6 +79,7 @@ func shouldSkipAuth(domain string, cfg *registry.AuthConfig, skip map[string]boo cfg.RegistryToken == "" } +//nolint:unused // utility function for future use func referenceSuffix(ref name.Reference) string { switch v := ref.(type) { case name.Tag: diff --git a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go index 7028bf7e9b98..e9250cae5692 100644 --- a/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go +++ b/lifecycle/staging/src/github.com/labring/image-cri-shim/pkg/shim/shim.go @@ -40,7 +40,7 @@ type Shim interface { // Stop stops the shim. Stop() // UpdateAuth refreshes registry credentials without restarting the shim. - UpdateAuth(*types.ShimAuthConfig) + UpdateAuth(cfg *types.ShimAuthConfig) } // shim is the implementation of Shim. @@ -117,11 +117,11 @@ func (r *shim) Stop() { r.server.Stop() } -func (r *shim) UpdateAuth(auth *types.ShimAuthConfig) { +func (r *shim) UpdateAuth(cfg *types.ShimAuthConfig) { if r.authStore == nil { return } - r.authStore.Update(auth) + r.authStore.Update(cfg) } func (r *shim) dialNotify(socket string, uid, gid int, mode os.FileMode, err error) { diff --git a/lifecycle/test/e2e/suites/operators/types.go b/lifecycle/test/e2e/suites/operators/types.go index 69eeda7772d1..987be117a006 100644 --- a/lifecycle/test/e2e/suites/operators/types.go +++ b/lifecycle/test/e2e/suites/operators/types.go @@ -39,7 +39,7 @@ type PodStruct struct { // if err != nil { // return nil, err //} - //return &pod, nil + // return &pod, nil Items []struct { ID string `json:"id"` Metadata struct { diff --git a/lifecycle/test/e2e/testhelper/kube/k8s.go b/lifecycle/test/e2e/testhelper/kube/k8s.go index 0f199c5453c2..cb514d95a80b 100644 --- a/lifecycle/test/e2e/testhelper/kube/k8s.go +++ b/lifecycle/test/e2e/testhelper/kube/k8s.go @@ -54,7 +54,7 @@ func NewK8sClient(kubeconfig, apiServer string) (K8s, error) { //} // // clientSet, err := kubernetes.NewForConfig(config) - //if err != nil { + // if err != nil { // return nil, err //} From ad72e5a156c863b4e59076fd6b5b536edb9896cf Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 17:44:49 +0800 Subject: [PATCH 05/11] refactor: improve error handling and standardize parameter naming in multiple files Signed-off-by: cuisongliu --- .../staging/src/github.com/labring/lvscare/care/interface.go | 2 +- .../staging/src/github.com/labring/lvscare/care/iptables.go | 4 ++-- .../github.com/labring/lvscare/care/iptables_unsupported.go | 4 ++-- .../staging/src/github.com/labring/lvscare/care/prober.go | 2 +- .../staging/src/github.com/labring/lvscare/care/proxier.go | 2 +- .../staging/src/github.com/labring/lvscare/care/route.go | 1 + .../staging/src/github.com/labring/lvscare/pkg/route/route.go | 4 ++-- .../src/github.com/labring/lvscare/pkg/version/version.go | 2 +- 8 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/interface.go b/lifecycle/staging/src/github.com/labring/lvscare/care/interface.go index 50f159c2974d..0c237dc84840 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/interface.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/interface.go @@ -17,7 +17,7 @@ package care import "github.com/spf13/pflag" type flagRegisterer interface { - RegisterFlags(*pflag.FlagSet) + RegisterFlags(fs *pflag.FlagSet) } type flagValidator interface { diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go index f42c534075db..e2f94dfc8932 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables.go @@ -119,7 +119,7 @@ func newIptablesImpl(iface string, masqueradeBit int, virtualIPs ...string) (Rul virtualEntries = append(virtualEntries, entry.String()) } - masqueradeValue := 1 << uint(masqueradeBit) + masqueradeValue := uint(1 << masqueradeBit) execer := exec.New() return &iptablesImpl{ @@ -194,7 +194,7 @@ func (impl *iptablesImpl) cleanupLeftovers() (encounteredError bool) { func ensureSysctl(sysctl utilsysctl.Interface, name string, newVal int) error { if oldVal, _ := sysctl.GetSysctl(name); oldVal != newVal { if err := sysctl.SetSysctl(name, newVal); err != nil { - return fmt.Errorf("can't set sysctl %s to %d: %v", name, newVal, err) + return fmt.Errorf("can't set sysctl %s to %d: %w", name, newVal, err) } logger.Debug("changed sysctl %s: before %d, after: %d", name, oldVal, newVal) } diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go index 72bbbc04ea5e..72a65a5097fa 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/iptables_unsupported.go @@ -77,8 +77,8 @@ type Prober interface { // httpProber provides a stub implementation for non-Linux platforms type httpProber struct{} -func (p *httpProber) Probe(addr string, path string) error { - logger.Info("HTTP probe is a no-op on non-Linux platform: %s%s", addr, path) +func (p *httpProber) Probe(host, port string) error { + logger.Info("HTTP probe is a no-op on non-Linux platform: %s%s", host, port) return nil } diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go b/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go index 4fdb1fa7ddb9..6a9819ec0475 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/prober.go @@ -34,7 +34,7 @@ import ( ) type Prober interface { - Probe(string, string) error + Probe(host, port string) error } type httpProber struct { diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go index 558e29ff3f91..a2df0f39ebd0 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go @@ -34,7 +34,7 @@ type Proxier interface { DeleteVirtualServer(vs string) error EnsureRealServer(vs, rs string) error DeleteRealServer(vs, rs string) error - RunLoop(context.Context) error + RunLoop(ctx context.Context) error TryRun() error } diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/route.go b/lifecycle/staging/src/github.com/labring/lvscare/care/route.go index 771cbee195f0..64e922338f46 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/route.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/route.go @@ -23,6 +23,7 @@ type routeImpl struct { *route.Route } +//nolint:unparam // error return is kept for interface compatibility func newRouteImpl(target, gw string) (Ruler, error) { return &routeImpl{ route.New(target, gw), diff --git a/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go b/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go index 1022cd2c6a84..2067b4ac6624 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/pkg/route/route.go @@ -56,7 +56,7 @@ func (r *Route) SetRoute() error { err := addRouteGatewayViaHost(r.Host, r.Gateway, 50) if err != nil && !errors.Is(err, os.ErrExist) /* return if route already exist */ { - return fmt.Errorf("failed to add %s route gateway via host err: %v", r.Host, err) + return fmt.Errorf("failed to add %s route gateway via host err: %w", r.Host, err) } logger.Info("success to set route.(host:%s, gateway:%s)", r.Host, r.Gateway) return nil @@ -69,7 +69,7 @@ func (r *Route) DelRoute() error { err := delRouteGatewayViaHost(r.Host, r.Gateway) if err != nil && !errors.Is(err, syscall.ESRCH) /* return if route does not exist */ { - return fmt.Errorf("failed to delete %s route gateway via host err: %v", r.Host, err) + return fmt.Errorf("failed to delete %s route gateway via host err: %w", r.Host, err) } logger.Info("success to del route.(host:%s, gateway:%s)", r.Host, r.Gateway) return nil diff --git a/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go b/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go index 051b683224c0..f61c4af6c9bc 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/pkg/version/version.go @@ -63,7 +63,7 @@ func parse(str string, semver bool) (*Version, error) { num, err := strconv.ParseUint(comp, 10, 0) if err != nil { return nil, fmt.Errorf( - "illegal non-numeric version component %q in %q: %v", + "illegal non-numeric version component %q in %q: %w", comp, str, err, From 5627934edb3a7c54f773fb862ea9980963e65c84 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 18:06:30 +0800 Subject: [PATCH 06/11] feat(retry): add retry logic for IPVS service and real server management Signed-off-by: cuisongliu --- lifecycle/pkg/utils/retry/retry.go | 46 ++++++++ .../labring/lvscare/care/proxier.go | 103 +++++++++++++----- .../github.com/labring/lvscare/care/runner.go | 19 +++- 3 files changed, 140 insertions(+), 28 deletions(-) diff --git a/lifecycle/pkg/utils/retry/retry.go b/lifecycle/pkg/utils/retry/retry.go index b225f8d96473..6f6f80b38e58 100644 --- a/lifecycle/pkg/utils/retry/retry.go +++ b/lifecycle/pkg/utils/retry/retry.go @@ -31,3 +31,49 @@ func Retry(tryTimes int, trySleepTime time.Duration, action func() error) error } return fmt.Errorf("retry action timeout: %w", err) } + +// RetryWithBackoff provides exponential backoff retry with jitter +func RetryWithBackoff(maxAttempts int, baseDelay time.Duration, action func() error) error { + var lastErr error + for attempt := 0; attempt < maxAttempts; attempt++ { + if err := action(); err == nil { + return nil + } else { + lastErr = err + } + + if attempt < maxAttempts-1 { + // Exponential backoff with jitter + delay := baseDelay * time.Duration(1< 5*time.Second { + delay = 5 * time.Second // cap the delay + } + // Add some jitter to avoid thundering herd + jitter := time.Duration(float64(delay) * 0.1 * (0.5 + 0.5*float64(attempt%2))) + time.Sleep(delay + jitter) + } + } + return fmt.Errorf("retry action failed after %d attempts: %w", maxAttempts, lastErr) +} + +// RetryConditionally retries only when the condition returns true +func RetryConditionally(maxAttempts int, baseDelay time.Duration, condition func(error) bool, action func() error) error { + var lastErr error + for attempt := 0; attempt < maxAttempts; attempt++ { + if err := action(); err == nil { + return nil + } else { + lastErr = err + if !condition(err) { + // Don't retry if condition is not met + break + } + } + + if attempt < maxAttempts-1 { + delay := baseDelay * time.Duration(2*attempt+1) + time.Sleep(delay) + } + } + return fmt.Errorf("retry action failed after %d attempts: %w", maxAttempts, lastErr) +} diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go index a2df0f39ebd0..28208461d0e8 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/proxier.go @@ -20,12 +20,14 @@ package care import ( "context" "errors" + "fmt" "net" "strconv" "sync" "time" "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/pkg/utils/retry" ipvs "k8s.io/kubernetes/pkg/proxy/ipvs/util" ) @@ -79,21 +81,32 @@ type realProxier struct { } func (p *realProxier) ensureVirtualServer(vs *ipvs.VirtualServer) (*ipvs.VirtualServer, error) { - applied, _ := p.ipvsHandle.GetVirtualServer(vs) - if applied == nil { - logger.Debug("Add new IPVS service", vs.String()) - if err := p.ipvsHandle.AddVirtualServer(vs); err != nil { - logger.Error("Failed to add IPVS service: %v", err) - return nil, err - } - } else if !applied.Equal(vs) { - logger.Debug("IPVS service is changed %s", applied.String()) - if err := p.ipvsHandle.UpdateVirtualServer(vs); err != nil { - logger.Error("Failed to update IPVS service: %v", err) - return nil, err + var result *ipvs.VirtualServer + + retryErr := retry.Retry(3, 100*time.Millisecond, func() error { + applied, _ := p.ipvsHandle.GetVirtualServer(vs) + if applied == nil { + logger.Debug("Add new IPVS service", vs.String()) + if err := p.ipvsHandle.AddVirtualServer(vs); err != nil { + logger.Error("Failed to add IPVS service: %v", err) + return fmt.Errorf("failed to add IPVS service: %w", err) + } + } else if !applied.Equal(vs) { + logger.Debug("IPVS service is changed %s", applied.String()) + if err := p.ipvsHandle.UpdateVirtualServer(vs); err != nil { + logger.Error("Failed to update IPVS service: %v", err) + return fmt.Errorf("failed to update IPVS service: %w", err) + } } + result = vs + return nil + }) + + if retryErr != nil { + return nil, retryErr } - return vs, nil + + return result, nil } func (p *realProxier) EnsureVirtualServer(vs string) error { @@ -179,11 +192,21 @@ func (p *realProxier) EnsureRealServer(vs, rs string) error { if rSrv != nil { return nil } + + // Add retry logic for adding real server rSrv = p.buildRealServer(&rsEp) - if err = p.ipvsHandle.AddRealServer(vSrv, rSrv); err != nil { - logger.Error("Failed to add real server: %v", err) - return err + retryErr := retry.Retry(3, 100*time.Millisecond, func() error { + if err = p.ipvsHandle.AddRealServer(vSrv, rSrv); err != nil { + logger.Error("Failed to add real server: %v", err) + return fmt.Errorf("failed to add real server: %w", err) + } + return nil + }) + + if retryErr != nil { + return retryErr } + return nil } @@ -264,33 +287,61 @@ func (p *realProxier) checkRealServer(wg *sync.WaitGroup, vSrv *ipvs.VirtualServ logger.Debug("probe error: %v", probeErr) if rSrv != nil { if rSrv.Weight != 0 { - logger.Debug("Trying to update wight to 0 for graceful termination") + logger.Debug("Trying to update weight to 0 for graceful termination") rSrv.Weight = 0 - if err = p.ipvsHandle.UpdateRealServer(vSrv, rSrv); err != nil { - logger.Warn("Failed to update real server wight: %v", err) + // Add retry logic for weight update + if retryErr := retry.Retry(2, 50*time.Millisecond, func() error { + if err = p.ipvsHandle.UpdateRealServer(vSrv, rSrv); err != nil { + logger.Warn("Failed to update real server weight: %v", err) + return fmt.Errorf("failed to update real server weight: %w", err) + } + return nil + }); retryErr != nil { + logger.Warn("Failed to update real server weight after retries: %v", retryErr) } return } logger.Debug("Trying to delete real server") - if err = p.ipvsHandle.DeleteRealServer(vSrv, rSrv); err != nil { - logger.Warn("Failed to delete real server: %v", err) + // Add retry logic for deletion + if retryErr := retry.Retry(2, 50*time.Millisecond, func() error { + if err = p.ipvsHandle.DeleteRealServer(vSrv, rSrv); err != nil { + logger.Warn("Failed to delete real server: %v", err) + return fmt.Errorf("failed to delete real server: %w", err) + } + return nil + }); retryErr != nil { + logger.Warn("Failed to delete real server after retries: %v", retryErr) } } return } if rSrv != nil { if rSrv.Weight == 0 { - logger.Debug("Trying to update wight to 1 to receive traffic") + logger.Debug("Trying to update weight to 1 to receive traffic") rSrv.Weight = 1 - if err = p.ipvsHandle.UpdateRealServer(vSrv, rSrv); err != nil { - logger.Warn("Failed to update real server wight: %v", err) + // Add retry logic for weight update + if retryErr := retry.Retry(2, 50*time.Millisecond, func() error { + if err = p.ipvsHandle.UpdateRealServer(vSrv, rSrv); err != nil { + logger.Warn("Failed to update real server weight: %v", err) + return fmt.Errorf("failed to update real server weight: %w", err) + } + return nil + }); retryErr != nil { + logger.Warn("Failed to update real server weight after retries: %v", retryErr) } } return } logger.Debug("Trying to add real server back") - if err = p.ipvsHandle.AddRealServer(vSrv, p.buildRealServer(&rs)); err != nil { - logger.Warn("Failed to add real server back: %v", err) + // Add retry logic for adding back + if retryErr := retry.Retry(2, 50*time.Millisecond, func() error { + if err = p.ipvsHandle.AddRealServer(vSrv, p.buildRealServer(&rs)); err != nil { + logger.Warn("Failed to add real server back: %v", err) + return fmt.Errorf("failed to add real server back: %w", err) + } + return nil + }); retryErr != nil { + logger.Warn("Failed to add real server back after retries: %v", retryErr) } } diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go index ba7e7129f468..3ab5b707b93e 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go @@ -24,6 +24,7 @@ import ( "time" "github.com/labring/sealos/pkg/utils/logger" + "github.com/labring/sealos/pkg/utils/retry" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/manager/signals" ) @@ -96,11 +97,25 @@ func (r *runner) Run() (err error) { // run once at startup func (r *runner) ensureIPVSRules() error { - if err := r.proxier.EnsureVirtualServer(r.options.VirtualServer); err != nil { + // Add retry logic for virtual server setup + if err := retry.Retry(3, 200*time.Millisecond, func() error { + if err := r.proxier.EnsureVirtualServer(r.options.VirtualServer); err != nil { + return fmt.Errorf("failed to ensure virtual server %s: %w", r.options.VirtualServer, err) + } + return nil + }); err != nil { return err } + + // Add retry logic for each real server for i := range r.options.RealServer { - if err := r.proxier.EnsureRealServer(r.options.VirtualServer, r.options.RealServer[i]); err != nil { + rs := r.options.RealServer[i] // create local variable to avoid closure issues + if err := retry.Retry(3, 200*time.Millisecond, func() error { + if err := r.proxier.EnsureRealServer(r.options.VirtualServer, rs); err != nil { + return fmt.Errorf("failed to ensure real server %s: %w", rs, err) + } + return nil + }); err != nil { return err } } From 50f64bc7312a9548c5f5c2ffef59ed3140c6e0da Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 19:16:17 +0800 Subject: [PATCH 07/11] refactor: improve error messages and string formatting in multiple files Signed-off-by: cuisongliu --- lifecycle/cmd/sealctl/cmd/static_pod.go | 4 ++-- lifecycle/pkg/utils/hosts/hosts.go | 7 +++---- .../staging/src/github.com/labring/lvscare/care/options.go | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lifecycle/cmd/sealctl/cmd/static_pod.go b/lifecycle/cmd/sealctl/cmd/static_pod.go index 53327bab5fc1..ee81fbaf8e9c 100755 --- a/lifecycle/cmd/sealctl/cmd/static_pod.go +++ b/lifecycle/cmd/sealctl/cmd/static_pod.go @@ -86,7 +86,7 @@ func newLvscareCmd() *cobra.Command { } func genNewPod(obj lvscarePod) error { - fileName := fmt.Sprintf("%s.%s", obj.name, constants.YamlFileSuffix) + fileName := obj.name + "." + constants.YamlFileSuffix yaml, err := ipvs.LvsStaticPodYaml(obj.vip, obj.master, obj.image, obj.name, obj.options) if err != nil { return err @@ -108,7 +108,7 @@ func genNewPod(obj lvscarePod) error { } func setNewPodImage(obj lvscarePod) error { - fileName := fmt.Sprintf("%s.%s", obj.name, constants.YamlFileSuffix) + fileName := obj.name + "." + constants.YamlFileSuffix podPath := path.Join(staticPodPath, fileName) if file.IsExist(podPath) { pod := &v1.Pod{} diff --git a/lifecycle/pkg/utils/hosts/hosts.go b/lifecycle/pkg/utils/hosts/hosts.go index 7cce7d322e2d..729a085a844d 100644 --- a/lifecycle/pkg/utils/hosts/hosts.go +++ b/lifecycle/pkg/utils/hosts/hosts.go @@ -16,7 +16,6 @@ package hosts import ( "bufio" - "errors" "fmt" "io" "net" @@ -64,8 +63,8 @@ func appendToFile(filePath string, hostname *hostname) { func (h *HostFile) ParseHostFile(path string) (*linkedhashmap.Map, error) { if !file.IsExist(path) { - logger.Warn("path %s is not exists", path) - return nil, errors.New("path %s is not exists") + logger.Warn("path %s does not exist", path) + return nil, fmt.Errorf("path %s does not exist", path) } fp, fpErr := os.Open(path) @@ -122,7 +121,7 @@ func (h *HostFile) AppendHost(domain, ip string) { func (h *HostFile) writeToFile(hostnameMap *linkedhashmap.Map, path string) { if !file.IsExist(path) { - logger.Warn("path %s is not exists", path) + logger.Warn("path %s does not exist", path) return } diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/options.go b/lifecycle/staging/src/github.com/labring/lvscare/care/options.go index aa68cf395316..01dd9235f274 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/options.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/options.go @@ -61,7 +61,7 @@ func (o *options) RegisterFlags(fs *pflag.FlagSet) { "name of dummy interface to created, same behavior as kube-proxy", ) fs.StringVar(&o.Logger, "logger", "INFO", "logger level: DEBG/INFO") - fs.StringVar(&o.Mode, "mode", routeMode, fmt.Sprintf("proxy mode: %s/%s", routeMode, linkMode)) + fs.StringVar(&o.Mode, "mode", routeMode, "proxy mode: "+routeMode+"/"+linkMode) fs.BoolVar(&o.RunOnce, "run-once", false, "create proxy rules and exit") fs.BoolVarP(&o.CleanAndExit, "clean", "C", false, "clean existing rules and then exit") fs.Var(&o.Interval, "interval", "health check interval") From 2d5973e884e27b46c41dee1961edca9246221b94 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 19:39:15 +0800 Subject: [PATCH 08/11] refactor: simplify parameter access and improve code readability in retry and runner modules Signed-off-by: cuisongliu --- lifecycle/pkg/utils/retry/retry.go | 13 ++++--- .../github.com/labring/lvscare/care/runner.go | 34 +++++++++---------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lifecycle/pkg/utils/retry/retry.go b/lifecycle/pkg/utils/retry/retry.go index 6f6f80b38e58..6ee124030ec9 100644 --- a/lifecycle/pkg/utils/retry/retry.go +++ b/lifecycle/pkg/utils/retry/retry.go @@ -35,7 +35,7 @@ func Retry(tryTimes int, trySleepTime time.Duration, action func() error) error // RetryWithBackoff provides exponential backoff retry with jitter func RetryWithBackoff(maxAttempts int, baseDelay time.Duration, action func() error) error { var lastErr error - for attempt := 0; attempt < maxAttempts; attempt++ { + for attempt := range maxAttempts { if err := action(); err == nil { return nil } else { @@ -44,7 +44,7 @@ func RetryWithBackoff(maxAttempts int, baseDelay time.Duration, action func() er if attempt < maxAttempts-1 { // Exponential backoff with jitter - delay := baseDelay * time.Duration(1< 5*time.Second { delay = 5 * time.Second // cap the delay } @@ -57,9 +57,14 @@ func RetryWithBackoff(maxAttempts int, baseDelay time.Duration, action func() er } // RetryConditionally retries only when the condition returns true -func RetryConditionally(maxAttempts int, baseDelay time.Duration, condition func(error) bool, action func() error) error { +func RetryConditionally( + maxAttempts int, + baseDelay time.Duration, + condition func(error) bool, + action func() error, +) error { var lastErr error - for attempt := 0; attempt < maxAttempts; attempt++ { + for attempt := range maxAttempts { if err := action(); err == nil { return nil } else { diff --git a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go index 3ab5b707b93e..c46ae2e39854 100644 --- a/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go +++ b/lifecycle/staging/src/github.com/labring/lvscare/care/runner.go @@ -44,7 +44,7 @@ type runner struct { } func (r *runner) Run() (err error) { - if !r.options.RunOnce { + if !r.RunOnce { defer func() { cleanupErr := r.cleanup() if err != nil { @@ -62,14 +62,14 @@ func (r *runner) Run() (err error) { } cleanVirtualServer := func() error { - logger.Info("delete IPVS service %s", r.options.VirtualServer) - err := r.proxier.DeleteVirtualServer(r.options.VirtualServer) + logger.Info("delete IPVS service %s", r.VirtualServer) + err := r.proxier.DeleteVirtualServer(r.VirtualServer) if err != nil { logger.Warn("failed to delete IPVS service: %v", err) } return err } - if r.options.CleanAndExit { + if r.CleanAndExit { r.cleanupFuncs = append(r.cleanupFuncs, cleanVirtualServer) if r.ruler != nil { r.cleanupFuncs = append(r.cleanupFuncs, r.ruler.Cleanup) @@ -99,8 +99,8 @@ func (r *runner) Run() (err error) { func (r *runner) ensureIPVSRules() error { // Add retry logic for virtual server setup if err := retry.Retry(3, 200*time.Millisecond, func() error { - if err := r.proxier.EnsureVirtualServer(r.options.VirtualServer); err != nil { - return fmt.Errorf("failed to ensure virtual server %s: %w", r.options.VirtualServer, err) + if err := r.proxier.EnsureVirtualServer(r.VirtualServer); err != nil { + return fmt.Errorf("failed to ensure virtual server %s: %w", r.VirtualServer, err) } return nil }); err != nil { @@ -108,10 +108,10 @@ func (r *runner) ensureIPVSRules() error { } // Add retry logic for each real server - for i := range r.options.RealServer { - rs := r.options.RealServer[i] // create local variable to avoid closure issues + for i := range r.RealServer { + rs := r.RealServer[i] // create local variable to avoid closure issues if err := retry.Retry(3, 200*time.Millisecond, func() error { - if err := r.proxier.EnsureRealServer(r.options.VirtualServer, rs); err != nil { + if err := r.proxier.EnsureRealServer(r.VirtualServer, rs); err != nil { return fmt.Errorf("failed to ensure real server %s: %w", rs, err) } return nil @@ -163,12 +163,12 @@ func (r *runner) ValidateAndSetDefaults() error { } } r.proxier = NewProxier( - r.options.scheduler, - time.Duration(r.options.Interval), + r.scheduler, + time.Duration(r.Interval), r.prober, r.periodicRun, ) - virtualIP, _, err := splitHostPort(r.options.VirtualServer) + virtualIP, _, err := splitHostPort(r.VirtualServer) if err != nil { return err } @@ -176,16 +176,16 @@ func (r *runner) ValidateAndSetDefaults() error { var ruler Ruler switch r.Mode { case routeMode: - if r.options.TargetIP == nil { + if r.TargetIP == nil { logger.Warn("running routeMode and Target IP is not valid IP, skipping") break } - ruler, err = newRouteImpl(virtualIP, r.options.TargetIP.String()) + ruler, err = newRouteImpl(virtualIP, r.TargetIP.String()) case linkMode: ruler, err = newIptablesImpl( - r.options.IfaceName, - r.options.MasqueradeBit, - r.options.VirtualServer, + r.IfaceName, + r.MasqueradeBit, + r.VirtualServer, ) case "": // do nothing, disable ruler From 20b0450b287341dfac96459dbd892e1d413e85b5 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 21:25:07 +0800 Subject: [PATCH 09/11] refactor: capture loop variables to avoid closure issues in goroutines Signed-off-by: cuisongliu --- lifecycle/pkg/apply/applydrivers/apply_drivers_default.go | 1 + lifecycle/pkg/apply/processor/create.go | 1 + lifecycle/pkg/apply/processor/delete.go | 1 + lifecycle/pkg/apply/processor/install.go | 1 + lifecycle/pkg/apply/processor/scale.go | 1 + lifecycle/pkg/buildah/manifest.go | 2 +- 6 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go b/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go index 487d65750d33..273a65d350cb 100644 --- a/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go +++ b/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go @@ -337,6 +337,7 @@ func (c *Applier) syncWorkdir() { } eg, _ := errgroup.WithContext(context.Background()) for _, ipAddr := range ipList { + ipAddr := ipAddr eg.Go(func() error { return execer.Copy(ipAddr, workDir, workDir) }) diff --git a/lifecycle/pkg/apply/processor/create.go b/lifecycle/pkg/apply/processor/create.go index a967a110af65..29abffeb6d14 100644 --- a/lifecycle/pkg/apply/processor/create.go +++ b/lifecycle/pkg/apply/processor/create.go @@ -121,6 +121,7 @@ func (c *CreateProcessor) RunConfig(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunConfig in CreateProcessor.") eg, _ := errgroup.WithContext(context.Background()) for _, cManifest := range cluster.Status.Mounts { + cManifest := cManifest eg.Go(func() error { cfg := config.NewConfiguration( cManifest.ImageName, diff --git a/lifecycle/pkg/apply/processor/delete.go b/lifecycle/pkg/apply/processor/delete.go index 5598e108f052..31d97ceaf89c 100644 --- a/lifecycle/pkg/apply/processor/delete.go +++ b/lifecycle/pkg/apply/processor/delete.go @@ -115,6 +115,7 @@ func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error { func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error { eg, _ := errgroup.WithContext(context.Background()) for _, mount := range cluster.Status.Mounts { + mount := mount eg.Go(func() error { return d.Buildah.Delete(mount.Name) }) diff --git a/lifecycle/pkg/apply/processor/install.go b/lifecycle/pkg/apply/processor/install.go index 6ded0f5dfadd..114f71257c91 100644 --- a/lifecycle/pkg/apply/processor/install.go +++ b/lifecycle/pkg/apply/processor/install.go @@ -240,6 +240,7 @@ func (c *InstallProcessor) RunConfig(_ *v2.Cluster) error { } eg, _ := errgroup.WithContext(context.Background()) for _, cManifest := range c.NewMounts { + cManifest := cManifest eg.Go(func() error { cfg := config.NewConfiguration( cManifest.ImageName, diff --git a/lifecycle/pkg/apply/processor/scale.go b/lifecycle/pkg/apply/processor/scale.go index bcb05d1e8874..9c7ec243bf91 100644 --- a/lifecycle/pkg/apply/processor/scale.go +++ b/lifecycle/pkg/apply/processor/scale.go @@ -264,6 +264,7 @@ func (c *ScaleProcessor) RunConfig(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunConfig in ScaleProcessor.") eg, _ := errgroup.WithContext(context.Background()) for _, cManifest := range cluster.Status.Mounts { + cManifest := cManifest eg.Go(func() error { cfg := config.NewConfiguration( cManifest.ImageName, diff --git a/lifecycle/pkg/buildah/manifest.go b/lifecycle/pkg/buildah/manifest.go index 192bab82ec29..8670600c00e4 100644 --- a/lifecycle/pkg/buildah/manifest.go +++ b/lifecycle/pkg/buildah/manifest.go @@ -988,7 +988,7 @@ func manifestInspect( if latestErr == nil { latestErr = e } else { - latestErr = fmt.Errorf("tried %w: %w", e, latestErr) + latestErr = fmt.Errorf("tried %v: %w", e, latestErr) } } From f7daeeda82b1fb15306cdb20f5c1596593c4cb1c Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 18 Oct 2025 21:40:03 +0800 Subject: [PATCH 10/11] refactor: capture loop variables to avoid closure issues in goroutines across multiple files Signed-off-by: cuisongliu --- lifecycle/cmd/sealos/cmd/exec.go | 1 + lifecycle/cmd/sealos/cmd/scp.go | 1 + lifecycle/pkg/buildah/create.go | 1 + lifecycle/pkg/filesystem/rootfs/rootfs_default.go | 3 +++ lifecycle/pkg/runtime/k3s/bootstrap.go | 1 + lifecycle/pkg/runtime/k3s/k3s.go | 1 + lifecycle/pkg/runtime/kubernetes/certs.go | 1 + lifecycle/pkg/runtime/kubernetes/kubeconfig.go | 1 + lifecycle/pkg/runtime/kubernetes/master.go | 2 ++ lifecycle/pkg/runtime/kubernetes/node.go | 2 ++ lifecycle/pkg/runtime/kubernetes/reset.go | 1 + lifecycle/pkg/runtime/kubernetes/runtime_getter.go | 1 + lifecycle/pkg/runtime/kubernetes/static_files.go | 1 + lifecycle/pkg/runtime/kubernetes/utils.go | 1 + 14 files changed, 18 insertions(+) diff --git a/lifecycle/cmd/sealos/cmd/exec.go b/lifecycle/cmd/sealos/cmd/exec.go index d7e1206f1538..b9233c014f80 100644 --- a/lifecycle/cmd/sealos/cmd/exec.go +++ b/lifecycle/cmd/sealos/cmd/exec.go @@ -89,6 +89,7 @@ func runCommand(cluster *v2.Cluster, targets, args []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, ipAddr := range targets { + ipAddr := ipAddr eg.Go(func() error { return execer.CmdAsync(ipAddr, args...) }) diff --git a/lifecycle/cmd/sealos/cmd/scp.go b/lifecycle/cmd/sealos/cmd/scp.go index b780447e2b84..7a4d53e191c6 100644 --- a/lifecycle/cmd/sealos/cmd/scp.go +++ b/lifecycle/cmd/sealos/cmd/scp.go @@ -73,6 +73,7 @@ func runCopy(cluster *v1beta1.Cluster, targets, args []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, ipAddr := range targets { + ipAddr := ipAddr eg.Go(func() error { return execer.Copy(ipAddr, args[0], args[1]) }) diff --git a/lifecycle/pkg/buildah/create.go b/lifecycle/pkg/buildah/create.go index 600b7b8ed1e9..2d87196e387f 100644 --- a/lifecycle/pkg/buildah/create.go +++ b/lifecycle/pkg/buildah/create.go @@ -162,6 +162,7 @@ func runRender(mountPoints, env []string) error { envs := maps.FromSlice(env) for _, mountPoint := range mountPoints { + mountPoint := mountPoint eg.Go(func() error { if !file.IsExist(mountPoint) { logger.Debug("MountPoint %s does not exist, skipping", mountPoint) diff --git a/lifecycle/pkg/filesystem/rootfs/rootfs_default.go b/lifecycle/pkg/filesystem/rootfs/rootfs_default.go index 74a73236c27c..57720b7c7c9a 100644 --- a/lifecycle/pkg/filesystem/rootfs/rootfs_default.go +++ b/lifecycle/pkg/filesystem/rootfs/rootfs_default.go @@ -58,6 +58,7 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error envProcessor := env.NewEnvProcessor(cluster) // TODO: remove this when rendering on client side is GA for _, mount := range f.mounts { + mount := mount eg.Go(func() error { if !file.IsExist(mount.MountPoint) { logger.Debug("Image %s not exist, render env continue", mount.ImageName) @@ -120,6 +121,7 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error rootfsEnvs := v2.MergeEnvWithBuiltinKeys(rootfs.Env, *rootfs) for _, ip := range ipList { + ip := ip eg.Go(func() error { var renderingRequired bool for i := range f.mounts { @@ -191,6 +193,7 @@ func (f *defaultRootfs) unmountRootfs(cluster *v2.Cluster, ipList []string) erro } for _, ip := range ipList { + ip := ip eg.Go(func() error { return execer.CmdAsync(ip, rmRootfs, deleteHomeDirCmd) }) diff --git a/lifecycle/pkg/runtime/k3s/bootstrap.go b/lifecycle/pkg/runtime/k3s/bootstrap.go index 884f5a59d83d..79a8e6486b20 100644 --- a/lifecycle/pkg/runtime/k3s/bootstrap.go +++ b/lifecycle/pkg/runtime/k3s/bootstrap.go @@ -268,6 +268,7 @@ func (k *K3s) copyKubeConfigFileToNodes(hosts ...string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, node := range hosts { + node := node eg.Go(func() error { home, err := k.execer.CmdToString(node, "echo $HOME", "") if err != nil { diff --git a/lifecycle/pkg/runtime/k3s/k3s.go b/lifecycle/pkg/runtime/k3s/k3s.go index c1a0ca37e3e7..ced7759d3137 100644 --- a/lifecycle/pkg/runtime/k3s/k3s.go +++ b/lifecycle/pkg/runtime/k3s/k3s.go @@ -135,6 +135,7 @@ func (k *K3s) SyncNodeIPVS(mastersIPList, nodeIPList []string) error { image := k.cluster.GetLvscareImage() eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodeIPList { + node := node eg.Go(func() error { logger.Info("start to sync lvscare static pod to node: %s master: %+v", node, masters) err := k.remoteUtil.StaticPod( diff --git a/lifecycle/pkg/runtime/kubernetes/certs.go b/lifecycle/pkg/runtime/kubernetes/certs.go index 565b4b64a034..f0da3877aec5 100644 --- a/lifecycle/pkg/runtime/kubernetes/certs.go +++ b/lifecycle/pkg/runtime/kubernetes/certs.go @@ -192,6 +192,7 @@ func (k *KubeadmRuntime) deleteAPIServer() error { logger.Info("delete pod apiserver from crictl") eg, _ := errgroup.WithContext(context.Background()) for _, master := range k.getMasterIPAndPortList() { + master := master eg.Go(func() error { podIDJson, err := k.sshCmdToString(master, podIDSh) if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/kubeconfig.go b/lifecycle/pkg/runtime/kubernetes/kubeconfig.go index 253e87c9ca7b..89af8ad0fe7d 100644 --- a/lifecycle/pkg/runtime/kubernetes/kubeconfig.go +++ b/lifecycle/pkg/runtime/kubernetes/kubeconfig.go @@ -29,6 +29,7 @@ func (k *KubeadmRuntime) copyKubeConfigFileToNodes(hosts ...string) error { src := k.pathResolver.AdminFile() eg, _ := errgroup.WithContext(context.Background()) for _, node := range hosts { + node := node eg.Go(func() error { home, err := k.execer.CmdToString(node, "echo $HOME", "") if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/master.go b/lifecycle/pkg/runtime/kubernetes/master.go index c8a1e07c4678..1779d29eafca 100644 --- a/lifecycle/pkg/runtime/kubernetes/master.go +++ b/lifecycle/pkg/runtime/kubernetes/master.go @@ -114,6 +114,7 @@ func (k *KubeadmRuntime) imagePull(hostAndPort, version string) error { func (k *KubeadmRuntime) sendJoinCPConfig(joinMaster []string) error { eg, _ := errgroup.WithContext(context.Background()) for _, master := range joinMaster { + master := master eg.Go(func() error { k.mu.Lock() defer k.mu.Unlock() @@ -224,6 +225,7 @@ func (k *KubeadmRuntime) deleteMasters(masters []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, master := range masters { + master := master eg.Go(func() error { logger.Info("start to delete master %s", master) if err := k.deleteMaster(master); err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/node.go b/lifecycle/pkg/runtime/kubernetes/node.go index 0fee4c9769bf..f7de44ede25d 100644 --- a/lifecycle/pkg/runtime/kubernetes/node.go +++ b/lifecycle/pkg/runtime/kubernetes/node.go @@ -41,6 +41,7 @@ func (k *KubeadmRuntime) joinNodes(newNodesIPList []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, node := range newNodesIPList { + node := node eg.Go(func() error { logger.Info("start to join %s as worker", node) k.mu.Lock() @@ -101,6 +102,7 @@ func (k *KubeadmRuntime) deleteNodes(nodes []string) error { } eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodes { + node := node eg.Go(func() error { logger.Info("start to delete worker %s", node) if err := k.deleteNode(node); err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/reset.go b/lifecycle/pkg/runtime/kubernetes/reset.go index 6e6d175d6ab5..b4a2acb0dd3c 100644 --- a/lifecycle/pkg/runtime/kubernetes/reset.go +++ b/lifecycle/pkg/runtime/kubernetes/reset.go @@ -42,6 +42,7 @@ func (k *KubeadmRuntime) resetNodes(nodes []string) { logger.Info("start to reset nodes: %v", nodes) eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodes { + node := node eg.Go(func() error { if err := k.resetNode(node, nil); err != nil { logger.Error("delete node %s failed %v", node, err) diff --git a/lifecycle/pkg/runtime/kubernetes/runtime_getter.go b/lifecycle/pkg/runtime/kubernetes/runtime_getter.go index c32ccdc4f3d6..8c6505386954 100644 --- a/lifecycle/pkg/runtime/kubernetes/runtime_getter.go +++ b/lifecycle/pkg/runtime/kubernetes/runtime_getter.go @@ -103,6 +103,7 @@ func (k *KubeadmRuntime) syncNodeIPVSYaml(masterIPs, nodesIPs []string) error { eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodesIPs { + node := node eg.Go(func() error { logger.Info("start to sync lvscare static pod to node: %s master: %+v", node, masters) err := k.execIPVSPod(node, masters) diff --git a/lifecycle/pkg/runtime/kubernetes/static_files.go b/lifecycle/pkg/runtime/kubernetes/static_files.go index 183bed12356e..4bc43f735713 100644 --- a/lifecycle/pkg/runtime/kubernetes/static_files.go +++ b/lifecycle/pkg/runtime/kubernetes/static_files.go @@ -58,6 +58,7 @@ func (k *KubeadmRuntime) copyStaticFiles(nodes []string) error { ) eg, _ := errgroup.WithContext(context.Background()) for _, host := range nodes { + host := host eg.Go(func() error { err := k.sshCmdAsync(host, cmdLinkStatic) if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/utils.go b/lifecycle/pkg/runtime/kubernetes/utils.go index 77df4f2fde04..9081a3482a2c 100644 --- a/lifecycle/pkg/runtime/kubernetes/utils.go +++ b/lifecycle/pkg/runtime/kubernetes/utils.go @@ -61,6 +61,7 @@ func (k *KubeadmRuntime) sendNewCertAndKey(hosts []string) error { func (k *KubeadmRuntime) sendFileToHosts(hosts []string, src, dst string) error { eg, _ := errgroup.WithContext(context.Background()) for _, node := range hosts { + node := node eg.Go(func() error { if err := k.sshCopy(node, src, dst); err != nil { return fmt.Errorf("send file failed %w", err) From 28bcd9d5bdfacea2d43cf9d28af9d39b87328e89 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 20 Oct 2025 11:18:51 +0800 Subject: [PATCH 11/11] refactor: capture loop variables to avoid closure issues in goroutines across multiple files Signed-off-by: cuisongliu --- lifecycle/cmd/sealos/cmd/exec.go | 6 +++--- lifecycle/cmd/sealos/cmd/scp.go | 6 +++--- .../pkg/apply/applydrivers/apply_drivers_default.go | 6 +++--- lifecycle/pkg/apply/processor/create.go | 4 ++-- lifecycle/pkg/apply/processor/delete.go | 4 ++-- lifecycle/pkg/apply/processor/install.go | 4 ++-- lifecycle/pkg/apply/processor/scale.go | 4 ++-- lifecycle/pkg/buildah/create.go | 10 +++++----- lifecycle/pkg/buildah/manifest.go | 2 +- lifecycle/pkg/filesystem/rootfs/rootfs_default.go | 12 ++++++------ lifecycle/pkg/runtime/k3s/bootstrap.go | 4 ++-- lifecycle/pkg/runtime/k3s/k3s.go | 4 ++-- lifecycle/pkg/runtime/kubernetes/certs.go | 5 +++-- lifecycle/pkg/runtime/kubernetes/kubeconfig.go | 4 ++-- lifecycle/pkg/runtime/kubernetes/master.go | 8 ++++---- lifecycle/pkg/runtime/kubernetes/node.go | 8 ++++---- lifecycle/pkg/runtime/kubernetes/reset.go | 4 ++-- lifecycle/pkg/runtime/kubernetes/runtime_getter.go | 4 ++-- lifecycle/pkg/runtime/kubernetes/static_files.go | 4 ++-- lifecycle/pkg/runtime/kubernetes/utils.go | 4 ++-- 20 files changed, 54 insertions(+), 53 deletions(-) diff --git a/lifecycle/cmd/sealos/cmd/exec.go b/lifecycle/cmd/sealos/cmd/exec.go index b9233c014f80..80b85376f661 100644 --- a/lifecycle/cmd/sealos/cmd/exec.go +++ b/lifecycle/cmd/sealos/cmd/exec.go @@ -88,10 +88,10 @@ func runCommand(cluster *v2.Cluster, targets, args []string) error { return err } eg, _ := errgroup.WithContext(context.Background()) - for _, ipAddr := range targets { - ipAddr := ipAddr + for i := range targets { + addr := targets[i] eg.Go(func() error { - return execer.CmdAsync(ipAddr, args...) + return execer.CmdAsync(addr, args...) }) } return eg.Wait() diff --git a/lifecycle/cmd/sealos/cmd/scp.go b/lifecycle/cmd/sealos/cmd/scp.go index 7a4d53e191c6..211cbe01edb8 100644 --- a/lifecycle/cmd/sealos/cmd/scp.go +++ b/lifecycle/cmd/sealos/cmd/scp.go @@ -72,10 +72,10 @@ func runCopy(cluster *v1beta1.Cluster, targets, args []string) error { return err } eg, _ := errgroup.WithContext(context.Background()) - for _, ipAddr := range targets { - ipAddr := ipAddr + for i := range targets { + addr := targets[i] eg.Go(func() error { - return execer.Copy(ipAddr, args[0], args[1]) + return execer.Copy(addr, args[0], args[1]) }) } if err = eg.Wait(); err != nil { diff --git a/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go b/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go index 273a65d350cb..fc5444417a50 100644 --- a/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go +++ b/lifecycle/pkg/apply/applydrivers/apply_drivers_default.go @@ -336,10 +336,10 @@ func (c *Applier) syncWorkdir() { logger.Error("failed to create ssh client: %v", err) } eg, _ := errgroup.WithContext(context.Background()) - for _, ipAddr := range ipList { - ipAddr := ipAddr + for i := range ipList { + addr := ipList[i] eg.Go(func() error { - return execer.Copy(ipAddr, workDir, workDir) + return execer.Copy(addr, workDir, workDir) }) } if err := eg.Wait(); err != nil { diff --git a/lifecycle/pkg/apply/processor/create.go b/lifecycle/pkg/apply/processor/create.go index 29abffeb6d14..3b5e1b8c9a84 100644 --- a/lifecycle/pkg/apply/processor/create.go +++ b/lifecycle/pkg/apply/processor/create.go @@ -120,8 +120,8 @@ func (c *CreateProcessor) preProcess(cluster *v2.Cluster) error { func (c *CreateProcessor) RunConfig(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunConfig in CreateProcessor.") eg, _ := errgroup.WithContext(context.Background()) - for _, cManifest := range cluster.Status.Mounts { - cManifest := cManifest + for i := range cluster.Status.Mounts { + cManifest := cluster.Status.Mounts[i] eg.Go(func() error { cfg := config.NewConfiguration( cManifest.ImageName, diff --git a/lifecycle/pkg/apply/processor/delete.go b/lifecycle/pkg/apply/processor/delete.go index 31d97ceaf89c..4af688ab0b4b 100644 --- a/lifecycle/pkg/apply/processor/delete.go +++ b/lifecycle/pkg/apply/processor/delete.go @@ -114,8 +114,8 @@ func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error { func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error { eg, _ := errgroup.WithContext(context.Background()) - for _, mount := range cluster.Status.Mounts { - mount := mount + for i := range cluster.Status.Mounts { + mount := cluster.Status.Mounts[i] eg.Go(func() error { return d.Buildah.Delete(mount.Name) }) diff --git a/lifecycle/pkg/apply/processor/install.go b/lifecycle/pkg/apply/processor/install.go index 114f71257c91..e8d7b57f15cf 100644 --- a/lifecycle/pkg/apply/processor/install.go +++ b/lifecycle/pkg/apply/processor/install.go @@ -239,8 +239,8 @@ func (c *InstallProcessor) RunConfig(_ *v2.Cluster) error { return nil } eg, _ := errgroup.WithContext(context.Background()) - for _, cManifest := range c.NewMounts { - cManifest := cManifest + for i := range c.NewMounts { + cManifest := c.NewMounts[i] eg.Go(func() error { cfg := config.NewConfiguration( cManifest.ImageName, diff --git a/lifecycle/pkg/apply/processor/scale.go b/lifecycle/pkg/apply/processor/scale.go index 9c7ec243bf91..07a08e8e73a4 100644 --- a/lifecycle/pkg/apply/processor/scale.go +++ b/lifecycle/pkg/apply/processor/scale.go @@ -263,8 +263,8 @@ func (c *ScaleProcessor) PreProcessImage(cluster *v2.Cluster) error { func (c *ScaleProcessor) RunConfig(cluster *v2.Cluster) error { logger.Info("Executing pipeline RunConfig in ScaleProcessor.") eg, _ := errgroup.WithContext(context.Background()) - for _, cManifest := range cluster.Status.Mounts { - cManifest := cManifest + for i := range cluster.Status.Mounts { + cManifest := cluster.Status.Mounts[i] eg.Go(func() error { cfg := config.NewConfiguration( cManifest.ImageName, diff --git a/lifecycle/pkg/buildah/create.go b/lifecycle/pkg/buildah/create.go index 2d87196e387f..27d045567aaf 100644 --- a/lifecycle/pkg/buildah/create.go +++ b/lifecycle/pkg/buildah/create.go @@ -161,14 +161,14 @@ func runRender(mountPoints, env []string) error { eg, _ := errgroup.WithContext(context.Background()) envs := maps.FromSlice(env) - for _, mountPoint := range mountPoints { - mountPoint := mountPoint + for i := range mountPoints { + path := mountPoints[i] eg.Go(func() error { - if !file.IsExist(mountPoint) { - logger.Debug("MountPoint %s does not exist, skipping", mountPoint) + if !file.IsExist(path) { + logger.Debug("MountPoint %s does not exist, skipping", path) return nil } - return stringsutil.RenderTemplatesWithEnv(mountPoint, envs) + return stringsutil.RenderTemplatesWithEnv(path, envs) }) } diff --git a/lifecycle/pkg/buildah/manifest.go b/lifecycle/pkg/buildah/manifest.go index 8670600c00e4..6185c30d29df 100644 --- a/lifecycle/pkg/buildah/manifest.go +++ b/lifecycle/pkg/buildah/manifest.go @@ -988,7 +988,7 @@ func manifestInspect( if latestErr == nil { latestErr = e } else { - latestErr = fmt.Errorf("tried %v: %w", e, latestErr) + latestErr = errors.Join(fmt.Errorf("tried %w", e), latestErr) } } diff --git a/lifecycle/pkg/filesystem/rootfs/rootfs_default.go b/lifecycle/pkg/filesystem/rootfs/rootfs_default.go index 57720b7c7c9a..fd846132257d 100644 --- a/lifecycle/pkg/filesystem/rootfs/rootfs_default.go +++ b/lifecycle/pkg/filesystem/rootfs/rootfs_default.go @@ -57,8 +57,8 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error eg, _ := errgroup.WithContext(ctx) envProcessor := env.NewEnvProcessor(cluster) // TODO: remove this when rendering on client side is GA - for _, mount := range f.mounts { - mount := mount + for i := range f.mounts { + mount := f.mounts[i] eg.Go(func() error { if !file.IsExist(mount.MountPoint) { logger.Debug("Image %s not exist, render env continue", mount.ImageName) @@ -120,8 +120,8 @@ func (f *defaultRootfs) mountRootfs(cluster *v2.Cluster, ipList []string) error } rootfsEnvs := v2.MergeEnvWithBuiltinKeys(rootfs.Env, *rootfs) - for _, ip := range ipList { - ip := ip + for i := range ipList { + ip := ipList[i] eg.Go(func() error { var renderingRequired bool for i := range f.mounts { @@ -192,8 +192,8 @@ func (f *defaultRootfs) unmountRootfs(cluster *v2.Cluster, ipList []string) erro return err } - for _, ip := range ipList { - ip := ip + for i := range ipList { + ip := ipList[i] eg.Go(func() error { return execer.CmdAsync(ip, rmRootfs, deleteHomeDirCmd) }) diff --git a/lifecycle/pkg/runtime/k3s/bootstrap.go b/lifecycle/pkg/runtime/k3s/bootstrap.go index 79a8e6486b20..8454bbfb013f 100644 --- a/lifecycle/pkg/runtime/k3s/bootstrap.go +++ b/lifecycle/pkg/runtime/k3s/bootstrap.go @@ -267,8 +267,8 @@ func (k *K3s) copyKubeConfigFileToNodes(hosts ...string) error { return errors.WithMessage(err, "write admin.config file failed") } eg, _ := errgroup.WithContext(context.Background()) - for _, node := range hosts { - node := node + for i := range hosts { + node := hosts[i] eg.Go(func() error { home, err := k.execer.CmdToString(node, "echo $HOME", "") if err != nil { diff --git a/lifecycle/pkg/runtime/k3s/k3s.go b/lifecycle/pkg/runtime/k3s/k3s.go index ced7759d3137..3fa395236daa 100644 --- a/lifecycle/pkg/runtime/k3s/k3s.go +++ b/lifecycle/pkg/runtime/k3s/k3s.go @@ -134,8 +134,8 @@ func (k *K3s) SyncNodeIPVS(mastersIPList, nodeIPList []string) error { } image := k.cluster.GetLvscareImage() eg, _ := errgroup.WithContext(context.Background()) - for _, node := range nodeIPList { - node := node + for i := range nodeIPList { + node := nodeIPList[i] eg.Go(func() error { logger.Info("start to sync lvscare static pod to node: %s master: %+v", node, masters) err := k.remoteUtil.StaticPod( diff --git a/lifecycle/pkg/runtime/kubernetes/certs.go b/lifecycle/pkg/runtime/kubernetes/certs.go index f0da3877aec5..2efec84477c6 100644 --- a/lifecycle/pkg/runtime/kubernetes/certs.go +++ b/lifecycle/pkg/runtime/kubernetes/certs.go @@ -191,8 +191,9 @@ func (k *KubeadmRuntime) deleteAPIServer() error { } logger.Info("delete pod apiserver from crictl") eg, _ := errgroup.WithContext(context.Background()) - for _, master := range k.getMasterIPAndPortList() { - master := master + masters := k.getMasterIPAndPortList() + for i := range masters { + master := masters[i] eg.Go(func() error { podIDJson, err := k.sshCmdToString(master, podIDSh) if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/kubeconfig.go b/lifecycle/pkg/runtime/kubernetes/kubeconfig.go index 89af8ad0fe7d..8bb412e0fb68 100644 --- a/lifecycle/pkg/runtime/kubernetes/kubeconfig.go +++ b/lifecycle/pkg/runtime/kubernetes/kubeconfig.go @@ -28,8 +28,8 @@ const copyKubeAdminConfigCommand = `rm -rf $HOME/.kube/config && mkdir -p $HOME/ func (k *KubeadmRuntime) copyKubeConfigFileToNodes(hosts ...string) error { src := k.pathResolver.AdminFile() eg, _ := errgroup.WithContext(context.Background()) - for _, node := range hosts { - node := node + for i := range hosts { + node := hosts[i] eg.Go(func() error { home, err := k.execer.CmdToString(node, "echo $HOME", "") if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/master.go b/lifecycle/pkg/runtime/kubernetes/master.go index 1779d29eafca..d16a80c7cc1a 100644 --- a/lifecycle/pkg/runtime/kubernetes/master.go +++ b/lifecycle/pkg/runtime/kubernetes/master.go @@ -113,8 +113,8 @@ func (k *KubeadmRuntime) imagePull(hostAndPort, version string) error { // sendJoinCPConfig send join CP masters configuration func (k *KubeadmRuntime) sendJoinCPConfig(joinMaster []string) error { eg, _ := errgroup.WithContext(context.Background()) - for _, master := range joinMaster { - master := master + for i := range joinMaster { + master := joinMaster[i] eg.Go(func() error { k.mu.Lock() defer k.mu.Unlock() @@ -224,8 +224,8 @@ func (k *KubeadmRuntime) deleteMasters(masters []string) error { return nil } eg, _ := errgroup.WithContext(context.Background()) - for _, master := range masters { - master := master + for i := range masters { + master := masters[i] eg.Go(func() error { logger.Info("start to delete master %s", master) if err := k.deleteMaster(master); err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/node.go b/lifecycle/pkg/runtime/kubernetes/node.go index f7de44ede25d..d1be0e1741f4 100644 --- a/lifecycle/pkg/runtime/kubernetes/node.go +++ b/lifecycle/pkg/runtime/kubernetes/node.go @@ -40,8 +40,8 @@ func (k *KubeadmRuntime) joinNodes(newNodesIPList []string) error { return err } eg, _ := errgroup.WithContext(context.Background()) - for _, node := range newNodesIPList { - node := node + for i := range newNodesIPList { + node := newNodesIPList[i] eg.Go(func() error { logger.Info("start to join %s as worker", node) k.mu.Lock() @@ -101,8 +101,8 @@ func (k *KubeadmRuntime) deleteNodes(nodes []string) error { return nil } eg, _ := errgroup.WithContext(context.Background()) - for _, node := range nodes { - node := node + for i := range nodes { + node := nodes[i] eg.Go(func() error { logger.Info("start to delete worker %s", node) if err := k.deleteNode(node); err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/reset.go b/lifecycle/pkg/runtime/kubernetes/reset.go index b4a2acb0dd3c..1fa823837621 100644 --- a/lifecycle/pkg/runtime/kubernetes/reset.go +++ b/lifecycle/pkg/runtime/kubernetes/reset.go @@ -41,8 +41,8 @@ func (k *KubeadmRuntime) reset() error { func (k *KubeadmRuntime) resetNodes(nodes []string) { logger.Info("start to reset nodes: %v", nodes) eg, _ := errgroup.WithContext(context.Background()) - for _, node := range nodes { - node := node + for i := range nodes { + node := nodes[i] eg.Go(func() error { if err := k.resetNode(node, nil); err != nil { logger.Error("delete node %s failed %v", node, err) diff --git a/lifecycle/pkg/runtime/kubernetes/runtime_getter.go b/lifecycle/pkg/runtime/kubernetes/runtime_getter.go index 8c6505386954..1fcbf9d3e6b7 100644 --- a/lifecycle/pkg/runtime/kubernetes/runtime_getter.go +++ b/lifecycle/pkg/runtime/kubernetes/runtime_getter.go @@ -102,8 +102,8 @@ func (k *KubeadmRuntime) syncNodeIPVSYaml(masterIPs, nodesIPs []string) error { } eg, _ := errgroup.WithContext(context.Background()) - for _, node := range nodesIPs { - node := node + for i := range nodesIPs { + node := nodesIPs[i] eg.Go(func() error { logger.Info("start to sync lvscare static pod to node: %s master: %+v", node, masters) err := k.execIPVSPod(node, masters) diff --git a/lifecycle/pkg/runtime/kubernetes/static_files.go b/lifecycle/pkg/runtime/kubernetes/static_files.go index 4bc43f735713..f83820dcf091 100644 --- a/lifecycle/pkg/runtime/kubernetes/static_files.go +++ b/lifecycle/pkg/runtime/kubernetes/static_files.go @@ -57,8 +57,8 @@ func (k *KubeadmRuntime) copyStaticFiles(nodes []string) error { filepath.Join(file.DestinationDir, file.Name), ) eg, _ := errgroup.WithContext(context.Background()) - for _, host := range nodes { - host := host + for i := range nodes { + host := nodes[i] eg.Go(func() error { err := k.sshCmdAsync(host, cmdLinkStatic) if err != nil { diff --git a/lifecycle/pkg/runtime/kubernetes/utils.go b/lifecycle/pkg/runtime/kubernetes/utils.go index 9081a3482a2c..0466a7d03ad5 100644 --- a/lifecycle/pkg/runtime/kubernetes/utils.go +++ b/lifecycle/pkg/runtime/kubernetes/utils.go @@ -60,8 +60,8 @@ func (k *KubeadmRuntime) sendNewCertAndKey(hosts []string) error { func (k *KubeadmRuntime) sendFileToHosts(hosts []string, src, dst string) error { eg, _ := errgroup.WithContext(context.Background()) - for _, node := range hosts { - node := node + for i := range hosts { + node := hosts[i] eg.Go(func() error { if err := k.sshCopy(node, src, dst); err != nil { return fmt.Errorf("send file failed %w", err)