From 524a57a3b4a1091ed3b792e238b4659ee53bcfc6 Mon Sep 17 00:00:00 2001 From: Paco Xu Date: Thu, 12 Aug 2021 14:30:25 +0800 Subject: [PATCH] add patches for join/init/upgrade to the right place; use `/tmp/kubeadm-patches` as the test prepares in this folder Signed-off-by: Paco Xu --- .../cluster/manager/actions/kubeadm-config.go | 14 ++++----- kinder/pkg/constants/constants.go | 2 +- kinder/pkg/kubeadm/discovery.go | 31 ++++++++++++++----- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/kinder/pkg/cluster/manager/actions/kubeadm-config.go b/kinder/pkg/cluster/manager/actions/kubeadm-config.go index b59015ba..61e85985 100644 --- a/kinder/pkg/cluster/manager/actions/kubeadm-config.go +++ b/kinder/pkg/cluster/manager/actions/kubeadm-config.go @@ -253,6 +253,13 @@ func getKubeadmConfig(c *status.Cluster, n *status.Node, data kubeadm.ConfigData patches = append(patches, automaticCopyCertsPatches...) } + // add patches directory to the config + patchesDirectoryPatches, err := kubeadm.GetPatchesDirectoryPatches(kubeadmConfigVersion) + // skip if kubeadm config version is not v1beta3 + if err == nil { + patches = append(patches, patchesDirectoryPatches...) + } + // if requested to use file discovery and not the first control-plane, add patches for using file discovery if options.discoveryMode != TokenDiscovery && !(n == c.BootstrapControlPlane()) { // remove token from config @@ -276,13 +283,6 @@ func getKubeadmConfig(c *status.Cluster, n *status.Node, data kubeadm.ConfigData } patches = append(patches, fileDiscoveryPatch) - // add patches directory to the config - patchesDirectoryPatch, err := kubeadm.GetPatchesDirectoryPatch(kubeadmConfigVersion) - if err != nil { - return "", err - } - patches = append(patches, patchesDirectoryPatch) - // if the file discovery does not contains the authorization credentials, add tls discovery token if options.discoveryMode == FileDiscoveryWithoutCredentials { tlsBootstrapPatch, err := kubeadm.GetTLSBootstrapPatch(kubeadmConfigVersion) diff --git a/kinder/pkg/constants/constants.go b/kinder/pkg/constants/constants.go index 24e60fd8..f34d305b 100644 --- a/kinder/pkg/constants/constants.go +++ b/kinder/pkg/constants/constants.go @@ -113,7 +113,7 @@ const ( DiscoveryFile = "/kinder/discovery.conf" // PatchesDir defines the path to patches stored on node - PatchesDir = "/kinder/patches" + PatchesDir = "/tmp/kubeadm-patches" ) // kubernetes releases, used for branching code according to K8s release or kubeadm release version diff --git a/kinder/pkg/kubeadm/discovery.go b/kinder/pkg/kubeadm/discovery.go index dc0fb058..e6594636 100644 --- a/kinder/pkg/kubeadm/discovery.go +++ b/kinder/pkg/kubeadm/discovery.go @@ -18,6 +18,7 @@ package kubeadm import ( "fmt" + "os" "github.com/pkg/errors" @@ -93,24 +94,38 @@ discovery: file: kubeConfigPath: %s` -// GetPatchesDirectoryPatch returns the kubeadm config patch that will instruct kubeadm +// GetPatchesDirectoryPatches returns the kubeadm config patches that will instruct kubeadm // to use patches directory. -func GetPatchesDirectoryPatch(kubeadmConfigVersion string) (string, error) { +func GetPatchesDirectoryPatches(kubeadmConfigVersion string) ([]string, error) { // select the patches for the kubeadm config version log.Debugf("Preparing patches directory for kubeadm config %s", kubeadmConfigVersion) + if _, err := os.Stat(constants.PatchesDir); os.IsNotExist(err) { + return []string{}, nil + } - var patch string + var patchInit string + var patchJoin string switch kubeadmConfigVersion { case "v1beta3": - patch = patchesDirectoryPatchv1beta3 + patchInit = patchesDirectoryPatchInitv1beta3 + patchJoin = patchesDirectoryPatchJoinv1beta3 default: - return "", errors.Errorf("unknown kubeadm config version: %s", kubeadmConfigVersion) + return []string{}, errors.Errorf("unknown kubeadm config version: %s", kubeadmConfigVersion) } - - return fmt.Sprintf(patch, constants.PatchesDir), nil + return []string{ + fmt.Sprintf(patchInit, constants.PatchesDir), + fmt.Sprintf(patchJoin, constants.PatchesDir), + }, nil } -const patchesDirectoryPatchv1beta3 = `apiVersion: kubeadm.k8s.io/v1beta3 +const patchesDirectoryPatchInitv1beta3 = `apiVersion: kubeadm.k8s.io/v1beta3 +kind: InitConfiguration +metadata: + name: config +patches: + directory: %s` + +const patchesDirectoryPatchJoinv1beta3 = `apiVersion: kubeadm.k8s.io/v1beta3 kind: JoinConfiguration metadata: name: config