Skip to content

Commit

Permalink
feat: private registry for k8s upgrade
Browse files Browse the repository at this point in the history
Reuse custom registry.
New flag
Fixes: siderolabs#8275

Co-authored-by:  @g3offrey
Signed-off-by: Matthieu STROHL <mstrohl@dive-in-it.com>
  • Loading branch information
mstrohl committed Feb 13, 2024
1 parent 383e528 commit e46224f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 34 deletions.
1 change: 1 addition & 0 deletions cmd/talosctl/cmd/talos/upgrade-k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func init() {
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.CustomRegistry, "custom-registry", false, "keep customized registry setting")
upgradeK8sCmd.Flags().BoolVar(&upgradeOptions.UpgradeKubelet, "upgrade-kubelet", true, "upgrade kubelet service")
addCommand(upgradeK8sCmd)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/cluster/kubernetes/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,14 @@ func upgradeKubeletPatcher(
options.Log(" > skipped in dry-run")
}
}
imageName := constants.KubeletImage

image := fmt.Sprintf("%s:v%s", constants.KubeletImage, options.Path.ToVersion())
privateRepo := strings.Split(config.MachineConfig.MachineKubelet.KubeletImage, ":")[0]
if privateRepo != imageName && options.CustomRegistry {
imageName = privateRepo
}

image := fmt.Sprintf("%s:v%s", imageName, options.Path.ToVersion())

if oldImage == image {
return errUpdateSkipped
Expand All @@ -210,7 +216,7 @@ func upgradeKubeletPatcher(
if options.DryRun {
return errUpdateSkipped
}

options.Log(" > using Kubelet Image %s", image)
config.MachineConfig.MachineKubelet.KubeletImage = image

return nil
Expand Down
89 changes: 57 additions & 32 deletions pkg/cluster/kubernetes/talos_managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ func patchKubeProxy(options UpgradeOptions) func(config *v1alpha1config.Config)
config.ClusterConfig.ProxyConfig = &v1alpha1config.ProxyConfig{}
}

config.ClusterConfig.ProxyConfig.ContainerImage = fmt.Sprintf("%s:v%s", constants.KubeProxyImage, options.Path.ToVersion())
imageName := constants.KubeProxyImage

privateRepo := strings.Split(config.ClusterConfig.ProxyConfig.ContainerImage, ":")[0]
if privateRepo != imageName && options.CustomRegistry {
imageName = privateRepo
}

config.ClusterConfig.ProxyConfig.ContainerImage = fmt.Sprintf("%s:v%s", imageName, options.Path.ToVersion())

return nil
}
Expand Down Expand Up @@ -304,7 +311,31 @@ func upgradeStaticPodOnNode(ctx context.Context, cluster UpgradeProvider, option

var errUpdateSkipped = fmt.Errorf("update skipped")

//nolint:gocyclo,cyclop
func staticPodImage(logUpdate func(oldImage string), service string, defaultImage string, containerImage string, configImage string, options UpgradeOptions) (string, error) {
imageName := defaultImage

privateRepo := strings.Split(containerImage, ":")[0]
if privateRepo != defaultImage && !options.PrePullImages && options.CustomRegistry {
imageName = privateRepo
}

image := fmt.Sprintf("%s:v%s", imageName, options.Path.ToVersion())
options.Log(" > using %s Image %s", service, image)

if containerImage == image || configImage == image {
return "", errUpdateSkipped
}

logUpdate(containerImage)

if options.DryRun {
return "", errUpdateSkipped
}

return image, nil
}

//nolint:gocyclo
func upgradeStaticPodPatcher(options UpgradeOptions, service string, configResource resource.Resource) func(config *v1alpha1config.Config) error {
return func(config *v1alpha1config.Config) error {
if config.ClusterConfig == nil {
Expand Down Expand Up @@ -349,16 +380,14 @@ func upgradeStaticPodPatcher(options UpgradeOptions, service string, configResou
config.ClusterConfig.APIServerConfig = &v1alpha1config.APIServerConfig{}
}

image := fmt.Sprintf("%s:v%s", constants.KubernetesAPIServerImage, options.Path.ToVersion())

if config.ClusterConfig.APIServerConfig.ContainerImage == image || configImage == image {
return errUpdateSkipped
}

logUpdate(config.ClusterConfig.APIServerConfig.ContainerImage)

if options.DryRun {
return errUpdateSkipped
image, err := staticPodImage(logUpdate,
service,
constants.KubernetesAPIServerImage,
config.ClusterConfig.APIServerConfig.ContainerImage,
configImage,
options)
if err != nil {
return err
}

config.ClusterConfig.APIServerConfig.ContainerImage = image
Expand All @@ -367,16 +396,14 @@ func upgradeStaticPodPatcher(options UpgradeOptions, service string, configResou
config.ClusterConfig.ControllerManagerConfig = &v1alpha1config.ControllerManagerConfig{}
}

image := fmt.Sprintf("%s:v%s", constants.KubernetesControllerManagerImage, options.Path.ToVersion())

if config.ClusterConfig.ControllerManagerConfig.ContainerImage == image || configImage == image {
return errUpdateSkipped
}

logUpdate(config.ClusterConfig.ControllerManagerConfig.ContainerImage)

if options.DryRun {
return errUpdateSkipped
image, err := staticPodImage(logUpdate,
service,
constants.KubernetesControllerManagerImage,
config.ClusterConfig.ControllerManagerConfig.ContainerImage,
configImage,
options)
if err != nil {
return err
}

config.ClusterConfig.ControllerManagerConfig.ContainerImage = image
Expand All @@ -385,16 +412,14 @@ func upgradeStaticPodPatcher(options UpgradeOptions, service string, configResou
config.ClusterConfig.SchedulerConfig = &v1alpha1config.SchedulerConfig{}
}

image := fmt.Sprintf("%s:v%s", constants.KubernetesSchedulerImage, options.Path.ToVersion())

if config.ClusterConfig.SchedulerConfig.ContainerImage == image || configImage == image {
return errUpdateSkipped
}

logUpdate(config.ClusterConfig.SchedulerConfig.ContainerImage)

if options.DryRun {
return errUpdateSkipped
image, err := staticPodImage(logUpdate,
service,
constants.KubernetesSchedulerImage,
config.ClusterConfig.SchedulerConfig.ContainerImage,
configImage,
options)
if err != nil {
return err
}

config.ClusterConfig.SchedulerConfig.ContainerImage = image
Expand Down
1 change: 1 addition & 0 deletions pkg/cluster/kubernetes/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type UpgradeOptions struct {
ControlPlaneEndpoint string
LogOutput io.Writer
PrePullImages bool
CustomRegistry bool
UpgradeKubelet bool
DryRun bool
EncoderOpt encoder.Option
Expand Down

0 comments on commit e46224f

Please sign in to comment.