forked from k3s-io/k3s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
52 lines (44 loc) · 1.17 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package kubeadm
import (
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/version"
"github.com/pkg/errors"
"github.com/urfave/cli"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
bootstraputil "k8s.io/cluster-bootstrap/token/util"
)
var (
NodeBootstrapTokenAuthGroup = "system:bootstrappers:" + version.Program + ":default-node-token"
)
// SetDefaults ensures that the default values are set on the token configuration.
// These are set here, rather than in the default Token struct, to avoid
// importing the cluster-bootstrap packages into the CLI.
func SetDefaults(clx *cli.Context, cfg *cmds.Token) error {
if !clx.IsSet("groups") {
cfg.Groups = []string{NodeBootstrapTokenAuthGroup}
}
if !clx.IsSet("usages") {
cfg.Usages = bootstrapapi.KnownTokenUsages
}
if cfg.Output == "" {
cfg.Output = "text"
} else {
switch cfg.Output {
case "text", "json", "yaml":
default:
return errors.New("invalid output format: " + cfg.Output)
}
}
args := clx.Args()
if len(args) > 0 {
cfg.Token = args[0]
}
if cfg.Token == "" {
var err error
cfg.Token, err = bootstraputil.GenerateBootstrapToken()
if err != nil {
return err
}
}
return nil
}