Skip to content

Commit 7b1f538

Browse files
authored
Add detecting higress installed by helm or not before install (alibaba#620)
1 parent 3440356 commit 7b1f538

File tree

8 files changed

+104
-10
lines changed

8 files changed

+104
-10
lines changed

pkg/cmd/hgctl/helm/profile.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,15 @@ func (p ProfileGlobal) Validate(install InstallMode) []error {
8383
}
8484

8585
type ProfileConsole struct {
86-
Port uint32 `json:"port,omitempty"`
87-
Replicas uint32 `json:"replicas,omitempty"`
88-
AdminPasswordValue string `json:"adminPasswordValue,omitempty"`
89-
O11yEnabled bool `json:"o11YEnabled,omitempty"`
86+
Port uint32 `json:"port,omitempty"`
87+
Replicas uint32 `json:"replicas,omitempty"`
88+
O11yEnabled bool `json:"o11YEnabled,omitempty"`
9089
}
9190

9291
func (p ProfileConsole) SetFlags(install InstallMode) ([]string, error) {
9392
sets := make([]string, 0)
9493
if install == InstallK8s || install == InstallLocalK8s {
9594
sets = append(sets, fmt.Sprintf("higress-console.replicaCount=%d", p.Replicas))
96-
sets = append(sets, fmt.Sprintf("higress-console.admin.password.value=%s", p.AdminPasswordValue))
9795
sets = append(sets, fmt.Sprintf("higress-console.o11y.enabled=%t", p.O11yEnabled))
9896
}
9997
return sets, nil

pkg/cmd/hgctl/installer/helm_agent.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2022 Alibaba Group Holding Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package installer
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"io"
21+
"os/exec"
22+
"strings"
23+
24+
"github.com/alibaba/higress/pkg/cmd/hgctl/helm"
25+
"github.com/alibaba/higress/pkg/cmd/options"
26+
)
27+
28+
type HelmRelease struct {
29+
appVersion string `json:"app_version,omitempty"`
30+
chart string `json:"chart,omitempty"`
31+
name string `json:"name,omitempty"`
32+
namespace string `json:"namespace,omitempty"`
33+
revision string `json:"revision,omitempty"`
34+
status string `json:"status,omitempty"`
35+
updated string `json:"updated,omitempty"`
36+
}
37+
38+
type HelmAgent struct {
39+
profile *helm.Profile
40+
writer io.Writer
41+
helmBinaryName string
42+
quiet bool
43+
}
44+
45+
func NewHelmAgent(profile *helm.Profile, writer io.Writer, quiet bool) *HelmAgent {
46+
return &HelmAgent{
47+
profile: profile,
48+
writer: writer,
49+
helmBinaryName: "helm",
50+
quiet: quiet,
51+
}
52+
}
53+
54+
func (h *HelmAgent) IsHigressInstalled() (bool, error) {
55+
args := []string{"list", "-n", h.profile.Global.Namespace, "-f", "higress"}
56+
if len(*options.DefaultConfigFlags.KubeConfig) > 0 {
57+
args = append(args, fmt.Sprintf("--kubeconfig=%s", *options.DefaultConfigFlags.KubeConfig))
58+
}
59+
if len(*options.DefaultConfigFlags.Context) > 0 {
60+
args = append(args, fmt.Sprintf("--kube-context=%s", *options.DefaultConfigFlags.Context))
61+
}
62+
if !h.quiet {
63+
fmt.Fprintf(h.writer, "\n📦 Running command: %s %s\n\n", h.helmBinaryName, strings.Join(args, " "))
64+
}
65+
cmd := exec.Command(h.helmBinaryName, args...)
66+
var out bytes.Buffer
67+
var stderr bytes.Buffer
68+
cmd.Stdout = &out
69+
cmd.Stderr = &stderr
70+
71+
if err := cmd.Start(); err != nil {
72+
return false, nil
73+
}
74+
75+
done := make(chan error, 1)
76+
go func() {
77+
done <- cmd.Wait()
78+
}()
79+
80+
select {
81+
case err := <-done:
82+
if err == nil {
83+
content := out.String()
84+
if !h.quiet {
85+
fmt.Fprintf(h.writer, "\n%s\n", content)
86+
}
87+
if strings.Contains(content, "deployed") {
88+
return true, nil
89+
}
90+
}
91+
}
92+
return false, nil
93+
}

pkg/cmd/hgctl/installer/installer_k8s.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ type K8sInstaller struct {
3535
}
3636

3737
func (o *K8sInstaller) Install() error {
38+
// check if higress is installed by helm
39+
fmt.Fprintf(o.writer, "\n⌛️ Detecting higress installed by helm or not... \n\n")
40+
helmAgent := NewHelmAgent(o.profile, o.writer, false)
41+
if helmInstalled, _ := helmAgent.IsHigressInstalled(); helmInstalled {
42+
fmt.Fprintf(o.writer, "\n🧐 You have already installed higress by helm, please use \"helm upgrade\" to upgrade higress!\n")
43+
return nil
44+
}
45+
3846
if _, err := GetProfileInstalledPath(); err != nil {
3947
return err
4048
}

pkg/cmd/hgctl/installer/standalone_agent.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func (a *Agent) profileArgs() []string {
7777
fmt.Sprintf("--nacos-password=%s", a.profile.Storage.Password),
7878
fmt.Sprintf("--nacos-username=%s", a.profile.Storage.Username),
7979
fmt.Sprintf("--data-enc-key=%s", a.profile.Storage.DataEncKey),
80-
fmt.Sprintf("--console-password=%s", a.profile.Console.AdminPasswordValue),
8180
fmt.Sprintf("--console-port=%d", a.profile.Console.Port),
8281
fmt.Sprintf("--gateway-http-port=%d", a.profile.Gateway.HttpPort),
8382
fmt.Sprintf("--gateway-https-port=%d", a.profile.Gateway.HttpsPort),

pkg/cmd/hgctl/manifests/profiles/_all.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ global:
99
console:
1010
port: 8080
1111
replicas: 1
12-
adminPasswordValue: admin
1312
o11yEnabled: false
1413

1514
gateway:

pkg/cmd/hgctl/manifests/profiles/k8s.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ global:
88

99
console:
1010
replicas: 1
11-
adminPasswordValue: admin
1211
o11yEnabled: false
1312

1413
gateway:

pkg/cmd/hgctl/manifests/profiles/local-docker.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ global:
44

55
console:
66
port: 8080
7-
adminPasswordValue: admin
87

98
gateway:
109
httpPort: 80

pkg/cmd/hgctl/manifests/profiles/local-k8s.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ global:
88

99
console:
1010
replicas: 1
11-
adminPasswordValue: admin
1211
o11yEnabled: true
1312

1413
gateway:

0 commit comments

Comments
 (0)