kubernetes/kops · error

cannot parse token %q as array-index

Error message

cannot parse token %q as array-index

What it means

ParseFieldPath lexes field paths; when a bracketed token is scanned as an integer but strconv.Atoi still fails (e.g. an integer literal too large for int), it returns this error. Practically it fires when the text inside brackets cannot be converted to an array index.

Source

Thrown at util/pkg/reflectutils/field_path.go:114

			})

		case '.', '/':
			// Skip

		case '[':
			{
				tok := scan.Scan()
				switch tok {
				case '*':
					elements = append(elements, FieldPathElement{
						Type: FieldPathElementTypeWildcardIndex,
					})

				case scanner.Int:
					v := scan.TokenText()
					n, err := strconv.Atoi(v)
					if err != nil {
						return nil, fmt.Errorf("cannot parse token %q as array-index", v)
					}
					elements = append(elements, FieldPathElement{
						Type:   FieldPathElementTypeArrayIndex,
						number: n,
					})

				default:
					return nil, fmt.Errorf("unexpected token %v (%s)", tok, scan.TokenText())
				}

				tok = scan.Scan()
				switch tok {
				case ']':
					// ok
				default:
					return nil, fmt.Errorf("unexpected token %v (%s)", tok, scan.TokenText())
				}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Use a small, in-range numeric index like [0], [1]
  2. List actual elements (`kops get instancegroups`) and index existing entries
  3. Fix path-generation code to validate indices before building the path

Example fix

// before
Unset(c, "spec.etcdClusters[99999999999999999999].name")
// after
Unset(c, "spec.etcdClusters[0].name")
Defensive patterns

Strategy: validation

Validate before calling

idx := "0"
n, err := strconv.Atoi(idx)
if err != nil || n < 0 {
	return fmt.Errorf("%q is not a valid array index", idx)
}

Type guard

func isValidArrayIndex(s string) bool {
	n, err := strconv.Atoi(s)
	return err == nil && n >= 0 && n < 1<<31
}

Try / catch

if err := doUnset(); err != nil && strings.Contains(err.Error(), "as array-index") {
	// correct the bracketed index and retry
}

Prevention

When it happens

Trigger: Paths like `spec.subnets[99999999999999999999]` — bracket contents scanned as Int but overflowing int — or other oversized/malformed index tokens.

Common situations: Copy-paste errors producing absurd index numbers, or generating paths programmatically from string data that was never validated as an index.

Related errors


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