From f363836b0a68078da235c712d56379baed833a23 Mon Sep 17 00:00:00 2001 From: jun <2456868764@qq.com> Date: Thu, 2 Nov 2023 14:15:10 +0800 Subject: [PATCH 1/3] Add detecting higress installed by helm or not before install --- pkg/cmd/hgctl/installer/helm_agent.go | 90 ++++++++++++++++++++++++ pkg/cmd/hgctl/installer/installer_k8s.go | 8 +++ 2 files changed, 98 insertions(+) create mode 100644 pkg/cmd/hgctl/installer/helm_agent.go diff --git a/pkg/cmd/hgctl/installer/helm_agent.go b/pkg/cmd/hgctl/installer/helm_agent.go new file mode 100644 index 0000000000..85745b027e --- /dev/null +++ b/pkg/cmd/hgctl/installer/helm_agent.go @@ -0,0 +1,90 @@ +// Copyright (c) 2022 Alibaba Group Holding Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package installer + +import ( + "bytes" + "fmt" + "io" + "os/exec" + "strings" + + "github.com/alibaba/higress/pkg/cmd/options" +) + +type HelmRelease struct { + appVersion string `json:"app_version,omitempty"` + chart string `json:"chart,omitempty"` + name string `json:"name,omitempty"` + namespace string `json:"namespace,omitempty"` + revision string `json:"revision,omitempty"` + status string `json:"status,omitempty"` + updated string `json:"updated,omitempty"` +} + +type HelmAgent struct { + writer io.Writer + helmBinaryName string + quiet bool +} + +func NewHelmAgent(writer io.Writer, quiet bool) *HelmAgent { + return &HelmAgent{ + writer: writer, + helmBinaryName: "helm", + quiet: quiet, + } +} + +func (h *HelmAgent) IsHigressInstalled() (bool, error) { + args := []string{"list", "-A", "-f", "higress"} + 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 false, nil + } + + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + + select { + case err := <-done: + if err == nil { + content := out.String() + if !h.quiet { + fmt.Fprintf(h.writer, "\n%s\n", content) + } + if strings.Contains(content, "deployed") { + return true, nil + } + } + } + return false, nil +} diff --git a/pkg/cmd/hgctl/installer/installer_k8s.go b/pkg/cmd/hgctl/installer/installer_k8s.go index 2fa606a5c5..1de2801b4c 100644 --- a/pkg/cmd/hgctl/installer/installer_k8s.go +++ b/pkg/cmd/hgctl/installer/installer_k8s.go @@ -35,6 +35,14 @@ type K8sInstaller struct { } 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.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 _, err := GetProfileInstalledPath(); err != nil { return err } From cc082df020d25f9993c039981b81fe757be723ff Mon Sep 17 00:00:00 2001 From: jun <2456868764@qq.com> Date: Thu, 2 Nov 2023 15:05:16 +0800 Subject: [PATCH 2/3] update console password property --- pkg/cmd/hgctl/helm/profile.go | 10 +++++----- pkg/cmd/hgctl/installer/helm_agent.go | 7 +++++-- pkg/cmd/hgctl/installer/installer_k8s.go | 2 +- pkg/cmd/hgctl/installer/standalone_agent.go | 2 +- pkg/cmd/hgctl/manifests/profiles/_all.yaml | 2 +- pkg/cmd/hgctl/manifests/profiles/k8s.yaml | 2 +- pkg/cmd/hgctl/manifests/profiles/local-docker.yaml | 2 +- pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml | 2 +- 8 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pkg/cmd/hgctl/helm/profile.go b/pkg/cmd/hgctl/helm/profile.go index 848143f5d6..b8b0aa376c 100644 --- a/pkg/cmd/hgctl/helm/profile.go +++ b/pkg/cmd/hgctl/helm/profile.go @@ -83,17 +83,17 @@ func (p ProfileGlobal) Validate(install InstallMode) []error { } type ProfileConsole struct { - Port uint32 `json:"port,omitempty"` - Replicas uint32 `json:"replicas,omitempty"` - AdminPasswordValue string `json:"adminPasswordValue,omitempty"` - O11yEnabled bool `json:"o11YEnabled,omitempty"` + Port uint32 `json:"port,omitempty"` + Replicas uint32 `json:"replicas,omitempty"` + AdminPassword string `json:"adminPassword,omitempty"` + O11yEnabled bool `json:"o11YEnabled,omitempty"` } func (p ProfileConsole) SetFlags(install InstallMode) ([]string, error) { sets := make([]string, 0) if install == InstallK8s || install == InstallLocalK8s { sets = append(sets, fmt.Sprintf("higress-console.replicaCount=%d", p.Replicas)) - sets = append(sets, fmt.Sprintf("higress-console.admin.password.value=%s", p.AdminPasswordValue)) + sets = append(sets, fmt.Sprintf("higress-console.admin.password=%s", p.AdminPassword)) sets = append(sets, fmt.Sprintf("higress-console.o11y.enabled=%t", p.O11yEnabled)) } return sets, nil diff --git a/pkg/cmd/hgctl/installer/helm_agent.go b/pkg/cmd/hgctl/installer/helm_agent.go index 85745b027e..3d13eb49e1 100644 --- a/pkg/cmd/hgctl/installer/helm_agent.go +++ b/pkg/cmd/hgctl/installer/helm_agent.go @@ -21,6 +21,7 @@ import ( "os/exec" "strings" + "github.com/alibaba/higress/pkg/cmd/hgctl/helm" "github.com/alibaba/higress/pkg/cmd/options" ) @@ -35,13 +36,15 @@ type HelmRelease struct { } type HelmAgent struct { + profile *helm.Profile writer io.Writer helmBinaryName string quiet bool } -func NewHelmAgent(writer io.Writer, quiet bool) *HelmAgent { +func NewHelmAgent(profile *helm.Profile, writer io.Writer, quiet bool) *HelmAgent { return &HelmAgent{ + profile: profile, writer: writer, helmBinaryName: "helm", quiet: quiet, @@ -49,7 +52,7 @@ func NewHelmAgent(writer io.Writer, quiet bool) *HelmAgent { } func (h *HelmAgent) IsHigressInstalled() (bool, error) { - args := []string{"list", "-A", "-f", "higress"} + 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)) } diff --git a/pkg/cmd/hgctl/installer/installer_k8s.go b/pkg/cmd/hgctl/installer/installer_k8s.go index 1de2801b4c..2b3f8d9646 100644 --- a/pkg/cmd/hgctl/installer/installer_k8s.go +++ b/pkg/cmd/hgctl/installer/installer_k8s.go @@ -37,7 +37,7 @@ type K8sInstaller struct { 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.writer, false) + 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 diff --git a/pkg/cmd/hgctl/installer/standalone_agent.go b/pkg/cmd/hgctl/installer/standalone_agent.go index 8b23f4eac9..b0669e55b1 100644 --- a/pkg/cmd/hgctl/installer/standalone_agent.go +++ b/pkg/cmd/hgctl/installer/standalone_agent.go @@ -77,7 +77,7 @@ func (a *Agent) profileArgs() []string { fmt.Sprintf("--nacos-password=%s", a.profile.Storage.Password), fmt.Sprintf("--nacos-username=%s", a.profile.Storage.Username), fmt.Sprintf("--data-enc-key=%s", a.profile.Storage.DataEncKey), - fmt.Sprintf("--console-password=%s", a.profile.Console.AdminPasswordValue), + fmt.Sprintf("--console-password=%s", a.profile.Console.AdminPassword), fmt.Sprintf("--console-port=%d", a.profile.Console.Port), fmt.Sprintf("--gateway-http-port=%d", a.profile.Gateway.HttpPort), fmt.Sprintf("--gateway-https-port=%d", a.profile.Gateway.HttpsPort), diff --git a/pkg/cmd/hgctl/manifests/profiles/_all.yaml b/pkg/cmd/hgctl/manifests/profiles/_all.yaml index 5acd780655..905a520772 100644 --- a/pkg/cmd/hgctl/manifests/profiles/_all.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/_all.yaml @@ -9,7 +9,7 @@ global: console: port: 8080 replicas: 1 - adminPasswordValue: admin + adminPassword: admin o11yEnabled: false gateway: diff --git a/pkg/cmd/hgctl/manifests/profiles/k8s.yaml b/pkg/cmd/hgctl/manifests/profiles/k8s.yaml index e7a1321ea7..6f8f9f7a58 100644 --- a/pkg/cmd/hgctl/manifests/profiles/k8s.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/k8s.yaml @@ -8,7 +8,7 @@ global: console: replicas: 1 - adminPasswordValue: admin + adminPassword: admin o11yEnabled: false gateway: diff --git a/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml b/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml index 69a4cd1524..6541f9fde7 100644 --- a/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml @@ -4,7 +4,7 @@ global: console: port: 8080 - adminPasswordValue: admin + adminPassword: admin gateway: httpPort: 80 diff --git a/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml b/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml index 83b698a53d..0499655d8a 100644 --- a/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml @@ -8,7 +8,7 @@ global: console: replicas: 1 - adminPasswordValue: admin + adminPassword: admin o11yEnabled: true gateway: From 66b18b4b38642645fdbb9c0980f30450cf49f585 Mon Sep 17 00:00:00 2001 From: jun <2456868764@qq.com> Date: Thu, 2 Nov 2023 15:33:59 +0800 Subject: [PATCH 3/3] remove adminPassword in profile --- pkg/cmd/hgctl/helm/profile.go | 8 +++----- pkg/cmd/hgctl/installer/standalone_agent.go | 1 - pkg/cmd/hgctl/manifests/profiles/_all.yaml | 1 - pkg/cmd/hgctl/manifests/profiles/k8s.yaml | 1 - pkg/cmd/hgctl/manifests/profiles/local-docker.yaml | 1 - pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml | 1 - 6 files changed, 3 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/hgctl/helm/profile.go b/pkg/cmd/hgctl/helm/profile.go index b8b0aa376c..7d6d1a2352 100644 --- a/pkg/cmd/hgctl/helm/profile.go +++ b/pkg/cmd/hgctl/helm/profile.go @@ -83,17 +83,15 @@ func (p ProfileGlobal) Validate(install InstallMode) []error { } type ProfileConsole struct { - Port uint32 `json:"port,omitempty"` - Replicas uint32 `json:"replicas,omitempty"` - AdminPassword string `json:"adminPassword,omitempty"` - O11yEnabled bool `json:"o11YEnabled,omitempty"` + Port uint32 `json:"port,omitempty"` + Replicas uint32 `json:"replicas,omitempty"` + O11yEnabled bool `json:"o11YEnabled,omitempty"` } func (p ProfileConsole) SetFlags(install InstallMode) ([]string, error) { sets := make([]string, 0) if install == InstallK8s || install == InstallLocalK8s { sets = append(sets, fmt.Sprintf("higress-console.replicaCount=%d", p.Replicas)) - sets = append(sets, fmt.Sprintf("higress-console.admin.password=%s", p.AdminPassword)) sets = append(sets, fmt.Sprintf("higress-console.o11y.enabled=%t", p.O11yEnabled)) } return sets, nil diff --git a/pkg/cmd/hgctl/installer/standalone_agent.go b/pkg/cmd/hgctl/installer/standalone_agent.go index b0669e55b1..805947d5c4 100644 --- a/pkg/cmd/hgctl/installer/standalone_agent.go +++ b/pkg/cmd/hgctl/installer/standalone_agent.go @@ -77,7 +77,6 @@ func (a *Agent) profileArgs() []string { fmt.Sprintf("--nacos-password=%s", a.profile.Storage.Password), fmt.Sprintf("--nacos-username=%s", a.profile.Storage.Username), fmt.Sprintf("--data-enc-key=%s", a.profile.Storage.DataEncKey), - fmt.Sprintf("--console-password=%s", a.profile.Console.AdminPassword), fmt.Sprintf("--console-port=%d", a.profile.Console.Port), fmt.Sprintf("--gateway-http-port=%d", a.profile.Gateway.HttpPort), fmt.Sprintf("--gateway-https-port=%d", a.profile.Gateway.HttpsPort), diff --git a/pkg/cmd/hgctl/manifests/profiles/_all.yaml b/pkg/cmd/hgctl/manifests/profiles/_all.yaml index 905a520772..8a92e68b44 100644 --- a/pkg/cmd/hgctl/manifests/profiles/_all.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/_all.yaml @@ -9,7 +9,6 @@ global: console: port: 8080 replicas: 1 - adminPassword: admin o11yEnabled: false gateway: diff --git a/pkg/cmd/hgctl/manifests/profiles/k8s.yaml b/pkg/cmd/hgctl/manifests/profiles/k8s.yaml index 6f8f9f7a58..e1e805e62d 100644 --- a/pkg/cmd/hgctl/manifests/profiles/k8s.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/k8s.yaml @@ -8,7 +8,6 @@ global: console: replicas: 1 - adminPassword: admin o11yEnabled: false gateway: diff --git a/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml b/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml index 6541f9fde7..377bc1f861 100644 --- a/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/local-docker.yaml @@ -4,7 +4,6 @@ global: console: port: 8080 - adminPassword: admin gateway: httpPort: 80 diff --git a/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml b/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml index 0499655d8a..9f6b09e346 100644 --- a/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml +++ b/pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml @@ -8,7 +8,6 @@ global: console: replicas: 1 - adminPassword: admin o11yEnabled: true gateway: