Skip to content

Commit b60a9bd

Browse files
committed
Merge pull request #12052 from ioterw:allow_unknown_fields
PiperOrigin-RevId: 797891905
2 parents dc0c437 + 0662917 commit b60a9bd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

runsc/specutils/specutils.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package specutils
1818

1919
import (
20+
"bytes"
2021
"encoding/json"
2122
"fmt"
2223
"io"
@@ -215,8 +216,14 @@ func ReadSpecFromFile(bundleDir string, specFile *os.File, conf *config.Config)
215216
return nil, fmt.Errorf("error reading spec from file %q: %v", specFile.Name(), err)
216217
}
217218
var spec specs.Spec
218-
if err := json.Unmarshal(specBytes, &spec); err != nil {
219-
return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), err, string(specBytes))
219+
decoder := json.NewDecoder(bytes.NewReader(specBytes))
220+
decoder.DisallowUnknownFields()
221+
if errLooseDecode := decoder.Decode(&spec); errLooseDecode != nil {
222+
if errStrictDecode := json.Unmarshal(specBytes, &spec); errStrictDecode != nil {
223+
return nil, fmt.Errorf("error unmarshaling spec from file %q: %v\n %s", specFile.Name(), errStrictDecode, string(specBytes))
224+
} else {
225+
log.Warningf("OCI spec file %q contains fields unknown to `runsc`: %v. Ignoring these fields and continuing anyway.", specFile.Name(), errLooseDecode)
226+
}
220227
}
221228
if err := ValidateSpec(&spec); err != nil {
222229
return nil, err

0 commit comments

Comments
 (0)