mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
kubeadm: simply struct of joinOptions
by removing joinControlPlane
Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
parent
48608cfe60
commit
d4fd5b77aa
@ -133,7 +133,6 @@ type joinOptions struct {
|
|||||||
controlPlane bool
|
controlPlane bool
|
||||||
ignorePreflightErrors []string
|
ignorePreflightErrors []string
|
||||||
externalcfg *kubeadmapiv1.JoinConfiguration
|
externalcfg *kubeadmapiv1.JoinConfiguration
|
||||||
joinControlPlane *kubeadmapiv1.JoinControlPlane
|
|
||||||
patchesDir string
|
patchesDir string
|
||||||
dryRun bool
|
dryRun bool
|
||||||
}
|
}
|
||||||
@ -210,7 +209,7 @@ func newCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
|
|||||||
Args: cobra.MaximumNArgs(1),
|
Args: cobra.MaximumNArgs(1),
|
||||||
}
|
}
|
||||||
|
|
||||||
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg, joinOptions.joinControlPlane)
|
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg)
|
||||||
addJoinOtherFlags(cmd.Flags(), joinOptions)
|
addJoinOtherFlags(cmd.Flags(), joinOptions)
|
||||||
|
|
||||||
joinRunner.AppendPhase(phases.NewPreflightPhase())
|
joinRunner.AppendPhase(phases.NewPreflightPhase())
|
||||||
@ -241,22 +240,22 @@ func newCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// addJoinConfigFlags adds join flags bound to the config to the specified flagset
|
// addJoinConfigFlags adds join flags bound to the config to the specified flagset
|
||||||
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1.JoinConfiguration, jcp *kubeadmapiv1.JoinControlPlane) {
|
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1.JoinConfiguration) {
|
||||||
flagSet.StringVar(
|
flagSet.StringVar(
|
||||||
&cfg.NodeRegistration.Name, options.NodeName, cfg.NodeRegistration.Name,
|
&cfg.NodeRegistration.Name, options.NodeName, cfg.NodeRegistration.Name,
|
||||||
`Specify the node name.`,
|
`Specify the node name.`,
|
||||||
)
|
)
|
||||||
flagSet.StringVar(
|
flagSet.StringVar(
|
||||||
&jcp.CertificateKey, options.CertificateKey, jcp.CertificateKey,
|
&cfg.ControlPlane.CertificateKey, options.CertificateKey, cfg.ControlPlane.CertificateKey,
|
||||||
"Use this key to decrypt the certificate secrets uploaded by init.",
|
"Use this key to decrypt the certificate secrets uploaded by init.",
|
||||||
)
|
)
|
||||||
// add control plane endpoint flags to the specified flagset
|
// add control plane endpoint flags to the specified flagset
|
||||||
flagSet.StringVar(
|
flagSet.StringVar(
|
||||||
&jcp.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, jcp.LocalAPIEndpoint.AdvertiseAddress,
|
&cfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress, options.APIServerAdvertiseAddress, cfg.ControlPlane.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.",
|
"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(
|
flagSet.Int32Var(
|
||||||
&jcp.LocalAPIEndpoint.BindPort, options.APIServerBindPort, jcp.LocalAPIEndpoint.BindPort,
|
&cfg.ControlPlane.LocalAPIEndpoint.BindPort, options.APIServerBindPort, cfg.ControlPlane.LocalAPIEndpoint.BindPort,
|
||||||
"If the node should host a new control plane instance, the port for the API Server to bind to.",
|
"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
|
// adds bootstrap token specific discovery flags to the specified flagset
|
||||||
@ -326,7 +325,6 @@ func newJoinOptions() *joinOptions {
|
|||||||
|
|
||||||
return &joinOptions{
|
return &joinOptions{
|
||||||
externalcfg: externalcfg,
|
externalcfg: externalcfg,
|
||||||
joinControlPlane: joinControlPlane,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,9 +369,6 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
|
|||||||
opt.externalcfg.Discovery.BootstrapToken.APIServerEndpoint = args[0]
|
opt.externalcfg.Discovery.BootstrapToken.APIServerEndpoint = args[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include the JoinControlPlane with user flags
|
|
||||||
opt.externalcfg.ControlPlane = opt.joinControlPlane
|
|
||||||
|
|
||||||
// If not passing --control-plane, unset the ControlPlane object
|
// If not passing --control-plane, unset the ControlPlane object
|
||||||
if !opt.controlPlane {
|
if !opt.controlPlane {
|
||||||
// Use a defaulted JoinControlPlane object to detect if the user has passed
|
// Use a defaulted JoinControlPlane object to detect if the user has passed
|
||||||
@ -388,7 +383,7 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
|
|||||||
options.APIServerBindPort,
|
options.APIServerBindPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
if *opt.joinControlPlane != *defaultJCP {
|
if *opt.externalcfg.ControlPlane != *defaultJCP {
|
||||||
klog.Warningf("[preflight] WARNING: --%s is also required when passing control-plane "+
|
klog.Warningf("[preflight] WARNING: --%s is also required when passing control-plane "+
|
||||||
"related flags such as [%s]", options.ControlPlane, strings.Join(joinControlPlaneFlags, ", "))
|
"related flags such as [%s]", options.ControlPlane, strings.Join(joinControlPlaneFlags, ", "))
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,15 @@ limitations under the License.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
||||||
@ -36,6 +39,8 @@ discovery:
|
|||||||
token: abcdef.0123456789abcdef
|
token: abcdef.0123456789abcdef
|
||||||
apiServerEndpoint: 1.2.3.4:6443
|
apiServerEndpoint: 1.2.3.4:6443
|
||||||
unsafeSkipCAVerification: true
|
unsafeSkipCAVerification: true
|
||||||
|
controlPlane:
|
||||||
|
certificateKey: c39a18bae4a72e71b178661f437363da218a3efb83ddb03f1cd91d9ae1da41bd
|
||||||
nodeRegistration:
|
nodeRegistration:
|
||||||
criSocket: /run/containerd/containerd.sock
|
criSocket: /run/containerd/containerd.sock
|
||||||
name: someName
|
name: someName
|
||||||
@ -74,6 +79,7 @@ func TestNewJoinData(t *testing.T) {
|
|||||||
flags map[string]string
|
flags map[string]string
|
||||||
validate func(*testing.T, *joinData)
|
validate func(*testing.T, *joinData)
|
||||||
expectError bool
|
expectError bool
|
||||||
|
expectWarn bool
|
||||||
}{
|
}{
|
||||||
// Join data passed using flags
|
// Join data passed using flags
|
||||||
{
|
{
|
||||||
@ -184,6 +190,7 @@ func TestNewJoinData(t *testing.T) {
|
|||||||
t.Errorf("Invalid ControlPlane")
|
t.Errorf("Invalid ControlPlane")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
expectWarn: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "fails if invalid preflight checks are provided",
|
name: "fails if invalid preflight checks are provided",
|
||||||
@ -246,6 +253,22 @@ func TestNewJoinData(t *testing.T) {
|
|||||||
},
|
},
|
||||||
validate: expectedJoinIgnorePreflightErrors(sets.NewString("a", "b", "c", "d")),
|
validate: expectedJoinIgnorePreflightErrors(sets.NewString("a", "b", "c", "d")),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "warn if --control-plane flag is not set",
|
||||||
|
flags: map[string]string{
|
||||||
|
options.APIServerBindPort: "8888",
|
||||||
|
options.FileDiscovery: "https://foo", //required only to pass discovery validation
|
||||||
|
},
|
||||||
|
expectWarn: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no warn if --control-plane flag is set",
|
||||||
|
flags: map[string]string{
|
||||||
|
options.APIServerBindPort: "8888",
|
||||||
|
options.FileDiscovery: "https://bar", //required only to pass discovery validation
|
||||||
|
options.ControlPlane: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
@ -253,6 +276,12 @@ func TestNewJoinData(t *testing.T) {
|
|||||||
joinOptions := newJoinOptions()
|
joinOptions := newJoinOptions()
|
||||||
cmd := newCmdJoin(nil, joinOptions)
|
cmd := newCmdJoin(nil, joinOptions)
|
||||||
|
|
||||||
|
// set klog output destination to bytes.Buffer so that log could be fetched and verified later.
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
klog.SetOutput(&buffer)
|
||||||
|
klog.LogToStderr(false)
|
||||||
|
defer klog.LogToStderr(true)
|
||||||
|
|
||||||
// sets cmd flags (that will be reflected on the join options)
|
// sets cmd flags (that will be reflected on the join options)
|
||||||
for f, v := range tc.flags {
|
for f, v := range tc.flags {
|
||||||
cmd.Flags().Set(f, v)
|
cmd.Flags().Set(f, v)
|
||||||
@ -260,6 +289,17 @@ func TestNewJoinData(t *testing.T) {
|
|||||||
|
|
||||||
// test newJoinData method
|
// test newJoinData method
|
||||||
data, err := newJoinData(cmd, tc.args, joinOptions, nil, kubeconfigFilePath)
|
data, err := newJoinData(cmd, tc.args, joinOptions, nil, kubeconfigFilePath)
|
||||||
|
klog.Flush()
|
||||||
|
msg := "WARNING: --control-plane is also required when passing control-plane"
|
||||||
|
if tc.expectWarn {
|
||||||
|
if !strings.Contains(buffer.String(), msg) {
|
||||||
|
t.Errorf("Haven't detected the warning message, expected: %v, actual: %v", msg, buffer.String())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if strings.Contains(buffer.String(), msg) {
|
||||||
|
t.Errorf("Expect no such warning message: %v, but got: %v", msg, buffer.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil && !tc.expectError {
|
if err != nil && !tc.expectError {
|
||||||
t.Fatalf("newJoinData returned unexpected error: %v", err)
|
t.Fatalf("newJoinData returned unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user