Skip to content

Commit c122d09

Browse files
committed
MachinePool: support aliasIPRanges
1 parent ebaf02a commit c122d09

File tree

4 files changed

+19
-67
lines changed

4 files changed

+19
-67
lines changed

cloud/scope/machine.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDisk
333333
}
334334

335335
// InstanceNetworkInterfaceSpec returns compute network interface spec.
336-
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string) *compute.NetworkInterface {
336+
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string, aliasIPRanges []infrav1.AliasIPRange) *compute.NetworkInterface {
337337
networkInterface := &compute.NetworkInterface{
338338
Network: path.Join("projects", cluster.NetworkProject(), "global", "networks", cluster.NetworkName()),
339339
}
@@ -351,18 +351,18 @@ func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, s
351351
networkInterface.Subnetwork = path.Join("projects", cluster.NetworkProject(), "regions", cluster.Region(), "subnetworks", *subnet)
352352
}
353353

354-
networkInterface.AliasIpRanges = m.InstanceNetworkInterfaceAliasIPRangesSpec()
354+
networkInterface.AliasIpRanges = InstanceNetworkInterfaceAliasIPRangesSpec(aliasIPRanges)
355355

356356
return networkInterface
357357
}
358358

359359
// InstanceNetworkInterfaceAliasIPRangesSpec returns a slice of Alias IP Range specs.
360-
func (m *MachineScope) InstanceNetworkInterfaceAliasIPRangesSpec() []*compute.AliasIpRange {
361-
if len(m.GCPMachine.Spec.AliasIPRanges) == 0 {
360+
func InstanceNetworkInterfaceAliasIPRangesSpec(spec []infrav1.AliasIPRange) []*compute.AliasIpRange {
361+
if len(spec) == 0 {
362362
return nil
363363
}
364-
aliasIPRanges := make([]*compute.AliasIpRange, 0, len(m.GCPMachine.Spec.AliasIPRanges))
365-
for _, alias := range m.GCPMachine.Spec.AliasIPRanges {
364+
aliasIPRanges := make([]*compute.AliasIpRange, 0, len(spec))
365+
for _, alias := range spec {
366366
aliasIPRange := &compute.AliasIpRange{
367367
IpCidrRange: alias.IPCidrRange,
368368
SubnetworkRangeName: alias.SubnetworkRangeName,
@@ -512,7 +512,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
512512

513513
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachine.Spec.AdditionalMetadata)
514514
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
515-
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet))
515+
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet, m.GCPMachine.Spec.AliasIPRanges))
516516
instance.GuestAccelerators = instanceGuestAcceleratorsSpec(m.GCPMachine.Spec.GuestAccelerators)
517517
if len(instance.GuestAccelerators) > 0 {
518518
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate

cloud/scope/machine_test.go

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,14 @@ func TestMachineLocalSSDDiskType(t *testing.T) {
7878

7979
// TestInstanceNetworkInterfaceAliasIPRangesSpec tests the InstanceNetworkInterfaceAliasIPRangesSpec function
8080
func TestInstanceNetworkInterfaceAliasIPRangesSpec(t *testing.T) {
81-
// Register the GCPMachine and GCPMachineList in a schema.
82-
schema, err := infrav1.SchemeBuilder.Register(&infrav1.GCPMachine{}, &infrav1.GCPMachineList{}).Build()
83-
assert.Nil(t, err)
84-
85-
// Create a controller fake client.
86-
testClient := fake.NewClientBuilder().WithScheme(schema).Build()
87-
88-
// Test machine parameter
89-
failureDomain := "us-central1-a"
90-
testMachine := clusterv1.Machine{
91-
Spec: clusterv1.MachineSpec{
92-
FailureDomain: &failureDomain,
93-
},
94-
}
95-
9681
t.Run("should return nil for empty alias IP ranges", func(t *testing.T) {
9782
testGCPMachine := infrav1.GCPMachine{
9883
Spec: infrav1.GCPMachineSpec{
9984
AliasIPRanges: []infrav1.AliasIPRange{},
10085
},
10186
}
10287

103-
testScopeParams := MachineScopeParams{
104-
Client: testClient,
105-
Machine: &testMachine,
106-
GCPMachine: &testGCPMachine,
107-
}
108-
109-
testMachineScope, err := NewMachineScope(testScopeParams)
110-
assert.Nil(t, err)
111-
assert.NotNil(t, testMachineScope)
112-
113-
result := testMachineScope.InstanceNetworkInterfaceAliasIPRangesSpec()
88+
result := InstanceNetworkInterfaceAliasIPRangesSpec(testGCPMachine.Spec.AliasIPRanges)
11489
assert.Nil(t, result)
11590
})
11691

@@ -126,17 +101,7 @@ func TestInstanceNetworkInterfaceAliasIPRangesSpec(t *testing.T) {
126101
},
127102
}
128103

129-
testScopeParams := MachineScopeParams{
130-
Client: testClient,
131-
Machine: &testMachine,
132-
GCPMachine: &testGCPMachine,
133-
}
134-
135-
testMachineScope, err := NewMachineScope(testScopeParams)
136-
assert.Nil(t, err)
137-
assert.NotNil(t, testMachineScope)
138-
139-
result := testMachineScope.InstanceNetworkInterfaceAliasIPRangesSpec()
104+
result := InstanceNetworkInterfaceAliasIPRangesSpec(testGCPMachine.Spec.AliasIPRanges)
140105
assert.NotNil(t, result)
141106
assert.Len(t, result, 1)
142107
assert.Equal(t, "10.0.0.0/24", result[0].IpCidrRange)
@@ -159,17 +124,7 @@ func TestInstanceNetworkInterfaceAliasIPRangesSpec(t *testing.T) {
159124
},
160125
}
161126

162-
testScopeParams := MachineScopeParams{
163-
Client: testClient,
164-
Machine: &testMachine,
165-
GCPMachine: &testGCPMachine,
166-
}
167-
168-
testMachineScope, err := NewMachineScope(testScopeParams)
169-
assert.Nil(t, err)
170-
assert.NotNil(t, testMachineScope)
171-
172-
result := testMachineScope.InstanceNetworkInterfaceAliasIPRangesSpec()
127+
result := InstanceNetworkInterfaceAliasIPRangesSpec(testGCPMachine.Spec.AliasIPRanges)
173128
assert.NotNil(t, result)
174129
assert.Len(t, result, 2)
175130
assert.Equal(t, "10.0.0.0/24", result[0].IpCidrRange)
@@ -190,17 +145,7 @@ func TestInstanceNetworkInterfaceAliasIPRangesSpec(t *testing.T) {
190145
},
191146
}
192147

193-
testScopeParams := MachineScopeParams{
194-
Client: testClient,
195-
Machine: &testMachine,
196-
GCPMachine: &testGCPMachine,
197-
}
198-
199-
testMachineScope, err := NewMachineScope(testScopeParams)
200-
assert.Nil(t, err)
201-
assert.NotNil(t, testMachineScope)
202-
203-
result := testMachineScope.InstanceNetworkInterfaceAliasIPRangesSpec()
148+
result := InstanceNetworkInterfaceAliasIPRangesSpec(testGCPMachine.Spec.AliasIPRanges)
204149
assert.NotNil(t, result)
205150
assert.Len(t, result, 1)
206151
assert.Equal(t, "10.100.0.0/24", result[0].IpCidrRange)

cloud/scope/machinepool.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ func (m *MachinePoolScope) InstanceTemplateResource(ctx context.Context) (*compu
348348
instance.Disks = append(instance.Disks, m.InstanceAdditionalDiskSpec()...)
349349
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachinePool.Spec.AdditionalMetadata)
350350
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachinePool.Spec.ServiceAccount))
351-
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachinePool.Spec.PublicIP, m.GCPMachinePool.Spec.Subnet))
351+
var aliasIPRanges []infrav1.AliasIPRange // Not supported by MachinePool
352+
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachinePool.Spec.PublicIP, m.GCPMachinePool.Spec.Subnet, aliasIPRanges))
352353
instance.GuestAccelerators = instanceGuestAcceleratorsSpec(m.GCPMachinePool.Spec.GuestAccelerators)
353354
if len(instance.GuestAccelerators) > 0 {
354355
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate

exp/api/v1beta1/gcpmachinepool_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type GCPMachinePoolSpec struct {
4545
// +optional
4646
Subnet *string `json:"subnet,omitempty"`
4747

48+
// Not meaningful by MachinePool (?)
49+
// // AliasIPRanges let you assign ranges of internal IP addresses as aliases to a VM's network interfaces.
50+
// // +optional
51+
// AliasIPRanges []infrav1.AliasIPRange `json:"aliasIPRanges,omitempty"`
52+
53+
// Not meaningful for MachinePool
4854
// // ProviderID is the unique identifier as specified by the cloud provider.
4955
// // +optional
5056
// ProviderID *string `json:"providerID,omitempty"`

0 commit comments

Comments
 (0)