Skip to content

Commit 9dc0ca0

Browse files
fix: error message with bottlerocket userdata (#8903)
Co-authored-by: Andrew Mitchell <andrew.j.mitchell.247@gmail.com>
1 parent 9e7ef98 commit 9dc0ca0

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

pkg/providers/amifamily/bootstrap/bottlerocketsettings.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,36 @@ func (c *BottlerocketConfig) UnmarshalTOML(ctx context.Context, data []byte) err
129129
s := struct {
130130
Settings BottlerocketSettings `toml:"settings"`
131131
}{}
132-
// use strict mode first to check if userData contains an unsupported value and log this, but don't return an error
133-
r := strings.NewReader(string(data))
134-
d := toml.NewDecoder(r)
135-
d.DisallowUnknownFields()
136-
if err := d.Decode(&s); err != nil {
137-
// only log in case we got an error of type toml.StrictMissingError
138-
var details *toml.StrictMissingError
139-
if errors.As(err, &details) {
140-
log.FromContext(ctx).Error(err, "Unknown parameter in userData K8s settings", "reason", details.String())
132+
133+
// unmarshal untyped settings
134+
if err := toml.Unmarshal(data, c); err != nil {
135+
return err
136+
}
137+
138+
// To log misconfigured / unsupported k8s userData, we re-marshal the k8s settings
139+
// and re-unmarshal with TOML strict mode to log any errors
140+
if k8sRaw, ok := c.SettingsRaw["kubernetes"]; ok {
141+
k8sData, err := toml.Marshal(k8sRaw)
142+
if err != nil {
143+
return err
144+
}
145+
146+
k8sSettings := BottlerocketKubernetes{}
147+
r := strings.NewReader(string(k8sData))
148+
d := toml.NewDecoder(r)
149+
d.DisallowUnknownFields()
150+
if err := d.Decode(&k8sSettings); err != nil {
151+
var details *toml.StrictMissingError
152+
if errors.As(err, &details) {
153+
log.FromContext(ctx).Error(err, "Unknown parameter in userData K8s settings", "reason", details.String())
154+
}
141155
}
142156
}
157+
143158
// proceed without strict mode
144159
if err := toml.Unmarshal(data, &s); err != nil {
145160
return err
146161
}
147-
// unmarshal untyped settings
148-
if err := toml.Unmarshal(data, c); err != nil {
149-
return err
150-
}
151162
c.Settings = s.Settings
152163
return nil
153164
}

0 commit comments

Comments
 (0)