From 1ab122a52428a3cc870fd1b2ecfcb9187f01c6b9 Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Tue, 7 May 2024 17:04:25 +0900 Subject: [PATCH] fix lint --- cmd/tortoisectl/commands/stop.go | 9 ++++--- cmd/tortoisectl/test/tortoisectl_test.go | 10 ++++--- go.mod | 3 --- go.sum | 9 ------- pkg/stoper/stoper.go | 34 ++++++++++++++---------- 5 files changed, 32 insertions(+), 33 deletions(-) diff --git a/cmd/tortoisectl/commands/stop.go b/cmd/tortoisectl/commands/stop.go index 9f38552f..f730c621 100644 --- a/cmd/tortoisectl/commands/stop.go +++ b/cmd/tortoisectl/commands/stop.go @@ -5,10 +5,6 @@ import ( "os" "path/filepath" - autoscalingv1beta3 "github.com/mercari/tortoise/api/v1beta3" - "github.com/mercari/tortoise/pkg/deployment" - "github.com/mercari/tortoise/pkg/pod" - "github.com/mercari/tortoise/pkg/stoper" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -18,6 +14,11 @@ import ( "k8s.io/client-go/tools/record" "k8s.io/client-go/util/homedir" "sigs.k8s.io/controller-runtime/pkg/client" + + autoscalingv1beta3 "github.com/mercari/tortoise/api/v1beta3" + "github.com/mercari/tortoise/pkg/deployment" + "github.com/mercari/tortoise/pkg/pod" + "github.com/mercari/tortoise/pkg/stoper" ) var stopCmd = &cobra.Command{ diff --git a/cmd/tortoisectl/test/tortoisectl_test.go b/cmd/tortoisectl/test/tortoisectl_test.go index 9e6ceb80..6912d6aa 100644 --- a/cmd/tortoisectl/test/tortoisectl_test.go +++ b/cmd/tortoisectl/test/tortoisectl_test.go @@ -15,8 +15,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/mercari/tortoise/api/v1beta3" - "github.com/mercari/tortoise/pkg/annotation" appv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -27,6 +25,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" "sigs.k8s.io/yaml" + + "github.com/mercari/tortoise/api/v1beta3" + "github.com/mercari/tortoise/pkg/annotation" ) func buildTortoiseCtl(t *testing.T) { @@ -219,10 +220,13 @@ func Test_TortoiseCtlStop(t *testing.T) { } for namespace := range namespaces { - wait.PollUntilContextCancel(context.Background(), 500*time.Millisecond, true, func(ctx context.Context) (bool, error) { + err := wait.PollUntilContextCancel(context.Background(), 500*time.Millisecond, true, func(ctx context.Context) (bool, error) { _, err := clientset.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}) return err != nil, nil }) + if err != nil { + t.Fatalf("Failed to wait for namespace deletion: %v", err) + } } }) diff --git a/go.mod b/go.mod index b34fb85c..bcba95cd 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,6 @@ require ( require ( github.com/kyokomi/emoji/v2 v2.2.12 github.com/spf13/cobra v1.7.0 - sigs.k8s.io/e2e-framework v0.3.0 ) require ( @@ -55,7 +54,6 @@ require ( github.com/json-iterator/go v1.1.12 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect - github.com/moby/spdystream v0.2.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect @@ -65,7 +63,6 @@ require ( github.com/prometheus/procfs v0.11.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.8.3 // indirect - github.com/vladimirvivien/gexe v0.2.0 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/net v0.20.0 // indirect diff --git a/go.sum b/go.sum index 8aa5f79e..7f340175 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= -github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -56,7 +54,6 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJY github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM= github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= @@ -83,8 +80,6 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= -github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= -github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -127,8 +122,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/vladimirvivien/gexe v0.2.0 h1:nbdAQ6vbZ+ZNsolCgSVb9Fno60kzSuvtzVh6Ytqi/xY= -github.com/vladimirvivien/gexe v0.2.0/go.mod h1:LHQL00w/7gDUKIak24n801ABp8C+ni6eBht9vGVst8w= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= @@ -240,8 +233,6 @@ k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCf k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/controller-runtime v0.15.1 h1:9UvgKD4ZJGcj24vefUFgZFP3xej/3igL9BsOUTb/+4c= sigs.k8s.io/controller-runtime v0.15.1/go.mod h1:7ngYvp1MLT+9GeZ+6lH3LOlcHkp/+tzA/fmHa4iq9kk= -sigs.k8s.io/e2e-framework v0.3.0 h1:eqQALBtPCth8+ulTs6lcPK7ytV5rZSSHJzQHZph4O7U= -sigs.k8s.io/e2e-framework v0.3.0/go.mod h1:C+ef37/D90Dc7Xq1jQnNbJYscrUGpxrWog9bx2KIa+c= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE= diff --git a/pkg/stoper/stoper.go b/pkg/stoper/stoper.go index d19ac822..b3a466fc 100644 --- a/pkg/stoper/stoper.go +++ b/pkg/stoper/stoper.go @@ -9,12 +9,13 @@ import ( "time" "github.com/kyokomi/emoji/v2" - "github.com/mercari/tortoise/api/v1beta3" - "github.com/mercari/tortoise/pkg/deployment" - "github.com/mercari/tortoise/pkg/pod" v1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/mercari/tortoise/api/v1beta3" + "github.com/mercari/tortoise/pkg/deployment" + "github.com/mercari/tortoise/pkg/pod" ) // Stopr is the struct for stopping tortoise safely. @@ -66,40 +67,40 @@ func (s *Stopr) Stop(ctx context.Context, tortoiseNames []string, namespace stri var finalerr error for _, target := range targets { - writer.Write([]byte(fmt.Sprintf("\n%s stopping your tortoise %s ... ", emoji.Sprint(":turtle:"), &target))) + write(writer, fmt.Sprintf("\n%s stopping your tortoise %s ... ", emoji.Sprint(":turtle:"), &target)) // 1. Stop Tortoise. tortoise, err := s.stopOne(ctx, target) if err != nil { if errors.Is(err, errTortoiseAlreadyStopped) { - writer.Write([]byte(fmt.Sprintf("this tortoise is already stopped %s\n", emoji.Sprint(":sleeping:")))) + write(writer, fmt.Sprintf("this tortoise is already stopped %s\n", emoji.Sprint(":sleeping:"))) continue } finalerr = errors.Join(finalerr, err) - writer.Write([]byte(fmt.Sprintf("failed to stop your tortoise %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), err))) + write(writer, fmt.Sprintf("failed to stop your tortoise %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), err)) continue } - writer.Write([]byte(fmt.Sprintf("Done %s\n", emoji.Sprint(":sleeping:")))) + write(writer, fmt.Sprintf("Done %s\n", emoji.Sprint(":sleeping:"))) // 2. Get the target deployment. dp, err := s.deploymentService.GetDeploymentOnTortoise(ctx, tortoise) if err != nil { finalerr = errors.Join(finalerr, err) - writer.Write([]byte(fmt.Sprintf("%s failed to get deployment on your tortoise %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), &target, err))) + write(writer, fmt.Sprintf("%s failed to get deployment on your tortoise %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), &target, err)) continue } // 3. [when NoLoweringResource is true] Patch the deployment to keep the resource requests high. if containsOption(opts, NoLoweringResource) { - writer.Write([]byte(fmt.Sprintf("%s patching your deployment to keep the resource requests high ... ", emoji.Sprint(":hammer_and_wrench:")))) + write(writer, fmt.Sprintf("%s patching your deployment to keep the resource requests high ... ", emoji.Sprint(":hammer_and_wrench:"))) updated, err := s.patchDeploymentToKeepResources(ctx, dp, tortoise) if err != nil { finalerr = errors.Join(finalerr, err) - writer.Write([]byte(fmt.Sprintf("%s failed to patch your deployment %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), &target, err))) + write(writer, fmt.Sprintf("%s failed to patch your deployment %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), &target, err)) continue } - writer.Write([]byte(fmt.Sprintf("Done %s\n", emoji.Sprint(":hammer_and_wrench:")))) + write(writer, fmt.Sprintf("Done %s\n", emoji.Sprint(":hammer_and_wrench:"))) if updated { // If the deployment is updated, we don't need to restart the deployment. @@ -108,18 +109,23 @@ func (s *Stopr) Stop(ctx context.Context, tortoiseNames []string, namespace stri } // 4. Restart the deployment to get back the original resource requests. - writer.Write([]byte(fmt.Sprintf("%s restarting your deployment to get back the original resource ... ", emoji.Sprint(":arrows_counterclockwise:")))) + write(writer, fmt.Sprintf("%s restarting your deployment to get back the original resource ... ", emoji.Sprint(":arrows_counterclockwise:"))) if err := s.deploymentService.RolloutRestart(ctx, dp, tortoise, time.Now()); err != nil { finalerr = errors.Join(finalerr, err) - writer.Write([]byte(fmt.Sprintf("%s failed to restart your deployment %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), &target, err))) + write(writer, fmt.Sprintf("%s failed to restart your deployment %s.\nError: %v\n", emoji.Sprint(":face_with_spiral_eyes:"), &target, err)) continue } - writer.Write([]byte(fmt.Sprintf("Done, your Pods should get the original resources soon %s\n", emoji.Sprint(":muscle:")))) + write(writer, fmt.Sprintf("Done, your Pods should get the original resources soon %s\n", emoji.Sprint(":muscle:"))) } return finalerr } +func write(writer io.Writer, msg string) { + //nolint:errcheck // intentionally ignore the error because it's not critical + writer.Write([]byte(msg)) +} + func containsOption(opts []StoprOption, opt StoprOption) bool { for _, o := range opts { if o == opt {