Skip to content

Commit

Permalink
feat: upgrade-k8s without comments
Browse files Browse the repository at this point in the history
This feature allows us to remove any comments from the machineconfig after
upgrading Kubernetes.

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
sergelogvinov authored and smira committed Sep 12, 2023
1 parent e448751 commit 3f52320
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
17 changes: 17 additions & 0 deletions cmd/talosctl/cmd/talos/upgrade-k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/siderolabs/talos/pkg/cluster"
k8s "github.com/siderolabs/talos/pkg/cluster/kubernetes"
"github.com/siderolabs/talos/pkg/machinery/client"
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
"github.com/siderolabs/talos/pkg/machinery/constants"
)

Expand All @@ -34,12 +35,17 @@ var upgradeOptions k8s.UpgradeOptions
var upgradeK8sCmdFlags struct {
FromVersion string
ToVersion string

withExamples bool
withDocs bool
}

func init() {
upgradeK8sCmd.Flags().StringVar(&upgradeK8sCmdFlags.FromVersion, "from", "", "the Kubernetes control plane version to upgrade from")
upgradeK8sCmd.Flags().StringVar(&upgradeK8sCmdFlags.ToVersion, "to", constants.DefaultKubernetesVersion, "the Kubernetes control plane version to upgrade to")
upgradeK8sCmd.Flags().StringVar(&upgradeOptions.ControlPlaneEndpoint, "endpoint", "", "the cluster control plane endpoint")
upgradeK8sCmd.Flags().BoolVarP(&upgradeK8sCmdFlags.withExamples, "with-examples", "", true, "patch all machine configs with the commented examples")
upgradeK8sCmd.Flags().BoolVarP(&upgradeK8sCmdFlags.withDocs, "with-docs", "", true, "patch all machine configs adding the documentation for each field")
upgradeK8sCmd.Flags().BoolVar(&upgradeOptions.DryRun, "dry-run", false, "skip the actual upgrade and show the upgrade plan instead")
upgradeK8sCmd.Flags().BoolVar(&upgradeOptions.PrePullImages, "pre-pull-images", true, "pre-pull images before upgrade")
upgradeK8sCmd.Flags().BoolVar(&upgradeOptions.UpgradeKubelet, "upgrade-kubelet", true, "upgrade kubelet service")
Expand Down Expand Up @@ -87,5 +93,16 @@ func upgradeKubernetes(ctx context.Context, c *client.Client) error {
return fmt.Errorf("error creating upgrade path %w", err)
}

commentsFlags := encoder.CommentsDisabled
if upgradeK8sCmdFlags.withDocs {
commentsFlags |= encoder.CommentsDocs
}

if upgradeK8sCmdFlags.withExamples {
commentsFlags |= encoder.CommentsExamples
}

upgradeOptions.EncoderOpt = encoder.WithComments(commentsFlags)

return k8s.Upgrade(ctx, &state, upgradeOptions)
}
3 changes: 3 additions & 0 deletions internal/integration/provision/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
clientconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
"github.com/siderolabs/talos/pkg/machinery/config"
"github.com/siderolabs/talos/pkg/machinery/config/bundle"
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
"github.com/siderolabs/talos/pkg/machinery/config/generate"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
Expand Down Expand Up @@ -699,6 +700,8 @@ func (suite *UpgradeSuite) upgradeKubernetes(fromVersion, toVersion string, skip

UpgradeKubelet: !skipKubeletUpgrade,
PrePullImages: true,

EncoderOpt: encoder.WithComments(encoder.CommentsAll),
}

suite.Require().NoError(kubernetes.Upgrade(suite.ctx, suite.clusterAccess, options))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/kubernetes/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func upgradeKubeletOnNode(ctx context.Context, cluster UpgradeProvider, options

skipWait := false

err = patchNodeConfig(ctx, cluster, node, upgradeKubeletPatcher(options, kubeletSpec))
err = patchNodeConfig(ctx, cluster, node, options.EncoderOpt, upgradeKubeletPatcher(options, kubeletSpec))
if err != nil {
if errors.Is(err, errUpdateSkipped) {
skipWait = true
Expand Down
5 changes: 3 additions & 2 deletions pkg/cluster/kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
"github.com/siderolabs/talos/pkg/machinery/config/container"
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
v1alpha1config "github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
)

// patchNodeConfig updates node configuration by means of patch function.
func patchNodeConfig(ctx context.Context, cluster UpgradeProvider, node string, patchFunc func(config *v1alpha1config.Config) error) error {
func patchNodeConfig(ctx context.Context, cluster UpgradeProvider, node string, encoderOpt encoder.Option, patchFunc func(config *v1alpha1config.Config) error) error {
c, err := cluster.Client()
if err != nil {
return fmt.Errorf("error building Talos API client: %w", err)
Expand All @@ -44,7 +45,7 @@ func patchNodeConfig(ctx context.Context, cluster UpgradeProvider, node string,
return fmt.Errorf("error patching config: %w", err)
}

cfgBytes, err := container.NewV1Alpha1(cfg).EncodeBytes()
cfgBytes, err := container.NewV1Alpha1(cfg).EncodeBytes(encoderOpt)
if err != nil {
return fmt.Errorf("error serializing config: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/kubernetes/talos_managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func upgradeKubeProxy(ctx context.Context, cluster UpgradeProvider, options Upgr
for _, node := range options.controlPlaneNodes {
options.Log(" > %q: starting update", node)

if err := patchNodeConfig(ctx, cluster, node, patchKubeProxy(options)); err != nil {
if err := patchNodeConfig(ctx, cluster, node, options.EncoderOpt, patchKubeProxy(options)); err != nil {
return fmt.Errorf("error updating node %q: %w", node, err)
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func upgradeStaticPodOnNode(ctx context.Context, cluster UpgradeProvider, option

skipConfigWait := false

err = patchNodeConfig(ctx, cluster, node, upgradeStaticPodPatcher(options, service, initialConfig))
err = patchNodeConfig(ctx, cluster, node, options.EncoderOpt, upgradeStaticPodPatcher(options, service, initialConfig))
if err != nil {
if errors.Is(err, errUpdateSkipped) {
skipConfigWait = true
Expand Down
3 changes: 3 additions & 0 deletions pkg/cluster/kubernetes/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"io"

"github.com/siderolabs/go-kubernetes/kubernetes/upgrade"

"github.com/siderolabs/talos/pkg/machinery/config/encoder"
)

const (
Expand All @@ -28,6 +30,7 @@ type UpgradeOptions struct {
PrePullImages bool
UpgradeKubelet bool
DryRun bool
EncoderOpt encoder.Option

controlPlaneNodes []string
workerNodes []string
Expand Down
2 changes: 2 additions & 0 deletions website/content/v1.6/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,8 @@ talosctl upgrade-k8s [flags]
--pre-pull-images pre-pull images before upgrade (default true)
--to string the Kubernetes control plane version to upgrade to (default "1.28.1")
--upgrade-kubelet upgrade kubelet service (default true)
--with-docs patch all machine configs adding the documentation for each field (default true)
--with-examples patch all machine configs with the commented examples (default true)
```

### Options inherited from parent commands
Expand Down

0 comments on commit 3f52320

Please sign in to comment.