Skip to content

Commit 63b3bd1

Browse files
committed
kubeadm: fix unit test requiring admin.conf and root
1 parent e56b4c3 commit 63b3bd1

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

cmd/kubeadm/app/cmd/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ go_test(
8787
"//cmd/kubeadm/app/constants:go_default_library",
8888
"//cmd/kubeadm/app/util:go_default_library",
8989
"//cmd/kubeadm/app/util/config:go_default_library",
90+
"//cmd/kubeadm/app/util/kubeconfig:go_default_library",
9091
"//cmd/kubeadm/app/util/output:go_default_library",
9192
"//cmd/kubeadm/app/util/runtime:go_default_library",
9293
"//staging/src/k8s.io/api/core/v1:go_default_library",

cmd/kubeadm/app/cmd/join.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
213213
// sets the data builder function, that will be used by the runner
214214
// both when running the entire workflow or single phases
215215
joinRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
216-
return newJoinData(cmd, args, joinOptions, out)
216+
return newJoinData(cmd, args, joinOptions, out, kubeadmconstants.GetAdminKubeConfigPath())
217217
})
218218

219219
// binds the Runner to kubeadm join command by altering
@@ -315,7 +315,7 @@ func newJoinOptions() *joinOptions {
315315
// newJoinData returns a new joinData struct to be used for the execution of the kubeadm join workflow.
316316
// This func takes care of validating joinOptions passed to the command, and then it converts
317317
// options into the internal JoinConfiguration type that is used as input all the phases in the kubeadm join workflow
318-
func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Writer) (*joinData, error) {
318+
func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Writer, adminKubeConfigPath string) (*joinData, error) {
319319

320320
// Validate the mixed arguments with --config and return early on errors
321321
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
@@ -379,7 +379,6 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
379379

380380
// if the admin.conf file already exists, use it for skipping the discovery process.
381381
// NB. this case can happen when we are joining a control-plane node only (and phases are invoked atomically)
382-
var adminKubeConfigPath = kubeadmconstants.GetAdminKubeConfigPath()
383382
var tlsBootstrapCfg *clientcmdapi.Config
384383
if _, err := os.Stat(adminKubeConfigPath); err == nil && opt.controlPlane {
385384
// use the admin.conf as tlsBootstrapCfg, that is the kubeconfig file used for reading the kubeadm-config during discovery

cmd/kubeadm/app/cmd/join_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"k8s.io/apimachinery/pkg/util/sets"
2626
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
27+
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
2728
)
2829

2930
const (
@@ -51,6 +52,11 @@ func TestNewJoinData(t *testing.T) {
5152
}
5253
defer os.RemoveAll(tmpDir)
5354

55+
// create kubeconfig
56+
kubeconfigFilePath := filepath.Join(tmpDir, "test-kubeconfig-file")
57+
kubeconfig := kubeconfigutil.CreateBasic("", "", "", []byte{})
58+
kubeconfigutil.WriteToDisk(kubeconfigFilePath, kubeconfig)
59+
5460
// create config file
5561
configFilePath := filepath.Join(tmpDir, "test-config-file")
5662
cfgFile, err := os.Create(configFilePath)
@@ -257,7 +263,7 @@ func TestNewJoinData(t *testing.T) {
257263
}
258264

259265
// test newJoinData method
260-
data, err := newJoinData(cmd, tc.args, joinOptions, nil)
266+
data, err := newJoinData(cmd, tc.args, joinOptions, nil, kubeconfigFilePath)
261267
if err != nil && !tc.expectError {
262268
t.Fatalf("newJoinData returned unexpected error: %v", err)
263269
}

0 commit comments

Comments
 (0)