Skip to content

Commit ed8752e

Browse files
committed
use providerid to get linode id
1 parent b8f7485 commit ed8752e

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

cloud/nodeipam/ipam/cloud_allocator.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ package ipam
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322
"net"
23+
"strconv"
24+
"strings"
2425
"time"
2526

27+
"github.com/linode/linodego"
2628
v1 "k8s.io/api/core/v1"
2729
apierrors "k8s.io/apimachinery/pkg/api/errors"
2830
"k8s.io/apimachinery/pkg/types"
@@ -43,7 +45,6 @@ import (
4345
netutils "k8s.io/utils/net"
4446

4547
linode "github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
46-
"github.com/linode/linodego"
4748
)
4849

4950
type cloudAllocator struct {
@@ -66,6 +67,8 @@ type cloudAllocator struct {
6667
queue workqueue.TypedRateLimitingInterface[any]
6768
}
6869

70+
const providerIDPrefix = "linode://"
71+
6972
var (
7073
_ CIDRAllocator = &cloudAllocator{}
7174
nodeCIDRMaskSizeIPv6 = 112
@@ -326,21 +329,21 @@ func (c *cloudAllocator) occupyCIDRs(ctx context.Context, node *v1.Node) error {
326329
// It then creates a new net.IPNet with the IPv6 address and mask size defined
327330
// by nodeCIDRMaskSizeIPv6. The function returns an error if it fails to retrieve
328331
// the instance configuration or parse the IPv6 range.
329-
func (c *cloudAllocator) allocateIPv6CIDR(node *v1.Node) (*net.IPNet, error) {
330-
filter := map[string]string{"label": fmt.Sprintf("%s", node.Name)}
331-
rawFilter, err := json.Marshal(filter)
332-
if err != nil {
333-
return nil, fmt.Errorf("json marshal should not have failed")
332+
func (c *cloudAllocator) allocateIPv6CIDR(ctx context.Context, node *v1.Node) (*net.IPNet, error) {
333+
if node.Spec.ProviderID == "" {
334+
return nil, fmt.Errorf("node %s has no ProviderID set, cannot calculate ipv6 range for it", node.Name)
335+
}
336+
// Extract the Linode ID from the ProviderID
337+
if !strings.HasPrefix(node.Spec.ProviderID, providerIDPrefix) {
338+
return nil, fmt.Errorf("node %s has invalid ProviderID %s, expected prefix '%s'", node.Name, node.Spec.ProviderID, providerIDPrefix)
334339
}
335-
linodes, err := c.linodeClient.ListInstances(context.TODO(), linodego.NewListOptions(1, string(rawFilter)))
340+
// Parse the Linode ID from the ProviderID
341+
id, err := strconv.Atoi(strings.TrimPrefix(node.Spec.ProviderID, providerIDPrefix))
336342
if err != nil {
337-
return nil, fmt.Errorf("failed to list linode instances: %w", err)
338-
} else if len(linodes) == 0 {
339-
return nil, fmt.Errorf("no linode instances found for node %s", node.Name)
340-
} else if len(linodes) > 1 {
341-
return nil, fmt.Errorf("multiple linode instances found for node %s, expected only one", node.Name)
343+
return nil, fmt.Errorf("failed to parse Linode ID from ProviderID %s: %w", node.Spec.ProviderID, err)
342344
}
343-
configs, err := c.linodeClient.ListInstanceConfigs(context.TODO(), linodes[0].ID, &linodego.ListOptions{})
345+
// Retrieve the instance configuration for the Linode ID
346+
configs, err := c.linodeClient.ListInstanceConfigs(ctx, id, &linodego.ListOptions{})
344347
if err != nil || len(configs) == 0 {
345348
return nil, fmt.Errorf("failed to list instance configs: %w", err)
346349
}
@@ -393,7 +396,7 @@ func (c *cloudAllocator) AllocateOrOccupyCIDR(ctx context.Context, node *v1.Node
393396
return fmt.Errorf("failed to allocate cidr from cluster cidr: %w", err)
394397
}
395398
allocatedCIDRs[0] = podCIDR
396-
if allocatedCIDRs[1], err = c.allocateIPv6CIDR(node); err != nil {
399+
if allocatedCIDRs[1], err = c.allocateIPv6CIDR(ctx, node); err != nil {
397400
return fmt.Errorf("failed to assign IPv6 CIDR: %w", err)
398401
}
399402

cloud/nodeipam/ipam/cloud_allocator_test.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package ipam
1818

1919
import (
20+
"fmt"
2021
"net"
2122
"testing"
2223
"time"
@@ -63,6 +64,9 @@ func TestOccupyPreExistingCIDR(t *testing.T) {
6364
ObjectMeta: metav1.ObjectMeta{
6465
Name: "node0",
6566
},
67+
Spec: v1.NodeSpec{
68+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
69+
},
6670
},
6771
},
6872
Clientset: fake.NewSimpleClientset(),
@@ -90,7 +94,8 @@ func TestOccupyPreExistingCIDR(t *testing.T) {
9094
Name: "node0",
9195
},
9296
Spec: v1.NodeSpec{
93-
PodCIDRs: []string{"10.10.1.0/24"},
97+
PodCIDRs: []string{"10.10.1.0/24"},
98+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
9499
},
95100
},
96101
},
@@ -120,7 +125,8 @@ func TestOccupyPreExistingCIDR(t *testing.T) {
120125
Name: "node0",
121126
},
122127
Spec: v1.NodeSpec{
123-
PodCIDRs: []string{"172.10.1.0/24"},
128+
PodCIDRs: []string{"172.10.1.0/24"},
129+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
124130
},
125131
},
126132
},
@@ -181,6 +187,9 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
181187
ObjectMeta: metav1.ObjectMeta{
182188
Name: "node0",
183189
},
190+
Spec: v1.NodeSpec{
191+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
192+
},
184193
Status: v1.NodeStatus{
185194
Addresses: []v1.NodeAddress{
186195
{
@@ -216,6 +225,9 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
216225
ObjectMeta: metav1.ObjectMeta{
217226
Name: "node0",
218227
},
228+
Spec: v1.NodeSpec{
229+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
230+
},
219231
Status: v1.NodeStatus{
220232
Addresses: []v1.NodeAddress{
221233
{
@@ -255,6 +267,9 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
255267
ObjectMeta: metav1.ObjectMeta{
256268
Name: "node0",
257269
},
270+
Spec: v1.NodeSpec{
271+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
272+
},
258273
Status: v1.NodeStatus{
259274
Addresses: []v1.NodeAddress{
260275
{
@@ -297,7 +312,8 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
297312
Name: "node0",
298313
},
299314
Spec: v1.NodeSpec{
300-
PodCIDRs: []string{"10.10.0.0/24"},
315+
PodCIDRs: []string{"10.10.0.0/24"},
316+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
301317
},
302318
Status: v1.NodeStatus{
303319
Addresses: []v1.NodeAddress{
@@ -313,7 +329,8 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
313329
Name: "node1",
314330
},
315331
Spec: v1.NodeSpec{
316-
PodCIDRs: []string{"10.10.2.0/24"},
332+
PodCIDRs: []string{"10.10.2.0/24"},
333+
ProviderID: fmt.Sprintf("%s22345", providerIDPrefix),
317334
},
318335
Status: v1.NodeStatus{
319336
Addresses: []v1.NodeAddress{
@@ -328,6 +345,9 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
328345
ObjectMeta: metav1.ObjectMeta{
329346
Name: "node2",
330347
},
348+
Spec: v1.NodeSpec{
349+
ProviderID: fmt.Sprintf("%s32345", providerIDPrefix),
350+
},
331351
Status: v1.NodeStatus{
332352
Addresses: []v1.NodeAddress{
333353
{
@@ -361,12 +381,6 @@ func TestAllocateOrOccupyCIDRSuccess(t *testing.T) {
361381
testFunc := func(tc testCase) {
362382
fakeNodeInformer := test.FakeNodeInformer(tc.fakeNodeHandler)
363383
nodeList, _ := tc.fakeNodeHandler.List(tCtx, metav1.ListOptions{})
364-
// Initialize the range allocator.
365-
tc.linodeClient.EXPECT().ListInstances(gomock.Any(), gomock.Any()).AnyTimes().Return([]linodego.Instance{
366-
{
367-
ID: 12345,
368-
},
369-
}, nil)
370384
tc.linodeClient.EXPECT().ListInstanceConfigs(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return([]linodego.InstanceConfig{
371385
{
372386
Interfaces: []linodego.InstanceConfigInterface{
@@ -463,6 +477,9 @@ func TestAllocateOrOccupyCIDRFailure(t *testing.T) {
463477
ObjectMeta: metav1.ObjectMeta{
464478
Name: "node0",
465479
},
480+
Spec: v1.NodeSpec{
481+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
482+
},
466483
},
467484
},
468485
Clientset: fake.NewSimpleClientset(),
@@ -572,6 +589,9 @@ func TestReleaseCIDRSuccess(t *testing.T) {
572589
ObjectMeta: metav1.ObjectMeta{
573590
Name: "node0",
574591
},
592+
Spec: v1.NodeSpec{
593+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
594+
},
575595
Status: v1.NodeStatus{
576596
Addresses: []v1.NodeAddress{
577597
{
@@ -613,6 +633,9 @@ func TestReleaseCIDRSuccess(t *testing.T) {
613633
ObjectMeta: metav1.ObjectMeta{
614634
Name: "node0",
615635
},
636+
Spec: v1.NodeSpec{
637+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
638+
},
616639
Status: v1.NodeStatus{
617640
Addresses: []v1.NodeAddress{
618641
{
@@ -650,12 +673,6 @@ func TestReleaseCIDRSuccess(t *testing.T) {
650673
}
651674
logger, tCtx := ktesting.NewTestContext(t)
652675
testFunc := func(tc releaseTestCase) {
653-
// Initialize the range allocator.
654-
tc.linodeClient.EXPECT().ListInstances(gomock.Any(), gomock.Any()).AnyTimes().Return([]linodego.Instance{
655-
{
656-
ID: 12345,
657-
},
658-
}, nil)
659676
tc.linodeClient.EXPECT().ListInstanceConfigs(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return([]linodego.InstanceConfig{
660677
{
661678
Interfaces: []linodego.InstanceConfigInterface{
@@ -782,8 +799,9 @@ func TestNodeDeletionReleaseCIDR(t *testing.T) {
782799
Name: "node0",
783800
},
784801
Spec: v1.NodeSpec{
785-
PodCIDR: allocatedCIDR.String(),
786-
PodCIDRs: []string{allocatedCIDR.String()},
802+
PodCIDR: allocatedCIDR.String(),
803+
PodCIDRs: []string{allocatedCIDR.String()},
804+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
787805
},
788806
},
789807
},
@@ -799,8 +817,9 @@ func TestNodeDeletionReleaseCIDR(t *testing.T) {
799817
DeletionTimestamp: &metav1.Time{Time: time.Now()},
800818
},
801819
Spec: v1.NodeSpec{
802-
PodCIDR: allocatedCIDR.String(),
803-
PodCIDRs: []string{allocatedCIDR.String()},
820+
PodCIDR: allocatedCIDR.String(),
821+
PodCIDRs: []string{allocatedCIDR.String()},
822+
ProviderID: fmt.Sprintf("%s12345", providerIDPrefix),
804823
},
805824
},
806825
},

0 commit comments

Comments
 (0)