From ef9ae612403373d45003d05882a09c2b828f8ce3 Mon Sep 17 00:00:00 2001 From: Alexander Campbell Date: Thu, 25 May 2017 13:37:56 -0700 Subject: [PATCH] kubectl: simplify code with help of linter --- pkg/kubectl/apply.go | 6 +----- pkg/kubectl/cmd/apply_set_last_applied.go | 11 ++--------- pkg/kubectl/cmd/convert.go | 6 +----- pkg/kubectl/cmd/cp.go | 5 ++--- pkg/kubectl/cmd/create.go | 8 ++++---- pkg/kubectl/cmd/delete.go | 2 +- pkg/kubectl/cmd/edit_test.go | 4 ++-- pkg/kubectl/cmd/expose.go | 14 +++++++------- pkg/kubectl/cmd/get.go | 3 +-- pkg/kubectl/cmd/get_test.go | 4 +--- pkg/kubectl/cmd/label.go | 2 +- pkg/kubectl/cmd/taint.go | 4 ++-- pkg/kubectl/cmd/version.go | 6 +----- pkg/kubectl/rolling_updater.go | 19 ++++++++----------- pkg/kubectl/run.go | 6 ------ pkg/kubectl/stop.go | 5 ----- 16 files changed, 34 insertions(+), 71 deletions(-) diff --git a/pkg/kubectl/apply.go b/pkg/kubectl/apply.go index b4b49b5826..ba74888952 100644 --- a/pkg/kubectl/apply.go +++ b/pkg/kubectl/apply.go @@ -61,11 +61,7 @@ func SetOriginalConfiguration(info *resource.Info, original []byte) error { } annots[api.LastAppliedConfigAnnotation] = string(original) - if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil { - return err - } - - return nil + return info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots) } // GetModifiedConfiguration retrieves the modified configuration of the object. diff --git a/pkg/kubectl/cmd/apply_set_last_applied.go b/pkg/kubectl/cmd/apply_set_last_applied.go index c86e90c6e5..188e54b09b 100644 --- a/pkg/kubectl/cmd/apply_set_last_applied.go +++ b/pkg/kubectl/cmd/apply_set_last_applied.go @@ -118,11 +118,7 @@ func (o *SetLastAppliedOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) } o.Namespace, o.EnforceNamespace, err = f.DefaultNamespace() - if err != nil { - return err - } - - return nil + return err } func (o *SetLastAppliedOptions) Validate(f cmdutil.Factory, cmd *cobra.Command) error { @@ -179,10 +175,7 @@ func (o *SetLastAppliedOptions) Validate(f cmdutil.Factory, cmd *cobra.Command) return nil }) - if err != nil { - return err - } - return nil + return err } func (o *SetLastAppliedOptions) RunSetLastApplied(f cmdutil.Factory, cmd *cobra.Command) error { diff --git a/pkg/kubectl/cmd/convert.go b/pkg/kubectl/cmd/convert.go index b673757310..c96e9420bd 100644 --- a/pkg/kubectl/cmd/convert.go +++ b/pkg/kubectl/cmd/convert.go @@ -158,11 +158,7 @@ func (o *ConvertOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.C } o.encoder = f.JSONEncoder() o.printer, err = f.PrinterForCommand(cmd, o.local, nil, printers.PrintOptions{}) - if err != nil { - return err - } - - return nil + return err } // RunConvert implements the generic Convert command diff --git a/pkg/kubectl/cmd/cp.go b/pkg/kubectl/cmd/cp.go index d5e6c18cd8..fd9eaeacd2 100644 --- a/pkg/kubectl/cmd/cp.go +++ b/pkg/kubectl/cmd/cp.go @@ -34,12 +34,11 @@ import ( ) var ( - cp_example = templates.Examples(i18n.T(` + cpExample = templates.Examples(i18n.T(` # !!!Important Note!!! # Requires that the 'tar' binary is present in your container # image. If 'tar' is not present, 'kubectl cp' will fail. - # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace kubectl cp /tmp/foo_dir :/tmp/bar_dir @@ -65,7 +64,7 @@ func NewCmdCp(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Command { Use: "cp ", Short: i18n.T("Copy files and directories to and from containers."), Long: "Copy files and directories to and from containers.", - Example: cp_example, + Example: cpExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(runCopy(f, cmd, cmdOut, cmdErr, args)) }, diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index 8112a6b7fe..7d942131b3 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -39,12 +39,12 @@ type CreateOptions struct { } var ( - create_long = templates.LongDesc(i18n.T(` + createLong = templates.LongDesc(i18n.T(` Create a resource from a file or from stdin. JSON and YAML formats are accepted.`)) - create_example = templates.Examples(i18n.T(` + createExample = templates.Examples(i18n.T(` # Create a pod using the data in pod.json. kubectl create -f ./pod.json @@ -61,8 +61,8 @@ func NewCmdCreate(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "create -f FILENAME", Short: i18n.T("Create a resource from a file or from stdin."), - Long: create_long, - Example: create_example, + Long: createLong, + Example: createExample, Run: func(cmd *cobra.Command, args []string) { if cmdutil.IsFilenameEmpty(options.FilenameOptions.Filenames) { defaultRunFunc := cmdutil.DefaultSubCommandRun(errOut) diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index e43ff2889e..8e9e178fe6 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -327,7 +327,7 @@ func deleteResource(info *resource.Info, out io.Writer, shortOutput bool, mapper return nil } -// objectDeletionWaitInterval is the interval to wait between checks for deletion. Exposed for testing. +// objectDeletionWaitInterval is the interval to wait between checks for deletion. var objectDeletionWaitInterval = time.Second // waitForObjectDeletion refreshes the object, waiting until it is deleted, a timeout is reached, or diff --git a/pkg/kubectl/cmd/edit_test.go b/pkg/kubectl/cmd/edit_test.go index 55516bb0be..1155efa6ef 100644 --- a/pkg/kubectl/cmd/edit_test.go +++ b/pkg/kubectl/cmd/edit_test.go @@ -116,7 +116,7 @@ func TestEdit(t *testing.T) { if step.StepType != "edit" { t.Fatalf("%s, step %d: expected edit step, got %s %s", name, i, req.Method, req.URL.Path) } - if bytes.Compare(body, expectedInput) != 0 { + if !bytes.Equal(body, expectedInput) { if updateInputFixtures { // Convenience to allow recapturing the input and persisting it here ioutil.WriteFile(inputFile, body, os.FileMode(0644)) @@ -139,7 +139,7 @@ func TestEdit(t *testing.T) { req.Method, req.URL.Path, req.Header.Get("Content-Type"), ) } - if bytes.Compare(body, expectedInput) != 0 { + if !bytes.Equal(body, expectedInput) { if updateInputFixtures { // Convenience to allow recapturing the input and persisting it here ioutil.WriteFile(inputFile, body, os.FileMode(0644)) diff --git a/pkg/kubectl/cmd/expose.go b/pkg/kubectl/cmd/expose.go index 2314044094..20580d8b39 100644 --- a/pkg/kubectl/cmd/expose.go +++ b/pkg/kubectl/cmd/expose.go @@ -34,9 +34,9 @@ import ( ) var ( - expose_resources = `pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)` + exposeResources = `pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)` - expose_long = templates.LongDesc(` + exposeLong = templates.LongDesc(` Expose a resource as a new Kubernetes service. Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector @@ -48,9 +48,9 @@ var ( Possible resources include (case insensitive): - ` + expose_resources) + ` + exposeResources) - expose_example = templates.Examples(i18n.T(` + exposeExample = templates.Examples(i18n.T(` # Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000. kubectl expose rc nginx --port=80 --target-port=8000 @@ -77,7 +77,7 @@ func NewCmdExposeService(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} validArgs, argAliases := []string{}, []string{} - resources := regexp.MustCompile(`\s*,`).Split(expose_resources, -1) + resources := regexp.MustCompile(`\s*,`).Split(exposeResources, -1) for _, r := range resources { validArgs = append(validArgs, strings.Fields(r)[0]) argAliases = kubectl.ResourceAliases(validArgs) @@ -86,8 +86,8 @@ func NewCmdExposeService(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]", Short: i18n.T("Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service"), - Long: expose_long, - Example: expose_example, + Long: exposeLong, + Example: exposeExample, Run: func(cmd *cobra.Command, args []string) { err := RunExpose(f, out, cmd, args, options) cmdutil.CheckErr(err) diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index f7ee34826f..833b86da61 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -289,8 +289,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ first = false return false, nil } - err := printer.PrintObj(e.Object, out) - return false, err + return false, printer.PrintObj(e.Object, out) }) return err }) diff --git a/pkg/kubectl/cmd/get_test.go b/pkg/kubectl/cmd/get_test.go index 4fa573cfcd..e2c80d5f62 100644 --- a/pkg/kubectl/cmd/get_test.go +++ b/pkg/kubectl/cmd/get_test.go @@ -506,9 +506,7 @@ func extractResourceList(objs []runtime.Object) ([]runtime.Object, error) { if err != nil { return nil, err } - for _, item := range items { - finalObjs = append(finalObjs, item) - } + finalObjs = append(finalObjs, items...) } return finalObjs, nil } diff --git a/pkg/kubectl/cmd/label.go b/pkg/kubectl/cmd/label.go index d9568e6c7f..33f2a1e9c3 100644 --- a/pkg/kubectl/cmd/label.go +++ b/pkg/kubectl/cmd/label.go @@ -308,7 +308,7 @@ func parseLabels(spec []string) (map[string]string, []string, error) { labels := map[string]string{} var remove []string for _, labelSpec := range spec { - if strings.Index(labelSpec, "=") != -1 { + if strings.Contains(labelSpec, "=") { parts := strings.Split(labelSpec, "=") if len(parts) != 2 { return nil, nil, fmt.Errorf("invalid label spec: %v", labelSpec) diff --git a/pkg/kubectl/cmd/taint.go b/pkg/kubectl/cmd/taint.go index 837a021962..2b2cb42933 100644 --- a/pkg/kubectl/cmd/taint.go +++ b/pkg/kubectl/cmd/taint.go @@ -178,7 +178,7 @@ func parseTaints(spec []string) ([]v1.Taint, []v1.Taint, error) { uniqueTaints := map[v1.TaintEffect]sets.String{} for _, taintSpec := range spec { - if strings.Index(taintSpec, "=") != -1 && strings.Index(taintSpec, ":") != -1 { + if strings.Contains(taintSpec, "=") && strings.Contains(taintSpec, ":") { newTaint, err := utiltaints.ParseTaint(taintSpec) if err != nil { return nil, nil, err @@ -197,7 +197,7 @@ func parseTaints(spec []string) ([]v1.Taint, []v1.Taint, error) { } else if strings.HasSuffix(taintSpec, "-") { taintKey := taintSpec[:len(taintSpec)-1] var effect v1.TaintEffect - if strings.Index(taintKey, ":") != -1 { + if strings.Contains(taintKey, ":") { parts := strings.Split(taintKey, ":") taintKey = parts[0] effect = v1.TaintEffect(parts[1]) diff --git a/pkg/kubectl/cmd/version.go b/pkg/kubectl/cmd/version.go index 56ecf51e60..b939a99e53 100644 --- a/pkg/kubectl/cmd/version.go +++ b/pkg/kubectl/cmd/version.go @@ -107,11 +107,7 @@ func RunVersion(f cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { } - if serverErr != nil { - return serverErr - } - - return nil + return serverErr } func retrieveServerVersion(f cmdutil.Factory) (*apimachineryversion.Info, error) { diff --git a/pkg/kubectl/rolling_updater.go b/pkg/kubectl/rolling_updater.go index 7adb8f37a1..3c50e3921e 100644 --- a/pkg/kubectl/rolling_updater.go +++ b/pkg/kubectl/rolling_updater.go @@ -565,10 +565,7 @@ func Rename(c coreclient.ReplicationControllersGetter, rc *api.ReplicationContro } // Then create the same RC with the new name. _, err = c.ReplicationControllers(rc.Namespace).Create(rc) - if err != nil { - return err - } - return nil + return err } func LoadExistingNextReplicationController(c coreclient.ReplicationControllersGetter, namespace, newName string) (*api.ReplicationController, error) { @@ -684,14 +681,14 @@ func UpdateExistingReplicationController(rcClient coreclient.ReplicationControll if _, found := oldRc.Spec.Selector[deploymentKey]; !found { SetNextControllerAnnotation(oldRc, newName) return AddDeploymentKeyToReplicationController(oldRc, rcClient, podClient, deploymentKey, deploymentValue, namespace, out) - } else { - // If we didn't need to update the controller for the deployment key, we still need to write - // the "next" controller. - applyUpdate := func(rc *api.ReplicationController) { - SetNextControllerAnnotation(rc, newName) - } - return updateRcWithRetries(rcClient, namespace, oldRc, applyUpdate) } + + // If we didn't need to update the controller for the deployment key, we still need to write + // the "next" controller. + applyUpdate := func(rc *api.ReplicationController) { + SetNextControllerAnnotation(rc, newName) + } + return updateRcWithRetries(rcClient, namespace, oldRc, applyUpdate) } func AddDeploymentKeyToReplicationController(oldRc *api.ReplicationController, rcClient coreclient.ReplicationControllersGetter, podClient coreclient.PodsGetter, deploymentKey, deploymentValue, namespace string, out io.Writer) (*api.ReplicationController, error) { diff --git a/pkg/kubectl/run.go b/pkg/kubectl/run.go index 2b00f68e33..2ec50978d5 100644 --- a/pkg/kubectl/run.go +++ b/pkg/kubectl/run.go @@ -868,9 +868,3 @@ func parseEnvs(envArray []string) ([]v1.EnvVar, error) { } return envs, nil } - -func newBool(val bool) *bool { - p := new(bool) - *p = val - return p -} diff --git a/pkg/kubectl/stop.go b/pkg/kubectl/stop.go index 4fcb9415fc..c75fe03f74 100644 --- a/pkg/kubectl/stop.go +++ b/pkg/kubectl/stop.go @@ -136,11 +136,6 @@ type StatefulSetReaper struct { pollInterval, timeout time.Duration } -type objInterface interface { - Delete(name string) error - Get(name string) (metav1.Object, error) -} - // getOverlappingControllers finds rcs that this controller overlaps, as well as rcs overlapping this controller. func getOverlappingControllers(rcClient coreclient.ReplicationControllerInterface, rc *api.ReplicationController) ([]api.ReplicationController, error) { rcs, err := rcClient.List(metav1.ListOptions{})