feat: opt hgctl install/uninstall/upgrade (#550)

Signed-off-by: bitliu <bitliu@tencent.com>
This commit is contained in:
Xunzhuo
2023-09-21 13:44:09 +08:00
committed by GitHub
parent 587267a733
commit b142f51776
4 changed files with 61 additions and 53 deletions

View File

@@ -120,14 +120,16 @@ func Install(writer io.Writer, iArgs *InstallArgs) error {
setFlags = append(setFlags, fmt.Sprintf("profile=%s", psf)) setFlags = append(setFlags, fmt.Sprintf("profile=%s", psf))
} }
if !promptInstall(writer, psf) {
return nil
}
_, profile, profileName, err := helm.GenerateConfig(iArgs.InFilenames, setFlags) _, profile, profileName, err := helm.GenerateConfig(iArgs.InFilenames, setFlags)
if err != nil { if err != nil {
return fmt.Errorf("generate config: %v", err) return fmt.Errorf("generate config: %v", err)
} }
fmt.Fprintf(writer, "start to install higress on profile:%s ......\n", profileName)
fmt.Fprintf(writer, "start to validate profile:%s ......\n", profileName)
fmt.Fprintf(writer, "🧐 Validating Profile: \"%s\" \n", profileName)
err = profile.Validate() err = profile.Validate()
if err != nil { if err != nil {
return err return err
@@ -140,6 +142,22 @@ func Install(writer io.Writer, iArgs *InstallArgs) error {
return nil return nil
} }
func promptInstall(writer io.Writer, profileName string) bool {
answer := ""
for {
fmt.Fprintf(writer, "\nThis will install Higress \"%s\" profile into the cluster. \nProceed? (y/N)", profileName)
fmt.Scanln(&answer)
if strings.TrimSpace(answer) == "y" {
fmt.Fprintf(writer, "\n")
return true
}
if strings.TrimSpace(answer) == "N" {
fmt.Fprintf(writer, "Cancelled.\n")
return false
}
}
}
func promptProfileName(writer io.Writer) string { func promptProfileName(writer io.Writer) string {
answer := "" answer := ""
fmt.Fprintf(writer, "Please select higress install configration profile:\n") fmt.Fprintf(writer, "Please select higress install configration profile:\n")
@@ -156,39 +174,31 @@ func promptProfileName(writer io.Writer) string {
} }
} }
return profileNameLocalK8s
} }
func InstallManifests(profile *helm.Profile, writer io.Writer) error { func InstallManifests(profile *helm.Profile, writer io.Writer) error {
fmt.Fprintf(writer, "start to check kubernetes cluster enviroment ......\n")
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)
} }
fmt.Fprintf(writer, "start to init higress installer ......\n")
op, err := installer.NewInstaller(profile, cliClient, writer) op, err := installer.NewInstaller(profile, cliClient, writer)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(writer, "start to run higress installer ......\n")
if err := op.Run(); err != nil { if err := op.Run(); err != nil {
return err return err
} }
fmt.Fprintf(writer, "start to render manifests ......\n")
manifestMap, err := op.RenderManifests() manifestMap, err := op.RenderManifests()
//for name, yaml := range manifestMap {
// fileName := "~/Downloads/higress/manifests/" + string(name) + ".yaml"
// os.WriteFile(fileName, []byte(yaml), 0640)
//}
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(writer, "start to apply manifests ......\n")
fmt.Fprintf(writer, "\n⌛ Processing installation... \n\n")
if err := op.ApplyManifests(manifestMap); err != nil { if err := op.ApplyManifests(manifestMap); err != nil {
return err return err
} }
fmt.Fprintf(writer, "install higress complete!\n") fmt.Fprintf(writer, "\n🎊 Install All Resources Complete!\n")
return nil return nil
} }

View File

@@ -114,18 +114,19 @@ func (h *HigressComponent) Enabled() bool {
func (h *HigressComponent) Run() error { func (h *HigressComponent) Run() error {
// Parse latest version // Parse latest version
if h.opts.Version == helm.RepoLatestVersion { if h.opts.Version == helm.RepoLatestVersion {
fmt.Fprintf(h.writer, "start to get higress helm chart latest version ......")
}
latestVersion, err := helm.ParseLatestVersion(h.opts.RepoURL, h.opts.Version)
if err != nil {
return err
}
fmt.Fprintf(h.writer, "latest version is %s\n", latestVersion)
// Reset helm chart version latestVersion, err := helm.ParseLatestVersion(h.opts.RepoURL, h.opts.Version)
h.opts.Version = latestVersion if err != nil {
h.renderer.SetVersion(latestVersion) return err
fmt.Fprintf(h.writer, "start to download higress helm chart version: %s, url: %s\n", h.opts.Version, h.opts.RepoURL) }
fmt.Fprintf(h.writer, "⚡️ Fetching Higress Helm Chart latest version \"%s\" \n", latestVersion)
// Reset Helm Chart version
h.opts.Version = latestVersion
h.renderer.SetVersion(latestVersion)
}
fmt.Fprintf(h.writer, "🏄 Downloading Higress Helm Chart version: %s, url: %s\n", h.opts.Version, h.opts.RepoURL)
if err := h.renderer.Init(); err != nil { if err := h.renderer.Init(); err != nil {
return err return err
@@ -138,7 +139,7 @@ func (h *HigressComponent) RenderManifest() (string, error) {
if !h.started { if !h.started {
return "", nil return "", nil
} }
fmt.Fprintf(h.writer, "start to render higress helm chart......\n") fmt.Fprintf(h.writer, "📦 Rendering Higress Helm Chart\n")
valsYaml, err := h.profile.ValuesYaml() valsYaml, err := h.profile.ValuesYaml()
if err != nil { if err != nil {
return "", err return "", err
@@ -251,7 +252,7 @@ func (i *IstioCRDComponent) Enabled() bool {
} }
func (i *IstioCRDComponent) Run() error { func (i *IstioCRDComponent) Run() error {
fmt.Fprintf(i.writer, "start to download istio helm chart version: %s, url: %s\n", i.opts.Version, i.opts.RepoURL) fmt.Fprintf(i.writer, "🏄 Downloading Istio Helm Chart version: %s, url: %s\n", i.opts.Version, i.opts.RepoURL)
if err := i.renderer.Init(); err != nil { if err := i.renderer.Init(); err != nil {
return err return err
} }
@@ -263,7 +264,7 @@ func (i *IstioCRDComponent) RenderManifest() (string, error) {
if !i.started { if !i.started {
return "", nil return "", nil
} }
fmt.Fprintf(i.writer, "start to render istio helm chart......\n") fmt.Fprintf(i.writer, "📦 Rendering Istio Helm Chart\n")
values := make(map[string]any) values := make(map[string]any)
manifest, err := renderComponentManifest(values, i.renderer, false, i.ComponentName(), i.opts.Namespace) manifest, err := renderComponentManifest(values, i.renderer, false, i.ComponentName(), i.opts.Namespace)
if err != nil { if err != nil {

View File

@@ -94,9 +94,9 @@ func (o *Installer) applyManifest(manifest string, ns string) error {
obj.UnstructuredObject().SetNamespace(ns) obj.UnstructuredObject().SetNamespace(ns)
} }
if o.isNamespacedObject(obj) { if o.isNamespacedObject(obj) {
fmt.Fprintf(o.writer, "start to apply object kind: %s, object name: %s on namespace: %s ......\n", obj.Kind, obj.Name, obj.Namespace) fmt.Fprintf(o.writer, "✔️ Installed %s:%s:%s.\n", obj.Kind, obj.Name, obj.Namespace)
} else { } else {
fmt.Fprintf(o.writer, "start to apply object kind: %s, object name: %s ......\n", obj.Kind, obj.Name) fmt.Fprintf(o.writer, "✔️ Installed %s::%s.\n", obj.Kind, obj.Name)
} }
if err := o.kubeCli.ApplyObject(obj.UnstructuredObject()); err != nil { if err := o.kubeCli.ApplyObject(obj.UnstructuredObject()); err != nil {
return err return err
@@ -132,9 +132,9 @@ func (o *Installer) deleteManifest(manifest string, ns string) error {
obj.UnstructuredObject().SetNamespace(ns) obj.UnstructuredObject().SetNamespace(ns)
} }
if o.isNamespacedObject(obj) { if o.isNamespacedObject(obj) {
fmt.Fprintf(o.writer, "start to delete object kind: %s, object name: %s on namespace: %s ......\n", obj.Kind, obj.Name, obj.Namespace) fmt.Fprintf(o.writer, "✔️ Removed %s:%s:%s.\n", obj.Kind, obj.Name, obj.Namespace)
} else { } else {
fmt.Fprintf(o.writer, "start to delete object kind: %s, object name: %s ......\n", obj.Kind, obj.Name) fmt.Fprintf(o.writer, "✔️ Removed %s::%s.\n", obj.Kind, obj.Name)
} }
if err := o.kubeCli.DeleteObject(obj.UnstructuredObject()); err != nil { if err := o.kubeCli.DeleteObject(obj.UnstructuredObject()); err != nil {
return err return err
@@ -154,7 +154,7 @@ func (o *Installer) isNamespacedObject(obj *object.K8sObject) bool {
func NewInstaller(profile *helm.Profile, cli kubernetes.CLIClient, writer io.Writer) (*Installer, error) { func NewInstaller(profile *helm.Profile, cli kubernetes.CLIClient, writer io.Writer) (*Installer, error) {
if profile == nil { if profile == nil {
return nil, errors.New("Install profile is empty") return nil, errors.New("install profile is empty")
} }
// initialize components // initialize components
components := make(map[ComponentName]Component) components := make(map[ComponentName]Component)

View File

@@ -73,18 +73,17 @@ func newUninstallCmd() *cobra.Command {
// uninstall uninstalls control plane by either pruning by target revision or deleting specified manifests. // uninstall uninstalls control plane by either pruning by target revision or deleting specified manifests.
func uninstall(writer io.Writer, uiArgs *uninstallArgs) error { func uninstall(writer io.Writer, uiArgs *uninstallArgs) error {
if !promptUninstall(writer) {
return nil
}
fmt.Fprintf(writer, "start to uninstall higress\n")
setFlags := make([]string, 0) setFlags := make([]string, 0)
profileName := helm.GetUninstallProfileName() profileName := helm.GetUninstallProfileName()
fmt.Fprintf(writer, "start to uninstall higress profile:%s\n", profileName)
_, profile, err := helm.GenProfile(profileName, "", setFlags) _, profile, err := helm.GenProfile(profileName, "", setFlags)
if err != nil { if err != nil {
return err return err
} }
if !promptUninstall(writer) {
return nil
}
profile.Global.EnableIstioAPI = uiArgs.purgeIstioCRD profile.Global.EnableIstioAPI = uiArgs.purgeIstioCRD
profile.Global.Namespace = uiArgs.namespace profile.Global.Namespace = uiArgs.namespace
profile.Global.IstioNamespace = uiArgs.istioNamespace profile.Global.IstioNamespace = uiArgs.istioNamespace
@@ -97,45 +96,43 @@ func uninstall(writer io.Writer, uiArgs *uninstallArgs) error {
func promptUninstall(writer io.Writer) bool { func promptUninstall(writer io.Writer) bool {
answer := "" answer := ""
fmt.Fprintf(writer, "Are you sure to uninstall higress?\n")
for { for {
fmt.Fprintf(writer, "Please input yes or no to select, input your selection:") fmt.Fprintf(writer, "All Higress resources will be uninstalled from the cluster. \nProceed? (y/N)")
fmt.Scanln(&answer) fmt.Scanln(&answer)
if strings.TrimSpace(answer) == "yes" { if strings.TrimSpace(answer) == "y" {
fmt.Fprintf(writer, "\n")
return true return true
} }
if strings.TrimSpace(answer) == "no" { if strings.TrimSpace(answer) == "N" {
fmt.Fprintf(writer, "Cancelled.\n")
return false return false
} }
} }
return false
} }
func UnInstallManifests(profile *helm.Profile, writer io.Writer) error { func UnInstallManifests(profile *helm.Profile, writer io.Writer) error {
fmt.Fprintf(writer, "start to check kubernetes cluster enviroment ......\n")
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)
} }
fmt.Fprintf(writer, "start to init higress installer ......\n")
op, err := installer.NewInstaller(profile, cliClient, writer) op, err := installer.NewInstaller(profile, cliClient, writer)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(writer, "start to run higress installer ......\n")
if err := op.Run(); err != nil { if err := op.Run(); err != nil {
return err return err
} }
fmt.Fprintf(writer, "start to render manifests ......\n")
manifestMap, err := op.RenderManifests() manifestMap, err := op.RenderManifests()
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(writer, "start to delete manifests ......\n")
fmt.Fprintf(writer, "\n⌛ Processing uninstallation... \n\n")
if err := op.DeleteManifests(manifestMap); err != nil { if err := op.DeleteManifests(manifestMap); err != nil {
return err return err
} }
fmt.Fprintf(writer, "uninstall higress complete!\n") fmt.Fprintf(writer, "\n🎊 Uninstall All Resources Complete!\n")
return nil return nil
} }