kubernetes/kops · error

cannot set field %q: %v

Error message

cannot set field %q: %v

What it means

When the parsed field path matches, SetString calls setType to coerce the string newValue to the field's type; this error wraps any coercion failure (e.g. setting a non-numeric string on an int field, invalid bool, or wrong time format). The path itself was correct; the VALUE was not convertible.

Source

Thrown at util/pkg/reflectutils/access.go:52

	targetFieldPath, err := ParseFieldPath(targetPath)
	if err != nil {
		return fmt.Errorf("cannot parse field path %q: %w", targetPath, err)
	}

	fieldSet := false

	visitor := func(path *FieldPath, field *reflect.StructField, v reflect.Value) error {
		if !targetFieldPath.HasPrefixMatch(path) {
			return nil
		}

		if targetFieldPath.Matches(path) {
			if !v.CanSet() {
				return fmt.Errorf("cannot set field %q (marked immutable)", path)
			}

			if err := setType(v, newValue); err != nil {
				return fmt.Errorf("cannot set field %q: %v", path, err)
			}

			fieldSet = true
			return nil
		}

		// Partial match, append the next explicitly indexed slice element.
		if v.Kind() == reflect.Slice {
			if len(targetFieldPath.elements) > len(path.elements) {
				next := targetFieldPath.elements[len(path.elements)]
				if next.Type == FieldPathElementTypeArrayIndex && next.number == v.Len() {
					if !v.CanSet() {
						return fmt.Errorf("cannot set field %q (marked immutable)", path)
					}

					newLen := v.Len() + 1
					grown := reflect.MakeSlice(v.Type(), newLen, newLen)
					reflect.Copy(grown, v)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check the target field's Go type in the kops API and supply a value it can parse (strconv-compatible)
  2. Quote/escape values in shell so an empty variable doesn't reach setType
  3. Validate the value with strconv.ParseInt/ParseBool yourself before calling SetString

Example fix

// before
reflectutils.SetString(ig, "spec.minSize", "three")
// after
reflectutils.SetString(ig, "spec.minSize", "3")
Defensive patterns

Strategy: validation

Validate before calling

// coerce-check before SetString
if n, err := strconv.Atoi(val); err != nil && fieldIsInt(path) {
	return fmt.Errorf("field %s needs an integer, got %q", path, val)
}

Type guard

func canParseAsInt(s string) bool { _, err := strconv.Atoi(s); return err == nil }
func canParseAsBool(s string) bool { _, err := strconv.ParseBool(s); return err == nil }

Try / catch

if err := reflectutils.SetString(target, path, val); err != nil {
	if strings.Contains(err.Error(), "cannot set field") && !strings.Contains(err.Error(), "immutable") {
		return fmt.Errorf("value %q not convertible for field %s: %w", val, path, err)
	}
	return err
}

Prevention

When it happens

Trigger: SetString(obj, "spec.nodeCount", "many") on an int field; "true"/"false" misspellings on bool fields; setting a duration/time field with an unparseable string.

Common situations: kops set cluster-field overrides from templates where a variable renders as empty or non-numeric; users passing version strings to numeric fields; region names into boolean flags.

Related errors


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