Skip to content

Commit 937d35b

Browse files
authored
Elemental Operator: manage empty config in MachineRegistrations (#213)
* operator: manage empty config in MachineRegistrations We don't deal with empty Spec:Config in MachineRegistrations: in that case we would end up with a nil Config structure, which we don't check causing the operator to panic. Just check and deal with empty (nil) MachineRegistration config. Fixes #202 Signed-off-by: Francesco Giudici <francesco.giudici@suse.com> * operator:trivial: rename var machineRegistration to registration In order to manage a MachineRegistration resource we instantiate a var named 'registration' in all the functions of the server package, but in the 'unauthenticatedResponse' function. Let's stay coherent: rename the variable. Signed-off-by: Francesco Giudici <francesco.giudici@suse.com> * operator: tests: expand the data structure TestInitNewInventory This has no functional change: just extend the configuration parameter that can be set in the data structure used for the tests. Make use of it in the following commit. Signed-off-by: Francesco Giudici <francesco.giudici@suse.com> * operator: tests: check empty config in MachineRegistrations Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
1 parent 98c9bff commit 937d35b

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

pkg/server/register.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,17 @@ func upgrade(resp http.ResponseWriter, req *http.Request) (*websocket.Conn, erro
121121
return conn, err
122122
}
123123

124-
func (i *InventoryServer) unauthenticatedResponse(machineRegistration *elm.MachineRegistration, writer io.Writer) error {
125-
mRRegistration := machineRegistration.Spec.Config.Elemental.Registration
124+
func (i *InventoryServer) unauthenticatedResponse(registration *elm.MachineRegistration, writer io.Writer) error {
125+
if registration.Spec.Config == nil {
126+
registration.Spec.Config = &config.Config{}
127+
}
128+
129+
mRRegistration := registration.Spec.Config.Elemental.Registration
126130

127131
return yaml.NewEncoder(writer).Encode(config.Config{
128132
Elemental: config.Elemental{
129133
Registration: config.Registration{
130-
URL: machineRegistration.Status.RegistrationURL,
134+
URL: registration.Status.RegistrationURL,
131135
CACert: i.getRancherCACert(),
132136
EmulateTPM: mRRegistration.EmulateTPM,
133137
EmulatedTPMSeed: mRRegistration.EmulatedTPMSeed,
@@ -220,6 +224,10 @@ func (i *InventoryServer) writeMachineInventoryCloudConfig(conn *websocket.Conn,
220224
}
221225
defer writer.Close()
222226

227+
if registration.Spec.Config == nil {
228+
registration.Spec.Config = &config.Config{}
229+
}
230+
223231
return yaml.NewEncoder(writer).Encode(config.Config{
224232
Elemental: config.Elemental{
225233
Registration: config.Registration{
@@ -275,6 +283,9 @@ func buildStringFromSmbiosData(data map[string]interface{}, name string) string
275283
func initInventory(inventory *elm.MachineInventory, registration *elm.MachineRegistration) {
276284
const namePrefix = "m-"
277285

286+
if registration.Spec.Config == nil {
287+
registration.Spec.Config = &config.Config{}
288+
}
278289
inventory.Name = registration.Spec.MachineName
279290
if inventory.Name == "" {
280291
if registration.Spec.Config.Elemental.Registration.NoSMBIOS {

pkg/server/register_test.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,59 @@ func TestInitNewInventory(t *testing.T) {
3232
// e.g., m-66588488-3eb6-4a6d-b642-c994f128c6f1
3333

3434
testCase := []struct {
35-
noSMBIOS bool
35+
config *config.Config
3636
initName string
3737
expectedName string
3838
}{
3939
{
40-
noSMBIOS: false,
40+
config: &config.Config{
41+
Elemental: config.Elemental{
42+
Registration: config.Registration{
43+
NoSMBIOS: false,
44+
},
45+
},
46+
},
4147
initName: "custom-name",
4248
expectedName: "custom-name",
4349
},
50+
4451
{
45-
noSMBIOS: false,
52+
config: &config.Config{
53+
Elemental: config.Elemental{
54+
Registration: config.Registration{
55+
NoSMBIOS: false,
56+
},
57+
},
58+
},
4659
expectedName: "m-${System Information/UUID}",
4760
},
4861
{
49-
noSMBIOS: true,
62+
config: &config.Config{
63+
Elemental: config.Elemental{
64+
Registration: config.Registration{
65+
NoSMBIOS: true,
66+
},
67+
},
68+
},
69+
},
70+
{
71+
config: nil,
72+
expectedName: "m-${System Information/UUID}",
5073
},
5174
}
5275

5376
for _, test := range testCase {
5477
registration := &elm.MachineRegistration{
5578
Spec: elm.MachineRegistrationSpec{
5679
MachineName: test.initName,
57-
Config: &config.Config{
58-
Elemental: config.Elemental{
59-
Registration: config.Registration{
60-
NoSMBIOS: test.noSMBIOS,
61-
},
62-
},
63-
},
80+
Config: test.config,
6481
},
6582
}
6683

6784
inventory := &elm.MachineInventory{}
6885
initInventory(inventory, registration)
6986

70-
if test.noSMBIOS {
87+
if test.config != nil && test.config.Elemental.Registration.NoSMBIOS {
7188
assert.Check(t, mUUID.Match([]byte(inventory.Name)), inventory.Name+" is not UUID based")
7289
} else {
7390
assert.Equal(t, inventory.Name, test.expectedName)

0 commit comments

Comments
 (0)