-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add option to enable/disable unknown fields in OCI config #12052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Please squash the commits into 1. Currently this branch has 7 commits. If we were to merge this, all the intermediate commits will make it to the master branch. |
9c3cb2d to
e740c7e
Compare
|
Should be good |
runsc/config/config.go
Outdated
|
|
||
| // AllowUnknownFields allows unknown fields in the OCI config. If disabled, | ||
| // unknown fields will result in an error. | ||
| AllowUnknownFields bool `flag:"allow-unknown-fields"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a bit too vaguely-named. How about allow-unknown-oci-spec-fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Changed to allow-unknown-oci-spec-fields
runsc/specutils/specutils.go
Outdated
| decoder := json.NewDecoder(bytes.NewReader(specBytes)) | ||
| if !conf.AllowUnknownFields { | ||
| decoder.DisallowUnknownFields() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I only thought about this after seconds review, but what do you think about removing the flag, and instead having the behavior here be like:
- Try to parse with the
DisallowUnknownFieldsoption set. - If it succeeds, great, go ahead.
- If it fails, try to parse again, this time without the
DisallowUnknownFieldsoption. - If that fails too (malformed JSON?), return the error from the second parsing attempt.
- If the second parsing attempt succeeds, then log a warning with the error from the first parsing attempt, but still continue with the originally-decoded JSON.
This way avoids introducing a new flag, and also makes any lack of specific OCI option support in runsc more obvious even with default flags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what is better in general, but looks fine for me too. Can change a bit later today :)
4dfb562 to
fc2f4df
Compare
|
No more fields. Only small warning. Should we make it more loud? Or small log.Warningf is fine? |
runsc/specutils/specutils.go
Outdated
| return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), err, string(specBytes)) | ||
| decoder := json.NewDecoder(bytes.NewReader(specBytes)) | ||
| decoder.DisallowUnknownFields() | ||
| if err := decoder.Decode(&spec); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please give the 2 errors distinctive names to differentiate them from each other, like errStrictDecode and errLooseDecode or something.
runsc/specutils/specutils.go
Outdated
| if err2 := json.Unmarshal(specBytes, &spec); err2 != nil { | ||
| return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), err2, string(specBytes)) | ||
| } else { | ||
| log.Warningf("Problem with spec file %q: %v. Consider removing unnecessary fields.", specFile.Name(), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can only happen in the case where there are unknown fields, and the message should also make it clear that this isn't a fatal error because the fields will be ignored.
log.Warningf("OCI spec file %q contains fields unknown to `runsc`: %v. Ignoring these fields and continuing anyway.")
|
@EtiennePerot Thx for showing the way. Good to squash? |
Currently in OCI config it is possible to add any extra json data, which will be ignored by runsc. I found it is a bit confusing when was learning OCI implementation of gvisor (adding wrong/non existent fields does not produce any error or warning), therefore decided to add flag `allow-unknown-fields`, which is `true` by default (does not modify current behavior). In case of being `false`, gvisor produces error like this: ``` reading spec: error unmarshaling spec from file "config.json": json: unknown field "oooooo" ``` FUTURE_COPYBARA_INTEGRATE_REVIEW=#12052 from ioterw:allow_unknown_fields 0662917 PiperOrigin-RevId: 797846468
Currently in OCI config it is possible to add any extra json data, which will be ignored by runsc. I found it is a bit confusing when was learning OCI implementation of gvisor (adding wrong/non existent fields does not produce any error or warning), therefore decided to add flag `allow-unknown-fields`, which is `true` by default (does not modify current behavior). In case of being `false`, gvisor produces error like this: ``` reading spec: error unmarshaling spec from file "config.json": json: unknown field "oooooo" ``` FUTURE_COPYBARA_INTEGRATE_REVIEW=#12052 from ioterw:allow_unknown_fields 0662917 PiperOrigin-RevId: 797846468
Currently in OCI config it is possible to add any extra json data, which will be ignored by runsc.
I found it is a bit confusing when was learning OCI implementation of gvisor (adding wrong/non existent fields does not produce any error or warning), therefore decided to add flag
allow-unknown-fields, which istrueby default (does not modify current behavior).In case of being
false, gvisor produces error like this: