Skip to content

Commit 81953bb

Browse files
authored
Merge pull request kubernetes#78676 from i02sopop/kubectl_optimizations
Do some Kubectl optimizations suggested by the golangci linter
2 parents 51f4b53 + cd2adbe commit 81953bb

File tree

9 files changed

+25
-27
lines changed

9 files changed

+25
-27
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/create/create_role_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestCreateRole(t *testing.T) {
140140
cmd.Run(cmd, []string{roleName})
141141
actual := &rbac.Role{}
142142
if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), buf.Bytes(), actual); err != nil {
143-
t.Log(string(buf.Bytes()))
143+
t.Log(buf.String())
144144
t.Fatal(err)
145145
}
146146
if !equality.Semantic.DeepEqual(test.expectedRole, actual) {

staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ func TestResourceErrors(t *testing.T) {
706706
}
707707

708708
if buf.Len() > 0 {
709-
t.Errorf("buffer should be empty: %s", string(buf.Bytes()))
709+
t.Errorf("buffer should be empty: %s", buf.String())
710710
}
711711
})
712712
}

staging/src/k8s.io/kubectl/pkg/cmd/describe/describe_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestDescribeUnknownSchemaObject(t *testing.T) {
5959
t.Errorf("unexpected describer: %#v", d)
6060
}
6161

62-
if buf.String() != fmt.Sprintf("%s", d.Output) {
62+
if buf.String() != d.Output {
6363
t.Errorf("unexpected output: %s", buf.String())
6464
}
6565
}
@@ -92,7 +92,7 @@ func TestDescribeUnknownNamespacedSchemaObject(t *testing.T) {
9292
t.Errorf("unexpected describer: %#v", d)
9393
}
9494

95-
if buf.String() != fmt.Sprintf("%s", d.Output) {
95+
if buf.String() != d.Output {
9696
t.Errorf("unexpected output: %s", buf.String())
9797
}
9898
}
@@ -133,7 +133,7 @@ func TestDescribeObject(t *testing.T) {
133133
t.Errorf("unexpected describer: %#v", d)
134134
}
135135

136-
if buf.String() != fmt.Sprintf("%s", d.Output) {
136+
if buf.String() != d.Output {
137137
t.Errorf("unexpected output: %s", buf.String())
138138
}
139139
}

staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func NewCmdPluginList(f cmdutil.Factory, streams genericclioptions.IOStreams) *c
9999
func (o *PluginListOptions) Complete(cmd *cobra.Command) error {
100100
o.Verifier = &CommandOverrideVerifier{
101101
root: cmd.Root(),
102-
seenPlugins: make(map[string]string, 0),
102+
seenPlugins: make(map[string]string),
103103
}
104104

105105
o.PluginPaths = filepath.SplitList(os.Getenv("PATH"))

staging/src/k8s.io/kubectl/pkg/cmd/set/env/env_parse.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string)
8181
return nil, nil, err
8282
}
8383
env = append(env, fileEnv...)
84-
case strings.Index(envSpec, "=") != -1:
84+
case strings.Contains(envSpec, "="):
8585
parts := strings.SplitN(envSpec, "=", 2)
8686
if len(parts) != 2 {
8787
return nil, nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
@@ -119,7 +119,8 @@ func readEnv(r io.Reader, envVarType string) ([]v1.EnvVar, error) {
119119
if pos := strings.Index(envSpec, "#"); pos != -1 {
120120
envSpec = envSpec[:pos]
121121
}
122-
if strings.Index(envSpec, "=") != -1 {
122+
123+
if strings.Contains(envSpec, "=") {
123124
parts := strings.SplitN(envSpec, "=", 2)
124125
if len(parts) != 2 {
125126
return nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
@@ -130,8 +131,10 @@ func readEnv(r io.Reader, envVarType string) ([]v1.EnvVar, error) {
130131
})
131132
}
132133
}
134+
133135
if err := scanner.Err(); err != nil && err != io.EOF {
134136
return nil, err
135137
}
138+
136139
return env, nil
137140
}

staging/src/k8s.io/kubectl/pkg/cmd/util/sanity/cmd_sanity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func RunCmdChecks(cmd *cobra.Command, cmdChecks []CmdCheck, skipCmd []string) []
8080
fmt.Fprintf(os.Stdout, "---+ RUNNING COMMAND CHECKS on %q\n", cmdPath)
8181

8282
for _, check := range cmdChecks {
83-
if err := check(cmd); err != nil && len(err) > 0 {
83+
if err := check(cmd); len(err) > 0 {
8484
errors = append(errors, err...)
8585
}
8686
}

staging/src/k8s.io/kubectl/pkg/describe/versioned/describe.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,11 @@ func describePodIPs(pod *corev1.Pod, w PrefixWriter, space string) {
777777
}
778778

779779
func describeVolumes(volumes []corev1.Volume, w PrefixWriter, space string) {
780-
if volumes == nil || len(volumes) == 0 {
780+
if len(volumes) == 0 {
781781
w.Write(LEVEL_0, "%sVolumes:\t<none>\n", space)
782782
return
783783
}
784+
784785
w.Write(LEVEL_0, "%sVolumes:\n", space)
785786
for _, volume := range volumes {
786787
nameIndent := ""
@@ -2426,7 +2427,6 @@ func describeIngressTLS(w PrefixWriter, ingTLS []networkingv1beta1.IngressTLS) {
24262427
w.Write(LEVEL_1, "%v terminates %v\n", t.SecretName, strings.Join(t.Hosts, ","))
24272428
}
24282429
}
2429-
return
24302430
}
24312431

24322432
// TODO: Move from annotations into Ingress status.
@@ -2435,7 +2435,6 @@ func describeIngressAnnotations(w PrefixWriter, annotations map[string]string) {
24352435
for k, v := range annotations {
24362436
w.Write(LEVEL_1, "%v:\t%s\n", k, v)
24372437
}
2438-
return
24392438
}
24402439

24412440
// ServiceDescriber generates information about a service.
@@ -2742,8 +2741,8 @@ func (d *ServiceAccountDescriber) Describe(namespace, name string, describerSett
27422741

27432742
for _, s := range secrets.Items {
27442743
if s.Type == corev1.SecretTypeServiceAccountToken {
2745-
name, _ := s.Annotations[corev1.ServiceAccountNameKey]
2746-
uid, _ := s.Annotations[corev1.ServiceAccountUIDKey]
2744+
name := s.Annotations[corev1.ServiceAccountNameKey]
2745+
uid := s.Annotations[corev1.ServiceAccountUIDKey]
27472746
if name == serviceAccount.Name && uid == string(serviceAccount.UID) {
27482747
tokens = append(tokens, s)
27492748
}
@@ -4324,7 +4323,7 @@ func printLabelsMultiline(w PrefixWriter, title string, labels map[string]string
43244323
func printLabelsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, labels map[string]string, skip sets.String) {
43254324
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
43264325

4327-
if labels == nil || len(labels) == 0 {
4326+
if len(labels) == 0 {
43284327
w.WriteLine("<none>")
43294328
return
43304329
}
@@ -4362,7 +4361,7 @@ func printNodeTaintsMultiline(w PrefixWriter, title string, taints []corev1.Tain
43624361
func printTaintsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, taints []corev1.Taint) {
43634362
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
43644363

4365-
if taints == nil || len(taints) == 0 {
4364+
if len(taints) == 0 {
43664365
w.WriteLine("<none>")
43674366
return
43684367
}
@@ -4393,7 +4392,7 @@ func printPodsMultiline(w PrefixWriter, title string, pods []corev1.Pod) {
43934392
func printPodsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, pods []corev1.Pod) {
43944393
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
43954394

4396-
if pods == nil || len(pods) == 0 {
4395+
if len(pods) == 0 {
43974396
w.WriteLine("<none>")
43984397
return
43994398
}
@@ -4424,7 +4423,7 @@ func printPodTolerationsMultiline(w PrefixWriter, title string, tolerations []co
44244423
func printTolerationsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, tolerations []corev1.Toleration) {
44254424
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
44264425

4427-
if tolerations == nil || len(tolerations) == 0 {
4426+
if len(tolerations) == 0 {
44284427
w.WriteLine("<none>")
44294428
return
44304429
}

staging/src/k8s.io/kubectl/pkg/drain/cordon.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ func NewCordonHelperFromRuntimeObject(nodeObject runtime.Object, scheme *runtime
6262
// or false when no change is needed
6363
func (c *CordonHelper) UpdateIfRequired(desired bool) bool {
6464
c.desired = desired
65-
if c.node.Spec.Unschedulable == c.desired {
66-
return false
67-
}
68-
return true
65+
66+
return c.node.Spec.Unschedulable != c.desired
6967
}
7068

7169
// PatchOrReplace uses given clientset to update the node status, either by patching or

staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ func findLanguage(root string, getLanguageFn func() string) string {
7676
langStr := getLanguageFn()
7777

7878
translations := knownTranslations[root]
79-
if translations != nil {
80-
for ix := range translations {
81-
if translations[ix] == langStr {
82-
return langStr
83-
}
79+
for ix := range translations {
80+
if translations[ix] == langStr {
81+
return langStr
8482
}
8583
}
8684
klog.V(3).Infof("Couldn't find translations for %s, using default", langStr)

0 commit comments

Comments
 (0)