Skip to content

Commit e56b4c3

Browse files
committed
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.
1 parent 5317a31 commit e56b4c3

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

cmd/kubeadm/app/cmd/join.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"os"
23+
"strings"
2324
"text/template"
2425

2526
"github.com/lithammer/dedent"
@@ -127,6 +128,7 @@ type joinOptions struct {
127128
controlPlane bool
128129
ignorePreflightErrors []string
129130
externalcfg *kubeadmapiv1beta2.JoinConfiguration
131+
joinControlPlane *kubeadmapiv1beta2.JoinControlPlane
130132
kustomizeDir string
131133
}
132134

@@ -199,7 +201,7 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
199201
Args: cobra.MaximumNArgs(1),
200202
}
201203

202-
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg)
204+
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg, joinOptions.joinControlPlane)
203205
addJoinOtherFlags(cmd.Flags(), joinOptions)
204206

205207
joinRunner.AppendPhase(phases.NewPreflightPhase())
@@ -222,22 +224,22 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
222224
}
223225

224226
// addJoinConfigFlags adds join flags bound to the config to the specified flagset
225-
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1beta2.JoinConfiguration) {
227+
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1beta2.JoinConfiguration, jcp *kubeadmapiv1beta2.JoinControlPlane) {
226228
flagSet.StringVar(
227229
&cfg.NodeRegistration.Name, options.NodeName, cfg.NodeRegistration.Name,
228230
`Specify the node name.`,
229231
)
230232
flagSet.StringVar(
231-
&cfg.ControlPlane.CertificateKey, options.CertificateKey, "",
233+
&jcp.CertificateKey, options.CertificateKey, jcp.CertificateKey,
232234
"Use this key to decrypt the certificate secrets uploaded by init.",
233235
)
234236
// add control plane endpoint flags to the specified flagset
235237
flagSet.StringVar(
236-
&cfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, cfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress,
238+
&jcp.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, jcp.LocalAPIEndpoint.AdvertiseAddress,
237239
"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.",
238240
)
239241
flagSet.Int32Var(
240-
&cfg.ControlPlane.LocalAPIEndpoint.BindPort, options.APIServerBindPort, cfg.ControlPlane.LocalAPIEndpoint.BindPort,
242+
&jcp.LocalAPIEndpoint.BindPort, options.APIServerBindPort, jcp.LocalAPIEndpoint.BindPort,
241243
"If the node should host a new control plane instance, the port for the API Server to bind to.",
242244
)
243245
// adds bootstrap token specific discovery flags to the specified flagset
@@ -297,18 +299,29 @@ func newJoinOptions() *joinOptions {
297299
externalcfg.Discovery.BootstrapToken = &kubeadmapiv1beta2.BootstrapTokenDiscovery{}
298300
externalcfg.ControlPlane = &kubeadmapiv1beta2.JoinControlPlane{}
299301

302+
// This object is used for storage of control-plane flags.
303+
joinControlPlane := &kubeadmapiv1beta2.JoinControlPlane{}
304+
300305
// Apply defaults
301306
kubeadmscheme.Scheme.Default(externalcfg)
307+
kubeadmapiv1beta2.SetDefaults_JoinControlPlane(joinControlPlane)
302308

303309
return &joinOptions{
304-
externalcfg: externalcfg,
310+
externalcfg: externalcfg,
311+
joinControlPlane: joinControlPlane,
305312
}
306313
}
307314

308315
// newJoinData returns a new joinData struct to be used for the execution of the kubeadm join workflow.
309316
// This func takes care of validating joinOptions passed to the command, and then it converts
310317
// options into the internal JoinConfiguration type that is used as input all the phases in the kubeadm join workflow
311318
func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Writer) (*joinData, error) {
319+
320+
// Validate the mixed arguments with --config and return early on errors
321+
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
322+
return nil, err
323+
}
324+
312325
// Re-apply defaults to the public kubeadm API (this will set only values not exposed/not set as a flags)
313326
kubeadmscheme.Scheme.Default(opt.externalcfg)
314327

@@ -340,10 +353,26 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
340353
opt.externalcfg.Discovery.BootstrapToken.APIServerEndpoint = args[0]
341354
}
342355

343-
// if not joining a control plane, unset the ControlPlane object
356+
// Include the JoinControlPlane with user flags
357+
opt.externalcfg.ControlPlane = opt.joinControlPlane
358+
359+
// If not passing --control-plane, unset the ControlPlane object
344360
if !opt.controlPlane {
345-
if opt.externalcfg.ControlPlane != nil {
346-
klog.Warningf("[preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when %s flag is not set.", options.ControlPlane)
361+
// Use a defaulted JoinControlPlane object to detect if the user has passed
362+
// other control-plane related flags.
363+
defaultJCP := &kubeadmapiv1beta2.JoinControlPlane{}
364+
kubeadmapiv1beta2.SetDefaults_JoinControlPlane(defaultJCP)
365+
366+
// This list must match the JCP flags in addJoinConfigFlags()
367+
joinControlPlaneFlags := []string{
368+
options.CertificateKey,
369+
options.APIServerAdvertiseAddress,
370+
options.APIServerBindPort,
371+
}
372+
373+
if *opt.joinControlPlane != *defaultJCP {
374+
klog.Warningf("[preflight] WARNING: --%s is also required when passing control-plane "+
375+
"related flags such as [%s]", options.ControlPlane, strings.Join(joinControlPlaneFlags, ", "))
347376
}
348377
opt.externalcfg.ControlPlane = nil
349378
}
@@ -361,10 +390,6 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
361390
}
362391
}
363392

364-
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
365-
return nil, err
366-
}
367-
368393
// Either use the config file if specified, or convert public kubeadm API to the internal JoinConfiguration
369394
// and validates JoinConfiguration
370395
if opt.externalcfg.NodeRegistration.Name == "" {

0 commit comments

Comments
 (0)