kubernetes/kops · error

marshaling to yaml for %v: %w

Error message

marshaling to yaml for %v: %w

What it means

When --output (d.output) is "yaml", dumpGVRNamespaces converts the marshaled JSON to YAML using sigs.k8s.io/yaml's JSONToYAML. If the conversion fails, the error is wrapped as "marshaling to yaml for %v: %w". Since the input is already valid JSON produced one step earlier, this usually means the JSON contains constructs sigs.k8s.io/yaml cannot represent (rare, e.g. invalid UTF-8 or pathological numeric values).

Source

Thrown at pkg/dump/resourcedumper.go:251

			results <- resourceDumpResult{
				err: fmt.Errorf("creating accessor for %v: %w", job, err),
			}
			continue
		}
		contents, err := resourceList.MarshalJSON()
		if err != nil {
			results <- resourceDumpResult{
				err: fmt.Errorf("marshaling to json for %v: %w", job, err),
			}
			continue
		}

		switch d.output {
		case "yaml":
			contents, err = yaml.JSONToYAML(contents)
			if err != nil {
				results <- resourceDumpResult{
					err: fmt.Errorf("marshaling to yaml for %v: %w", job, err),
				}
				continue
			}
		}
		_, err = resFile.Write(contents)
		if err != nil {
			results <- resourceDumpResult{
				err: fmt.Errorf("encoding resources for %v: %w", job, err),
			}
			continue
		}
		results <- resourceDumpResult{}
	}
}

func maskObject(obj runtime.Object) error {
	if obj.GetObjectKind().GroupVersionKind() == (schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Secret"}) {
		unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Dump with --out json instead of yaml to bypass JSONToYAML conversion.
  2. Inspect the offending resource's fields for invalid UTF-8 or odd values and fix/delete that object.
  3. Upgrade kOps to pick up newer sigs.k8s.io/yaml with fixed conversion handling.
  4. Check the serving API server/CRD for data corruption and correct the source of the bad data.

Example fix

// before
kops toolbox dump --out yaml
// after (workaround)
kops toolbox dump --out json
Defensive patterns

Strategy: fallback

Validate before calling

// Pre-check that the JSON payload converts cleanly before writing
canonical, err := yaml.JSONToYAML(jsonBytes)
if err != nil {
  log.Printf("yaml conversion failed (%v); falling back to json output", err)
  canonical = jsonBytes
}

Try / catch

contents, err := yaml.JSONToYAML(jsonBytes)
if err != nil {
  log.Printf("yaml conversion failed: %v; writing raw json instead", err)
  contents = jsonBytes // fallback to json output
}

Prevention

When it happens

Trigger: yaml.JSONToYAML(contents) fails on JSON that sigs.k8s.io/yaml (via gopkg.in/yaml.v2) cannot encode: invalid UTF-8 bytes in resource fields, out-of-range numbers, or deeply/irregularly typed unstructured data from a misbehaving API server.

Common situations: Custom resources containing invalid UTF-8 strings or unusual numeric values; dumping with -o yaml against an API server returning corrupt data; older sigs.k8s.io/yaml versions with conversion bugs.

Related errors


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