kubernetes/kops · error

--name is required

Error message

--name is required

What it means

The `kops trust keypair` command requires a cluster name. It takes it from rootCommand.ClusterName(true), which resolves the --name flag; if empty, the Args validator rejects the invocation with this error before any cluster access.

Source

Thrown at cmd/kops/trust_keypair.go:67

type TrustKeypairOptions struct {
	ClusterName string
	Keyset      string
	KeypairIDs  []string
}

func NewCmdTrustKeypair(f *util.Factory, out io.Writer) *cobra.Command {
	options := &TrustKeypairOptions{}

	cmd := &cobra.Command{
		Use:     "keypair KEYSET ID...",
		Short:   trustKeypairShort,
		Long:    trustKeypairLong,
		Example: trustKeypairExample,
		Args: func(cmd *cobra.Command, args []string) error {
			options.ClusterName = rootCommand.ClusterName(true)
			if options.ClusterName == "" {
				return fmt.Errorf("--name is required")
			}

			if len(args) == 0 {
				return fmt.Errorf("must specify name of keyset to trust keypair in")
			}
			options.Keyset = args[0]

			if len(args) == 1 {
				return fmt.Errorf("must specify names of keypairs to trust keypair in")
			}
			options.KeypairIDs = args[1:]

			return nil
		},
		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
			return completeTrustKeyset(cmd.Context(), f, options, args, toComplete)
		},
		RunE: func(cmd *cobra.Command, args []string) error {

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Add `--name <cluster-name>` to the command.
  2. Set KOPS_CLUSTER_NAME environment variable for the shell/session.
  3. Run `kops set cluster <cluster-name>` to set the default cluster context.

Example fix

// before
kops trust keypair kubelet 2026-01-01
// after
kops trust keypair --name mycluster.example.com kubelet 2026-01-01
Defensive patterns

Strategy: validation

Validate before calling

cluster := os.Getenv("KOPS_CLUSTER_NAME")
if cluster == "" { return errors.New("set --name or KOPS_CLUSTER_NAME before running kops trust keypair") }

Try / catch

out, err := exec.Command("kops", "trust", "keypair", "--name", cluster, args...).CombinedOutput()
if strings.Contains(string(out), "--name is required") { prompt for / resolve cluster name and retry }

Prevention

When it happens

Trigger: Running `kops trust keypair <keyset> <keypair-ids...>` without --name and without KOPS_CLUSTER_NAME or a kops-context set cluster.

Common situations: Running from a directory with no cluster context; forgetting --name when scripting; environment variable not exported in CI.

Understand the failure class

Background: "--flag is required" and "must specify" CLI errors: how missing-required-flag validation works and how to fix it — this error's family across 20 libraries.

Related errors


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