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 nil

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Read the wrapped %v cause to identify the offending field/type.
  2. Fall back to `-o json` to still get machine-readable output while isolating the YAML issue.
  3. Rebuild kops with a clean module cache (`make kops` after `go clean -modcache`) to rule out dependency drift.
  4. 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

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


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/d64ee704c07819de. Report an issue: GitHub.