kubeadm: fix unit test requiring admin.conf and root

This commit is contained in:
Lubomir I. Ivanov 2020-03-27 23:28:33 +02:00
parent e56b4c3172
commit 63b3bd1826
3 changed files with 10 additions and 4 deletions

View File

@ -87,6 +87,7 @@ go_test(
"//cmd/kubeadm/app/constants:go_default_library",
"//cmd/kubeadm/app/util:go_default_library",
"//cmd/kubeadm/app/util/config:go_default_library",
"//cmd/kubeadm/app/util/kubeconfig:go_default_library",
"//cmd/kubeadm/app/util/output:go_default_library",
"//cmd/kubeadm/app/util/runtime:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",

View File

@ -213,7 +213,7 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
// sets the data builder function, that will be used by the runner
// both when running the entire workflow or single phases
joinRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
return newJoinData(cmd, args, joinOptions, out)
return newJoinData(cmd, args, joinOptions, out, kubeadmconstants.GetAdminKubeConfigPath())
})
// binds the Runner to kubeadm join command by altering
@ -315,7 +315,7 @@ func newJoinOptions() *joinOptions {
// newJoinData returns a new joinData struct to be used for the execution of the kubeadm join workflow.
// 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) {
func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Writer, adminKubeConfigPath string) (*joinData, error) {
// Validate the mixed arguments with --config and return early on errors
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
@ -379,7 +379,6 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
// if the admin.conf file already exists, use it for skipping the discovery process.
// NB. this case can happen when we are joining a control-plane node only (and phases are invoked atomically)
var adminKubeConfigPath = kubeadmconstants.GetAdminKubeConfigPath()
var tlsBootstrapCfg *clientcmdapi.Config
if _, err := os.Stat(adminKubeConfigPath); err == nil && opt.controlPlane {
// use the admin.conf as tlsBootstrapCfg, that is the kubeconfig file used for reading the kubeadm-config during discovery

View File

@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
)
const (
@ -51,6 +52,11 @@ func TestNewJoinData(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
// create kubeconfig
kubeconfigFilePath := filepath.Join(tmpDir, "test-kubeconfig-file")
kubeconfig := kubeconfigutil.CreateBasic("", "", "", []byte{})
kubeconfigutil.WriteToDisk(kubeconfigFilePath, kubeconfig)
// create config file
configFilePath := filepath.Join(tmpDir, "test-config-file")
cfgFile, err := os.Create(configFilePath)
@ -257,7 +263,7 @@ func TestNewJoinData(t *testing.T) {
}
// test newJoinData method
data, err := newJoinData(cmd, tc.args, joinOptions, nil)
data, err := newJoinData(cmd, tc.args, joinOptions, nil, kubeconfigFilePath)
if err != nil && !tc.expectError {
t.Fatalf("newJoinData returned unexpected error: %v", err)
}