{"record":{"id":"18b9fdd67d83d983","repo":"kubernetes/kops","slug":"api-version-must-be-specified","errorCode":null,"errorMessage":"api-version must be specified","messagePattern":"api-version must be specified","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/helpers/kubectl_auth.go","lineNumber":102,"sourceCode":"\tcmd.Flags().StringVar(&options.ClusterName, \"cluster\", options.ClusterName, \"cluster to target\")\n\tcmd.Flags().DurationVar(&options.Lifetime, \"lifetime\", options.Lifetime, \"lifetime of the credential to issue\")\n\n\treturn cmd\n}\n\n// RunKubectlAuthHelper implements the kubectl auth helper, which creates an authentication token\nfunc RunKubectlAuthHelper(ctx context.Context, f *util.Factory, out io.Writer, options *HelperKubectlAuthOptions) error {\n\tif options.ClusterName == \"\" {\n\t\treturn fmt.Errorf(\"ClusterName is required\")\n\t}\n\n\texecCredential := &ExecCredential{\n\t\tKind: \"ExecCredential\",\n\t}\n\n\tswitch options.APIVersion {\n\tcase \"\":\n\t\treturn fmt.Errorf(\"api-version must be specified\")\n\tcase \"v1alpha1\":\n\t\texecCredential.APIVersion = \"client.authentication.k8s.io/v1alpha1\"\n\tcase \"v1beta1\":\n\t\texecCredential.APIVersion = \"client.authentication.k8s.io/v1beta1\"\n\n\tdefault:\n\t\treturn fmt.Errorf(\"api-version %q is not supported\", options.APIVersion)\n\t}\n\n\tcacheFilePath := cacheFilePath(f.KopsStateStore(), options.ClusterName)\n\tcached, err := loadCachedExecCredential(cacheFilePath)\n\tif err != nil {\n\t\tklog.Infof(\"cached credential %q was not valid: %v\", cacheFilePath, err)\n\t\tcached = nil\n\t}\n\n\tif cached != nil && cached.APIVersion != execCredential.APIVersion {\n\t\tklog.Infof(\"cached credential had wrong api version\")","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/helpers/kubectl_auth.go#L84-L120","documentation":"The helper must emit an ExecCredential with a concrete client.authentication.k8s.io apiVersion (v1alpha1 or v1beta1). If options.APIVersion is empty, the switch in RunKubectlAuthHelper returns this error because kubectl requires a known schema version to parse the output. Normally InitDefaults() sets v1beta1, so the empty value only occurs when defaults were skipped or explicitly cleared.","triggerScenarios":"Calling RunKubectlAuthHelper with an options struct that never had InitDefaults() called and APIVersion unset (empty string), or a flag wiring bug that passes --api-version \"\".","commonSituations":"Constructing HelperKubectlAuthOptions directly in Go without calling InitDefaults; a wrapper script that sets --api-version to an empty variable; refactoring the cobra command away from the default-initialization path.","solutions":["Call options.InitDefaults() before running, or pass --api-version v1beta1 explicitly.","If Go code bypasses NewCmdHelperKubectlAuth, initialize APIVersion to \"v1beta1\" (or \"v1alpha1\") yourself.","Fix shell quoting so an empty $VAR doesn't result in an empty flag value."],"exampleFix":"// before\noptions := &helpers.HelperKubectlAuthOptions{}\nerr := helpers.RunKubectlAuthHelper(ctx, f, out, options)\n// after\noptions := &helpers.HelperKubectlAuthOptions{}\noptions.InitDefaults()\nerr := helpers.RunKubectlAuthHelper(ctx, f, out, options)","handlingStrategy":"validation","validationCode":"opts := &helpers.HelperKubectlAuthOptions{}\nopts.InitDefaults() // sets APIVersion to v1beta1\nif opts.APIVersion == \"\" {\n    opts.APIVersion = \"v1beta1\"\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never construct HelperKubectlAuthOptions without calling InitDefaults().","Pass --api-version v1beta1 explicitly in scripts.","Keep the kubeconfig exec args consistent with kubectl's expected schema version."],"tags":["cli","api-version","authentication"],"backgroundTag":"missing-required-argument","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}