kubernetes/kops · error

invalid target: %q

Error message

invalid target: %q

What it means

kops.Target implements the pflag.Value interface for the --target CLI flag and only accepts Direct, DryRun, or Terraform (case-insensitive). Any other value is rejected with this error before the cluster update runs.

Source

Thrown at upup/pkg/fi/cloudup/target.go:51

	TargetDryRun Target = "dryrun"
	// TargetTerraform means we will generate terraform code.
	TargetTerraform Target = "terraform"
)

// Target can be used as a flag value.
var _ pflag.Value = (*Target)(nil)

func (t *Target) String() string {
	return string(*t)
}

func (t *Target) Set(value string) error {
	switch strings.ToLower(value) {
	case string(TargetDirect), string(TargetDryRun), string(TargetTerraform):
		*t = Target(value)
		return nil
	default:
		return fmt.Errorf("invalid target: %q", value)
	}
}

func (t *Target) Type() string {
	return "target"
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Use one of the supported values: --target direct, --target dryrun, or --target terraform
  2. If Terraform output is wanted, use `--target terraform` and run terraform plan/apply in the out/ directory
  3. Check `kops update cluster --help` for the current list of valid targets on your kOps version

Example fix

// before
kops update cluster mycluster --target cloudformation
// after
kops update cluster mycluster --target terraform
Defensive patterns

Strategy: validation

Validate before calling

switch strings.ToLower(target) {
case "direct", "dryrun", "terraform":
	// ok
default:
	return fmt.Errorf("unsupported --target %q", target)
}

Type guard

func isValidTarget(v string) bool {
	switch strings.ToLower(v) {
	case "direct", "dryrun", "terraform":
		return true
	}
	return false
}

Try / catch

var t target.Target
if err := t.Set(flagValue); err != nil {
	fmt.Fprintf(os.Stderr, "usage: --target direct|dryrun|terraform\n")
	os.Exit(2)
}

Prevention

When it happens

Trigger: Running `kops update cluster --target <value>` with a value outside {direct, dryrun, terraform}, e.g. `--target cloudformation` or `--target apply`.

Common situations: Users expecting other IaC backends (CloudFormation, Pulumi); scripting with a typo like `--target terrafrom`; copying flags from other tools.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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