...
1
16
17 package options
18
19 import (
20 "fmt"
21 "strings"
22
23 "github.com/spf13/pflag"
24
25 bootstrapapi "k8s.io/cluster-bootstrap/token/api"
26
27 bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1"
28 kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
29 )
30
31
32 func NewBootstrapTokenOptions() *BootstrapTokenOptions {
33 bto := &BootstrapTokenOptions{&bootstraptokenv1.BootstrapToken{}, ""}
34 bootstraptokenv1.SetDefaults_BootstrapToken(bto.BootstrapToken)
35 return bto
36 }
37
38
39
40
41 type BootstrapTokenOptions struct {
42 *bootstraptokenv1.BootstrapToken
43 TokenStr string `datapolicy:"token"`
44 }
45
46
47 func (bto *BootstrapTokenOptions) AddTokenFlag(fs *pflag.FlagSet) {
48 fs.StringVar(
49 &bto.TokenStr, TokenStr, "",
50 "The token to use for establishing bidirectional trust between nodes and control-plane nodes. The format is [a-z0-9]{6}\\.[a-z0-9]{16} - e.g. abcdef.0123456789abcdef",
51 )
52 }
53
54
55 func (bto *BootstrapTokenOptions) AddTTLFlag(fs *pflag.FlagSet) {
56 bto.AddTTLFlagWithName(fs, TokenTTL)
57 }
58
59
60 func (bto *BootstrapTokenOptions) AddTTLFlagWithName(fs *pflag.FlagSet, flagName string) {
61 fs.DurationVar(
62 &bto.TTL.Duration, flagName, bto.TTL.Duration,
63 "The duration before the token is automatically deleted (e.g. 1s, 2m, 3h). If set to '0', the token will never expire",
64 )
65 }
66
67
68 func (bto *BootstrapTokenOptions) AddUsagesFlag(fs *pflag.FlagSet) {
69 fs.StringSliceVar(
70 &bto.Usages, TokenUsages, bto.Usages,
71 fmt.Sprintf("Describes the ways in which this token can be used. You can pass --usages multiple times or provide a comma separated list of options. Valid options: [%s]", strings.Join(bootstraptokenv1.DefaultTokenUsages, ",")),
72 )
73 }
74
75
76 func (bto *BootstrapTokenOptions) AddGroupsFlag(fs *pflag.FlagSet) {
77 fs.StringSliceVar(
78 &bto.Groups, TokenGroups, bto.Groups,
79 fmt.Sprintf("Extra groups that this token will authenticate as when used for authentication. Must match %q", bootstrapapi.BootstrapGroupPattern),
80 )
81 }
82
83
84 func (bto *BootstrapTokenOptions) AddDescriptionFlag(fs *pflag.FlagSet) {
85 fs.StringVar(
86 &bto.Description, TokenDescription, bto.Description,
87 "A human friendly description of how this token is used.",
88 )
89 }
90
91
92
93 func (bto *BootstrapTokenOptions) ApplyTo(cfg *kubeadmapiv1.InitConfiguration) error {
94 if len(bto.TokenStr) > 0 {
95 var err error
96 bto.Token, err = bootstraptokenv1.NewBootstrapTokenString(bto.TokenStr)
97 if err != nil {
98 return err
99 }
100 }
101
102
103 cfg.BootstrapTokens = []bootstraptokenv1.BootstrapToken{*bto.BootstrapToken}
104 return nil
105 }
106
View as plain text