fix: hgctl latest version bug (#816)

This commit is contained in:
Jun
2024-02-03 12:17:22 +08:00
committed by GitHub
parent 7097eef6ba
commit 5026973d59
9 changed files with 55 additions and 17 deletions

View File

@@ -584,7 +584,7 @@ func locateChart(cpOpts *action.ChartPathOptions, name string, settings *cli.Env
return fileAbsPath, nil return fileAbsPath, nil
} }
func ParseLatestVersion(repoUrl string, version string) (string, error) { func ParseLatestVersion(repoUrl string, version string, devel bool) (string, error) {
cpOpts := &action.ChartPathOptions{ cpOpts := &action.ChartPathOptions{
RepoURL: repoUrl, RepoURL: repoUrl,
@@ -632,7 +632,16 @@ func ParseLatestVersion(repoUrl string, version string) (string, error) {
// get higress helm chart latest version // get higress helm chart latest version
if entries, ok := indexFile.Entries[RepoChartIndexYamlHigressIndex]; ok { 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") return "", errors.New("can't find higress latest version")

View File

@@ -52,6 +52,8 @@ type InstallArgs struct {
Set []string Set []string
// ManifestsPath is a path to a ManifestsPath and profiles directory in the local filesystem with a release tgz. // ManifestsPath is a path to a ManifestsPath and profiles directory in the local filesystem with a release tgz.
ManifestsPath string 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 { 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().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr)
cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr) cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.ManifestsPath, "manifests", "d", "", manifestsFlagHelpStr) 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= // --manifests is an alias for --set installPackagePath=
@@ -141,7 +144,7 @@ func install(writer io.Writer, iArgs *InstallArgs) error {
return err return err
} }
err = installManifests(profile, writer) err = installManifests(profile, writer, iArgs.Devel)
if err != nil { if err != nil {
return fmt.Errorf("failed to install manifests: %v", err) 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 { func installManifests(profile *helm.Profile, writer io.Writer, devel bool) error {
installer, err := installer.NewInstaller(profile, writer, false) installer, err := installer.NewInstaller(profile, writer, false, devel, installer.InstallInstallerMode)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -52,6 +52,8 @@ type ComponentOptions struct {
Quiet bool Quiet bool
// Capabilities // Capabilities
Capabilities *chartutil.Capabilities Capabilities *chartutil.Capabilities
// devel
Devel bool
} }
type ComponentOption func(*ComponentOptions) 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) { func renderComponentManifest(spec any, renderer helm.Renderer, addOn bool, name ComponentName, namespace string) (string, error) {
var valsBytes []byte var valsBytes []byte
var valsYaml string var valsYaml string

View File

@@ -52,7 +52,7 @@ func (h *HigressComponent) Run() error {
// Parse latest version // Parse latest version
if h.opts.Version == helm.RepoLatestVersion { 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 { if err != nil {
return err return err
} }

View File

@@ -28,6 +28,8 @@ import (
"k8s.io/client-go/util/homedir" "k8s.io/client-go/util/homedir"
) )
type InstallerMode int32
const ( const (
HgctlHomeDirPath = ".hgctl" HgctlHomeDirPath = ".hgctl"
StandaloneInstalledPath = "higress-standalone" StandaloneInstalledPath = "higress-standalone"
@@ -37,20 +39,26 @@ const (
DefaultIstioNamespace = "istio-system" DefaultIstioNamespace = "istio-system"
) )
const (
InstallInstallerMode InstallerMode = iota
UpgradeInstallerMode
UninstallInstallerMode
)
type Installer interface { type Installer interface {
Install() error Install() error
UnInstall() error UnInstall() error
Upgrade() 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 { switch profile.Global.Install {
case helm.InstallK8s, helm.InstallLocalK8s: case helm.InstallK8s, helm.InstallLocalK8s:
cliClient, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader()) cliClient, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader())
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to build kubernetes client: %w", err) 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 return installer, err
case helm.InstallLocalDocker: case helm.InstallLocalDocker:
installer, err := NewDockerInstaller(profile, writer, quiet) installer, err := NewDockerInstaller(profile, writer, quiet)

View File

@@ -254,7 +254,7 @@ func (o *K8sInstaller) isNamespacedObject(obj *object.K8sObject) bool {
return false 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 { if profile == nil {
return nil, errors.New("install profile is empty") 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) fmt.Fprintf(writer, "%s\n", capabilities.KubeVersion.Version)
// initialize components // initialize components
higressVersion := profile.Charts.Higress.Version
if installerMode == UninstallInstallerMode {
// uninstall
higressVersion = profile.HigressVersion
}
components := make(map[ComponentName]Component) components := make(map[ComponentName]Component)
opts := []ComponentOption{ opts := []ComponentOption{
WithComponentNamespace(profile.Global.Namespace), WithComponentNamespace(profile.Global.Namespace),
WithComponentChartPath(profile.InstallPackagePath), WithComponentChartPath(profile.InstallPackagePath),
WithComponentVersion(profile.Charts.Higress.Version), WithComponentVersion(higressVersion),
WithComponentRepoURL(profile.Charts.Higress.Url), WithComponentRepoURL(profile.Charts.Higress.Url),
WithComponentChartName(profile.Charts.Higress.Name), WithComponentChartName(profile.Charts.Higress.Name),
WithComponentCapabilities(capabilities), WithComponentCapabilities(capabilities),
WithDevel(devel),
} }
if quiet { if quiet {
opts = append(opts, WithQuiet()) opts = append(opts, WithQuiet())

View File

@@ -37,6 +37,8 @@ type ManifestArgs struct {
Set []string Set []string
// ManifestsPath is a path to a ManifestsPath and profiles directory in the local filesystem with a release tgz. // ManifestsPath is a path to a ManifestsPath and profiles directory in the local filesystem with a release tgz.
ManifestsPath string 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 { 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().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr)
cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr) cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.ManifestsPath, "manifests", "d", "", manifestsFlagHelpStr) 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 // 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 return err
} }
err = genManifests(profile, writer) err = genManifests(profile, writer, iArgs.Devel)
if err != nil { if err != nil {
return fmt.Errorf("failed to install manifests: %v", err) return fmt.Errorf("failed to install manifests: %v", err)
} }
return nil 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()) cliClient, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader())
if err != nil { if err != nil {
return fmt.Errorf("failed to build kubernetes client: %w", err) 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 { if err != nil {
return err return err
} }

View File

@@ -126,7 +126,7 @@ func promptUninstall(writer io.Writer) bool {
} }
func uninstallManifests(profile *helm.Profile, writer io.Writer, uiArgs *uninstallArgs) error { 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 { if err != nil {
return err return err
} }

View File

@@ -37,6 +37,7 @@ func addUpgradeFlags(cmd *cobra.Command, args *upgradeArgs) {
cmd.PersistentFlags().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr) cmd.PersistentFlags().StringSliceVarP(&args.InFilenames, "filename", "f", nil, filenameFlagHelpStr)
cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr) cmd.PersistentFlags().StringArrayVarP(&args.Set, "set", "s", nil, setFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.ManifestsPath, "manifests", "d", "", manifestsFlagHelpStr) 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. // newUpgradeCmd upgrades Istio control plane in-place with eligibility checks.
@@ -91,7 +92,7 @@ func upgrade(writer io.Writer, iArgs *InstallArgs) error {
return nil return nil
} }
err = upgradeManifests(profile, writer) err = upgradeManifests(profile, writer, iArgs.Devel)
if err != nil { if err != nil {
return err return err
} }
@@ -120,8 +121,8 @@ func promptUpgrade(writer io.Writer) bool {
} }
} }
func upgradeManifests(profile *helm.Profile, writer io.Writer) error { func upgradeManifests(profile *helm.Profile, writer io.Writer, devel bool) error {
installer, err := installer.NewInstaller(profile, writer, false) installer, err := installer.NewInstaller(profile, writer, false, devel, installer.UpgradeInstallerMode)
if err != nil { if err != nil {
return err return err
} }