Skip to content

Commit

Permalink
Switch from using a function to just pure map in create token
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Szulik <soltysh@gmail.com>

Kubernetes-commit: 87139335b0e6ce5f5bc08d860a870fb9f16392b2
  • Loading branch information
soltysh authored and k8s-publishing-bot committed Jan 23, 2025
1 parent 1ab40ed commit d74f0e1
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions pkg/cmd/create/create_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,10 @@ var (
`)
)

func boundObjectKindToAPIVersions() map[string]string {
kinds := map[string]string{
"Pod": "v1",
"Secret": "v1",
"Node": "v1",
}

return kinds
var boundObjectKinds = map[string]string{
"Pod": "v1",
"Secret": "v1",
"Node": "v1",
}

func NewTokenOpts(ioStreams genericiooptions.IOStreams) *TokenOptions {
Expand Down Expand Up @@ -149,7 +145,7 @@ func NewCmdCreateToken(f cmdutil.Factory, ioStreams genericiooptions.IOStreams)
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.List(sets.KeySet(boundObjectKindToAPIVersions())), ", ")+". "+
"Supported kinds are "+strings.Join(sets.List(sets.KeySet(boundObjectKinds)), ", ")+". "+
"If set, --bound-object-name must be provided.")
cmd.Flags().StringVar(&o.BoundObjectName, "bound-object-name", o.BoundObjectName, "Name of an object to bind the token to. "+
"The token will expire when the object is deleted. "+
Expand Down Expand Up @@ -226,8 +222,8 @@ func (o *TokenOptions) Validate() error {
return fmt.Errorf("--bound-object-uid can only be set if --bound-object-kind is provided")
}
} else {
if _, ok := boundObjectKindToAPIVersions()[o.BoundObjectKind]; !ok {
return fmt.Errorf("supported --bound-object-kind values are %s", strings.Join(sets.List(sets.KeySet(boundObjectKindToAPIVersions())), ", "))
if _, ok := boundObjectKinds[o.BoundObjectKind]; !ok {
return fmt.Errorf("supported --bound-object-kind values are %s", strings.Join(sets.List(sets.KeySet(boundObjectKinds)), ", "))
}
if len(o.BoundObjectName) == 0 {
return fmt.Errorf("--bound-object-name is required if --bound-object-kind is provided")
Expand All @@ -250,7 +246,7 @@ func (o *TokenOptions) Run() error {
if len(o.BoundObjectKind) > 0 {
request.Spec.BoundObjectRef = &authenticationv1.BoundObjectReference{
Kind: o.BoundObjectKind,
APIVersion: boundObjectKindToAPIVersions()[o.BoundObjectKind],
APIVersion: boundObjectKinds[o.BoundObjectKind],
Name: o.BoundObjectName,
UID: types.UID(o.BoundObjectUID),
}
Expand Down

0 comments on commit d74f0e1

Please sign in to comment.