Skip to content

Commit f310dc3

Browse files
authored
Add SubnetIDs field to support multiple subnets for machine instances (gardener#347)
* Add SubnetIDs field to support multiple subnets for machine instances * make SubnetIDs optional
1 parent 01a1b3b commit f310dc3

7 files changed

Lines changed: 81 additions & 6 deletions

File tree

hack/api-reference/api.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,20 @@ string
145145
</td>
146146
<td>
147147
<em>(Optional)</em>
148-
<p>SubnetID is the ID of the subnet the instance should belong to. If SubnetID is not specified</p>
148+
<p>SubnetID is the ID of the subnet the instance should belong to.
149+
Deprecated - use <code>SubnetIDs</code> instead.</p>
150+
</td>
151+
</tr>
152+
<tr>
153+
<td>
154+
<code>subnetIDs</code></br>
155+
<em>
156+
[]string
157+
</em>
158+
</td>
159+
<td>
160+
<em>(Optional)</em>
161+
<p>SubnetIDs is a list of IDs of the subnets the instance should belong to.</p>
149162
</td>
150163
</tr>
151164
<tr>
@@ -363,7 +376,20 @@ string
363376
</td>
364377
<td>
365378
<em>(Optional)</em>
366-
<p>SubnetID is the ID of the subnet the instance should belong to. If SubnetID is not specified</p>
379+
<p>SubnetID is the ID of the subnet the instance should belong to.
380+
Deprecated - use <code>SubnetIDs</code> instead.</p>
381+
</td>
382+
</tr>
383+
<tr>
384+
<td>
385+
<code>subnetIDs</code></br>
386+
<em>
387+
[]string
388+
</em>
389+
</td>
390+
<td>
391+
<em>(Optional)</em>
392+
<p>SubnetIDs is a list of IDs of the subnets the instance should belong to.</p>
367393
</td>
368394
</tr>
369395
<tr>

pkg/apis/openstack/types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ type MachineProviderConfigSpec struct {
3737
Tags map[string]string
3838
// NetworkID is the ID of the network the instance should belong to.
3939
NetworkID string
40-
// SubnetID is the ID of the subnet the instance should belong to. If SubnetID is not specified
40+
// SubnetID is the ID of the subnet the instance should belong to.
41+
// Deprecated - use `SubnetIDs` instead.
4142
SubnetID *string
43+
// SubnetIDs is a list of IDs of the subnets the instance should belong to.
44+
SubnetIDs []string
4245
// PodNetworkCidr is the CIDR range for the pods assigned to this instance.
4346
// Deprecated - use `PodNetworkCIDRs` instead.
4447
PodNetworkCidr string

pkg/apis/openstack/v1alpha1/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ type MachineProviderConfigSpec struct {
3939
Tags map[string]string `json:"tags,omitempty"`
4040
// NetworkID is the ID of the network the instance should belong to.
4141
NetworkID string `json:"networkID"`
42-
// SubnetID is the ID of the subnet the instance should belong to. If SubnetID is not specified
42+
// SubnetID is the ID of the subnet the instance should belong to.
43+
// Deprecated - use `SubnetIDs` instead.
4344
// +optional
4445
SubnetID *string `json:"subnetID,omitempty"`
46+
// SubnetIDs is a list of IDs of the subnets the instance should belong to.
47+
// +optional
48+
SubnetIDs []string `json:"subnetIDs,omitempty"`
4549
// PodNetworkCidr is the CIDR range for the pods assigned to this instance.
4650
// Deprecated: use PodNetworkCIDRs instead
4751
// +optional

pkg/apis/openstack/v1alpha1/zz_generated.conversion.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/openstack/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/openstack/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/driver/executor/executor.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ func (ex *Executor) getOrCreatePort(ctx context.Context, machineName string) (st
574574
port, err := ex.Network.CreatePort(ctx, &ports.CreateOpts{
575575
Name: machineName,
576576
NetworkID: ex.Config.Spec.NetworkID,
577-
FixedIPs: []ports.IP{{SubnetID: *ex.Config.Spec.SubnetID}},
577+
FixedIPs: ex.buildFixedIPs(),
578578
SecurityGroups: &securityGroupIDs,
579579
})
580580
if err != nil {
@@ -596,6 +596,32 @@ func (ex *Executor) getOrCreatePort(ctx context.Context, machineName string) (st
596596
return port.ID, nil
597597
}
598598

599+
// buildFixedIPs creates a list of FixedIPs from SubnetID and SubnetIDs, avoiding duplicates
600+
func (ex *Executor) buildFixedIPs() []ports.IP {
601+
// Use a set to track unique subnet IDs and avoid duplicates
602+
subnetIDSet := sets.NewString()
603+
604+
// Add the single SubnetID if specified
605+
if ex.Config.Spec.SubnetID != nil && *ex.Config.Spec.SubnetID != "" {
606+
subnetIDSet.Insert(*ex.Config.Spec.SubnetID)
607+
}
608+
609+
// Add all SubnetIDs if specified
610+
for _, subnetID := range ex.Config.Spec.SubnetIDs {
611+
if subnetID != "" {
612+
subnetIDSet.Insert(subnetID)
613+
}
614+
}
615+
616+
// Convert to []ports.IP
617+
var fixedIPs []ports.IP
618+
for _, subnetID := range subnetIDSet.List() {
619+
fixedIPs = append(fixedIPs, ports.IP{SubnetID: subnetID})
620+
}
621+
622+
return fixedIPs
623+
}
624+
599625
func (ex *Executor) deletePort(ctx context.Context, machineName string) error {
600626
portList, err := ex.Network.ListPorts(ctx, ports.ListOpts{
601627
Name: machineName,
@@ -752,5 +778,9 @@ func (ex *Executor) listServers(ctx context.Context) ([]servers.Server, error) {
752778

753779
// isUserManagedNetwork returns true if the port used by the machine will be created and managed by MCM.
754780
func (ex *Executor) isUserManagedNetwork() bool {
755-
return !isEmptyString(ptr.To(ex.Config.Spec.NetworkID)) && !isEmptyString(ex.Config.Spec.SubnetID)
781+
hasNetworkID := !isEmptyString(ptr.To(ex.Config.Spec.NetworkID))
782+
hasSubnetID := !isEmptyString(ex.Config.Spec.SubnetID)
783+
hasSubnetIDs := len(ex.Config.Spec.SubnetIDs) > 0
784+
785+
return hasNetworkID && (hasSubnetID || hasSubnetIDs)
756786
}

0 commit comments

Comments
 (0)