-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): support helm-dashboard plugin
- Loading branch information
Showing
7 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/cnrancher/autok3s/pkg/common" | ||
"github.com/cnrancher/autok3s/pkg/settings" | ||
k3dutil "github.com/k3d-io/k3d/v5/cmd/util" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
var ( | ||
dashboardCmd = &cobra.Command{ | ||
Use: "helm-dashboard", | ||
Short: "Enable helm-dashboard for K3s cluster", | ||
Example: "autok3s helm-dashboard", | ||
} | ||
|
||
dashboardPort = 0 | ||
) | ||
|
||
func init() { | ||
dashboardCmd.Flags().IntVarP(&dashboardPort, "port", "", dashboardPort, "Set http port for helm-dashboard") | ||
} | ||
|
||
// DashboardCommand will start a helm-dashboard server for specified K3s cluster | ||
func DashboardCommand() *cobra.Command { | ||
dashboardCmd.PreRunE = func(cmd *cobra.Command, args []string) error { | ||
cfg, err := clientcmd.LoadFromFile(filepath.Join(common.CfgPath, common.KubeCfgFile)) | ||
if err != nil { | ||
return err | ||
} | ||
if len(cfg.Contexts) == 0 { | ||
logrus.Fatalln("cannot enable helm dashboard without K3s cluster") | ||
} | ||
return nil | ||
} | ||
dashboardCmd.Run = func(cmd *cobra.Command, args []string) { | ||
if err := common.CheckCommandExist(common.HelmDashboardCommand); err != nil { | ||
logrus.Fatalln(err) | ||
} | ||
if dashboardPort == 0 { | ||
port, err := k3dutil.GetFreePort() | ||
if err != nil { | ||
logrus.Fatalf("failed to get free port for kube-explorer: %v", err) | ||
} | ||
dashboardPort = port | ||
} | ||
if err := settings.HelmDashboardPort.Set(strconv.Itoa(dashboardPort)); err != nil { | ||
logrus.Fatalln(err) | ||
} | ||
err := common.StartHelmDashboard(dashboardCmd.Context(), strconv.Itoa(dashboardPort)) | ||
if err != nil { | ||
logrus.Fatalln(err) | ||
} | ||
logrus.Infof("Helm dashboard started with 127.0.0.1:%d", dashboardPort) | ||
} | ||
|
||
return dashboardCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cnrancher/autok3s/pkg/settings" | ||
k3dutil "github.com/k3d-io/k3d/v5/cmd/util" | ||
"github.com/sirupsen/logrus" | ||
|
||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
const HelmDashboardCommand = "helm-dashboard" | ||
|
||
func SwitchDashboard(ctx context.Context, enabled string) error { | ||
if enabled == "true" { | ||
// check if cluster list is empty | ||
clusters, err := DefaultDB.ListCluster("") | ||
if err != nil { | ||
return err | ||
} | ||
if len(clusters) == 0 { | ||
return errors.New("cannot enable helm-dashboard with empty cluster list") | ||
} | ||
} | ||
if err := CheckCommandExist(HelmDashboardCommand); err != nil { | ||
return err | ||
} | ||
|
||
// command execution validate | ||
if err := checkDashboardCmd(); err != nil { | ||
return err | ||
} | ||
|
||
isEnabled := settings.HelmDashboardEnabled.Get() | ||
if !strings.EqualFold(isEnabled, enabled) { | ||
if err := settings.HelmDashboardEnabled.Set(enabled); err != nil { | ||
return err | ||
} | ||
if enabled == "true" { | ||
enableDashboard(ctx) | ||
} else { | ||
if err := settings.HelmDashboardEnabled.Set("false"); err != nil { | ||
return err | ||
} | ||
logrus.Info("Shutting down helm-dashboard...") | ||
DashboardCanceled() | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func InitDashboard(ctx context.Context) { | ||
isEnabled := settings.HelmDashboardEnabled.Get() | ||
if isEnabled == "true" { | ||
enableDashboard(ctx) | ||
} | ||
} | ||
|
||
func StartHelmDashboard(ctx context.Context, port string) error { | ||
_ = os.Setenv(clientcmd.RecommendedConfigPathEnvVar, filepath.Join(CfgPath, KubeCfgFile)) | ||
dashboard := exec.CommandContext(ctx, HelmDashboardCommand, "-b", fmt.Sprintf("--port=%s", port)) | ||
dashboard.Stdout = os.Stdout | ||
dashboard.Stderr = os.Stderr | ||
if err := dashboard.Start(); err != nil { | ||
logrus.Errorf("fail to start helm-dashboard: %v", err) | ||
} | ||
logrus.Infof("helm-dashboard will listen on 127.0.0.1:%s ...", port) | ||
return dashboard.Wait() | ||
} | ||
|
||
func checkDashboardCmd() error { | ||
explorerVersion := exec.Command(HelmDashboardCommand, "--version") | ||
return explorerVersion.Run() | ||
} | ||
|
||
func enableDashboard(ctx context.Context) { | ||
clusters, err := DefaultDB.ListCluster("") | ||
if err != nil { | ||
logrus.Errorf("failed to list clusters: %v", err) | ||
return | ||
} | ||
// disable helm-dashboard if there's no cluster context | ||
if len(clusters) == 0 { | ||
if err := settings.HelmDashboardEnabled.Set("false"); err != nil { | ||
logrus.Errorf("failed to disable helm-dashboard due to empty cluster list: %v", err) | ||
return | ||
} | ||
logrus.Warn("disabled helm-dashboard with empty cluster list") | ||
return | ||
} | ||
dashboardPort := settings.HelmDashboardPort.Get() | ||
if dashboardPort == "" { | ||
freePort, err := k3dutil.GetFreePort() | ||
if err != nil { | ||
logrus.Errorf("failed to get free port for helm-dashboard: %v", err) | ||
return | ||
} | ||
dashboardPort = strconv.Itoa(freePort) | ||
err = settings.HelmDashboardPort.Set(dashboardPort) | ||
if err != nil { | ||
logrus.Errorf("failed to save helm-dashboard port to settings: %v", err) | ||
return | ||
} | ||
} | ||
logrus.Info("Enable helm-dashboard server...") | ||
dashboardCtx, cancel := context.WithCancel(ctx) | ||
DashboardCanceled = cancel | ||
go func(ctx context.Context, port string) { | ||
StartHelmDashboard(ctx, port) | ||
}(dashboardCtx, dashboardPort) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters