Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit ab6aeb7

Browse files
committed
Treewide various cleanups
1 parent d66154f commit ab6aeb7

File tree

10 files changed

+21
-35
lines changed

10 files changed

+21
-35
lines changed

cmd/cloud-network/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ func main() {
157157
}
158158
}()
159159

160-
log.Infof("Local Instance Metadata Cache Server listening at '%+v':'%+v'", ip, port)
160+
log.Infof("Local instance metadata cache Server listening at '%+v':'%+v'", ip, port)
161161
log.Info(srv.ListenAndServe())
162162
}

pkg/cloud/cloud.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,20 @@ func DetectEC2() bool {
7070

7171
func DetectGCP() bool {
7272
productName, _ := ioutil.ReadFile("/sys/class/dmi/id/product_name")
73-
7473
return strings.Contains(string(productName), "Google Compute Engine")
7574
}
7675

7776
func DetectAlibaba() bool {
7877
productName, _ := ioutil.ReadFile("/sys/class/dmi/id/product_name")
79-
8078
return strings.Contains(string(productName), "Alibaba Cloud")
8179
}
8280

8381
func DetectDigitalOcean() bool {
8482
vendor, _ := ioutil.ReadFile("/sys/class/dmi/id/sys_vendor")
85-
8683
return strings.Contains(string(vendor), "DigitalOcean")
8784
}
8885

8986
func DetectOracle() bool {
9087
chassisAssetTag, _ := ioutil.ReadFile("/sys/class/dmi/id/chassis_asset_tag")
91-
9288
return strings.Contains(string(chassisAssetTag), "OracleCloud")
9389
}

pkg/network/link.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ func LinkSetOperStateUp(ifIndex int) error {
7474
return err
7575
}
7676

77-
err = netlink.LinkSetUp(link)
78-
if err != nil {
77+
if err := netlink.LinkSetUp(link);err != nil {
7978
return err
8079
}
8180

pkg/parser/parser.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
func ParseIp(ip string) (net.IP, error) {
1414
a := net.ParseIP(ip)
15-
1615
if a.To4() == nil || a.To16() == nil {
1716
return nil, errors.New("invalid IP")
1817
}

provider/azure.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (az *Azure) FetchCloudMetadata() error {
159159
func (az *Azure) parseIpv4AddressesFromMetadataByMac(mac string) (map[string]bool, error) {
160160
a := make(map[string]bool)
161161

162-
for i := 0; i < len(az.meta.Network.Interface); i++ {
162+
for i := range az.meta.Network.Interface {
163163
if strings.ToLower(parser.ParseMAC(az.meta.Network.Interface[i].MacAddress)) != mac {
164164
continue
165165
}
@@ -171,7 +171,7 @@ func (az *Azure) parseIpv4AddressesFromMetadataByMac(mac string) (map[string]boo
171171
continue
172172
}
173173

174-
for j := 0; j < len(az.meta.Network.Interface[i].Ipv4.IPAddress); j++ {
174+
for j := range az.meta.Network.Interface[i].Ipv4.IPAddress {
175175
privateIp := az.meta.Network.Interface[i].Ipv4.IPAddress[j].PrivateIpAddress + "/" + subnet.Prefix
176176
a[privateIp] = true
177177
}
@@ -182,7 +182,7 @@ func (az *Azure) parseIpv4AddressesFromMetadataByMac(mac string) (map[string]boo
182182
}
183183

184184
func (az *Azure) ConfigureNetworkFromCloudMeta(m *Environment) error {
185-
for i := 0; i < len(az.meta.Network.Interface); i++ {
185+
for i := range az.meta.Network.Interface {
186186
mac := strings.ToLower(parser.ParseMAC(az.meta.Network.Interface[i].MacAddress))
187187

188188
l, ok := m.Links.LinksByMAC[mac]
@@ -212,7 +212,7 @@ func (az *Azure) SaveCloudMetadata() error {
212212
}
213213

214214
func (az *Azure) LinkSaveCloudMetadata(m *Environment) error {
215-
for i := 0; i < len(az.meta.Network.Interface); i++ {
215+
for i := range az.meta.Network.Interface {
216216
mac := strings.ToLower(parser.ParseMAC(az.meta.Network.Interface[i].MacAddress))
217217
l, b := m.Links.LinksByMAC[mac]
218218
if !b {

provider/ec2.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,27 @@ func (ec2 *EC2) FetchCloudMetadata() error {
236236
return err
237237
}
238238

239-
c, err := web.Dispatch("http://" + EC2Endpoint + EC2MetaDataURLBase + EC2MetaDataIdentityCredentials, nil)
239+
c, err := web.Dispatch("http://"+EC2Endpoint+EC2MetaDataURLBase+EC2MetaDataIdentityCredentials, nil)
240240
if err != nil {
241241
return err
242242
}
243243

244-
doc, err := web.Dispatch("http://" + EC2Endpoint + EC2MetaDataDynamicIdentityDocument + "document", nil)
244+
doc, err := web.Dispatch("http://"+EC2Endpoint+EC2MetaDataDynamicIdentityDocument+"document", nil)
245245
if err != nil {
246246
return err
247247
}
248248

249-
pkcs7, err := web.Dispatch("http://" + EC2Endpoint + EC2MetaDataDynamicIdentityDocument + "pkcs7", nil)
249+
pkcs7, err := web.Dispatch("http://"+EC2Endpoint+EC2MetaDataDynamicIdentityDocument+"pkcs7", nil)
250250
if err != nil {
251251
return err
252252
}
253253

254-
signature, err := web.Dispatch("http://" + EC2Endpoint + EC2MetaDataDynamicIdentityDocument + "signature", nil)
254+
signature, err := web.Dispatch("http://"+EC2Endpoint+EC2MetaDataDynamicIdentityDocument+"signature", nil)
255255
if err != nil {
256256
return err
257257
}
258258

259-
rsa2048, err := web.Dispatch("http://" + EC2Endpoint + EC2MetaDataDynamicIdentityDocument + "rsa2048", nil)
259+
rsa2048, err := web.Dispatch("http://"+EC2Endpoint+EC2MetaDataDynamicIdentityDocument+"rsa2048", nil)
260260
if err != nil {
261261
return err
262262
}
@@ -333,15 +333,13 @@ func (ec2 *EC2) ConfigureNetworkFromCloudMeta(m *Environment) error {
333333
continue
334334
}
335335

336-
err = m.configureNetwork(&link, newAddresses)
337-
if err != nil {
336+
if err = m.configureNetwork(&link, newAddresses); err != nil {
338337
continue
339338
}
340339

341340
// EC2's primary interface looses connectivity if the second interface gets configured.
342341
// Hence add a default route for the primary interface too and rules for each address
343-
err = network.ConfigureByIndex(2)
344-
if err != nil {
342+
if err := network.ConfigureByIndex(2); err != nil {
345343
log.Errorf("Failed to configure network for link='%+v' ifindex='%+v': %+v", link.Name, link.Ifindex, err)
346344
}
347345
}

provider/gcp.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (g *GCP) FetchCloudMetadata() error {
136136
}
137137

138138
func (g *GCP) ParseIpv4GatewayFromMetadataByMac(mac string) (string, error) {
139-
for i := 0; i < len(g.meta.Instance.Networkinterfaces); i++ {
139+
for i := range g.meta.Instance.Networkinterfaces {
140140
if mac == g.meta.Instance.Networkinterfaces[i].Mac {
141141
return g.meta.Instance.Networkinterfaces[i].Gateway, nil
142142
}
@@ -146,7 +146,7 @@ func (g *GCP) ParseIpv4GatewayFromMetadataByMac(mac string) (string, error) {
146146
}
147147

148148
func (g *GCP) ParseLinkMTUFromMetadataByMac(mac string) (int, error) {
149-
for i := 0; i < len(g.meta.Instance.Networkinterfaces); i++ {
149+
for i:= range g.meta.Instance.Networkinterfaces {
150150
if mac == g.meta.Instance.Networkinterfaces[i].Mac {
151151
return g.meta.Instance.Networkinterfaces[i].Mtu, nil
152152
}
@@ -158,7 +158,7 @@ func (g *GCP) ParseLinkMTUFromMetadataByMac(mac string) (int, error) {
158158
func (g *GCP) parseIpv4AddressesFromMetadataByMac(mac string) (map[string]bool, error) {
159159
m := make(map[string]bool)
160160

161-
for i := 0; i < len(g.meta.Instance.Networkinterfaces); i++ {
161+
for i := range g.meta.Instance.Networkinterfaces {
162162
if mac == g.meta.Instance.Networkinterfaces[i].Mac {
163163
ip := g.meta.Instance.Networkinterfaces[i].IP
164164
mask := net.IPMask(net.ParseIP(g.meta.Instance.Networkinterfaces[i].Subnetmask).To4())
@@ -178,7 +178,7 @@ func (g *GCP) parseIpv4AddressesFromMetadataByMac(mac string) (map[string]bool,
178178
}
179179

180180
func (g *GCP) ConfigureNetworkFromCloudMeta(m *Environment) error {
181-
for i := 0; i < len(g.meta.Instance.Networkinterfaces); i++ {
181+
for i := range g.meta.Instance.Networkinterfaces {
182182
l, ok := m.Links.LinksByMAC[g.meta.Instance.Networkinterfaces[i].Mac]
183183
if !ok {
184184
log.Errorf("Failed to find link having MAC Address='%+v'", g.meta.Instance.Networkinterfaces[i].Mac)
@@ -206,7 +206,7 @@ func (g *GCP) SaveCloudMetadata() error {
206206
}
207207

208208
func (g *GCP) LinkSaveCloudMetadata(m *Environment) error {
209-
for i := 0; i < len(g.meta.Instance.Networkinterfaces); i++ {
209+
for i := range g.meta.Instance.Networkinterfaces {
210210
l, b := m.Links.LinksByMAC[g.meta.Instance.Networkinterfaces[i].Mac]
211211
if !b {
212212
continue

provider/provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ func New(provider string) *Environment {
5050
case cloud.GCP:
5151
m.gcp = NewGCP()
5252
default:
53+
return nil
5354
}
5455

5556
return m
5657
}
5758

5859
func AcquireCloudMetadata(m *Environment) error {
59-
var err error
60-
6160
m.Mutex.Lock()
6261
defer m.Mutex.Unlock()
6362

63+
var err error
6464
m.Links, err = network.AcquireLinks()
6565
if err != nil {
6666
log.Errorf("Failed to acquire link information: %+v", err)

provider/provider_network.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func (m *Environment) configureNetwork(link *network.Link, newAddresses map[stri
4848
for i := range newAddresses {
4949
_, ok := existingAddresses[i]
5050
if !ok {
51-
5251
if link.OperState == "down" {
5352
if err := network.LinkSetOperStateUp(link.Ifindex); err != nil {
5453
log.Errorf("Failed to bring up the link='%s' ifindex='%d': %+v", link.Name, link.Ifindex, err)
@@ -121,12 +120,11 @@ func (m *Environment) configureNetwork(link *network.Link, newAddresses map[stri
121120

122121
func (m *Environment) configureRoute(link *network.Link) error {
123122
var gw string
124-
var err error
125-
126123
if m.Kind == "gcp" {
127124
gw, _ = m.gcp.ParseIpv4GatewayFromMetadataByMac(link.Mac)
128125
}
129126

127+
var err error
130128
if len(gw) <= 0 {
131129
gw, err = network.GetIpv4Gateway(link.Ifindex)
132130
if err != nil {
@@ -149,7 +147,6 @@ func (m *Environment) configureRoute(link *network.Link) error {
149147
m.RoutesByIndex[link.Ifindex] = &rt
150148

151149
log.Infof("Successfully added default gateway='%s' for link='%s' ifindex='%+v' table='%d'", gw, link.Name, link.Ifindex, m.RouteTable+link.Ifindex)
152-
153150
log.Infof("Link='%s' ifindex='%d' is now configured", link.Name, link.Ifindex)
154151

155152
return nil

provider/watch_network.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,5 @@ func (m *Environment) dropConfiguration(ifIndex int, address string) {
132132
log.Debugf("Dropping routing rules link='%s' ifindex='%d' address='%s'", link.Name, link.Ifindex, address)
133133

134134
m.removeRoutingPolicyRule(address, &link)
135-
136-
log.Debugf("Dropping addresses link='%s' ifindex='%d' address='%s'", link.Name, link.Ifindex, address)
137-
138135
delete(m.AddressesByMAC[mac], address)
139136
}

0 commit comments

Comments
 (0)