Skip to content

Commit 3d4b6c1

Browse files
committed
Change dhcp to pointer
1 parent 5f6a4e2 commit 3d4b6c1

File tree

8 files changed

+27
-19
lines changed

8 files changed

+27
-19
lines changed

api/v1alpha1/helpers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1alpha1
1919
import (
2020
corev1 "k8s.io/api/core/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/utils/ptr"
2223
)
2324

2425
// SetInClusterIPPoolRef will set the reference to the provided InClusterIPPool.
@@ -155,11 +156,11 @@ func (c *ProxmoxCluster) addNodeLocation(loc NodeLocation, isControlPlane bool)
155156
// DHCPEnabled returns whether DHCP is enabled.
156157
func (c ClusterNetworkConfig) DHCPEnabled() bool {
157158
switch {
158-
case (c.IPv6Config != nil && c.IPv6Config.DHCP) && (c.IPv4Config != nil && c.IPv4Config.DHCP):
159+
case (c.IPv6Config != nil && ptr.Deref(c.IPv6Config.DHCP, false)) && (c.IPv4Config != nil && ptr.Deref(c.IPv4Config.DHCP, false)):
159160
return true
160-
case (c.IPv6Config != nil && c.IPv6Config.DHCP) && c.IPv4Config == nil:
161+
case (c.IPv6Config != nil && ptr.Deref(c.IPv6Config.DHCP, false)) && c.IPv4Config == nil:
161162
return true
162-
case (c.IPv4Config != nil && c.IPv4Config.DHCP) && c.IPv6Config == nil:
163+
case (c.IPv4Config != nil && ptr.Deref(c.IPv4Config.DHCP, false)) && c.IPv6Config == nil:
163164
return true
164165
default:
165166
return false

api/v1alpha1/proxmoxcluster_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type IPConfig struct {
9191
// mutually exclusive with Addresses.
9292
// +optional
9393
// +kubebuilder:default=false
94-
DHCP bool `json:"dhcp,omitempty"`
94+
DHCP *bool `json:"dhcp,omitempty"`
9595
}
9696

9797
// SchedulerHints allows to pass the scheduler instructions to (dis)allow over- or enforce underprovisioning of resources.

api/v1alpha1/proxmoxcluster_types_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/stretchr/testify/require"
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/utils/ptr"
2829
ipamicv1 "sigs.k8s.io/cluster-api-ipam-provider-in-cluster/api/v1alpha2"
2930
"sigs.k8s.io/controller-runtime/pkg/client"
3031
)
@@ -114,7 +115,7 @@ var _ = Describe("ProxmoxCluster Test", func() {
114115

115116
It("Should allow DHCP for IPv4 config", func() {
116117
dc := defaultCluster()
117-
dc.Spec.ClusterNetworkConfig.IPv4Config.DHCP = true
118+
dc.Spec.ClusterNetworkConfig.IPv4Config.DHCP = ptr.To(true)
118119

119120
Expect(k8sClient.Create(context.Background(), dc)).To(Succeed())
120121
})
@@ -146,7 +147,7 @@ var _ = Describe("ProxmoxCluster Test", func() {
146147
It("Should allow DHCP for IPV6 config", func() {
147148
dc := defaultCluster()
148149
dc.Spec.IPv6Config = &IPConfig{
149-
DHCP: true,
150+
DHCP: ptr.To(true),
150151
}
151152
Expect(k8sClient.Create(context.Background(), dc)).Should(Succeed())
152153
})
@@ -217,18 +218,18 @@ func TestClusterNetworkConfig_DHCPEnabled(t *testing.T) {
217218
cl := defaultCluster()
218219
require.False(t, cl.Spec.ClusterNetworkConfig.DHCPEnabled())
219220

220-
cl.Spec.ClusterNetworkConfig.IPv4Config.DHCP = true
221+
cl.Spec.ClusterNetworkConfig.IPv4Config.DHCP = ptr.To(true)
221222
require.True(t, cl.Spec.ClusterNetworkConfig.DHCPEnabled())
222223

223-
cl.Spec.ClusterNetworkConfig.IPv4Config.DHCP = true
224-
cl.Spec.ClusterNetworkConfig.IPv6Config = &IPConfig{DHCP: true}
224+
cl.Spec.ClusterNetworkConfig.IPv4Config.DHCP = ptr.To(true)
225+
cl.Spec.ClusterNetworkConfig.IPv6Config = &IPConfig{DHCP: ptr.To(true)}
225226
require.True(t, cl.Spec.ClusterNetworkConfig.DHCPEnabled())
226227

227228
cl.Spec.ClusterNetworkConfig.IPv4Config = nil
228-
cl.Spec.ClusterNetworkConfig.IPv6Config = &IPConfig{DHCP: true}
229+
cl.Spec.ClusterNetworkConfig.IPv6Config = &IPConfig{DHCP: ptr.To(true)}
229230
require.True(t, cl.Spec.ClusterNetworkConfig.DHCPEnabled())
230231

231-
cl.Spec.ClusterNetworkConfig.IPv4Config = &IPConfig{DHCP: true}
232+
cl.Spec.ClusterNetworkConfig.IPv4Config = &IPConfig{DHCP: ptr.To(true)}
232233
cl.Spec.ClusterNetworkConfig.IPv6Config = nil
233234
require.True(t, cl.Spec.ClusterNetworkConfig.DHCPEnabled())
234235
}

api/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.

internal/controller/proxmoxcluster_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/client-go/tools/record"
2727
"k8s.io/klog/v2"
28+
"k8s.io/utils/ptr"
2829
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2930
"sigs.k8s.io/cluster-api/util"
3031
"sigs.k8s.io/cluster-api/util/annotations"
@@ -194,7 +195,7 @@ func (r *ProxmoxClusterReconciler) reconcileIPAM(ctx context.Context, clusterSco
194195
return ctrl.Result{}, err
195196
}
196197

197-
if clusterScope.ProxmoxCluster.Spec.IPv4Config != nil && !clusterScope.ProxmoxCluster.Spec.IPv4Config.DHCP {
198+
if clusterScope.ProxmoxCluster.Spec.IPv4Config != nil && !ptr.Deref(clusterScope.ProxmoxCluster.Spec.IPv4Config.DHCP, false) {
198199
poolV4, err := clusterScope.IPAMHelper.GetDefaultInClusterIPPool(ctx, infrav1alpha1.IPV4Format)
199200
if err != nil {
200201
if apierrors.IsNotFound(err) {
@@ -205,7 +206,7 @@ func (r *ProxmoxClusterReconciler) reconcileIPAM(ctx context.Context, clusterSco
205206
}
206207
clusterScope.ProxmoxCluster.SetInClusterIPPoolRef(poolV4)
207208
}
208-
if clusterScope.ProxmoxCluster.Spec.IPv6Config != nil && !clusterScope.ProxmoxCluster.Spec.IPv6Config.DHCP {
209+
if clusterScope.ProxmoxCluster.Spec.IPv6Config != nil && !ptr.Deref(clusterScope.ProxmoxCluster.Spec.IPv6Config.DHCP, false) {
209210
poolV6, err := clusterScope.IPAMHelper.GetDefaultInClusterIPPool(ctx, infrav1alpha1.IPV6Format)
210211
if err != nil {
211212
if apierrors.IsNotFound(err) {

internal/service/vmservice/ip.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func handleDefaultDevice(ctx context.Context, machineScope *scope.MachineScope,
170170
return false, nil
171171
}
172172

173-
//nolint
173+
// nolint
174174
func handleAdditionalDevices(ctx context.Context, machineScope *scope.MachineScope, addresses map[string]infrav1.IPAddress) (bool, error) {
175175
// additional network devices.
176176
for _, net := range machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices {
@@ -227,9 +227,9 @@ func hasDHCPEnabled(config *infrav1.IPConfig, device *infrav1.NetworkDevice, dev
227227
switch {
228228
case deviceName == infrav1.DefaultNetworkDevice:
229229
if format == infrav1.IPV4Format {
230-
return (config != nil && config.DHCP) || (device != nil && device.DHCP4)
230+
return (config != nil && ptr.Deref(config.DHCP, false)) || (device != nil && device.DHCP4)
231231
} else if format == infrav1.IPV6Format {
232-
return (config != nil && config.DHCP) || (device != nil && device.DHCP6)
232+
return (config != nil && ptr.Deref(config.DHCP, false)) || (device != nil && device.DHCP6)
233233
}
234234
default:
235235
// additionalDevices don't rely on the cluster network configuration.

internal/service/vmservice/vm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func reconcileMachineAddresses(ctx context.Context, scope *scope.MachineScope) e
243243
}
244244

245245
// getMachineAddresses returns the ip addresses for the machine
246-
//nolint
246+
// nolint
247247
func getMachineAddresses(ctx context.Context, scope *scope.MachineScope) ([]clusterv1.MachineAddress, error) {
248248
if !machineHasIPAddress(scope.ProxmoxMachine) {
249249
return nil, errors.New("machine does not yet have an ip address")

pkg/kubernetes/ipam/ipam.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var ErrMissingAddresses = errors.New("no valid ip addresses defined for the ip p
6767
// by Proxmox in order to avoid conflicts.
6868
func (h *Helper) CreateOrUpdateInClusterIPPool(ctx context.Context) error {
6969
// ipv4
70-
if h.cluster.Spec.IPv4Config != nil && !h.cluster.Spec.IPv4Config.DHCP {
70+
if h.cluster.Spec.IPv4Config != nil && !ptr.Deref(h.cluster.Spec.IPv4Config.DHCP, false) {
7171
ipv4Config := h.cluster.Spec.IPv4Config
7272

7373
v4Pool := &ipamicv1.InClusterIPPool{
@@ -94,7 +94,7 @@ func (h *Helper) CreateOrUpdateInClusterIPPool(ctx context.Context) error {
9494
}
9595

9696
// ipv6
97-
if h.cluster.Spec.IPv6Config != nil && !h.cluster.Spec.IPv6Config.DHCP {
97+
if h.cluster.Spec.IPv6Config != nil && !ptr.Deref(h.cluster.Spec.IPv6Config.DHCP, false) {
9898
v6Pool := &ipamicv1.InClusterIPPool{
9999
ObjectMeta: metav1.ObjectMeta{
100100
Name: InClusterPoolFormat(h.cluster, infrav1.IPV6Format),

0 commit comments

Comments
 (0)