Skip to content

Commit

Permalink
✨ allow setting dhcp for additional networks (#203)
Browse files Browse the repository at this point in the history
**What is the purpose of this pull request/Why do we need it?**

Some people might want to have a custom DHCP server running and
therefore would like to disable DHCP for additional networks.

**Description of changes:**

Additional networks can now disable DHCP. The default value is `true`

**Checklist:**
- [x] Unit Tests added
- [x] Includes
[emojis](https://github.com/kubernetes-sigs/kubebuilder-release-tools?tab=readme-ov-file#kubebuilder-project-versioning)
  • Loading branch information
lubedacht authored Aug 8, 2024
1 parent 7b133fc commit d9080d1
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
7 changes: 6 additions & 1 deletion api/v1alpha1/ionoscloudmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ type IonosCloudMachineSpec struct {
Disk *Volume `json:"disk"`

// AdditionalNetworks defines the additional network configurations for the VM.
// NOTE(lubedacht): We currently only support networks with DHCP enabled.
//+optional
AdditionalNetworks Networks `json:"additionalNetworks,omitempty"`

Expand Down Expand Up @@ -183,6 +182,12 @@ type Network struct {
// This LAN will be excluded from the deletion process.
//+kubebuilder:validation:Minimum=1
NetworkID int32 `json:"networkID"`

// DHCP indicates whether DHCP is enabled for the LAN.
// The primary NIC will always have DHCP enabled.
//+kubebuilder:default=true
//+optional
DHCP *bool `json:"dhcp,omitempty"`
}

// Volume is the physical storage on the VM.
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/ionoscloudmachine_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ var _ = Describe("IonosCloudMachine Tests", func() {
m.Spec.AdditionalNetworks[0].NetworkID = -1
Expect(k8sClient.Create(context.Background(), m)).ToNot(Succeed())
})
It("DHCP should default to true", func() {
m := defaultMachine()
Expect(k8sClient.Create(context.Background(), m)).To(Succeed())
Expect(*m.Spec.AdditionalNetworks[0].DHCP).To(BeTrue())
})
})
})
Context("FailoverIP", func() {
Expand Down
13 changes: 11 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ spec:
description: IonosCloudMachineSpec defines the desired state of IonosCloudMachine.
properties:
additionalNetworks:
description: |-
AdditionalNetworks defines the additional network configurations for the VM.
NOTE(lubedacht): We currently only support networks with DHCP enabled.
description: AdditionalNetworks defines the additional network configurations
for the VM.
items:
description: Network contains the config for additional LANs.
properties:
dhcp:
default: true
description: |-
DHCP indicates whether DHCP is enabled for the LAN.
The primary NIC will always have DHCP enabled.
type: boolean
networkID:
description: |-
NetworkID represents an ID an existing LAN in the data center.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ spec:
description: Spec is the IonosCloudMachineSpec for the IonosCloudMachineTemplate.
properties:
additionalNetworks:
description: |-
AdditionalNetworks defines the additional network configurations for the VM.
NOTE(lubedacht): We currently only support networks with DHCP enabled.
description: AdditionalNetworks defines the additional network
configurations for the VM.
items:
description: Network contains the config for additional
LANs.
properties:
dhcp:
default: true
description: |-
DHCP indicates whether DHCP is enabled for the LAN.
The primary NIC will always have DHCP enabled.
type: boolean
networkID:
description: |-
NetworkID represents an ID an existing LAN in the data center.
Expand Down
3 changes: 2 additions & 1 deletion internal/service/cloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ func (s *Service) buildServerEntities(ms *scope.Machine, params serverEntityPara

for _, nic := range ms.IonosMachine.Spec.AdditionalNetworks {
items = append(items, sdk.Nic{Properties: &sdk.NicProperties{
Lan: &nic.NetworkID,
Lan: &nic.NetworkID,
Dhcp: nic.DHCP,
}})
}

Expand Down
39 changes: 39 additions & 0 deletions internal/service/cloud/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,45 @@ func (s *serverSuite) TestReconcileServerRequestDoneStateAvailableTurnedOff() {
s.True(requeue)
}

func (s *serverSuite) TestReconcileServerAdditionalNetworks() {
s.machineScope.IonosMachine.Spec.AdditionalNetworks = []infrav1.Network{{
NetworkID: 43,
DHCP: ptr.To(false),
}, {
NetworkID: 44,
DHCP: ptr.To(true),
}}

s.prepareReconcileServerRequestTest()
s.mockGetServerCreationRequestCall().Return([]sdk.Request{}, nil)
s.mockListLANsCall().Return(&sdk.Lans{Items: &[]sdk.Lan{{
Id: ptr.To(exampleLANID),
Properties: &sdk.LanProperties{
Name: ptr.To(s.service.lanName(s.clusterScope.Cluster)),
Public: ptr.To(true),
},
}}}, nil)

properties, entities := s.defaultServerComponents()
*entities.Nics.Items = append(*entities.Nics.Items, sdk.Nic{
Properties: &sdk.NicProperties{
Lan: ptr.To[int32](43),
Dhcp: ptr.To(false),
},
}, sdk.Nic{
Properties: &sdk.NicProperties{
Lan: ptr.To[int32](44),
Dhcp: ptr.To(true),
},
})
s.mockCreateServerCall(properties, entities).Return(&sdk.Server{Id: ptr.To("12345")}, "location/to/server", nil)

requeue, err := s.service.ReconcileServer(s.ctx, s.machineScope)
s.Equal("ionos://12345", ptr.Deref(s.machineScope.IonosMachine.Spec.ProviderID, ""))
s.NoError(err)
s.True(requeue)
}

func (s *serverSuite) TestReconcileEnterpriseServerNoRequest() {
s.prepareReconcileServerRequestTest()
s.mockGetServerCreationRequestCall().Return([]sdk.Request{}, nil)
Expand Down

0 comments on commit d9080d1

Please sign in to comment.