Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/app/machined/pkg/controllers/config/acquire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,15 @@ func (suite *AcquireSuite) TestFromDiskFailure() {
ev := suite.platformEvent.getEvents()[0]
suite.Assert().Equal(platform.EventTypeFailure, ev.Type)
suite.Assert().Equal("Error loading and validating Talos machine config.", ev.Message)
suite.Assert().Equal("failed to load \"config.yaml\" from STATE: unknown keys found during decoding:\naaaversion: v1alpha1 # Indicates the schema used to decode the contents.\n", ev.Error.Error())
suite.Assert().Equal(
"failed to load \"config.yaml\" from STATE: error decoding document /v1alpha1/ (line 1): unknown keys found during decoding:\n"+
"aaaversion: v1alpha1 # Indicates the schema used to decode the contents.\n",
ev.Error.Error(),
)

suite.Assert().Equal(&machineapi.ConfigLoadErrorEvent{
Error: "failed to load \"config.yaml\" from STATE: unknown keys found during decoding:\naaaversion: v1alpha1 # Indicates the schema used to decode the contents.\n",
Error: "failed to load \"config.yaml\" from STATE: error decoding document /v1alpha1/ (line 1): unknown keys found during decoding:\n" +
"aaaversion: v1alpha1 # Indicates the schema used to decode the contents.\n",
}, suite.eventPublisher.getEvents()[0])
}

Expand Down
22 changes: 18 additions & 4 deletions pkg/machinery/config/configloader/internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func parse(r io.Reader, allowPatchDelete bool) (decoded []config.Document, err e
}

if manifests.Kind != yaml.DocumentNode {
return nil, errors.New("expected a document")
return nil, fmt.Errorf("expected a document at line %d", manifests.Line)
}

if allowPatchDelete {
decoded, err = AppendDeletesTo(&manifests, decoded, i)
if err != nil {
return nil, err
return nil, fmt.Errorf("error processing patch delete statements at line %d: %w", manifests.Line, err)
}

if manifests.IsZero() {
Expand All @@ -100,22 +100,36 @@ func parse(r io.Reader, allowPatchDelete bool) (decoded []config.Document, err e
}

for _, manifest := range manifests.Content {
switch manifest.Kind { //nolint:exhaustive
case yaml.MappingNode:
// expected
case yaml.ScalarNode:
if manifest.Tag == "!!null" {
// skip null documents
continue
}

fallthrough
default:
return nil, fmt.Errorf("expected a YAML document at line %d", manifest.Line)
}

id := documentID{
APIVersion: findValue(manifest, ManifestAPIVersionKey, false),
Kind: cmp.Or(findValue(manifest, ManifestKindKey, false), "v1alpha1"),
Name: findValue(manifest, "name", false),
}

if _, ok := knownDocuments[id]; ok {
return nil, fmt.Errorf("duplicate document %s/%s/%s is not allowed", id.APIVersion, id.Kind, id.Name)
return nil, fmt.Errorf("duplicate document %s/%s/%s is not allowed (line %d)", id.APIVersion, id.Kind, id.Name, manifest.Line)
}

knownDocuments[id] = struct{}{}

var target config.Document

if target, err = decode(manifest); err != nil {
return nil, err
return nil, fmt.Errorf("error decoding document %s/%s/%s (line %d): %w", id.APIVersion, id.Kind, id.Name, manifest.Line, err)
}

decoded = append(decoded, target)
Expand Down
30 changes: 23 additions & 7 deletions pkg/machinery/config/configloader/internal/decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ func TestDecoder(t *testing.T) {
kind: mock
apiVersion: v1alpha1
test: true
`),
expected: []config.Document{
&Mock{
Test: true,
},
},
expectedErr: "",
},
{
name: "empty docs",
source: []byte(`---
kind: mock
apiVersion: v1alpha1
test: true
---
---
`),
expected: []config.Document{
&Mock{
Expand All @@ -135,7 +151,7 @@ apiVersion: v1alpha2
test: true
`),
expected: nil,
expectedErr: "missing kind",
expectedErr: "error decoding document v1alpha2/v1alpha1/ (line 2): missing kind",
},
{
name: "empty kind",
Expand All @@ -145,7 +161,7 @@ apiVersion: v1alpha2
test: true
`),
expected: nil,
expectedErr: "missing kind",
expectedErr: "error decoding document v1alpha2/v1alpha1/ (line 2): missing kind",
},
{
name: "tab instead of spaces",
Expand All @@ -167,7 +183,7 @@ test: true
extra: fail
`),
expected: nil,
expectedErr: "unknown keys found during decoding:\nextra: fail\n",
expectedErr: "error decoding document v1alpha1/mock/ (line 2): unknown keys found during decoding:\nextra: fail\n",
},
{
name: "extra fields in map",
Expand All @@ -180,7 +196,7 @@ map:
extra: me
`),
expected: nil,
expectedErr: "unknown keys found during decoding:\nmap:\n first:\n extra: me\n",
expectedErr: "error decoding document v1alpha2/mock/ (line 2): unknown keys found during decoding:\nmap:\n first:\n extra: me\n",
},
{
name: "extra fields in slice",
Expand All @@ -194,7 +210,7 @@ slice:
fields: here
`),
expected: nil,
expectedErr: "unknown keys found during decoding:\nslice:\n - fields: here\n more: extra\n not: working\n",
expectedErr: "error decoding document v1alpha2/mock/ (line 2): unknown keys found during decoding:\nslice:\n - fields: here\n more: extra\n not: working\n",
},
{
name: "extra zero fields in map",
Expand All @@ -207,7 +223,7 @@ map:
b: {}
`),
expected: nil,
expectedErr: "unknown keys found during decoding:\nmap:\n second:\n a:\n b: {}\n",
expectedErr: "error decoding document v1alpha2/mock/ (line 2): unknown keys found during decoding:\nmap:\n second:\n a:\n b: {}\n",
},
{
name: "valid nested",
Expand Down Expand Up @@ -306,7 +322,7 @@ config:
- content: MONITOR ${upsmonHost} 1 remote pass foo
mountPath: /usr/local/etc/nut/upsmon.conf
`),
expectedErr: "\"ExtensionServiceConfig\" \"\": not registered",
expectedErr: "error decoding document /ExtensionServiceConfig/ (line 2): \"ExtensionServiceConfig\" \"\": not registered",
},
}

Expand Down