Skip to content

Commit 81fd414

Browse files
committed
MachinePool: support aliasIPRanges
1 parent 1dd2dd2 commit 81fd414

File tree

4 files changed

+16
-69
lines changed

4 files changed

+16
-69
lines changed

cloud/scope/machine.go

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

328328
// InstanceNetworkInterfaceSpec returns compute network interface spec.
329-
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string) *compute.NetworkInterface {
329+
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string, aliasIPRanges []infrav1.AliasIPRange) *compute.NetworkInterface {
330330
networkInterface := &compute.NetworkInterface{
331331
Network: path.Join("projects", cluster.NetworkProject(), "global", "networks", cluster.NetworkName()),
332332
}
@@ -344,18 +344,18 @@ func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, s
344344
networkInterface.Subnetwork = path.Join("projects", cluster.NetworkProject(), "regions", cluster.Region(), "subnetworks", *subnet)
345345
}
346346

347-
networkInterface.AliasIpRanges = m.InstanceNetworkInterfaceAliasIPRangesSpec()
347+
networkInterface.AliasIpRanges = InstanceNetworkInterfaceAliasIPRangesSpec(aliasIPRanges)
348348

349349
return networkInterface
350350
}
351351

352352
// InstanceNetworkInterfaceAliasIPRangesSpec returns a slice of Alias IP Range specs.
353-
func (m *MachineScope) InstanceNetworkInterfaceAliasIPRangesSpec() []*compute.AliasIpRange {
354-
if len(m.GCPMachine.Spec.AliasIPRanges) == 0 {
353+
func InstanceNetworkInterfaceAliasIPRangesSpec(spec []infrav1.AliasIPRange) []*compute.AliasIpRange {
354+
if len(spec) == 0 {
355355
return nil
356356
}
357-
aliasIPRanges := make([]*compute.AliasIpRange, 0, len(m.GCPMachine.Spec.AliasIPRanges))
358-
for _, alias := range m.GCPMachine.Spec.AliasIPRanges {
357+
aliasIPRanges := make([]*compute.AliasIpRange, 0, len(spec))
358+
for _, alias := range spec {
359359
aliasIPRange := &compute.AliasIpRange{
360360
IpCidrRange: alias.IPCidrRange,
361361
SubnetworkRangeName: alias.SubnetworkRangeName,
@@ -505,7 +505,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
505505

506506
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachine.Spec.AdditionalMetadata)
507507
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
508-
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet))
508+
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet, m.GCPMachine.Spec.AliasIPRanges))
509509
instance.GuestAccelerators = instanceGuestAcceleratorsSpec(m.GCPMachine.Spec.GuestAccelerators)
510510
if len(instance.GuestAccelerators) > 0 {
511511
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
@@ -342,7 +342,8 @@ func (m *MachinePoolScope) InstanceTemplateResource(ctx context.Context) (*compu
342342
instance.Disks = append(instance.Disks, m.InstanceAdditionalDiskSpec()...)
343343
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachinePool.Spec.AdditionalMetadata)
344344
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachinePool.Spec.ServiceAccount))
345-
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachinePool.Spec.PublicIP, m.GCPMachinePool.Spec.Subnet))
345+
var aliasIPRanges []infrav1.AliasIPRange // Not supported by MachinePool
346+
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachinePool.Spec.PublicIP, m.GCPMachinePool.Spec.Subnet, aliasIPRanges))
346347
instance.GuestAccelerators = instanceGuestAcceleratorsSpec(m.GCPMachinePool.Spec.GuestAccelerators)
347348
if len(instance.GuestAccelerators) > 0 {
348349
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate

exp/api/v1beta1/gcpmachinepool_types.go

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

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

5253
// ImageFamily is the full reference to a valid image family to be used for this machine.
5354
// +optional

0 commit comments

Comments
 (0)