From 5026973d59393d6ff37866a77fa06fcd8892ddd5 Mon Sep 17 00:00:00 2001 From: Jun <108045855+2456868764@users.noreply.github.com> Date: Sat, 3 Feb 2024 12:17:22 +0800 Subject: [PATCH] fix: hgctl latest version bug (#816) --- pkg/cmd/hgctl/helm/render.go | 13 +++++++++++-- pkg/cmd/hgctl/install.go | 9 ++++++--- pkg/cmd/hgctl/installer/component.go | 8 ++++++++ pkg/cmd/hgctl/installer/higress.go | 2 +- pkg/cmd/hgctl/installer/installer.go | 12 ++++++++++-- pkg/cmd/hgctl/installer/installer_k8s.go | 10 ++++++++-- pkg/cmd/hgctl/manifest.go | 9 ++++++--- pkg/cmd/hgctl/uninstall.go | 2 +- pkg/cmd/hgctl/upgrade.go | 7 ++++--- 9 files changed, 55 insertions(+), 17 deletions(-) diff --git a/pkg/cmd/hgctl/helm/render.go b/pkg/cmd/hgctl/helm/render.go index e46736bdb..2afba8dc1 100644 --- a/pkg/cmd/hgctl/helm/render.go +++ b/pkg/cmd/hgctl/helm/render.go @@ -584,7 +584,7 @@ func locateChart(cpOpts *action.ChartPathOptions, name string, settings *cli.Env return fileAbsPath, nil } -func ParseLatestVersion(repoUrl string, version string) (string, error) { +func ParseLatestVersion(repoUrl string, version string, devel bool) (string, error) { cpOpts := &action.ChartPathOptions{ RepoURL: repoUrl, @@ -632,7 +632,16 @@ func ParseLatestVersion(repoUrl string, version string) (string, error) { // get higress helm chart latest version if entries, ok := indexFile.Entries[RepoChartIndexYamlHigressIndex]; ok { - return entries[0].AppVersion, nil + if devel { + return entries[0].AppVersion, nil + } + + if chatVersion, err := indexFile.Get(RepoChartIndexYamlHigressIndex, ""); err != nil { + return "", errors.New("can't find higress latest version") + } else { + return chatVersion.Version, nil + } + } return "", errors.New("can't find higress latest version") diff --git a/pkg/cmd/hgctl/install.go b/pkg/cmd/hgctl/install.go index d28c4e691..8ee744d08 100644 --- a/pkg/cmd/hgctl/install.go +++ b/pkg/cmd/hgctl/install.go @@ -52,6 +52,8 @@ type InstallArgs struct { Set []string // ManifestsPath is a path to a ManifestsPath and profiles directory in the local filesystem with a release tgz. ManifestsPath string + // Devel if set true when version is latest, it will get latest version, otherwise it will get latest stable version + Devel bool } func (a *InstallArgs) String() string { @@ -67,6 +69,7 @@ func addInstallFlags(cmd *cobra.Command, args *InstallArgs) { cmd.PersistentFlags().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr) cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr) cmd.PersistentFlags().StringVarP(&args.ManifestsPath, "manifests", "d", "", manifestsFlagHelpStr) + cmd.PersistentFlags().BoolVar(&args.Devel, "devel", false, "use development versions (alpha, beta, and release candidate releases), If version is set, this is ignored") } // --manifests is an alias for --set installPackagePath= @@ -141,7 +144,7 @@ func install(writer io.Writer, iArgs *InstallArgs) error { return err } - err = installManifests(profile, writer) + err = installManifests(profile, writer, iArgs.Devel) if err != nil { return fmt.Errorf("failed to install manifests: %v", err) } @@ -192,8 +195,8 @@ func promptProfileName(writer io.Writer) string { } -func installManifests(profile *helm.Profile, writer io.Writer) error { - installer, err := installer.NewInstaller(profile, writer, false) +func installManifests(profile *helm.Profile, writer io.Writer, devel bool) error { + installer, err := installer.NewInstaller(profile, writer, false, devel, installer.InstallInstallerMode) if err != nil { return err } diff --git a/pkg/cmd/hgctl/installer/component.go b/pkg/cmd/hgctl/installer/component.go index 9230fd89f..2de761427 100644 --- a/pkg/cmd/hgctl/installer/component.go +++ b/pkg/cmd/hgctl/installer/component.go @@ -52,6 +52,8 @@ type ComponentOptions struct { Quiet bool // Capabilities Capabilities *chartutil.Capabilities + // devel + Devel bool } type ComponentOption func(*ComponentOptions) @@ -98,6 +100,12 @@ func WithQuiet() ComponentOption { } } +func WithDevel(devel bool) ComponentOption { + return func(opts *ComponentOptions) { + opts.Devel = devel + } +} + func renderComponentManifest(spec any, renderer helm.Renderer, addOn bool, name ComponentName, namespace string) (string, error) { var valsBytes []byte var valsYaml string diff --git a/pkg/cmd/hgctl/installer/higress.go b/pkg/cmd/hgctl/installer/higress.go index 45d152625..852730ab0 100644 --- a/pkg/cmd/hgctl/installer/higress.go +++ b/pkg/cmd/hgctl/installer/higress.go @@ -52,7 +52,7 @@ func (h *HigressComponent) Run() error { // Parse latest version if h.opts.Version == helm.RepoLatestVersion { - latestVersion, err := helm.ParseLatestVersion(h.opts.RepoURL, h.opts.Version) + latestVersion, err := helm.ParseLatestVersion(h.opts.RepoURL, h.opts.Version, h.opts.Devel) if err != nil { return err } diff --git a/pkg/cmd/hgctl/installer/installer.go b/pkg/cmd/hgctl/installer/installer.go index 7baaacc70..996df4d21 100644 --- a/pkg/cmd/hgctl/installer/installer.go +++ b/pkg/cmd/hgctl/installer/installer.go @@ -28,6 +28,8 @@ import ( "k8s.io/client-go/util/homedir" ) +type InstallerMode int32 + const ( HgctlHomeDirPath = ".hgctl" StandaloneInstalledPath = "higress-standalone" @@ -37,20 +39,26 @@ const ( DefaultIstioNamespace = "istio-system" ) +const ( + InstallInstallerMode InstallerMode = iota + UpgradeInstallerMode + UninstallInstallerMode +) + type Installer interface { Install() error UnInstall() error Upgrade() error } -func NewInstaller(profile *helm.Profile, writer io.Writer, quiet bool) (Installer, error) { +func NewInstaller(profile *helm.Profile, writer io.Writer, quiet bool, devel bool, installerMode InstallerMode) (Installer, error) { switch profile.Global.Install { case helm.InstallK8s, helm.InstallLocalK8s: cliClient, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader()) if err != nil { return nil, fmt.Errorf("failed to build kubernetes client: %w", err) } - installer, err := NewK8sInstaller(profile, cliClient, writer, quiet) + installer, err := NewK8sInstaller(profile, cliClient, writer, quiet, devel, installerMode) return installer, err case helm.InstallLocalDocker: installer, err := NewDockerInstaller(profile, writer, quiet) diff --git a/pkg/cmd/hgctl/installer/installer_k8s.go b/pkg/cmd/hgctl/installer/installer_k8s.go index 01905c57c..5966ef7b9 100644 --- a/pkg/cmd/hgctl/installer/installer_k8s.go +++ b/pkg/cmd/hgctl/installer/installer_k8s.go @@ -254,7 +254,7 @@ func (o *K8sInstaller) isNamespacedObject(obj *object.K8sObject) bool { return false } -func NewK8sInstaller(profile *helm.Profile, cli kubernetes.CLIClient, writer io.Writer, quiet bool) (*K8sInstaller, error) { +func NewK8sInstaller(profile *helm.Profile, cli kubernetes.CLIClient, writer io.Writer, quiet bool, devel bool, installerMode InstallerMode) (*K8sInstaller, error) { if profile == nil { return nil, errors.New("install profile is empty") } @@ -267,14 +267,20 @@ func NewK8sInstaller(profile *helm.Profile, cli kubernetes.CLIClient, writer io. } fmt.Fprintf(writer, "%s\n", capabilities.KubeVersion.Version) // initialize components + higressVersion := profile.Charts.Higress.Version + if installerMode == UninstallInstallerMode { + // uninstall + higressVersion = profile.HigressVersion + } components := make(map[ComponentName]Component) opts := []ComponentOption{ WithComponentNamespace(profile.Global.Namespace), WithComponentChartPath(profile.InstallPackagePath), - WithComponentVersion(profile.Charts.Higress.Version), + WithComponentVersion(higressVersion), WithComponentRepoURL(profile.Charts.Higress.Url), WithComponentChartName(profile.Charts.Higress.Name), WithComponentCapabilities(capabilities), + WithDevel(devel), } if quiet { opts = append(opts, WithQuiet()) diff --git a/pkg/cmd/hgctl/manifest.go b/pkg/cmd/hgctl/manifest.go index df8316476..307153eb6 100644 --- a/pkg/cmd/hgctl/manifest.go +++ b/pkg/cmd/hgctl/manifest.go @@ -37,6 +37,8 @@ type ManifestArgs struct { Set []string // ManifestsPath is a path to a ManifestsPath and profiles directory in the local filesystem with a release tgz. ManifestsPath string + // Devel if set true when version is latest, it will get latest version, otherwise it will get latest stable version + Devel bool } func (a *ManifestArgs) String() string { @@ -70,6 +72,7 @@ func addManifestFlags(cmd *cobra.Command, args *ManifestArgs) { cmd.PersistentFlags().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr) cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr) cmd.PersistentFlags().StringVarP(&args.ManifestsPath, "manifests", "d", "", manifestsFlagHelpStr) + cmd.PersistentFlags().BoolVar(&args.Devel, "devel", false, "use development versions (alpha, beta, and release candidate releases), If version is set, this is ignored") } // newManifestGenerateCmd generates a higress install manifest and applies it to a cluster @@ -113,20 +116,20 @@ func generate(writer io.Writer, iArgs *ManifestArgs) error { return err } - err = genManifests(profile, writer) + err = genManifests(profile, writer, iArgs.Devel) if err != nil { return fmt.Errorf("failed to install manifests: %v", err) } return nil } -func genManifests(profile *helm.Profile, writer io.Writer) error { +func genManifests(profile *helm.Profile, writer io.Writer, devel bool) error { cliClient, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader()) if err != nil { return fmt.Errorf("failed to build kubernetes client: %w", err) } - op, err := installer.NewK8sInstaller(profile, cliClient, writer, true) + op, err := installer.NewK8sInstaller(profile, cliClient, writer, true, devel, installer.InstallInstallerMode) if err != nil { return err } diff --git a/pkg/cmd/hgctl/uninstall.go b/pkg/cmd/hgctl/uninstall.go index 055aa4da7..ea1b6911b 100644 --- a/pkg/cmd/hgctl/uninstall.go +++ b/pkg/cmd/hgctl/uninstall.go @@ -126,7 +126,7 @@ func promptUninstall(writer io.Writer) bool { } func uninstallManifests(profile *helm.Profile, writer io.Writer, uiArgs *uninstallArgs) error { - installer, err := installer.NewInstaller(profile, writer, false) + installer, err := installer.NewInstaller(profile, writer, false, false, installer.UninstallInstallerMode) if err != nil { return err } diff --git a/pkg/cmd/hgctl/upgrade.go b/pkg/cmd/hgctl/upgrade.go index b90497af6..1a5df3ed7 100644 --- a/pkg/cmd/hgctl/upgrade.go +++ b/pkg/cmd/hgctl/upgrade.go @@ -37,6 +37,7 @@ func addUpgradeFlags(cmd *cobra.Command, args *upgradeArgs) { cmd.PersistentFlags().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr) cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr) cmd.PersistentFlags().StringVarP(&args.ManifestsPath, "manifests", "d", "", manifestsFlagHelpStr) + cmd.PersistentFlags().BoolVar(&args.Devel, "devel", false, "use development versions (alpha, beta, and release candidate releases), If version is set, this is ignored") } // newUpgradeCmd upgrades Istio control plane in-place with eligibility checks. @@ -91,7 +92,7 @@ func upgrade(writer io.Writer, iArgs *InstallArgs) error { return nil } - err = upgradeManifests(profile, writer) + err = upgradeManifests(profile, writer, iArgs.Devel) if err != nil { return err } @@ -120,8 +121,8 @@ func promptUpgrade(writer io.Writer) bool { } } -func upgradeManifests(profile *helm.Profile, writer io.Writer) error { - installer, err := installer.NewInstaller(profile, writer, false) +func upgradeManifests(profile *helm.Profile, writer io.Writer, devel bool) error { + installer, err := installer.NewInstaller(profile, writer, false, devel, installer.UpgradeInstallerMode) if err != nil { return err }