Do some Kubectl optimizations suggested by the golangci linter

The tool golangci-lint gives a bunch of warnings. This PR solves the easier/less controversial ones, so the code is simpler and a little bit more optimal, since it removes some if conditions.

Kubernetes-commit: cd2adbe760641f844d84d411f9adcf17fb6982ff
This commit is contained in:
ritho 2019-06-04 01:25:43 -05:00 committed by Kubernetes Publisher
parent feacf2b3d4
commit b0cd1c2290
9 changed files with 25 additions and 27 deletions

View File

@ -140,7 +140,7 @@ func TestCreateRole(t *testing.T) {
cmd.Run(cmd, []string{roleName}) cmd.Run(cmd, []string{roleName})
actual := &rbac.Role{} actual := &rbac.Role{}
if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), buf.Bytes(), actual); err != nil { if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), buf.Bytes(), actual); err != nil {
t.Log(string(buf.Bytes())) t.Log(buf.String())
t.Fatal(err) t.Fatal(err)
} }
if !equality.Semantic.DeepEqual(test.expectedRole, actual) { if !equality.Semantic.DeepEqual(test.expectedRole, actual) {

View File

@ -706,7 +706,7 @@ func TestResourceErrors(t *testing.T) {
} }
if buf.Len() > 0 { if buf.Len() > 0 {
t.Errorf("buffer should be empty: %s", string(buf.Bytes())) t.Errorf("buffer should be empty: %s", buf.String())
} }
}) })
} }

View File

@ -59,7 +59,7 @@ func TestDescribeUnknownSchemaObject(t *testing.T) {
t.Errorf("unexpected describer: %#v", d) t.Errorf("unexpected describer: %#v", d)
} }
if buf.String() != fmt.Sprintf("%s", d.Output) { if buf.String() != d.Output {
t.Errorf("unexpected output: %s", buf.String()) t.Errorf("unexpected output: %s", buf.String())
} }
} }
@ -92,7 +92,7 @@ func TestDescribeUnknownNamespacedSchemaObject(t *testing.T) {
t.Errorf("unexpected describer: %#v", d) t.Errorf("unexpected describer: %#v", d)
} }
if buf.String() != fmt.Sprintf("%s", d.Output) { if buf.String() != d.Output {
t.Errorf("unexpected output: %s", buf.String()) t.Errorf("unexpected output: %s", buf.String())
} }
} }
@ -133,7 +133,7 @@ func TestDescribeObject(t *testing.T) {
t.Errorf("unexpected describer: %#v", d) t.Errorf("unexpected describer: %#v", d)
} }
if buf.String() != fmt.Sprintf("%s", d.Output) { if buf.String() != d.Output {
t.Errorf("unexpected output: %s", buf.String()) t.Errorf("unexpected output: %s", buf.String())
} }
} }

View File

@ -99,7 +99,7 @@ func NewCmdPluginList(f cmdutil.Factory, streams genericclioptions.IOStreams) *c
func (o *PluginListOptions) Complete(cmd *cobra.Command) error { func (o *PluginListOptions) Complete(cmd *cobra.Command) error {
o.Verifier = &CommandOverrideVerifier{ o.Verifier = &CommandOverrideVerifier{
root: cmd.Root(), root: cmd.Root(),
seenPlugins: make(map[string]string, 0), seenPlugins: make(map[string]string),
} }
o.PluginPaths = filepath.SplitList(os.Getenv("PATH")) o.PluginPaths = filepath.SplitList(os.Getenv("PATH"))

View File

@ -81,7 +81,7 @@ func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string)
return nil, nil, err return nil, nil, err
} }
env = append(env, fileEnv...) env = append(env, fileEnv...)
case strings.Index(envSpec, "=") != -1: case strings.Contains(envSpec, "="):
parts := strings.SplitN(envSpec, "=", 2) parts := strings.SplitN(envSpec, "=", 2)
if len(parts) != 2 { if len(parts) != 2 {
return nil, nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec) return nil, nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
@ -119,7 +119,8 @@ func readEnv(r io.Reader, envVarType string) ([]v1.EnvVar, error) {
if pos := strings.Index(envSpec, "#"); pos != -1 { if pos := strings.Index(envSpec, "#"); pos != -1 {
envSpec = envSpec[:pos] envSpec = envSpec[:pos]
} }
if strings.Index(envSpec, "=") != -1 {
if strings.Contains(envSpec, "=") {
parts := strings.SplitN(envSpec, "=", 2) parts := strings.SplitN(envSpec, "=", 2)
if len(parts) != 2 { if len(parts) != 2 {
return nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec) return nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
@ -130,8 +131,10 @@ func readEnv(r io.Reader, envVarType string) ([]v1.EnvVar, error) {
}) })
} }
} }
if err := scanner.Err(); err != nil && err != io.EOF { if err := scanner.Err(); err != nil && err != io.EOF {
return nil, err return nil, err
} }
return env, nil return env, nil
} }

View File

@ -80,7 +80,7 @@ func RunCmdChecks(cmd *cobra.Command, cmdChecks []CmdCheck, skipCmd []string) []
fmt.Fprintf(os.Stdout, "---+ RUNNING COMMAND CHECKS on %q\n", cmdPath) fmt.Fprintf(os.Stdout, "---+ RUNNING COMMAND CHECKS on %q\n", cmdPath)
for _, check := range cmdChecks { for _, check := range cmdChecks {
if err := check(cmd); err != nil && len(err) > 0 { if err := check(cmd); len(err) > 0 {
errors = append(errors, err...) errors = append(errors, err...)
} }
} }

View File

@ -777,10 +777,11 @@ func describePodIPs(pod *corev1.Pod, w PrefixWriter, space string) {
} }
func describeVolumes(volumes []corev1.Volume, w PrefixWriter, space string) { func describeVolumes(volumes []corev1.Volume, w PrefixWriter, space string) {
if volumes == nil || len(volumes) == 0 { if len(volumes) == 0 {
w.Write(LEVEL_0, "%sVolumes:\t<none>\n", space) w.Write(LEVEL_0, "%sVolumes:\t<none>\n", space)
return return
} }
w.Write(LEVEL_0, "%sVolumes:\n", space) w.Write(LEVEL_0, "%sVolumes:\n", space)
for _, volume := range volumes { for _, volume := range volumes {
nameIndent := "" nameIndent := ""
@ -2426,7 +2427,6 @@ func describeIngressTLS(w PrefixWriter, ingTLS []networkingv1beta1.IngressTLS) {
w.Write(LEVEL_1, "%v terminates %v\n", t.SecretName, strings.Join(t.Hosts, ",")) w.Write(LEVEL_1, "%v terminates %v\n", t.SecretName, strings.Join(t.Hosts, ","))
} }
} }
return
} }
// TODO: Move from annotations into Ingress status. // TODO: Move from annotations into Ingress status.
@ -2435,7 +2435,6 @@ func describeIngressAnnotations(w PrefixWriter, annotations map[string]string) {
for k, v := range annotations { for k, v := range annotations {
w.Write(LEVEL_1, "%v:\t%s\n", k, v) w.Write(LEVEL_1, "%v:\t%s\n", k, v)
} }
return
} }
// ServiceDescriber generates information about a service. // ServiceDescriber generates information about a service.
@ -2742,8 +2741,8 @@ func (d *ServiceAccountDescriber) Describe(namespace, name string, describerSett
for _, s := range secrets.Items { for _, s := range secrets.Items {
if s.Type == corev1.SecretTypeServiceAccountToken { if s.Type == corev1.SecretTypeServiceAccountToken {
name, _ := s.Annotations[corev1.ServiceAccountNameKey] name := s.Annotations[corev1.ServiceAccountNameKey]
uid, _ := s.Annotations[corev1.ServiceAccountUIDKey] uid := s.Annotations[corev1.ServiceAccountUIDKey]
if name == serviceAccount.Name && uid == string(serviceAccount.UID) { if name == serviceAccount.Name && uid == string(serviceAccount.UID) {
tokens = append(tokens, s) tokens = append(tokens, s)
} }
@ -4324,7 +4323,7 @@ func printLabelsMultiline(w PrefixWriter, title string, labels map[string]string
func printLabelsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, labels map[string]string, skip sets.String) { func printLabelsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, labels map[string]string, skip sets.String) {
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent) w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
if labels == nil || len(labels) == 0 { if len(labels) == 0 {
w.WriteLine("<none>") w.WriteLine("<none>")
return return
} }
@ -4362,7 +4361,7 @@ func printNodeTaintsMultiline(w PrefixWriter, title string, taints []corev1.Tain
func printTaintsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, taints []corev1.Taint) { func printTaintsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, taints []corev1.Taint) {
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent) w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
if taints == nil || len(taints) == 0 { if len(taints) == 0 {
w.WriteLine("<none>") w.WriteLine("<none>")
return return
} }
@ -4393,7 +4392,7 @@ func printPodsMultiline(w PrefixWriter, title string, pods []corev1.Pod) {
func printPodsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, pods []corev1.Pod) { func printPodsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, pods []corev1.Pod) {
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent) w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
if pods == nil || len(pods) == 0 { if len(pods) == 0 {
w.WriteLine("<none>") w.WriteLine("<none>")
return return
} }
@ -4424,7 +4423,7 @@ func printPodTolerationsMultiline(w PrefixWriter, title string, tolerations []co
func printTolerationsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, tolerations []corev1.Toleration) { func printTolerationsMultilineWithIndent(w PrefixWriter, initialIndent, title, innerIndent string, tolerations []corev1.Toleration) {
w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent) w.Write(LEVEL_0, "%s%s:%s", initialIndent, title, innerIndent)
if tolerations == nil || len(tolerations) == 0 { if len(tolerations) == 0 {
w.WriteLine("<none>") w.WriteLine("<none>")
return return
} }

View File

@ -62,10 +62,8 @@ func NewCordonHelperFromRuntimeObject(nodeObject runtime.Object, scheme *runtime
// or false when no change is needed // or false when no change is needed
func (c *CordonHelper) UpdateIfRequired(desired bool) bool { func (c *CordonHelper) UpdateIfRequired(desired bool) bool {
c.desired = desired c.desired = desired
if c.node.Spec.Unschedulable == c.desired {
return false return c.node.Spec.Unschedulable != c.desired
}
return true
} }
// PatchOrReplace uses given clientset to update the node status, either by patching or // PatchOrReplace uses given clientset to update the node status, either by patching or

View File

@ -76,13 +76,11 @@ func findLanguage(root string, getLanguageFn func() string) string {
langStr := getLanguageFn() langStr := getLanguageFn()
translations := knownTranslations[root] translations := knownTranslations[root]
if translations != nil {
for ix := range translations { for ix := range translations {
if translations[ix] == langStr { if translations[ix] == langStr {
return langStr return langStr
} }
} }
}
klog.V(3).Infof("Couldn't find translations for %s, using default", langStr) klog.V(3).Infof("Couldn't find translations for %s, using default", langStr)
return "default" return "default"
} }