Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hgctl upgrade support from-helm flag #824

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 65 additions & 5 deletions pkg/cmd/hgctl/installer/helm_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/alibaba/higress/pkg/cmd/hgctl/helm"
"github.com/alibaba/higress/pkg/cmd/options"
"sigs.k8s.io/yaml"
)

type HelmRelease struct {
Expand Down Expand Up @@ -51,7 +52,7 @@ func NewHelmAgent(profile *helm.Profile, writer io.Writer, quiet bool) *HelmAgen
}
}

func (h *HelmAgent) IsHigressInstalled() (bool, error) {
func (h *HelmAgent) GetHigressInformance() (bool, map[string]any, error) {
args := []string{"list", "-n", h.profile.Global.Namespace, "-f", "higress"}
if len(*options.DefaultConfigFlags.KubeConfig) > 0 {
args = append(args, fmt.Sprintf("--kubeconfig=%s", *options.DefaultConfigFlags.KubeConfig))
Expand All @@ -69,7 +70,7 @@ func (h *HelmAgent) IsHigressInstalled() (bool, error) {
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
return false, nil
return false, nil, nil
}

done := make(chan error, 1)
Expand All @@ -84,10 +85,69 @@ func (h *HelmAgent) IsHigressInstalled() (bool, error) {
if !h.quiet {
fmt.Fprintf(h.writer, "\n%s\n", content)
}
if strings.Contains(content, "deployed") {
return true, nil
split := strings.Split(content, "\n")
for i, line := range split {
if i == 0 {
continue
}
param := strings.Split(line, "\t")
if len(param) != 7 {
continue
}
// check chart contains higress
if strings.Contains(param[5], "higress") && strings.Contains(param[4], "deployed") {
releaseName := param[0]
valueFlag := h.getValueFlag(releaseName)
return true, valueFlag, nil
}
}
}
}
return false, nil
return false, nil, nil
}

func (h *HelmAgent) getValueFlag(releaseName string) map[string]any {
args := []string{"status", releaseName, "-n", h.profile.Global.Namespace, "-o", "yaml"}
if len(*options.DefaultConfigFlags.KubeConfig) > 0 {
args = append(args, fmt.Sprintf("--kubeconfig=%s", *options.DefaultConfigFlags.KubeConfig))
}
if len(*options.DefaultConfigFlags.Context) > 0 {
args = append(args, fmt.Sprintf("--kube-context=%s", *options.DefaultConfigFlags.Context))
}
if !h.quiet {
fmt.Fprintf(h.writer, "\n📦 Running command: %s %s\n\n", h.helmBinaryName, strings.Join(args, " "))
}
cmd := exec.Command(h.helmBinaryName, args...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
return nil
}

done := make(chan error, 1)

go func() {
done <- cmd.Wait()
}()

select {
case err := <-done:
if err == nil {
content := out.String()
statusMap := make(map[string]any)
err = yaml.Unmarshal([]byte(content), &statusMap)
if err != nil {
return nil
}
if config, ok := statusMap["config"]; ok {
if valueMap, ok := config.(map[string]any); ok {
return valueMap
}
}
}
}
return nil
}
25 changes: 22 additions & 3 deletions pkg/cmd/hgctl/installer/installer_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/alibaba/higress/pkg/cmd/hgctl/helm/object"
"github.com/alibaba/higress/pkg/cmd/hgctl/kubernetes"
"github.com/alibaba/higress/pkg/cmd/hgctl/util"
"sigs.k8s.io/yaml"
)

type K8sInstaller struct {
Expand All @@ -34,16 +35,33 @@ type K8sInstaller struct {
kubeCli kubernetes.CLIClient
profile *helm.Profile
writer io.Writer
upgrade bool
profileStore ProfileStore
}

func (o *K8sInstaller) Install() error {
// check if higress is installed by helm
fmt.Fprintf(o.writer, "\n⌛️ Detecting higress installed by helm or not... \n\n")
helmAgent := NewHelmAgent(o.profile, o.writer, false)
if helmInstalled, _ := helmAgent.IsHigressInstalled(); helmInstalled {
fmt.Fprintf(o.writer, "\n🧐 You have already installed higress by helm, please use \"helm upgrade\" to upgrade higress!\n")
return nil
if helmInstalled, valueMap, _ := helmAgent.GetHigressInformance(); helmInstalled {
if !o.upgrade {
fmt.Fprintf(o.writer, "\n🧐 You have already installed higress by helm, please use \"hgctl upgrade\" to upgrade higress!\n")
return nil
}
if o.profile.Values == nil {
o.profile.Values = valueMap
} else {
baseYaml := util.ToYAML(o.profile.Values)
overlayYaml := util.ToYAML(valueMap)
mergedYaml, err := util.OverlayYAML(baseYaml, overlayYaml)
if err != nil {
return err
}
err = yaml.Unmarshal([]byte(mergedYaml), &o.profile.Values)
if err != nil {
return err
}
}
}

if err := o.Run(); err != nil {
Expand Down Expand Up @@ -107,6 +125,7 @@ func (o *K8sInstaller) UnInstall() error {
}

func (o *K8sInstaller) Upgrade() error {
o.upgrade = true
return o.Install()
}

Expand Down
57 changes: 39 additions & 18 deletions pkg/cmd/hgctl/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,31 @@ import (

type upgradeArgs struct {
*InstallArgs
// FromHelm if set true, it will convert helm chart to higress profile
FromHelm bool
}

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")
cmd.PersistentFlags().BoolVar(&args.FromHelm, "from-helm", false, "upgrade by read helm release")
}

// newUpgradeCmd upgrades Istio control plane in-place with eligibility checks.
func newUpgradeCmd() *cobra.Command {
upgradeArgs := &upgradeArgs{
InstallArgs: &InstallArgs{},
FromHelm: false,
}
upgradeCmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade Higress in-place",
Long: "The upgrade command is an alias for the install command" +
" that performs additional upgrade-related checks.",
RunE: func(cmd *cobra.Command, args []string) (e error) {
return upgrade(cmd.OutOrStdout(), upgradeArgs.InstallArgs)
return upgrade(cmd.OutOrStdout(), upgradeArgs)
},
}
addUpgradeFlags(upgradeCmd, upgradeArgs)
Expand All @@ -61,29 +65,46 @@ func newUpgradeCmd() *cobra.Command {
}

// upgrade upgrade higress resources from the cluster.
func upgrade(writer io.Writer, iArgs *InstallArgs) error {
func upgrade(writer io.Writer, iArgs *upgradeArgs) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

升级时候 如果 --from-helm = true, 可以读取helm 安装所有 release 列表,让用户选择升级那个 release, 如果用户选择某个RELEASE 后,要检查这个 RELEASE 是否已经升级( 是否已经有 install profile),如果没有让用户升级。

setFlags := applyFlagAliases(iArgs.Set, iArgs.ManifestsPath)
fmt.Fprintf(writer, "⌛️ Checking higress installed profiles...\n")
profileContexts, _ := getAllProfiles()
if len(profileContexts) == 0 {
fmt.Fprintf(writer, "\nHigress hasn't been installed yet!\n")
return nil
}

valuesOverlay, err := helm.GetValuesOverylayFromFiles(iArgs.InFilenames)
if err != nil {
return err
}
var profile *helm.Profile

profileContext := promptProfileContexts(writer, profileContexts)
if !iArgs.FromHelm {
fmt.Fprintf(writer, "⌛️ Checking higress installed profiles...\n")
profileContexts, _ := getAllProfiles()
if len(profileContexts) == 0 {
fmt.Fprintf(writer, "\nHigress hasn't been installed yet!\n")
return nil
}

_, profile, err := helm.GenProfileFromProfileContent(util.ToYAML(profileContext.Profile), valuesOverlay, setFlags)
if err != nil {
return err
valuesOverlay, err := helm.GetValuesOverylayFromFiles(iArgs.InFilenames)
if err != nil {
return err
}

profileContext := promptProfileContexts(writer, profileContexts)

_, profile, err = helm.GenProfileFromProfileContent(util.ToYAML(profileContext.Profile), valuesOverlay, setFlags)
if err != nil {
return err
}

fmt.Fprintf(writer, "\n🧐 Validating Profile: \"%s\" \n", profileContext.PathOrName)
} else {
helmAgent := installer.NewHelmAgent(nil, writer, false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 profile 为 nil, 会 报 nil pointer

installed, _, err := helmAgent.GetHigressInformance()
if err != nil {
return err
}
if !installed {
fmt.Fprintf(writer, "\nHigress hasn't been installed by helm yet!\n")
return nil
}
// TODO: convert helm values to higress profile
}

fmt.Fprintf(writer, "\n🧐 Validating Profile: \"%s\" \n", profileContext.PathOrName)
err = profile.Validate()
err := profile.Validate()
if err != nil {
return err
}
Expand Down