Skip to content

Commit

Permalink
feat(climc): support webconsole-container-exec (#21466)
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi authored Oct 25, 2024
1 parent 33b6715 commit 96b3b56
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
7 changes: 7 additions & 0 deletions cmd/climc/shell/compute/webconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
21 changes: 12 additions & 9 deletions pkg/mcclient/modules/webconsole/mod_webconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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
})
}

Expand Down Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions pkg/mcclient/options/webconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,8 @@ type WebConsoleServerRdpOptions struct {
Height *int
Dpi *int
}

type WebConsoleContainerExecOptions struct {
WebConsoleOptions
ID string `help:"Container id or name"`
}
3 changes: 3 additions & 0 deletions pkg/webconsole/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 96b3b56

Please sign in to comment.