Skip to content

Commit 4dd6f6f

Browse files
committed
fix gometalinter violations
1 parent 936760e commit 4dd6f6f

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

pkg/drivers/linode/linode.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package linode
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io/ioutil"
89
"net"
910
"net/http"
10-
"strings"
1111
"strconv"
12-
"encoding/json"
12+
"strings"
1313

1414
"github.com/docker/machine/libmachine/drivers"
1515
"github.com/docker/machine/libmachine/log"
@@ -61,7 +61,7 @@ const (
6161
defaultContainerLinuxSSHUser = "core"
6262
)
6363

64-
// NewDriver
64+
// NewDriver creates and returns a new instance of the Linode driver
6565
func NewDriver(hostName, storePath string) *Driver {
6666
return &Driver{
6767
InstanceImage: defaultInstanceImage,
@@ -75,7 +75,7 @@ func NewDriver(hostName, storePath string) *Driver {
7575
}
7676
}
7777

78-
// Get Linode Client
78+
// getClient prepares the Linode APIv4 Client
7979
func (d *Driver) getClient() *linodego.Client {
8080
if d.client == nil {
8181
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.APIToken})
@@ -133,7 +133,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
133133
EnvVar: "LINODE_LABEL",
134134
Name: "linode-label",
135135
Usage: "Linode Instance Label",
136-
},
136+
},
137137
mcnflag.StringFlag{
138138
EnvVar: "LINODE_REGION",
139139
Name: "linode-region",
@@ -192,7 +192,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
192192
EnvVar: "LINODE_STACKSCRIPT_DATA",
193193
Name: "linode-stackscript-data",
194194
Usage: "A JSON string specifying data for the selected StackScript",
195-
Value: "",
195+
Value: "",
196196
},
197197
}
198198
}
@@ -234,8 +234,6 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
234234
d.SwapSize = flags.Int("linode-swap-size")
235235
d.DockerPort = flags.Int("linode-docker-port")
236236

237-
log.Infof("Using SSH port %d", d.SSHPort)
238-
239237
d.SetSwarmConfigFromFlags(flags)
240238

241239
if d.APIToken == "" {
@@ -264,7 +262,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
264262

265263
err := json.Unmarshal([]byte(stackScriptData), &d.StackScriptData)
266264
if err != nil {
267-
return fmt.Errorf("linode StackScript data must be valid JSON: %v", err)
265+
return fmt.Errorf("Linode StackScript data must be valid JSON: %s", err)
268266
}
269267
}
270268
}
@@ -276,19 +274,20 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
276274
return nil
277275
}
278276

277+
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
279278
func (d *Driver) PreCreateCheck() error {
280-
// TODO linode-stackscript-file should be read and uploaded (private), then used for boot.
279+
// TODO(displague) linode-stackscript-file should be read and uploaded (private), then used for boot.
281280
// RevNote could be sha256 of file so the file can be referenced instead of reuploaded.
282281

283282
client := d.getClient()
284283

285284
if d.StackScriptUser != "" {
286285
/* N.B. username isn't on the list of filterable fields, however
287-
adding it doesn't make anything fail, so if it becomes
288-
filterable in future this will become more efficient */
289-
options := map[string]string {
286+
adding it doesn't make anything fail, so if it becomes
287+
filterable in future this will become more efficient */
288+
options := map[string]string{
290289
"username": d.StackScriptUser,
291-
"label": d.StackScriptLabel,
290+
"label": d.StackScriptLabel,
292291
}
293292
b, err := json.Marshal(options)
294293
if err != nil {
@@ -299,7 +298,7 @@ func (d *Driver) PreCreateCheck() error {
299298
if err != nil {
300299
return err
301300
}
302-
var script *linodego.Stackscript = nil
301+
var script *linodego.Stackscript
303302
for _, s := range stackscripts {
304303
if s.Username == d.StackScriptUser {
305304
script = &s
@@ -313,11 +312,11 @@ func (d *Driver) PreCreateCheck() error {
313312
d.StackScriptUser = script.Username
314313
d.StackScriptLabel = script.Label
315314
d.StackScriptID = script.ID
316-
} else if (d.StackScriptID != 0) {
315+
} else if d.StackScriptID != 0 {
317316
script, err := client.GetStackscript(context.TODO(), d.StackScriptID)
318317

319318
if err != nil {
320-
return fmt.Errorf("StackScript %d could not be used: %v", err)
319+
return fmt.Errorf("StackScript %d could not be used: %s", d.StackScriptID, err)
321320
}
322321

323322
d.StackScriptUser = script.Username
@@ -331,6 +330,10 @@ func (d *Driver) PreCreateCheck() error {
331330
func (d *Driver) Create() error {
332331
log.Info("Creating Linode machine instance...")
333332

333+
if d.SSHPort != defaultSSHPort {
334+
log.Infof("Using SSH port %d", d.SSHPort)
335+
}
336+
334337
publicKey, err := d.createSSHKey()
335338
if err != nil {
336339
return err
@@ -499,10 +502,15 @@ func (d *Driver) publicSSHKeyPath() string {
499502
// privateIP determines if an IP is for private use (RFC1918)
500503
// https://stackoverflow.com/a/41273687
501504
func privateIP(ip net.IP) bool {
502-
private := false
503-
_, private24BitBlock, _ := net.ParseCIDR("10.0.0.0/8")
504-
_, private20BitBlock, _ := net.ParseCIDR("172.16.0.0/12")
505-
_, private16BitBlock, _ := net.ParseCIDR("192.168.0.0/16")
506-
private = private24BitBlock.Contains(ip) || private20BitBlock.Contains(ip) || private16BitBlock.Contains(ip)
507-
return private
505+
return ipInCIDR(ip, "10.0.0.0/8") || ipInCIDR(ip, "172.16.0.0/12") || ipInCIDR(ip, "192.168.0.0/16")
506+
}
507+
508+
func ipInCIDR(ip net.IP, CIDR string) bool {
509+
_, ipNet, err := net.ParseCIDR(CIDR)
510+
if err != nil {
511+
log.Errorf("Error parsing CIDR %s: %s", CIDR, err)
512+
513+
return false
514+
}
515+
return ipNet.Contains(ip)
508516
}

pkg/drivers/linode/linode_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package linode
22

33
import (
4+
"net"
45
"testing"
56

67
"github.com/docker/machine/libmachine/drivers"
@@ -23,3 +24,32 @@ func TestSetConfigFromFlags(t *testing.T) {
2324
assert.NoError(t, err)
2425
assert.Empty(t, checkFlags.InvalidFlags)
2526
}
27+
28+
func TestPrivateIP(t *testing.T) {
29+
ip := net.IP{}
30+
for _, addr := range [][]byte{
31+
[]byte("172.16.0.1"),
32+
[]byte("192.168.0.1"),
33+
[]byte("10.0.0.1"),
34+
} {
35+
if err := ip.UnmarshalText(addr); err != nil {
36+
t.Error(err)
37+
}
38+
assert.True(t, privateIP(ip))
39+
}
40+
41+
if err := ip.UnmarshalText([]byte("1.1.1.1")); err != nil {
42+
t.Error(err)
43+
}
44+
assert.False(t, privateIP(ip))
45+
}
46+
47+
func TestIPInCIDR(t *testing.T) {
48+
tenOne := net.IP{}
49+
50+
if err := tenOne.UnmarshalText([]byte("10.0.0.1")); err != nil {
51+
t.Error(err)
52+
}
53+
assert.True(t, ipInCIDR(tenOne, "10.0.0.0/8"), "10.0.0.1 is in 10.0.0.0/8")
54+
assert.False(t, ipInCIDR(tenOne, "254.0.0.0/8"), "10.0.0.1 is not in 254.0.0.0/8")
55+
}

0 commit comments

Comments
 (0)