kubeadm: fix unit test requiring admin.conf and root
This commit is contained in:
		@@ -87,6 +87,7 @@ go_test(
 | 
				
			|||||||
        "//cmd/kubeadm/app/constants:go_default_library",
 | 
					        "//cmd/kubeadm/app/constants:go_default_library",
 | 
				
			||||||
        "//cmd/kubeadm/app/util:go_default_library",
 | 
					        "//cmd/kubeadm/app/util:go_default_library",
 | 
				
			||||||
        "//cmd/kubeadm/app/util/config: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/output:go_default_library",
 | 
				
			||||||
        "//cmd/kubeadm/app/util/runtime:go_default_library",
 | 
					        "//cmd/kubeadm/app/util/runtime:go_default_library",
 | 
				
			||||||
        "//staging/src/k8s.io/api/core/v1:go_default_library",
 | 
					        "//staging/src/k8s.io/api/core/v1:go_default_library",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
						// sets the data builder function, that will be used by the runner
 | 
				
			||||||
	// both when running the entire workflow or single phases
 | 
						// both when running the entire workflow or single phases
 | 
				
			||||||
	joinRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
 | 
						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
 | 
						// 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.
 | 
					// 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
 | 
					// 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
 | 
					// 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
 | 
						// Validate the mixed arguments with --config and return early on errors
 | 
				
			||||||
	if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
 | 
						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.
 | 
						// 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)
 | 
						// 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
 | 
						var tlsBootstrapCfg *clientcmdapi.Config
 | 
				
			||||||
	if _, err := os.Stat(adminKubeConfigPath); err == nil && opt.controlPlane {
 | 
						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
 | 
							// use the admin.conf as tlsBootstrapCfg, that is the kubeconfig file used for reading the kubeadm-config during discovery
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,6 +24,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/util/sets"
 | 
						"k8s.io/apimachinery/pkg/util/sets"
 | 
				
			||||||
	"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
 | 
						"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
 | 
				
			||||||
 | 
						kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -51,6 +52,11 @@ func TestNewJoinData(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer os.RemoveAll(tmpDir)
 | 
						defer os.RemoveAll(tmpDir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// create kubeconfig
 | 
				
			||||||
 | 
						kubeconfigFilePath := filepath.Join(tmpDir, "test-kubeconfig-file")
 | 
				
			||||||
 | 
						kubeconfig := kubeconfigutil.CreateBasic("", "", "", []byte{})
 | 
				
			||||||
 | 
						kubeconfigutil.WriteToDisk(kubeconfigFilePath, kubeconfig)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// create config file
 | 
						// create config file
 | 
				
			||||||
	configFilePath := filepath.Join(tmpDir, "test-config-file")
 | 
						configFilePath := filepath.Join(tmpDir, "test-config-file")
 | 
				
			||||||
	cfgFile, err := os.Create(configFilePath)
 | 
						cfgFile, err := os.Create(configFilePath)
 | 
				
			||||||
@@ -257,7 +263,7 @@ func TestNewJoinData(t *testing.T) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// test newJoinData method
 | 
								// 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 {
 | 
								if err != nil && !tc.expectError {
 | 
				
			||||||
				t.Fatalf("newJoinData returned unexpected error: %v", err)
 | 
									t.Fatalf("newJoinData returned unexpected error: %v", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user