cleanup: add zero as a value for token create duration and act as default, added test, updated the help text

change duration validator text message in create token

unit bug fixed
This commit is contained in:
ah8ad3 2024-02-28 21:52:58 +03:30
parent aeeb029d83
commit 3b5a79f647
2 changed files with 19 additions and 4 deletions

View File

@ -148,7 +148,7 @@ func NewCmdCreateToken(f cmdutil.Factory, ioStreams genericiooptions.IOStreams)
cmd.Flags().StringArrayVar(&o.Audiences, "audience", o.Audiences, "Audience of the requested token. If unset, defaults to requesting a token for use with the Kubernetes API server. May be repeated to request a token valid for multiple audiences.")
cmd.Flags().DurationVar(&o.Duration, "duration", o.Duration, "Requested lifetime of the issued token. If not set, the lifetime will be determined by the server automatically. The server may return a token with a longer or shorter lifetime.")
cmd.Flags().DurationVar(&o.Duration, "duration", o.Duration, "Requested lifetime of the issued token. If not set or if set to 0, the lifetime will be determined by the server automatically. The server may return a token with a longer or shorter lifetime.")
cmd.Flags().StringVar(&o.BoundObjectKind, "bound-object-kind", o.BoundObjectKind, "Kind of an object to bind the token to. "+
"Supported kinds are "+strings.Join(sets.StringKeySet(boundObjectKindToAPIVersions()).List(), ", ")+". "+
@ -208,8 +208,8 @@ func (o *TokenOptions) Validate() error {
if len(o.Namespace) == 0 {
return fmt.Errorf("--namespace is required")
}
if o.Duration < 0 || (o.Duration == 0 && o.Flags.Changed("duration")) {
return fmt.Errorf("--duration must be positive")
if o.Duration < 0 {
return fmt.Errorf("--duration must be greater than or equal to 0")
}
if o.Duration%time.Second != 0 {
return fmt.Errorf("--duration cannot be expressed in units less than seconds")

View File

@ -219,7 +219,7 @@ status:
test: "invalid duration",
name: "mysa",
duration: -1,
expectStderr: `error: --duration must be positive`,
expectStderr: `error: --duration must be greater than or equal to 0`,
},
{
test: "invalid duration unit",
@ -243,7 +243,22 @@ status:
serverResponseToken: "abc",
expectStdout: "abc",
},
{
test: "zero duration act as default",
name: "mysa",
duration: 0 * time.Second,
expectRequestPath: "/api/v1/namespaces/test/serviceaccounts/mysa/token",
expectTokenRequest: &authenticationv1.TokenRequest{
TypeMeta: metav1.TypeMeta{APIVersion: "authentication.k8s.io/v1", Kind: "TokenRequest"},
Spec: authenticationv1.TokenRequestSpec{
ExpirationSeconds: nil,
},
},
serverResponseToken: "abc",
expectStdout: "abc",
},
{
test: "server error",
name: "mysa",