From 17993020cdca7eea2fb96a993e048d2d0624fa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E6=8C=AF=E5=85=B410069964?= Date: Mon, 12 Apr 2021 10:45:08 +0800 Subject: [PATCH] code cleanup for kubectl --- .../src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go | 2 +- .../k8s.io/kubectl/pkg/cmd/certificates/certificates.go | 3 +-- .../src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go | 3 +-- .../k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go | 9 ++++----- staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go | 3 +-- staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go | 7 +++---- staging/src/k8s.io/kubectl/pkg/describe/describe.go | 7 +++---- 7 files changed, 14 insertions(+), 20 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go b/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go index bb09cbec91e..5a3977eec59 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/autoscale/autoscale.go @@ -121,7 +121,7 @@ func NewCmdAutoscale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) * cmd.Flags().Int32Var(&o.Min, "min", -1, "The lower limit for the number of pods that can be set by the autoscaler. If it's not specified or negative, the server will apply a default value.") cmd.Flags().Int32Var(&o.Max, "max", -1, "The upper limit for the number of pods that can be set by the autoscaler. Required.") cmd.MarkFlagRequired("max") - cmd.Flags().Int32Var(&o.CPUPercent, "cpu-percent", -1, fmt.Sprintf("The target average CPU utilization (represented as a percent of requested CPU) over all the pods. If it's not specified or negative, a default autoscaling policy will be used.")) + cmd.Flags().Int32Var(&o.CPUPercent, "cpu-percent", -1, "The target average CPU utilization (represented as a percent of requested CPU) over all the pods. If it's not specified or negative, a default autoscaling policy will be used.") cmd.Flags().StringVar(&o.Name, "name", "", i18n.T("The name for the newly created object. If not specified, the name of the input resource will be used.")) cmdutil.AddDryRunFlag(cmd) cmdutil.AddFilenameOptionFlags(cmd, o.FilenameOptions, "identifying the resource to autoscale.") diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates.go b/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates.go index d7caefe33da..20d6ed2ffaf 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates.go @@ -26,7 +26,6 @@ import ( certificatesv1 "k8s.io/api/certificates/v1" certificatesv1beta1 "k8s.io/api/certificates/v1beta1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -251,7 +250,7 @@ func (o *CertificateOptions) modifyCertificateCondition(builder *resource.Builde default: return fmt.Errorf("can only handle certificates.k8s.io CertificateSigningRequest objects, got %T", modifiedCSR) } - if errors.IsConflict(err) && i < 10 { + if apierrors.IsConflict(err) && i < 10 { if err := info.Get(); err != nil { return err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go index fc4bd83581b..57b1095d083 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress.go @@ -320,8 +320,7 @@ func (o *CreateIngressOptions) buildIngressSpec() networkingv1.IngressSpec { } func (o *CreateIngressOptions) buildTLSRules() []networkingv1.IngressTLS { - var hostAlreadyPresent map[string]struct{} - hostAlreadyPresent = make(map[string]struct{}) + hostAlreadyPresent := make(map[string]struct{}) ingressTLSs := []networkingv1.IngressTLS{} var secret string diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go index 6d27167548a..d53e80b4b20 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_ingress_test.go @@ -20,7 +20,6 @@ import ( "testing" networkingv1 "k8s.io/api/networking/v1" - v1 "k8s.io/api/networking/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -192,7 +191,7 @@ func TestCreateIngress(t *testing.T) { }, }, }, - TLS: []v1.IngressTLS{}, + TLS: []networkingv1.IngressTLS{}, Rules: []networkingv1.IngressRule{ { Host: "", @@ -245,7 +244,7 @@ func TestCreateIngress(t *testing.T) { }, }, }, - TLS: []v1.IngressTLS{ + TLS: []networkingv1.IngressTLS{ { SecretName: "secret1", }, @@ -305,7 +304,7 @@ func TestCreateIngress(t *testing.T) { }, }, }, - TLS: []v1.IngressTLS{ + TLS: []networkingv1.IngressTLS{ { Hosts: []string{ "foo.com", @@ -456,7 +455,7 @@ func TestCreateIngress(t *testing.T) { }, }, Spec: networkingv1.IngressSpec{ - TLS: []v1.IngressTLS{ + TLS: []networkingv1.IngressTLS{ { Hosts: []string{ "foo.com", diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go index e4e5b6f5b6b..19a3c9f192e 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go @@ -26,7 +26,6 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" cmdtesting "k8s.io/kubectl/pkg/cmd/testing" @@ -1272,7 +1271,7 @@ func TestCompleteAndValidate(t *testing.T) { args: "--image=busybox --env=FOO=BAR mypod", wantOpts: &DebugOptions{ Args: []string{}, - Env: []v1.EnvVar{{Name: "FOO", Value: "BAR"}}, + Env: []corev1.EnvVar{{Name: "FOO", Value: "BAR"}}, Image: "busybox", Namespace: "test", ShareProcesses: true, diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go index 004b292a4cb..3d0acc82d8f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go @@ -19,7 +19,6 @@ package get import ( "bytes" "encoding/json" - encjson "encoding/json" "fmt" "io" "io/ioutil" @@ -124,13 +123,13 @@ func TestGetUnknownSchemaObject(t *testing.T) { for i, obj := range actual { expectedJSON := runtime.EncodeOrDie(codec, expected[i]) expectedMap := map[string]interface{}{} - if err := encjson.Unmarshal([]byte(expectedJSON), &expectedMap); err != nil { + if err := json.Unmarshal([]byte(expectedJSON), &expectedMap); err != nil { t.Fatal(err) } actualJSON := runtime.EncodeOrDie(codec, obj) actualMap := map[string]interface{}{} - if err := encjson.Unmarshal([]byte(actualJSON), &actualMap); err != nil { + if err := json.Unmarshal([]byte(actualJSON), &actualMap); err != nil { t.Fatal(err) } @@ -879,7 +878,7 @@ func TestGetSortedObjectsUnstructuredTable(t *testing.T) { if err != nil { t.Fatal(err) } - unstructuredBytes, err := encjson.MarshalIndent(unstructuredMap, "", " ") + unstructuredBytes, err := json.MarshalIndent(unstructuredMap, "", " ") if err != nil { t.Fatal(err) } diff --git a/staging/src/k8s.io/kubectl/pkg/describe/describe.go b/staging/src/k8s.io/kubectl/pkg/describe/describe.go index d4d7dda5aac..2d4875abaf6 100644 --- a/staging/src/k8s.io/kubectl/pkg/describe/describe.go +++ b/staging/src/k8s.io/kubectl/pkg/describe/describe.go @@ -53,7 +53,6 @@ import ( rbacv1 "k8s.io/api/rbac/v1" schedulingv1 "k8s.io/api/scheduling/v1" storagev1 "k8s.io/api/storage/v1" - "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/resource" @@ -413,7 +412,7 @@ func (d *NamespaceDescriber) Describe(namespace, name string, describerSettings return newList, nil }) if err != nil { - if errors.IsNotFound(err) { + if apierrors.IsNotFound(err) { // Server does not support resource quotas. // Not an error, will not show resource quotas information. resourceQuotaList = nil @@ -433,7 +432,7 @@ func (d *NamespaceDescriber) Describe(namespace, name string, describerSettings return newList, nil }) if err != nil { - if errors.IsNotFound(err) { + if apierrors.IsNotFound(err) { // Server does not support limit ranges. // Not an error, will not show limit ranges information. limitRangeList = nil @@ -3483,7 +3482,7 @@ func (d *NodeDescriber) Describe(namespace, name string, describerSettings Descr } nodeNonTerminatedPodsList, err := getPodsInChunks(d.CoreV1().Pods(namespace), initialOpts) if err != nil { - if !errors.IsForbidden(err) { + if !apierrors.IsForbidden(err) { return "", err } canViewPods = false