kubeadm: remove misleading warning on kubeadm join

If the user does not provide --config or --control-plane
but provides some other flags such as --certificate-key
kubeadm is supposed to print a warning.

The logic around printing the warning is bogus. Implement
proper checks of when to print the warning.
This commit is contained in:
Lubomir I. Ivanov
2020-03-27 22:48:54 +02:00
parent 5317a3160c
commit e56b4c3172

View File

@@ -20,6 +20,7 @@ import (
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/lithammer/dedent"
@@ -127,6 +128,7 @@ type joinOptions struct {
controlPlane bool
ignorePreflightErrors []string
externalcfg *kubeadmapiv1beta2.JoinConfiguration
joinControlPlane *kubeadmapiv1beta2.JoinControlPlane
kustomizeDir string
}
@@ -199,7 +201,7 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
Args: cobra.MaximumNArgs(1),
}
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg)
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg, joinOptions.joinControlPlane)
addJoinOtherFlags(cmd.Flags(), joinOptions)
joinRunner.AppendPhase(phases.NewPreflightPhase())
@@ -222,22 +224,22 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
}
// addJoinConfigFlags adds join flags bound to the config to the specified flagset
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1beta2.JoinConfiguration) {
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1beta2.JoinConfiguration, jcp *kubeadmapiv1beta2.JoinControlPlane) {
flagSet.StringVar(
&cfg.NodeRegistration.Name, options.NodeName, cfg.NodeRegistration.Name,
`Specify the node name.`,
)
flagSet.StringVar(
&cfg.ControlPlane.CertificateKey, options.CertificateKey, "",
&jcp.CertificateKey, options.CertificateKey, jcp.CertificateKey,
"Use this key to decrypt the certificate secrets uploaded by init.",
)
// add control plane endpoint flags to the specified flagset
flagSet.StringVar(
&cfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, cfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress,
&jcp.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, jcp.LocalAPIEndpoint.AdvertiseAddress,
"If the node should host a new control plane instance, the IP address the API Server will advertise it's listening on. If not set the default network interface will be used.",
)
flagSet.Int32Var(
&cfg.ControlPlane.LocalAPIEndpoint.BindPort, options.APIServerBindPort, cfg.ControlPlane.LocalAPIEndpoint.BindPort,
&jcp.LocalAPIEndpoint.BindPort, options.APIServerBindPort, jcp.LocalAPIEndpoint.BindPort,
"If the node should host a new control plane instance, the port for the API Server to bind to.",
)
// adds bootstrap token specific discovery flags to the specified flagset
@@ -297,11 +299,16 @@ func newJoinOptions() *joinOptions {
externalcfg.Discovery.BootstrapToken = &kubeadmapiv1beta2.BootstrapTokenDiscovery{}
externalcfg.ControlPlane = &kubeadmapiv1beta2.JoinControlPlane{}
// This object is used for storage of control-plane flags.
joinControlPlane := &kubeadmapiv1beta2.JoinControlPlane{}
// Apply defaults
kubeadmscheme.Scheme.Default(externalcfg)
kubeadmapiv1beta2.SetDefaults_JoinControlPlane(joinControlPlane)
return &joinOptions{
externalcfg: externalcfg,
externalcfg: externalcfg,
joinControlPlane: joinControlPlane,
}
}
@@ -309,6 +316,12 @@ func newJoinOptions() *joinOptions {
// This func takes care of validating joinOptions passed to the command, and then it converts
// options into the internal JoinConfiguration type that is used as input all the phases in the kubeadm join workflow
func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Writer) (*joinData, error) {
// Validate the mixed arguments with --config and return early on errors
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
return nil, err
}
// Re-apply defaults to the public kubeadm API (this will set only values not exposed/not set as a flags)
kubeadmscheme.Scheme.Default(opt.externalcfg)
@@ -340,10 +353,26 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
opt.externalcfg.Discovery.BootstrapToken.APIServerEndpoint = args[0]
}
// if not joining a control plane, unset the ControlPlane object
// Include the JoinControlPlane with user flags
opt.externalcfg.ControlPlane = opt.joinControlPlane
// If not passing --control-plane, unset the ControlPlane object
if !opt.controlPlane {
if opt.externalcfg.ControlPlane != nil {
klog.Warningf("[preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when %s flag is not set.", options.ControlPlane)
// Use a defaulted JoinControlPlane object to detect if the user has passed
// other control-plane related flags.
defaultJCP := &kubeadmapiv1beta2.JoinControlPlane{}
kubeadmapiv1beta2.SetDefaults_JoinControlPlane(defaultJCP)
// This list must match the JCP flags in addJoinConfigFlags()
joinControlPlaneFlags := []string{
options.CertificateKey,
options.APIServerAdvertiseAddress,
options.APIServerBindPort,
}
if *opt.joinControlPlane != *defaultJCP {
klog.Warningf("[preflight] WARNING: --%s is also required when passing control-plane "+
"related flags such as [%s]", options.ControlPlane, strings.Join(joinControlPlaneFlags, ", "))
}
opt.externalcfg.ControlPlane = nil
}
@@ -361,10 +390,6 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
}
}
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
return nil, err
}
// Either use the config file if specified, or convert public kubeadm API to the internal JoinConfiguration
// and validates JoinConfiguration
if opt.externalcfg.NodeRegistration.Name == "" {