Skip to content

Commit

Permalink
fixup! cli: refactor upgrade apply cmd to match name
Browse files Browse the repository at this point in the history
add debug logging
  • Loading branch information
derpsteb committed Feb 13, 2023
1 parent f13f6cb commit 5d758cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
12 changes: 10 additions & 2 deletions cli/internal/cloudcmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Upgrader struct {
helmClient helmInterface

outWriter io.Writer
log debugLog
}

// NewUpgrader returns a new Upgrader.
Expand Down Expand Up @@ -90,6 +91,7 @@ func NewUpgrader(outWriter io.Writer, log debugLog) (*Upgrader, error) {
dynamicInterface: &dynamicClient{client: unstructuredClient},
helmClient: helmClient,
outWriter: outWriter,
log: log,
}, nil
}

Expand Down Expand Up @@ -139,6 +141,7 @@ func (u *Upgrader) UpgradeK8s(ctx context.Context, newClusterVersion string, com
return ErrInProgress
}

u.log.Debugf("Upgrading cluster's Kubernetes version from %s to %s", nodeVersion.Spec.KubernetesClusterVersion, newClusterVersion)
configMap, err := internalk8s.ConstructK8sComponentsCM(components, newClusterVersion)
if err != nil {
return fmt.Errorf("constructing k8s-components ConfigMap: %w", err)
Expand All @@ -157,6 +160,7 @@ func (u *Upgrader) UpgradeK8s(ctx context.Context, newClusterVersion string, com
if err != nil {
return fmt.Errorf("converting nodeVersion to unstructured: %w", err)
}
u.log.Debugf("Triggering Kubernetes version upgrade now")
// Send the updated NodeVersion resource
updated, err := u.dynamicInterface.update(ctx, &unstructured.Unstructured{Object: raw})
if err != nil {
Expand All @@ -176,7 +180,8 @@ func (u *Upgrader) UpgradeK8s(ctx context.Context, newClusterVersion string, com
return errors.New("failed to update NodeVersion resource")
}

return err
fmt.Fprintf(u.outWriter, "Successfully updated the cluster's Kubernetes version to %s. Nodes will restart automatically\n", newClusterVersion)
return nil
}

// KubernetesVersion returns the version of Kubernetes the Constellation is currently running on.
Expand Down Expand Up @@ -248,6 +253,7 @@ func (u *Upgrader) updateMeasurements(ctx context.Context, newMeasurements measu
return fmt.Errorf("marshaling measurements: %w", err)
}
existingConf.Data[constants.MeasurementsFilename] = string(measurementsJSON)
u.log.Debugf("Triggering measurements config map update now")
_, err = u.stableInterface.updateConfigMap(ctx, existingConf)
if err != nil {
return fmt.Errorf("setting new measurements: %w", err)
Expand All @@ -258,18 +264,20 @@ func (u *Upgrader) updateMeasurements(ctx context.Context, newMeasurements measu
}

func (u *Upgrader) updateImage(ctx context.Context, nodeVersion updatev1alpha1.NodeVersion, newImageRef, newImageVersion string) error {
u.log.Debugf("Upgrading cluster's image version from %s to %s", nodeVersion.Spec.ImageVersion, newImageVersion)
nodeVersion.Spec.ImageReference = newImageRef
nodeVersion.Spec.ImageVersion = newImageVersion

raw, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&nodeVersion)
if err != nil {
return fmt.Errorf("converting nodeVersion to unstructured: %w", err)
}
u.log.Debugf("Triggering image version upgrade now")
if _, err := u.dynamicInterface.update(ctx, &unstructured.Unstructured{Object: raw}); err != nil {
return fmt.Errorf("setting new image: %w", err)
}

fmt.Fprintln(u.outWriter, "Successfully updated the cluster's image, nodes will restart automatically")
fmt.Fprintf(u.outWriter, "Successfully updated the cluster's image version to %s. Nodes will restart automatically\n", newImageVersion)
return nil
}

Expand Down
30 changes: 18 additions & 12 deletions cli/internal/cmd/upgradeapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ func runUpgradeApply(cmd *cobra.Command, args []string) error {
return err
}

return upgradeApply(cmd, imageFetcher, upgrader, fileHandler)
applyCmd := upgradeApplyCmd{upgrader: upgrader, log: log}
return applyCmd.upgradeApply(cmd, imageFetcher, fileHandler)
}

func upgradeApply(cmd *cobra.Command, imageFetcher imageFetcher, upgrader cloudUpgrader, fileHandler file.Handler) error {
type upgradeApplyCmd struct {
upgrader *cloudcmd.Upgrader
log debugLog
}

func (u *upgradeApplyCmd) upgradeApply(cmd *cobra.Command, imageFetcher imageFetcher, fileHandler file.Handler) error {
flags, err := parseUpgradeApplyFlags(cmd)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
Expand All @@ -72,12 +78,12 @@ func upgradeApply(cmd *cobra.Command, imageFetcher imageFetcher, upgrader cloudU
return config.DisplayValidationErrors(cmd.ErrOrStderr(), err)
}

if err := handleServiceUpgrade(cmd, upgrader, flags); err != nil {
if err := u.handleServiceUpgrade(cmd, flags); err != nil {
return fmt.Errorf("service upgrade: %w", err)
}

invalidUpgradeErr := &cloudcmd.InvalidUpgradeError{}
err = handleK8sUpgrade(cmd.Context(), conf, upgrader)
err = u.handleK8sUpgrade(cmd.Context(), conf)
switch {
case errors.Is(err, cloudcmd.ErrInProgress):
cmd.PrintErrln("Skipping Kubernetes components upgrades. Another Kubernetes components upgrade is in progress")
Expand All @@ -87,7 +93,7 @@ func upgradeApply(cmd *cobra.Command, imageFetcher imageFetcher, upgrader cloudU
return fmt.Errorf("upgrading Kubernetes components: %w", err)
}

err = handleImageUpgrade(cmd.Context(), conf, imageFetcher, upgrader)
err = u.handleImageUpgrade(cmd.Context(), conf, imageFetcher)
switch {
case errors.Is(err, cloudcmd.ErrInProgress):
cmd.PrintErrln("Skipping image upgrades. Another image upgrade is in progress")
Expand All @@ -100,8 +106,8 @@ func upgradeApply(cmd *cobra.Command, imageFetcher imageFetcher, upgrader cloudU
return nil
}

func handleServiceUpgrade(cmd *cobra.Command, upgrader cloudUpgrader, flags upgradeApplyFlags) error {
err := upgrader.UpgradeHelmServices(cmd.Context(), flags.upgradeTimeout, helm.DenyDestructive)
func (u *upgradeApplyCmd) handleServiceUpgrade(cmd *cobra.Command, flags upgradeApplyFlags) error {
err := u.upgrader.UpgradeHelmServices(cmd.Context(), flags.upgradeTimeout, helm.DenyDestructive)
if errors.Is(err, helm.ErrConfirmationMissing) {
if !flags.yes {
cmd.PrintErrln("WARNING: Upgrading cert-manager will destroy all custom resources you have manually created that are based on the current version of cert-manager.")
Expand All @@ -114,7 +120,7 @@ func handleServiceUpgrade(cmd *cobra.Command, upgrader cloudUpgrader, flags upgr
return nil
}
}
err = upgrader.UpgradeHelmServices(cmd.Context(), flags.upgradeTimeout, helm.AllowDestructive)
err = u.upgrader.UpgradeHelmServices(cmd.Context(), flags.upgradeTimeout, helm.AllowDestructive)
}
if err != nil {
return fmt.Errorf("upgrading helm: %w", err)
Expand All @@ -123,7 +129,7 @@ func handleServiceUpgrade(cmd *cobra.Command, upgrader cloudUpgrader, flags upgr
return nil
}

func handleImageUpgrade(ctx context.Context, conf *config.Config, imageFetcher imageFetcher, upgrader cloudUpgrader) error {
func (u *upgradeApplyCmd) handleImageUpgrade(ctx context.Context, conf *config.Config, imageFetcher imageFetcher) error {
imageReference, err := imageFetcher.FetchReference(ctx, conf)
if err != nil {
return fmt.Errorf("fetching image reference: %w", err)
Expand All @@ -134,22 +140,22 @@ func handleImageUpgrade(ctx context.Context, conf *config.Config, imageFetcher i
return fmt.Errorf("parsing version from image short path: %w", err)
}

err = upgrader.UpgradeImage(ctx, imageReference, imageVersion.Version, conf.Upgrade.Measurements)
err = u.upgrader.UpgradeImage(ctx, imageReference, imageVersion.Version, conf.Upgrade.Measurements)
if err != nil {
return fmt.Errorf("upgrading image: %w", err)
}

return nil
}

func handleK8sUpgrade(ctx context.Context, conf *config.Config, upgrader cloudUpgrader) error {
func (u *upgradeApplyCmd) handleK8sUpgrade(ctx context.Context, conf *config.Config) error {
currentVersion, err := versions.NewValidK8sVersion(conf.KubernetesVersion)
if err != nil {
return fmt.Errorf("getting Kubernetes version: %w", err)
}
versionConfig := versions.VersionConfigs[currentVersion]

err = upgrader.UpgradeK8s(ctx, versionConfig.ClusterVersion, versionConfig.KubernetesComponents)
err = u.upgrader.UpgradeK8s(ctx, versionConfig.ClusterVersion, versionConfig.KubernetesComponents)
if err != nil {
return fmt.Errorf("upgrading Kubernetes: %w", err)
}
Expand Down

0 comments on commit 5d758cb

Please sign in to comment.