kubernetes/kops · error

unable to marshal YAML: %v

Error message

unable to marshal YAML: %v

What it means

In YAML output mode, `kops get sshpublickeys` serializes the collected SSHKeyItem slice with gopkg.in/yaml.v2/api Marshal. Marshal of these plain structs essentially never fails; if it does (unserializable types introduced by code changes), the command returns "unable to marshal YAML: %v".

Source

Thrown at cmd/kops/get_sshpublickeys.go:123

		items = append(items, item)
	}

	switch options.Output {

	case OutputTable:
		if len(items) == 0 {
			return fmt.Errorf("no SSH public key found")
		}
		t := &tables.Table{}
		t.AddColumn("ID", func(i *SSHKeyItem) string {
			return i.ID
		})
		return t.Render(items, out, "ID")

	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. Check the wrapped error after 'unable to marshal YAML' to identify the offending field/type
  2. If you patched the code, ensure all new fields on SSHKeyItem are YAML-serializable (add yaml tags or omit the field)
  3. Fall back to `-o json` to see if JSON marshaling succeeds and isolate the YAML-specific issue

Example fix

// before
foo func() `yaml:"foo"`
// after
Foo string `yaml:"foo,omitempty"` // replace unserializable field type
Defensive patterns

Strategy: try-catch

Try / catch

if err := runGetSSHPublicKeys(opts); err != nil {
	if strings.HasPrefix(err.Error(), "unable to marshal YAML:") {
		// fall back to JSON output
		opts.Output = OutputJSON
		return runGetSSHPublicKeys(opts)
	}
	return err
}

Prevention

When it happens

Trigger: Practically only when the SSHKeyItem struct gains a field whose type cannot be YAML-serialized (e.g. func, chan, or a cycle) after a code change; not user-triggerable via flags.

Common situations: Custom builds/patches of kOps that altered the item struct; a bug in a fork; otherwise almost never seen by end users.

Related errors


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