...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/cmd/options/token.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/cmd/options

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // NewBootstrapTokenOptions creates a new BootstrapTokenOptions object with the default values
    32  func NewBootstrapTokenOptions() *BootstrapTokenOptions {
    33  	bto := &BootstrapTokenOptions{&bootstraptokenv1.BootstrapToken{}, ""}
    34  	bootstraptokenv1.SetDefaults_BootstrapToken(bto.BootstrapToken)
    35  	return bto
    36  }
    37  
    38  // BootstrapTokenOptions is a wrapper struct for adding bootstrap token-related flags to a FlagSet
    39  // and applying the parsed flags to a InitConfiguration object later at runtime
    40  // TODO: In the future, we might want to group the flags in a better way than adding them all individually like this
    41  type BootstrapTokenOptions struct {
    42  	*bootstraptokenv1.BootstrapToken
    43  	TokenStr string `datapolicy:"token"`
    44  }
    45  
    46  // AddTokenFlag adds the --token flag to the given flagset
    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  // AddTTLFlag adds the --token-ttl flag to the given flagset
    55  func (bto *BootstrapTokenOptions) AddTTLFlag(fs *pflag.FlagSet) {
    56  	bto.AddTTLFlagWithName(fs, TokenTTL)
    57  }
    58  
    59  // AddTTLFlagWithName adds the --token-ttl flag with a custom flag name given flagset
    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  // AddUsagesFlag adds the --usages flag to the given flagset
    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  // AddGroupsFlag adds the --groups flag to the given flagset
    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  // AddDescriptionFlag adds the --description flag to the given flagset
    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  // ApplyTo applies the values set internally in the BootstrapTokenOptions object to a InitConfiguration object at runtime
    92  // If --token was specified in the CLI (as a string), it's parsed and validated before it's added to the BootstrapToken object.
    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  	// Set the token specified by the flags as the first and only token to create in case --config is not specified
   103  	cfg.BootstrapTokens = []bootstraptokenv1.BootstrapToken{*bto.BootstrapToken}
   104  	return nil
   105  }
   106  

View as plain text