Skip to content

Commit

Permalink
use patches to patch patchesDirectory for upgrade; experimental-patch…
Browse files Browse the repository at this point in the history
…es for 1.20-1.21

Signed-off-by: Paco Xu <[email protected]>
Co-authored-by: Lubomir I. Ivanov <[email protected]>
  • Loading branch information
pacoxu and neolit123 committed Aug 5, 2021
1 parent 11626e7 commit d696803
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 19 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
21 changes: 18 additions & 3 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,6 +99,12 @@ func kubeadmInit(cp1 *status.Node, copyCertsMode CopyCertsMode, patchesDir, igno
)
}

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

if err := cp1.Command(
"kubeadm", initArgs...,
).RunWithEcho(); err != nil {
Expand Down Expand Up @@ -140,6 +143,13 @@ func kubeadmInitWithPhases(cp1 *status.Node, copyCertsMode CopyCertsMode, patche
controlplaneArgs := []string{
"init", "phase", "control-plane", "all", fmt.Sprintf("--config=%s", constants.KubeadmConfigPath), fmt.Sprintf("--v=%d", vLevel),
}

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

if err := cp1.Command(
"kubeadm", controlplaneArgs...,
).RunWithEcho(); err != nil {
Expand All @@ -149,6 +159,11 @@ func kubeadmInitWithPhases(cp1 *status.Node, copyCertsMode CopyCertsMode, patche
etcdArgs := []string{
"init", "phase", "etcd", "local", fmt.Sprintf("--config=%s", constants.KubeadmConfigPath), fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
if cp1.MustKubeadmVersion().LessThan(constants.V1_22) {
etcdArgs = append(etcdArgs, "--experimental-patches", constants.PatchesDir)
}
}
if err := cp1.Command(
"kubeadm", etcdArgs...,
).RunWithEcho(); err != nil {
Expand Down
22 changes: 16 additions & 6 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 @@ -105,7 +100,11 @@ func kubeadmJoinControlPlane(cp *status.Node, patchesDir, ignorePreflightErrors
fmt.Sprintf("--ignore-preflight-errors=%s", ignorePreflightErrors),
fmt.Sprintf("--v=%d", vLevel),
}

if patchesDir != "" {
if cp.MustKubeadmVersion().LessThan(constants.V1_21) {
joinArgs = append(joinArgs, "--experimental-patches", constants.PatchesDir)
}
}
if err := cp.Command(
"kubeadm", joinArgs...,
).RunWithEcho(); err != nil {
Expand Down Expand Up @@ -137,6 +136,12 @@ func kubeadmJoinControlPlaneWithPhases(cp *status.Node, patchesDir, ignorePrefli
fmt.Sprintf("--v=%d", vLevel),
}

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

if err := cp.Command(
"kubeadm", prepareArgs...,
).RunWithEcho(); err != nil {
Expand All @@ -158,6 +163,11 @@ func kubeadmJoinControlPlaneWithPhases(cp *status.Node, patchesDir, ignorePrefli
fmt.Sprintf("--config=%s", constants.KubeadmConfigPath),
fmt.Sprintf("--v=%d", vLevel),
}
if patchesDir != "" {
if cp.MustKubeadmVersion().LessThan(constants.V1_21) {
controlPlaneArgs = append(controlPlaneArgs, "--experimental-patches", constants.PatchesDir)
}
}

if err := cp.Command(
"kubeadm", controlPlaneArgs...,
Expand Down
7 changes: 3 additions & 4 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,9 @@ 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, "--patches", constants.PatchesDir)
if cp1.MustKubeadmVersion().LessThan(constants.V1_22) {
applyArgs = append(applyArgs, "--patches", constants.PatchesDir)
}
}
if err := cp1.Command(
"kubeadm", applyArgs...,
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
4 changes: 0 additions & 4 deletions kinder/pkg/kubeadm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ nodeRegistration:
criSocket: "/run/containerd/containerd.sock"
kubeletExtraArgs:
node-ip: "{{ .NodeAddress }}"
patches:
directory: "/kinder/patches"
---
# no-op entry that exists solely so it can be patched
apiVersion: kubeadm.k8s.io/v1beta2
Expand All @@ -196,8 +194,6 @@ discovery:
apiServerEndpoint: "{{ .ControlPlaneEndpoint }}"
token: "{{ .Token }}"
unsafeSkipCAVerification: true
patches:
directory: "/kinder/patches"
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
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 d696803

Please sign in to comment.