From 96b3b568bc8c4de4e470d88e4d67fa894282df06 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Fri, 25 Oct 2024 20:57:43 +0800 Subject: [PATCH] feat(climc): support webconsole-container-exec (#21466) --- cmd/climc/shell/compute/webconsole.go | 7 +++++++ .../modules/webconsole/mod_webconsole.go | 21 +++++++++++-------- pkg/mcclient/options/webconsole.go | 5 +++++ pkg/webconsole/service/handlers.go | 3 +++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cmd/climc/shell/compute/webconsole.go b/cmd/climc/shell/compute/webconsole.go index 34119efc45e..0a2620a9026 100644 --- a/cmd/climc/shell/compute/webconsole.go +++ b/cmd/climc/shell/compute/webconsole.go @@ -148,4 +148,11 @@ func init() { return nil }) + R(&o.WebConsoleContainerExecOptions{}, "webconsole-container-exec", "Container exec", func(s *mcclient.ClientSession, args *o.WebConsoleContainerExecOptions) error { + ret, err := webconsole.WebConsole.DoContainerExec(s, jsonutils.Marshal(map[string]interface{}{"container_id": args.ID})) + if err != nil { + return err + } + return handleResult(s, args.WebConsoleOptions, ret) + }) } diff --git a/pkg/mcclient/modules/webconsole/mod_webconsole.go b/pkg/mcclient/modules/webconsole/mod_webconsole.go index 69319797796..417c1dd14c3 100644 --- a/pkg/mcclient/modules/webconsole/mod_webconsole.go +++ b/pkg/mcclient/modules/webconsole/mod_webconsole.go @@ -111,10 +111,16 @@ func (m WebConsoleManager) doContainerAction(s *mcclient.ClientSession, data jso } func (m WebConsoleManager) DoContainerExec(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { + return m.doContainerAction(s, data, func(containerId string) []string { + return []string{"container-exec", containerId, "sh"} + }) +} + +func (m WebConsoleManager) DoContainerLog(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { opt := new(compute_options.ContainerLogOptions) data.Unmarshal(opt) return m.doContainerAction(s, data, func(containerId string) []string { - args := []string{"container-exec"} + args := []string{"container-log"} if opt.Tail > 0 { args = append(args, "--tail", fmt.Sprintf("%d", opt.Tail)) } @@ -128,13 +134,7 @@ func (m WebConsoleManager) DoContainerExec(s *mcclient.ClientSession, data jsonu args = append(args, "-f") } args = append(args, containerId) - return []string{"container-exec", containerId, "sh"} - }) -} - -func (m WebConsoleManager) DoContainerLog(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - return m.doContainerAction(s, data, func(containerId string) []string { - return []string{"container-log", containerId} + return args }) } @@ -163,7 +163,10 @@ func (m WebConsoleManager) doActionWithClimcPod( s *mcclient.ClientSession, af func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error), ) (jsonutils.JSONObject, error) { - adminSession := auth.GetAdminSession(s.GetContext(), s.GetRegion()) + adminSession := s + if auth.IsAuthed() { + adminSession = auth.GetAdminSession(s.GetContext(), s.GetRegion()) + } query := jsonutils.NewDict() query.Add(jsonutils.JSONTrue, "system") diff --git a/pkg/mcclient/options/webconsole.go b/pkg/mcclient/options/webconsole.go index d8b431316bd..8d6632ac8f0 100644 --- a/pkg/mcclient/options/webconsole.go +++ b/pkg/mcclient/options/webconsole.go @@ -108,3 +108,8 @@ type WebConsoleServerRdpOptions struct { Height *int Dpi *int } + +type WebConsoleContainerExecOptions struct { + WebConsoleOptions + ID string `help:"Container id or name"` +} diff --git a/pkg/webconsole/service/handlers.go b/pkg/webconsole/service/handlers.go index 55c0a1c0447..02bbc9a0847 100644 --- a/pkg/webconsole/service/handlers.go +++ b/pkg/webconsole/service/handlers.go @@ -81,6 +81,9 @@ func initHandlers(app *appsrv.Application) { func fetchK8sEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) (*command.K8sEnv, error) { params, _, body := appsrv.FetchEnv(ctx, w, r) + if !gotypes.IsNil(body) && body.Contains("webconsole") { + body, _ = body.Get("webconsole") + } k8sReq := webconsole_api.SK8sRequest{} err := body.Unmarshal(&k8sReq)