kubernetes/kops · error

converting kubeconfig to yaml: %w

Error message

converting kubeconfig to yaml: %w

What it means

createKopsControlPlane builds an in-cluster kubeconfig (map[string]any) and serializes it to YAML before embedding it in an Opaque Secret. This error wraps a yaml.Marshal failure. Since the input is a plain map of YAML-safe values, this is practically unreachable except for types that cannot be marshaled or data containing values that sigs.k8s.io/yaml cannot convert to JSON.

Source

Thrown at pkg/controllers/clusterapi/cluster_controller.go:288

					"name": "in-cluster",
				},
			},
			"current-context": "in-cluster",
			"kind":            "Config",
			"preferences":     map[string]any{},
			"users": []map[string]any{
				{
					"name": "in-cluster",
					"user": map[string]any{ //nolint:gosec // This is a kubeconfig field name, not a credential.
						"tokenFile": "/var/run/secrets/kubernetes.io/serviceaccount/token",
					},
				},
			},
		}

		kubeconfigBytes, err := yaml.Marshal(kubeconfig)
		if err != nil {
			return fmt.Errorf("converting kubeconfig to yaml: %w", err)
		}

		obj := map[string]any{
			"apiVersion": "v1",
			"kind":       "Secret",
			"metadata": map[string]any{
				"name":      name + "-kubeconfig",
				"namespace": s.namespace(),
			},
			"data": map[string]any{
				"value": kubeconfigBytes,
			},
			"type": "Opaque",
		}

		u := &unstructured.Unstructured{Object: obj}

		// Needed so that capi manager has "permission" to read the secret

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Inspect the wrapped %w error to identify the non-marshalable value
  2. Ensure all kubeconfig map values are JSON/YAML-safe basic types (string, []map[string]any)
  3. Update kops / sigs.k8s.io/yaml to a version with relevant marshal fixes if a library bug is suspected
Defensive patterns

Strategy: try-catch

Try / catch

kubeconfigBytes, err := yaml.Marshal(kubeconfig)
if err != nil {
    return fmt.Errorf("converting kubeconfig to yaml: %w", err)
}

Prevention

When it happens

Trigger: yaml.Marshal on the kubeconfig map fails — essentially only if a value in the map is not JSON-marshalable (e.g. a func, channel, or circular structure) or the marshal operation fails at the JSON round-trip level used by sigs.k8s.io/yaml.

Common situations: A code change introduces a non-marshalable value into the kubeconfig map (custom type, unexported/unsupported field); extremely rare at runtime for users of the library.

Related errors


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