diff --git a/cmd/talosctl/cmd/talos/upgrade-k8s.go b/cmd/talosctl/cmd/talos/upgrade-k8s.go index 27484157da..6262b6a026 100644 --- a/cmd/talosctl/cmd/talos/upgrade-k8s.go +++ b/cmd/talosctl/cmd/talos/upgrade-k8s.go @@ -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" ) @@ -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") @@ -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) } diff --git a/internal/integration/provision/upgrade.go b/internal/integration/provision/upgrade.go index c5b1ec8891..e88076eb32 100644 --- a/internal/integration/provision/upgrade.go +++ b/internal/integration/provision/upgrade.go @@ -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" @@ -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)) diff --git a/pkg/cluster/kubernetes/kubelet.go b/pkg/cluster/kubernetes/kubelet.go index 6a5f1b91ee..86cea3a53d 100644 --- a/pkg/cluster/kubernetes/kubelet.go +++ b/pkg/cluster/kubernetes/kubelet.go @@ -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 diff --git a/pkg/cluster/kubernetes/patch.go b/pkg/cluster/kubernetes/patch.go index a5b8438c90..97e5714f94 100644 --- a/pkg/cluster/kubernetes/patch.go +++ b/pkg/cluster/kubernetes/patch.go @@ -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) @@ -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) } diff --git a/pkg/cluster/kubernetes/talos_managed.go b/pkg/cluster/kubernetes/talos_managed.go index e553fc7980..66a98f70d0 100644 --- a/pkg/cluster/kubernetes/talos_managed.go +++ b/pkg/cluster/kubernetes/talos_managed.go @@ -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) } } @@ -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 diff --git a/pkg/cluster/kubernetes/upgrade.go b/pkg/cluster/kubernetes/upgrade.go index 156839eb15..959fa5ea22 100644 --- a/pkg/cluster/kubernetes/upgrade.go +++ b/pkg/cluster/kubernetes/upgrade.go @@ -9,6 +9,8 @@ import ( "io" "github.com/siderolabs/go-kubernetes/kubernetes/upgrade" + + "github.com/siderolabs/talos/pkg/machinery/config/encoder" ) const ( @@ -28,6 +30,7 @@ type UpgradeOptions struct { PrePullImages bool UpgradeKubelet bool DryRun bool + EncoderOpt encoder.Option controlPlaneNodes []string workerNodes []string diff --git a/website/content/v1.6/reference/cli.md b/website/content/v1.6/reference/cli.md index e13a7bf7b6..0daa7bd84b 100644 --- a/website/content/v1.6/reference/cli.md +++ b/website/content/v1.6/reference/cli.md @@ -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