kubernetes/kops · error
unable to marshal YAML: %v
Error message
unable to marshal YAML: %v
What it means
In RunGetKeypairs, the OutputYaml branch marshals the []*keypairItem slice with yaml.Marshal. If marshaling fails (unsupported type in the items, marshaler misconfiguration), the error is wrapped as "unable to marshal YAML: %v". This is a serialization failure before any bytes reach the output writer.
Source
Thrown at cmd/kops/get_keypairs.go:241
return ""
})
t.AddColumn("HASPRIVATE", func(i *keypairItem) string {
if i.HasPrivateKey {
return "*"
}
return ""
})
columnNames := []string{"NAME", "ID", "ISSUED", "EXPIRES"}
if options.Distrusted {
columnNames = append(columnNames, "DISTRUSTED")
}
columnNames = append(columnNames, "PRIMARY", "HASPRIVATE")
return t.Render(items, out, columnNames...)
case OutputYaml:
y, err := yaml.Marshal(items)
if err != nil {
return fmt.Errorf("unable to marshal YAML: %v", err)
}
if _, err := out.Write(y); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
case OutputJSON:
j, err := json.Marshal(items)
if err != nil {
return fmt.Errorf("unable to marshal JSON: %v", err)
}
if _, err := out.Write(j); err != nil {
return fmt.Errorf("error writing to output: %v", err)
}
default:
return fmt.Errorf("unknown output format: %q", options.Output)
}
return nilView on GitHub (pinned to 4c8573c808)
Solutions
- Read the wrapped %v cause to identify the offending field/type.
- Fall back to `-o json` to still get machine-readable output while isolating the YAML issue.
- Rebuild kops with a clean module cache (`make kops` after `go clean -modcache`) to rule out dependency drift.
- If reproducible on upstream kops, file an issue with the wrapped error text.
Example fix
// before kops get keypairs -o yaml # unable to marshal YAML // after (workaround) kops get keypairs -o json
Defensive patterns
Strategy: fallback
Try / catch
if err := runKops("get", "keypairs", "-o", "yaml"); err != nil {
if strings.Contains(err.Error(), "unable to marshal YAML") {
// fallback to JSON output and convert with yq
return runKops("get", "keypairs", "-o", "json")
}
return err
} Prevention
- Prefer -o json for machine consumption; convert to YAML with yq if needed.
- Keep the kops build clean of local keypairItem type modifications.
- Rebuild with a clean module cache after dependency upgrades.
- Capture the wrapped marshal error text when reporting issues.
When it happens
Trigger: `kops get keypairs -o yaml` where yaml.Marshal(items) errors — practically rare for keypairItem slices, but possible via unserializable field types or a yaml library incompatibility after a dependency upgrade.
Common situations: Custom builds where keypairItem gained a field of an unsupported type; vendoring/go.mod drift between kubernetes-sigs/yaml versions; binary built against mismatched library versions.
Related errors
- error writing yaml to stdout: %v
- unable to marshal YAML: %v
- unable to marshal YAML: %v
- error marshaling yaml: %v
- error marshalling %s/%s: %w
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/d64ee704c07819de.
Report an issue: GitHub.