Merge pull request #113320 from chendave/test

kubeadm: simply struct of `joinOptions` by removing `joinControlPlane`
This commit is contained in:
Kubernetes Prow Robot 2022-11-01 02:48:45 -07:00 committed by GitHub
commit 22f3e64039
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 12 deletions

View File

@ -133,7 +133,6 @@ type joinOptions struct {
controlPlane bool
ignorePreflightErrors []string
externalcfg *kubeadmapiv1.JoinConfiguration
joinControlPlane *kubeadmapiv1.JoinControlPlane
patchesDir string
dryRun bool
}
@ -210,7 +209,7 @@ func newCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
Args: cobra.MaximumNArgs(1),
}
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg, joinOptions.joinControlPlane)
addJoinConfigFlags(cmd.Flags(), joinOptions.externalcfg)
addJoinOtherFlags(cmd.Flags(), joinOptions)
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
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1.JoinConfiguration, jcp *kubeadmapiv1.JoinControlPlane) {
func addJoinConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1.JoinConfiguration) {
flagSet.StringVar(
&cfg.NodeRegistration.Name, options.NodeName, cfg.NodeRegistration.Name,
`Specify the node name.`,
)
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.",
)
// add control plane endpoint flags to the specified flagset
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.",
)
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.",
)
// adds bootstrap token specific discovery flags to the specified flagset
@ -325,8 +324,7 @@ func newJoinOptions() *joinOptions {
kubeadmapiv1.SetDefaults_JoinControlPlane(joinControlPlane)
return &joinOptions{
externalcfg: externalcfg,
joinControlPlane: joinControlPlane,
externalcfg: externalcfg,
}
}
@ -371,9 +369,6 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
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 !opt.controlPlane {
// 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,
}
if *opt.joinControlPlane != *defaultJCP {
if *opt.externalcfg.ControlPlane != *defaultJCP {
klog.Warningf("[preflight] WARNING: --%s is also required when passing control-plane "+
"related flags such as [%s]", options.ControlPlane, strings.Join(joinControlPlaneFlags, ", "))
}

View File

@ -17,12 +17,15 @@ limitations under the License.
package cmd
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
@ -36,6 +39,8 @@ discovery:
token: abcdef.0123456789abcdef
apiServerEndpoint: 1.2.3.4:6443
unsafeSkipCAVerification: true
controlPlane:
certificateKey: c39a18bae4a72e71b178661f437363da218a3efb83ddb03f1cd91d9ae1da41bd
nodeRegistration:
criSocket: /run/containerd/containerd.sock
name: someName
@ -74,6 +79,7 @@ func TestNewJoinData(t *testing.T) {
flags map[string]string
validate func(*testing.T, *joinData)
expectError bool
expectWarn bool
}{
// Join data passed using flags
{
@ -184,6 +190,7 @@ func TestNewJoinData(t *testing.T) {
t.Errorf("Invalid ControlPlane")
}
},
expectWarn: true,
},
{
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")),
},
{
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 {
t.Run(tc.name, func(t *testing.T) {
@ -253,6 +276,12 @@ func TestNewJoinData(t *testing.T) {
joinOptions := newJoinOptions()
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)
for f, v := range tc.flags {
cmd.Flags().Set(f, v)
@ -260,6 +289,17 @@ func TestNewJoinData(t *testing.T) {
// test newJoinData method
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 {
t.Fatalf("newJoinData returned unexpected error: %v", err)
}