Skip to content

Commit

Permalink
Merge pull request #2537 from pacoxu/kubeadm-patches
Browse files Browse the repository at this point in the history
Kubeadm patches: use config for newer versions; --experimental-patches for older
  • Loading branch information
k8s-ci-robot authored Aug 11, 2021
2 parents 714c016 + 2dd2806 commit 41d7edd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
7 changes: 7 additions & 0 deletions kinder/pkg/cluster/manager/actions/kubeadm-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ 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)
Expand Down
15 changes: 9 additions & 6 deletions kinder/pkg/cluster/manager/actions/kubeadm-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ func KubeadmInit(c *status.Cluster, usePhases bool, copyCertsMode CopyCertsMode,

// if patcheDir is defined, copy the patches to the node
if patchesDir != "" {
if cp1.MustKubeadmVersion().LessThan(constants.V1_19) {
return errors.New("--patches can't be used with kubeadm older than v1.19")
}
if err := copyPatchesToNode(cp1, patchesDir); err != nil {
return err
}
Expand Down Expand Up @@ -102,7 +99,9 @@ func kubeadmInit(cp1 *status.Node, copyCertsMode CopyCertsMode, patchesDir, igno
)
}
if patchesDir != "" {
initArgs = append(initArgs, "--experimental-patches", constants.PatchesDir)
if cp1.MustKubeadmVersion().LessThan(constants.V1_22) {
initArgs = append(initArgs, "--experimental-patches", constants.PatchesDir)
}
}

if err := cp1.Command(
Expand Down Expand Up @@ -144,7 +143,9 @@ func kubeadmInitWithPhases(cp1 *status.Node, copyCertsMode CopyCertsMode, patche
"init", "phase", "control-plane", "all", fmt.Sprintf("--config=%s", constants.KubeadmConfigPath), fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
controlplaneArgs = append(controlplaneArgs, "--experimental-patches", constants.PatchesDir)
if cp1.MustKubeadmVersion().LessThan(constants.V1_22) {
controlplaneArgs = append(controlplaneArgs, "--experimental-patches", constants.PatchesDir)
}
}
if err := cp1.Command(
"kubeadm", controlplaneArgs...,
Expand All @@ -156,7 +157,9 @@ func kubeadmInitWithPhases(cp1 *status.Node, copyCertsMode CopyCertsMode, patche
"init", "phase", "etcd", "local", fmt.Sprintf("--config=%s", constants.KubeadmConfigPath), fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
etcdArgs = append(etcdArgs, "--experimental-patches", constants.PatchesDir)
if cp1.MustKubeadmVersion().LessThan(constants.V1_22) {
etcdArgs = append(etcdArgs, "--experimental-patches", constants.PatchesDir)
}
}
if err := cp1.Command(
"kubeadm", etcdArgs...,
Expand Down
17 changes: 9 additions & 8 deletions kinder/pkg/cluster/manager/actions/kubeadm-join.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"time"

"github.com/pkg/errors"

"k8s.io/kubeadm/kinder/pkg/cluster/status"
"k8s.io/kubeadm/kinder/pkg/constants"
)
Expand All @@ -45,9 +43,6 @@ func joinControlPlanes(c *status.Cluster, usePhases bool, copyCertsMode CopyCert
for _, cp2 := range c.SecondaryControlPlanes().EligibleForActions() {
// if patcheDir is defined, copy the patches to the node
if patchesDir != "" {
if cp2.MustKubeadmVersion().LessThan(constants.V1_19) {
return errors.New("--patches can't be used with kubeadm older than v1.19")
}
if err := copyPatchesToNode(cp2, patchesDir); err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +101,9 @@ func kubeadmJoinControlPlane(cp *status.Node, patchesDir, ignorePreflightErrors
fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
joinArgs = append(joinArgs, "--experimental-patches", constants.PatchesDir)
if cp.MustKubeadmVersion().LessThan(constants.V1_22) {
joinArgs = append(joinArgs, "--experimental-patches", constants.PatchesDir)
}
}

if err := cp.Command(
Expand Down Expand Up @@ -141,7 +138,9 @@ func kubeadmJoinControlPlaneWithPhases(cp *status.Node, patchesDir, ignorePrefli
}

if patchesDir != "" {
prepareArgs = append(prepareArgs, "--experimental-patches", constants.PatchesDir)
if cp.MustKubeadmVersion().LessThan(constants.V1_22) {
prepareArgs = append(prepareArgs, "--experimental-patches", constants.PatchesDir)
}
}

if err := cp.Command(
Expand All @@ -166,7 +165,9 @@ func kubeadmJoinControlPlaneWithPhases(cp *status.Node, patchesDir, ignorePrefli
fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
controlPlaneArgs = append(controlPlaneArgs, "--experimental-patches", constants.PatchesDir)
if cp.MustKubeadmVersion().LessThan(constants.V1_22) {
controlPlaneArgs = append(controlPlaneArgs, "--experimental-patches", constants.PatchesDir)
}
}

if err := cp.Command(
Expand Down
15 changes: 10 additions & 5 deletions kinder/pkg/cluster/manager/actions/kubeadm-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ func KubeadmUpgrade(c *status.Cluster, upgradeVersion *K8sVersion.Version, patch
for _, n := range nodeList {
// if patcheDir is defined, copy the patches to the node
if patchesDir != "" {
if n.MustKubeadmVersion().LessThan(constants.V1_19) {
return errors.New("--patches can't be used with kubeadm older than v1.19")
}
if err := copyPatchesToNode(n, patchesDir); err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +128,11 @@ func kubeadmUpgradeApply(c *status.Cluster, cp1 *status.Node, upgradeVersion *K8
"upgrade", "apply", "-f", fmt.Sprintf("v%s", upgradeVersion), fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
applyArgs = append(applyArgs, "--experimental-patches", constants.PatchesDir)
if cp1.MustKubeadmVersion().LessThan(constants.V1_22) {
applyArgs = append(applyArgs, "--experimental-patches", constants.PatchesDir)
} else {
applyArgs = append(applyArgs, "--patches", constants.PatchesDir)
}
}
if err := cp1.Command(
"kubeadm", applyArgs...,
Expand Down Expand Up @@ -159,7 +160,11 @@ func kubeadmUpgradeNode(c *status.Cluster, n *status.Node, upgradeVersion *K8sVe
"upgrade", "node", fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
nodeArgs = append(nodeArgs, fmt.Sprintf("--experimental-patches=%s", constants.PatchesDir))
if n.MustKubeadmVersion().LessThan(constants.V1_22) {
nodeArgs = append(nodeArgs, fmt.Sprintf("--experimental-patches=%s", constants.PatchesDir))
} else {
nodeArgs = append(nodeArgs, fmt.Sprintf("--patches=%s", constants.PatchesDir))
}
}
if err := n.Command(
"kubeadm", nodeArgs...,
Expand Down
4 changes: 2 additions & 2 deletions kinder/pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ const (

// kubernetes releases, used for branching code according to K8s release or kubeadm release version
var (
// V1.19 minor version
V1_19 = K8sVersion.MustParseSemantic("v1.19.0-0")
// V1.22 minor version
V1_22 = K8sVersion.MustParseSemantic("v1.22.0-0")
)

// other constants
Expand Down
24 changes: 24 additions & 0 deletions kinder/pkg/kubeadm/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ discovery:
file:
kubeConfigPath: %s`

// GetPatchesDirectoryPatch returns the kubeadm config patch that will instruct kubeadm
// to use patches directory.
func GetPatchesDirectoryPatch(kubeadmConfigVersion string) (string, error) {
// select the patches for the kubeadm config version
log.Debugf("Preparing patches directory for kubeadm config %s", kubeadmConfigVersion)

var patch string
switch kubeadmConfigVersion {
case "v1beta3":
patch = patchesDirectoryPatchv1beta3
default:
return "", errors.Errorf("unknown kubeadm config version: %s", kubeadmConfigVersion)
}

return fmt.Sprintf(patch, constants.PatchesDir), nil
}

const patchesDirectoryPatchv1beta3 = `apiVersion: kubeadm.k8s.io/v1beta3
kind: JoinConfiguration
metadata:
name: config
patches:
directory: %s`

// GetTLSBootstrapPatch returns the kubeadm config patch that will instruct kubeadm
// to use a TLSBootstrap token.
// NB. for sake of semplicity, we are using the same Token already used for Token discovery
Expand Down

0 comments on commit 41d7edd

Please sign in to comment.