Skip to content

Commit 3712176

Browse files
committed
fixing lint issues for default linters enabled by golangci
1 parent ec2252e commit 3712176

File tree

15 files changed

+47
-80
lines changed

15 files changed

+47
-80
lines changed

cmd/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ func main() {
9494
mc.Start()
9595

9696
cloud := aws.New(options.AWSAPIMaxRetries, options.AWSAPIDebug, options.config.ClusterName, mc, cc)
97-
controller.Initialize(&options.config, mgr, mc, cloud)
97+
if err := controller.Initialize(&options.config, mgr, mc, cloud); err != nil {
98+
glog.Fatal(err)
99+
}
98100

99101
mux := http.NewServeMux()
100102
if options.ProfilingEnabled {
@@ -130,7 +132,7 @@ func registerHandlers(mux *http.ServeMux) {
130132
mux.HandleFunc("/build", func(w http.ResponseWriter, r *http.Request) {
131133
w.WriteHeader(http.StatusOK)
132134
b, _ := json.Marshal(version.String())
133-
w.Write(b)
135+
_, _ = w.Write(b)
134136
})
135137

136138
mux.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {

cmd/options.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ func getOptions() (*Options, error) {
108108
`Enable profiling via web interface host:port/debug/pprof/`)
109109
options.config.BindFlags(flags)
110110

111-
flags.MarkDeprecated("aws-sync-period", `No longer used, will be removed in next release`)
112-
flags.MarkDeprecated("default-backend-service", `No longer used, will be removed in next release`)
111+
_ = flags.MarkDeprecated("aws-sync-period", `No longer used, will be removed in next release`)
112+
_ = flags.MarkDeprecated("default-backend-service", `No longer used, will be removed in next release`)
113113

114-
flag.Set("logtostderr", "true")
114+
_ = flag.Set("logtostderr", "true")
115115
flags.AddGoFlagSet(flag.CommandLine)
116-
flags.Parse(os.Args)
116+
_ = flags.Parse(os.Args)
117117

118118
err := configOptionsByEnvironmentVariables(options)
119119
if err != nil {
@@ -184,6 +184,6 @@ func validateOptions(options *Options) error {
184184

185185
func generateALBNamePrefix(clusterName string) string {
186186
hash := crc32.New(crc32.MakeTable(0xedb88320))
187-
hash.Write([]byte(clusterName))
187+
_, _ = hash.Write([]byte(clusterName))
188188
return hex.EncodeToString(hash.Sum(nil))
189189
}

internal/alb/generator/name.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type NameGenerator struct {
1919

2020
func (gen *NameGenerator) NameLB(namespace string, ingressName string) string {
2121
hasher := md5.New()
22-
hasher.Write([]byte(namespace + ingressName))
22+
_, _ = hasher.Write([]byte(namespace + ingressName))
2323
hash := hex.EncodeToString(hasher.Sum(nil))[:4]
2424

2525
r, _ := regexp.Compile("[[:^alnum:]]")
@@ -40,11 +40,11 @@ func (gen *NameGenerator) NameTG(namespace string, ingressName string, serviceNa
4040
LBName := gen.NameLB(namespace, ingressName)
4141

4242
hasher := md5.New()
43-
hasher.Write([]byte(LBName))
44-
hasher.Write([]byte(serviceName))
45-
hasher.Write([]byte(servicePort))
46-
hasher.Write([]byte(protocol))
47-
hasher.Write([]byte(targetType))
43+
_, _ = hasher.Write([]byte(LBName))
44+
_, _ = hasher.Write([]byte(serviceName))
45+
_, _ = hasher.Write([]byte(servicePort))
46+
_, _ = hasher.Write([]byte(protocol))
47+
_, _ = hasher.Write([]byte(targetType))
4848

4949
return fmt.Sprintf("%.12s-%.19s", gen.ALBNamePrefix, hex.EncodeToString(hasher.Sum(nil)))
5050
}

internal/alb/lb/loadbalancer.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ func (controller *defaultController) ensureLBInstance(ctx context.Context, lbCon
185185
}
186186
return instance, nil
187187
}
188-
controller.reconcileLBInstance(ctx, instance, lbConfig)
188+
if err := controller.reconcileLBInstance(ctx, instance, lbConfig); err != nil {
189+
return nil, err
190+
}
189191
return instance, nil
190192
}
191193

@@ -260,7 +262,7 @@ func (controller *defaultController) reconcileWAF(ctx context.Context, lbArn str
260262
if err != nil {
261263
return fmt.Errorf("error fetching web acl %v: %v", aws.StringValue(webACLID), err)
262264
}
263-
if b == false {
265+
if !b {
264266
return fmt.Errorf("web acl %v does not exist", aws.StringValue(webACLID))
265267
}
266268
}

internal/alb/lb/loadbalancer_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15-
func ec2Filter(k string, v ...string) *ec2.Filter {
16-
return &ec2.Filter{
17-
Name: aws.String(k),
18-
Values: aws.StringSlice(v),
19-
}
20-
}
21-
2215
func TestCloud_ResolveSecurityGroupNames(t *testing.T) {
2316
idmap := map[string]string{
2417
"sg1": "sg-123456",

internal/alb/sg/lb_attachment.go

+17-30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sg
33
import (
44
"context"
55
"fmt"
6+
"k8s.io/apimachinery/pkg/util/sets"
67

78
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/albctx"
89

@@ -40,10 +41,9 @@ func (controller *lbAttachmentController) Reconcile(ctx context.Context, attachm
4041
return fmt.Errorf("loadBalancer %s doesn't exists", attachment.LbArn)
4142
}
4243

43-
groupsInLb := aws.StringValueSlice(loadBalancer.SecurityGroups)
44-
groupsToAdd := diffStringSet(attachment.GroupIDs, groupsInLb)
45-
groupsToDelete := diffStringSet(groupsInLb, attachment.GroupIDs)
46-
if len(groupsToAdd) != 0 || len(groupsToDelete) != 0 {
44+
desiredGroups := sets.NewString(attachment.GroupIDs...)
45+
currentGroups := sets.NewString(aws.StringValueSlice(loadBalancer.SecurityGroups)...)
46+
if !desiredGroups.Equal(currentGroups) {
4747
albctx.GetLogger(ctx).Infof("modify securityGroup on LoadBalancer %s to be %v", attachment.LbArn, attachment.GroupIDs)
4848
_, err := controller.cloud.SetSecurityGroupsWithContext(ctx, &elbv2.SetSecurityGroupsInput{
4949
LoadBalancerArn: aws.String(attachment.LbArn),
@@ -65,21 +65,22 @@ func (controller *lbAttachmentController) Delete(ctx context.Context, attachment
6565
return fmt.Errorf("loadBalancer %s doesn't exist", attachment.LbArn)
6666
}
6767

68-
groupsInLb := aws.StringValueSlice(loadBalancer.SecurityGroups)
69-
groupsShouldRemain := diffStringSet(groupsInLb, attachment.GroupIDs)
70-
if len(groupsShouldRemain) != len(groupsInLb) {
71-
if len(groupsShouldRemain) == 0 {
68+
undesiredGroups := sets.NewString(attachment.GroupIDs...)
69+
currentGroups := sets.NewString(aws.StringValueSlice(loadBalancer.SecurityGroups)...)
70+
groupsToKeep := currentGroups.Difference(undesiredGroups)
71+
if len(groupsToKeep) != len(currentGroups) {
72+
if len(groupsToKeep) == 0 {
7273
defaultSGID, err := controller.getDefaultSecurityGroupID()
7374
if err != nil {
7475
return fmt.Errorf("failed to get default securityGroup for current vpc due to %v", err)
7576
}
76-
groupsShouldRemain = append(groupsShouldRemain, *defaultSGID)
77+
groupsToKeep.Insert(defaultSGID)
7778
}
78-
79-
albctx.GetLogger(ctx).Infof("modify securityGroup on LoadBalancer %s to be %v", attachment.LbArn, groupsShouldRemain)
79+
desiredGroups := groupsToKeep.List()
80+
albctx.GetLogger(ctx).Infof("modify securityGroup on LoadBalancer %s to be %v", attachment.LbArn, desiredGroups)
8081
_, err := controller.cloud.SetSecurityGroupsWithContext(ctx, &elbv2.SetSecurityGroupsInput{
8182
LoadBalancerArn: aws.String(attachment.LbArn),
82-
SecurityGroups: aws.StringSlice(groupsShouldRemain),
83+
SecurityGroups: aws.StringSlice(desiredGroups),
8384
})
8485
if err != nil {
8586
return err
@@ -88,29 +89,15 @@ func (controller *lbAttachmentController) Delete(ctx context.Context, attachment
8889
return nil
8990
}
9091

91-
func (controller *lbAttachmentController) getDefaultSecurityGroupID() (*string, error) {
92+
func (controller *lbAttachmentController) getDefaultSecurityGroupID() (string, error) {
9293
vpcID, err := controller.cloud.GetVPCID()
9394
if err != nil {
94-
return nil, err
95+
return "", err
9596
}
9697

9798
defaultSG, err := controller.cloud.GetSecurityGroupByName(*vpcID, "default")
9899
if err != nil {
99-
return nil, err
100-
}
101-
return defaultSG.GroupId, nil
102-
}
103-
104-
// diffStringSet calcuates the set_difference as source - target
105-
func diffStringSet(source []string, target []string) (diffs []string) {
106-
targetSet := make(map[string]bool)
107-
for _, t := range target {
108-
targetSet[t] = true
109-
}
110-
for _, s := range source {
111-
if _, ok := targetSet[s]; !ok {
112-
diffs = append(diffs, s)
113-
}
100+
return "", err
114101
}
115-
return diffs
102+
return aws.StringValue(defaultSG.GroupId), nil
116103
}

internal/alb/sg/namer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Namer interface {
1515
type namer struct{}
1616

1717
func (namer *namer) NameLbSG(loadBalancerID string) string {
18-
return fmt.Sprintf("%s", loadBalancerID)
18+
return loadBalancerID
1919
}
2020

2121
func (namer *namer) NameInstanceSG(loadBalancerID string) string {

internal/alb/sg/security_group.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func diffIPPermissions(source []*ec2.IpPermission, target []*ec2.IpPermission) (
179179
break
180180
}
181181
}
182-
if containsInTarget == false {
182+
if !containsInTarget {
183183
diffs = append(diffs, sPermission)
184184
}
185185
}
@@ -223,7 +223,7 @@ func diffIPRanges(source []*ec2.IpRange, target []*ec2.IpRange) (diffs []*ec2.Ip
223223
break
224224
}
225225
}
226-
if containsInTarget == false {
226+
if !containsInTarget {
227227
diffs = append(diffs, sRange)
228228
}
229229
}
@@ -245,7 +245,7 @@ func diffUserIDGroupPairs(source []*ec2.UserIdGroupPair, target []*ec2.UserIdGro
245245
break
246246
}
247247
}
248-
if containsInTarget == false {
248+
if !containsInTarget {
249249
diffs = append(diffs, sPair)
250250
}
251251
}
@@ -255,8 +255,5 @@ func diffUserIDGroupPairs(source []*ec2.UserIdGroupPair, target []*ec2.UserIdGro
255255
// userIDGroupPairEquals test whether two UserIdGroupPair equals
256256
// currently we only check for groupId
257257
func userIDGroupPairEquals(source *ec2.UserIdGroupPair, target *ec2.UserIdGroupPair) bool {
258-
if aws.StringValue(source.GroupId) != aws.StringValue(target.GroupId) {
259-
return false
260-
}
261-
return true
258+
return aws.StringValue(source.GroupId) == aws.StringValue(target.GroupId)
262259
}

internal/alb/tg/targets_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func Test_NewTargets(t *testing.T) {
2222
targetType string
2323
ingress *extensions.Ingress
2424
backend *extensions.IngressBackend
25-
output *Targets
2625
}{
2726
{
2827
name: "std params",

internal/aws/ec2.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
)
1515

1616
const (
17-
instSpecifierTag = "instance"
1817
ManagedByKey = "ManagedBy"
1918
ManagedByValue = "alb-ingress"
2019

internal/aws/elbv2.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import (
1010
"github.com/aws/aws-sdk-go/service/elbv2"
1111
)
1212

13-
const (
14-
// Amount of time between each deletion attempt (or reattempt) for a target group
15-
deleteTargetGroupReattemptSleep int = 10
16-
// Maximum attempts should be made to delete a target group
17-
deleteTargetGroupReattemptMax int = 10
18-
)
19-
2013
type ELBV2API interface {
2114
StatusELBV2() func() error
2215

@@ -140,9 +133,7 @@ func (c *Cloud) GetRules(ctx context.Context, listenerArn string) ([]*elbv2.Rule
140133
}
141134
for p.Next() {
142135
page := p.Page().(*elbv2.DescribeRulesOutput)
143-
for _, rule := range page.Rules {
144-
rules = append(rules, rule)
145-
}
136+
rules = append(rules, page.Rules...)
146137
}
147138
return rules, p.Err()
148139
}
@@ -167,9 +158,7 @@ func (c *Cloud) ListListenersByLoadBalancer(ctx context.Context, lbArn string) (
167158
if p == nil {
168159
return false
169160
}
170-
for _, listener := range p.Listeners {
171-
listeners = append(listeners, listener)
172-
}
161+
listeners = append(listeners, p.Listeners...)
173162
return true
174163
})
175164
if err != nil {

internal/ingress/backend/endpoint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func (resolver *endpointResolver) resolveInstance(ingress *extensions.Ingress, b
6868
instanceID, err := resolver.store.GetNodeInstanceID(node)
6969
if err != nil {
7070
return nil, err
71-
} else if b, err := resolver.cloud.IsNodeHealthy(instanceID); err != nil {
71+
} else if healthy, err := resolver.cloud.IsNodeHealthy(instanceID); err != nil {
7272
return nil, err
73-
} else if b != true {
73+
} else if !healthy {
7474
continue
7575
}
7676
result = append(result, &elbv2.TargetDescription{

internal/ingress/controller/store/dummy.go

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func (d Dummy) GetIngressAnnotations(key string) (*annotations.Ingress, error) {
6363

6464
// Run ...
6565
func (d Dummy) Run(stopCh chan struct{}) {
66-
return
6766
}
6867

6968
// GetConfig ...

internal/ingress/controller/store/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func New(mgr manager.Manager, cfg *config.Configuration) (Storer, error) {
186186
if !class.IsValidIngress(cfg.IngressClass, ing) {
187187
return
188188
}
189-
store.listers.IngressAnnotation.Delete(ing)
189+
_ = store.listers.IngressAnnotation.Delete(ing)
190190
},
191191
UpdateFunc: func(old, cur interface{}) {
192192
curIng := cur.(*extensions.Ingress)

pkg/util/types/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func init() {
1717

1818
func DeepEqual(x, y interface{}) bool {
1919
b := awsutil.DeepEqual(x, y)
20-
if b == false {
20+
if !b {
2121
logger.DebugLevelf(3, "DeepEqual(%v, %v) found inequality", log.Prettify(x), log.Prettify(y))
2222
}
2323
return b

0 commit comments

Comments
 (0)