Skip to content

Commit 99ed8ae

Browse files
committed
update proto to pass guestCofig struct update validation
1 parent 0960cc4 commit 99ed8ae

9 files changed

Lines changed: 208 additions & 101 deletions

File tree

broker/machinebroker/server/machine.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ func (s *Server) convertAggregateIronCoreMachine(aggIronCoreMachine *AggregateIr
251251
return nil, err
252252
}
253253

254-
var hostName string
254+
var guestConfig *iri.GuestConfig
255255
if aggIronCoreMachine.Machine.Spec.GuestConfig != nil {
256-
hostName = aggIronCoreMachine.Machine.Spec.GuestConfig.Hostname
256+
guestConfig = &iri.GuestConfig{
257+
Hostname: aggIronCoreMachine.Machine.Spec.GuestConfig.Hostname,
258+
}
257259
}
258260

259261
return &iri.Machine{
@@ -264,7 +266,7 @@ func (s *Server) convertAggregateIronCoreMachine(aggIronCoreMachine *AggregateIr
264266
IgnitionData: ignitionData,
265267
Volumes: volumes,
266268
NetworkInterfaces: nics,
267-
HostName: hostName,
269+
GuestConfig: guestConfig,
268270
},
269271
Status: &iri.MachineStatus{
270272
ObservedGeneration: aggIronCoreMachine.Machine.Status.ObservedGeneration,

broker/machinebroker/server/machine_create.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ type IronCoreMachineConfig struct {
3131
IgnitionData []byte
3232
NetworkInterfaceConfigs []*IronCoreNetworkInterfaceConfig
3333
VolumeConfigs []*IronCoreVolumeConfig
34-
HostName string
34+
GuestConfig *IroncoreGuestConfig
35+
}
36+
37+
type IroncoreGuestConfig struct {
38+
Hostname string
3539
}
3640

3741
func (s *Server) ironcoreMachinePoolRef() *corev1.LocalObjectReference {
@@ -106,6 +110,12 @@ func (s *Server) getIronCoreMachineConfig(machine *iri.Machine) (*IronCoreMachin
106110
return nil, fmt.Errorf("error preparing ironcore machine annotations: %w", err)
107111
}
108112

113+
var ironcoreGuestConfig *IroncoreGuestConfig
114+
if machine.Spec.GuestConfig != nil {
115+
ironcoreGuestConfig = &IroncoreGuestConfig{
116+
Hostname: machine.Spec.GuestConfig.Hostname,
117+
}
118+
}
109119
return &IronCoreMachineConfig{
110120
Labels: labels,
111121
Annotations: annotations,
@@ -114,7 +124,7 @@ func (s *Server) getIronCoreMachineConfig(machine *iri.Machine) (*IronCoreMachin
114124
IgnitionData: machine.Spec.IgnitionData,
115125
NetworkInterfaceConfigs: ironcoreNicCfgs,
116126
VolumeConfigs: ironcoreVolumeCfgs,
117-
HostName: machine.Spec.HostName,
127+
GuestConfig: ironcoreGuestConfig,
118128
}, nil
119129
}
120130

@@ -129,7 +139,6 @@ func (s *Server) createIronCoreMachine(
129139
var (
130140
ignitionRef *commonv1alpha1.SecretKeySelector
131141
ignitionSecret *corev1.Secret
132-
guestConfig *computev1alpha1.MachineGuestConfig
133142
)
134143
if ignitionData := cfg.IgnitionData; len(ignitionData) > 0 {
135144
log.V(1).Info("Creating ironcore ignition secret")
@@ -181,9 +190,10 @@ func (s *Server) createIronCoreMachine(
181190
}
182191
}
183192

184-
if cfg.HostName != "" {
185-
guestConfig = &computev1alpha1.MachineGuestConfig{
186-
Hostname: cfg.HostName,
193+
var ironcoreGuestConfig *computev1alpha1.MachineGuestConfig
194+
if cfg.GuestConfig != nil {
195+
ironcoreGuestConfig = &computev1alpha1.MachineGuestConfig{
196+
Hostname: cfg.GuestConfig.Hostname,
187197
}
188198
}
189199

@@ -205,7 +215,7 @@ func (s *Server) createIronCoreMachine(
205215
NetworkInterfaces: ironcoreMachineNics,
206216
Volumes: ironcoreMachineVolumes,
207217
IgnitionRef: ignitionRef,
208-
GuestConfig: guestConfig,
218+
GuestConfig: ironcoreGuestConfig,
209219
},
210220
}
211221
log.V(1).Info("Creating ironcore machine")

broker/machinebroker/server/machine_create_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ var _ = Describe("CreateMachine", func() {
4040
},
4141
},
4242
}},
43-
HostName: "foo-machine",
43+
GuestConfig: &iri.GuestConfig{
44+
Hostname: "foo-machine",
45+
},
4446
},
4547
},
4648
})

internal/apis/compute/validation/machine.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ func validateMachineSpecUpdate(new, old *compute.MachineSpec, fldPath *field.Pat
343343
}
344344
}
345345

346+
if old.GuestConfig == nil && new.GuestConfig != nil && new.GuestConfig.Hostname != "" {
347+
allErrs = append(allErrs, field.Invalid(fldPath.Child("guestConfig").Child("hostname"), new.GuestConfig.Hostname, "hostname must not be set if it was not set before"))
348+
}
349+
350+
if old.GuestConfig != nil && old.GuestConfig.Hostname == "" {
351+
if new.GuestConfig != nil && new.GuestConfig.Hostname != "" {
352+
allErrs = append(allErrs, field.Invalid(fldPath.Child("guestConfig").Child("hostname"), new.GuestConfig.Hostname, "hostname must not be set if it was not set before"))
353+
}
354+
}
355+
346356
if old.GuestConfig != nil && old.GuestConfig.Hostname != "" {
347357
if new.GuestConfig == nil || new.GuestConfig.Hostname == "" {
348358
allErrs = append(allErrs, field.Invalid(fldPath.Child("guestConfig").Child("hostname"), nil, "hostname must not be cleared"))

internal/apis/compute/validation/machine_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,34 @@ var _ = Describe("Machine", func() {
574574
},
575575
ContainElement(DuplicateField("spec.volume[1].name")),
576576
),
577+
Entry("set hostname if not set and guestConfig is not set",
578+
&compute.Machine{
579+
Spec: compute.MachineSpec{
580+
GuestConfig: &compute.MachineGuestConfig{
581+
Hostname: "foo-hostname",
582+
},
583+
},
584+
},
585+
&compute.Machine{
586+
Spec: compute.MachineSpec{},
587+
},
588+
ContainElement(InvalidField("spec.guestConfig.hostname")),
589+
),
590+
Entry("set hostname if not set and guestConfig is set",
591+
&compute.Machine{
592+
Spec: compute.MachineSpec{
593+
GuestConfig: &compute.MachineGuestConfig{
594+
Hostname: "foo-hostname",
595+
},
596+
},
597+
},
598+
&compute.Machine{
599+
Spec: compute.MachineSpec{
600+
GuestConfig: &compute.MachineGuestConfig{},
601+
},
602+
},
603+
ContainElement(InvalidField("spec.guestConfig.hostname")),
604+
),
577605
Entry("remove hostname if set",
578606
&compute.Machine{
579607
Spec: compute.MachineSpec{

0 commit comments

Comments
 (0)