Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Enable "gosimple" linter plugin #3802

Merged
merged 4 commits into from
Sep 7, 2018
Merged

Conversation

mboersma
Copy link
Member

@mboersma mboersma commented Sep 5, 2018

What this PR does / why we need it:

Simplifies some code as suggested by the gosimple plugin to gometalinter and enables that plugin for the make test-style target.

(We can't run this plugin in general for linting, however, because it suggests some incorrect changes to pkg/api/types_test.go.)

Special notes for your reviewer:

Obviously not a critical change, but I've had this branch sitting around for a month so I freshened it up and made a PR.

If applicable:

  • documentation
  • unit tests
  • tested backward compatibility (ie. deploy with previous version, upgrade with this branch)

Release note:

NONE

@mboersma mboersma self-assigned this Sep 5, 2018
@ghost ghost added the in progress label Sep 5, 2018
@acs-bot acs-bot added the size/M label Sep 5, 2018
@@ -618,12 +618,12 @@ func TestSetVMSSDefaults(t *testing.T) {

properties.AgentPoolProfiles[0].Count = 110
setPropertiesDefaults(&mockCS, false, false)
if *properties.AgentPoolProfiles[0].SinglePlacementGroup != false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

@@ -618,12 +618,12 @@ func TestSetVMSSDefaults(t *testing.T) {

properties.AgentPoolProfiles[0].Count = 110
setPropertiesDefaults(&mockCS, false, false)
if *properties.AgentPoolProfiles[0].SinglePlacementGroup != false {
if *properties.AgentPoolProfiles[0].SinglePlacementGroup {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the * syntax here at all?

@@ -23,7 +23,7 @@ type APIModelValue struct {

// MapValues converts an arraw of rwa ApiModel values (like ["masterProfile.count=4","linuxProfile.adminUsername=admin"]) to a map
func MapValues(m map[string]APIModelValue, setFlagValues []string) {
if setFlagValues == nil || len(setFlagValues) == 0 {
if len(setFlagValues) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no... really, are you sure you're unset?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/acsengine/transform/apimodel_merger.go:26:5:warning: should omit nil check; len() for nil slices is defined as zero (S1009) (gosimple).

@@ -40,7 +40,7 @@ func (l *LinuxProfile) Validate() error {

// Validate implements APIObject
func (a *AADProfile) Validate(rbacEnabled *bool) error {
if rbacEnabled == nil || *rbacEnabled == false {
if rbacEnabled == nil || !*rbacEnabled {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see helpers.IsTrueBoolPointer(), I think that's a more expressive way to put the above. E.g.:

if !helper.IsTrueBoolPointer(rbacEnabled) {...

@@ -244,7 +244,6 @@ func TestConvertToV20180331AddonProfile(t *testing.T) {

func TestConvertKubernetesConfigToEnableRBACV20180331AgentPoolOnly(t *testing.T) {
var kc *KubernetesConfig
kc = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol I'm surprised the compiler accepts this

@@ -384,7 +384,7 @@ func convertV20180331AgentPoolOnlyWindowsProfile(obj *v20180331.WindowsProfile)
}

func convertV20180331AgentPoolOnlyKubernetesConfig(enableRBAC *bool) *KubernetesConfig {
if enableRBAC != nil && *enableRBAC == true {
if enableRBAC != nil && *enableRBAC {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my above suggestion

@@ -226,19 +226,18 @@ func TestConvertV20170831AgentPoolOnlyOrchestratorProfile_KubernetesConfig(t *te
t.Error("OrchestratorProfile.KubernetesConfig expected not to be nil")
}

if op.KubernetesConfig.EnableRbac == nil || *op.KubernetesConfig.EnableRbac == true {
if op.KubernetesConfig.EnableRbac == nil || *op.KubernetesConfig.EnableRbac {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

t.Error("OrchestratorProfile.KubernetesConfig.EnableRbac expected to be *false")
}

if op.KubernetesConfig.EnableSecureKubelet == nil || *op.KubernetesConfig.EnableSecureKubelet == true {
if op.KubernetesConfig.EnableSecureKubelet == nil || *op.KubernetesConfig.EnableSecureKubelet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

t.Error("OrchestratorProfile.KubernetesConfig.EnableSecureKubelet expected to be *false")
}

}

func TestConvertV20180331AgentPoolOnlyKubernetesConfig(t *testing.T) {
var kc *KubernetesConfig
kc = convertV20180331AgentPoolOnlyKubernetesConfig(helpers.PointerToBool(true))
var kc = convertV20180331AgentPoolOnlyKubernetesConfig(helpers.PointerToBool(true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥

@@ -272,15 +271,15 @@ func TestConvertV20180331AgentPoolOnlyKubernetesConfig(t *testing.T) {
t.Error("EnableRbac expected not to be nil")
}

if *kc.EnableRbac != false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow

t.Error("EnableRbac expected to be false")
}

if kc.EnableSecureKubelet == nil {
t.Error("EnableSecureKubelet expected not to be nil")
}

if *kc.EnableSecureKubelet != false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@@ -297,15 +296,15 @@ func TestConvertV20180331AgentPoolOnlyKubernetesConfig(t *testing.T) {
t.Error("EnableRbac expected not to be nil")
}

if *kc.EnableRbac != false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

t.Error("EnableRbac expected to be false")
}

if kc.EnableSecureKubelet == nil {
t.Error("EnableSecureKubelet expected not to be nil")
}

if *kc.EnableSecureKubelet != false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it stop

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It stops here.

@@ -112,7 +112,7 @@ func (az *AzureClient) CreateRoleAssignmentSimple(ctx context.Context, resourceG
},
}

re := regexp.MustCompile("(?i)status=(\\d+)")
re := regexp.MustCompile(`(?i)status=(\d+)`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, that extra \, wouldn't that mean that that regex wouldn't be looking for a digit? How did the prior regex even work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original regex is a normal, interpreted string literal, so it needed to escape the backslash. Using the backtics syntax makes it a raw string literal, so no escaping is needed. Raw string literals are preferable for regexes for readability and copy-and-pasting.

}

return false
return strings.Contains(e.ExpandedDefinition.Properties.OrchestratorProfile.KubernetesConfig.NetworkPolicy, name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥 💥

@@ -212,7 +212,7 @@ func (d *Deployment) WaitForReplicas(n int, sleep, duration time.Duration) ([]po
select {
case err := <-errCh:
return pods, err
case _ = <-readyCh:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wut

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can elide the no-op assignment inside a select statement.

@@ -554,7 +554,7 @@ func (p *Pod) ValidateOmsAgentLogs(execCmdString string, sleep, duration time.Du

// CheckWindowsOutboundConnection will keep retrying the check if an error is received until the timeout occurs or it passes. This helps us when DNS may not be available for some time after a pod starts.
func (p *Pod) CheckWindowsOutboundConnection(sleep, duration time.Duration) (bool, error) {
exp, err := regexp.Compile("(StatusCode\\s*:\\s*200)")
exp, err := regexp.Compile(`(StatusCode\s*:\s*200)`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm getting it now that the surrounding quotes were escaping the slashes and then passing those into the regex compiler literally

@codecov
Copy link

codecov bot commented Sep 6, 2018

Codecov Report

Merging #3802 into master will increase coverage by 0.05%.
The diff coverage is 20%.

@@            Coverage Diff            @@
##           master   #3802      +/-   ##
=========================================
+ Coverage   55.65%   55.7%   +0.05%     
=========================================
  Files         109     109              
  Lines       16233   16232       -1     
=========================================
+ Hits         9034    9042       +8     
+ Misses       6410    6398      -12     
- Partials      789     792       +3

@acs-bot acs-bot added approved and removed size/M labels Sep 6, 2018
@acs-bot acs-bot added the size/L label Sep 6, 2018
@mboersma mboersma changed the title Simplify code as suggested by gosimple Enable "gosimple" linter plugin Sep 6, 2018
@jackfrancis
Copy link
Member

/lgtm

@acs-bot
Copy link

acs-bot commented Sep 7, 2018

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jackfrancis, mboersma

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [jackfrancis,mboersma]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@jackfrancis jackfrancis merged commit 8eda202 into Azure:master Sep 7, 2018
@ghost ghost removed the awaiting review label Sep 7, 2018
@mboersma mboersma deleted the linter-updates branch September 7, 2018 22:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants