Skip to content

Commit 8bd57c3

Browse files
committed
Retrieve ipv4s from linode.Instance instead of calling GetInstanceIPAddresses
We have to manually check whether each IP is public or private, but it saves us from calling the API once again.
1 parent d54a86a commit 8bd57c3

2 files changed

Lines changed: 23 additions & 57 deletions

File tree

cloud/linode/instances.go

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,6 @@ func (e instanceNoIPAddressesError) Error() string {
2727
return fmt.Sprintf("instance %d has no IP addresses", e.id)
2828
}
2929

30-
func (i *instances) nodeAddresses(ctx context.Context, linode *linodego.Instance) ([]v1.NodeAddress, error) {
31-
var addresses []v1.NodeAddress
32-
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeHostName, Address: linode.Label})
33-
34-
ips, err := i.client.GetInstanceIPAddresses(ctx, linode.ID)
35-
if err != nil {
36-
return nil, err
37-
}
38-
39-
if (len(ips.IPv4.Public) == 0) && (len(ips.IPv4.Private) == 0) {
40-
return nil, instanceNoIPAddressesError{linode.ID}
41-
}
42-
43-
if len(ips.IPv4.Public) > 0 {
44-
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: ips.IPv4.Public[0].Address})
45-
}
46-
// Allow Nodes to not have an ExternalIP, if this proves problematic this will be reverted
47-
48-
if len(ips.IPv4.Private) > 0 {
49-
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeInternalIP, Address: ips.IPv4.Private[0].Address})
50-
}
51-
// Allow Nodes to not have an InternalIP, if this proves problematic this will be reverted
52-
53-
return addresses, nil
54-
}
55-
5630
func (i *instances) lookupLinode(ctx context.Context, node *v1.Node) (*linodego.Instance, error) {
5731
providerID := node.Spec.ProviderID
5832
nodeName := types.NodeName(node.Name)
@@ -102,9 +76,18 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
10276
return nil, err
10377
}
10478

105-
addresses, err := i.nodeAddresses(ctx, linode)
106-
if err != nil {
107-
return nil, err
79+
if len(linode.IPv4) == 0 {
80+
return nil, instanceNoIPAddressesError{linode.ID}
81+
}
82+
83+
addresses := []v1.NodeAddress{{Type: v1.NodeHostName, Address: linode.Label}}
84+
85+
for _, ip := range linode.IPv4 {
86+
ipType := v1.NodeExternalIP
87+
if ip.IsPrivate() {
88+
ipType = v1.NodeInternalIP
89+
}
90+
addresses = append(addresses, v1.NodeAddress{Type: ipType, Address: ip.String()})
10891
}
10992

11093
// note that Zone is omitted as it's not a thing in Linode

cloud/linode/instances_test.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package linode
33
import (
44
"context"
55
"errors"
6+
"net"
67
"net/http"
78
"strconv"
89
"testing"
@@ -135,12 +136,6 @@ func TestMetadataRetrieval(t *testing.T) {
135136
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return([]linodego.Instance{
136137
{ID: id, Label: name},
137138
}, nil)
138-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
139-
IPv4: &linodego.InstanceIPv4Response{
140-
Public: []*linodego.InstanceIP{},
141-
Private: []*linodego.InstanceIP{},
142-
},
143-
}, nil)
144139

145140
meta, err := instances.InstanceMetadata(ctx, node)
146141
assert.Error(t, err, instanceNoIPAddressesError{id})
@@ -151,19 +146,13 @@ func TestMetadataRetrieval(t *testing.T) {
151146
id := 123
152147
name := "mock-instance"
153148
node := nodeWithName(name)
154-
publicIPv4 := "45.76.101.25"
155-
privateIPv4 := "192.168.133.65"
149+
publicIPv4 := net.ParseIP("45.76.101.25")
150+
privateIPv4 := net.ParseIP("192.168.133.65")
156151
linodeType := "g6-standard-1"
157152
region := "us-east"
158153
filter := linodeFilterListOptions(name)
159154
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return([]linodego.Instance{
160-
{ID: id, Label: name, Type: linodeType, Region: region},
161-
}, nil)
162-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
163-
IPv4: &linodego.InstanceIPv4Response{
164-
Public: []*linodego.InstanceIP{{Address: publicIPv4}},
165-
Private: []*linodego.InstanceIP{{Address: privateIPv4}},
166-
},
155+
{ID: id, Label: name, Type: linodeType, Region: region, IPv4: []*net.IP{&publicIPv4, &privateIPv4}},
167156
}, nil)
168157

169158
meta, err := instances.InstanceMetadata(ctx, node)
@@ -177,11 +166,11 @@ func TestMetadataRetrieval(t *testing.T) {
177166
},
178167
{
179168
Type: v1.NodeExternalIP,
180-
Address: publicIPv4,
169+
Address: publicIPv4.String(),
181170
},
182171
{
183172
Type: v1.NodeInternalIP,
184-
Address: privateIPv4,
173+
Address: privateIPv4.String(),
185174
},
186175
})
187176
})
@@ -191,18 +180,12 @@ func TestMetadataRetrieval(t *testing.T) {
191180
name := "my-instance"
192181
providerID := providerIDPrefix + strconv.Itoa(id)
193182
node := nodeWithProviderID(providerID)
194-
publicIPv4 := "32.74.121.25"
195-
privateIPv4 := "192.168.121.42"
183+
publicIPv4 := net.ParseIP("32.74.121.25")
184+
privateIPv4 := net.ParseIP("192.168.121.42")
196185
linodeType := "g6-standard-1"
197186
region := "us-east"
198187
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
199-
ID: id, Label: name, Type: linodeType, Region: region,
200-
}, nil)
201-
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
202-
IPv4: &linodego.InstanceIPv4Response{
203-
Public: []*linodego.InstanceIP{{Address: publicIPv4}},
204-
Private: []*linodego.InstanceIP{{Address: privateIPv4}},
205-
},
188+
ID: id, Label: name, Type: linodeType, Region: region, IPv4: []*net.IP{&publicIPv4, &privateIPv4},
206189
}, nil)
207190

208191
meta, err := instances.InstanceMetadata(ctx, node)
@@ -217,11 +200,11 @@ func TestMetadataRetrieval(t *testing.T) {
217200
},
218201
{
219202
Type: v1.NodeExternalIP,
220-
Address: publicIPv4,
203+
Address: publicIPv4.String(),
221204
},
222205
{
223206
Type: v1.NodeInternalIP,
224-
Address: privateIPv4,
207+
Address: privateIPv4.String(),
225208
},
226209
})
227210
})

0 commit comments

Comments
 (0)