Skip to content

Commit

Permalink
fix: get the container access docker compose ps (#870)
Browse files Browse the repository at this point in the history
Signed-off-by: fengshunli <[email protected]>
  • Loading branch information
fengshunli authored Apr 18, 2024
1 parent 8c817cf commit f1cadcb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
52 changes: 26 additions & 26 deletions pkg/cmd/hgctl/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"runtime"
"strings"

"github.com/alibaba/higress/pkg/cmd/hgctl/kubernetes"
"github.com/alibaba/higress/pkg/cmd/options"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
types2 "github.com/docker/docker/api/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"

"github.com/alibaba/higress/pkg/cmd/hgctl/docker"
"github.com/alibaba/higress/pkg/cmd/hgctl/kubernetes"
"github.com/alibaba/higress/pkg/cmd/options"
)

var (
Expand All @@ -55,7 +54,9 @@ var (

proxyAdminPort int

docker = false
project = "higress"

dockerCli = false
)

const (
Expand Down Expand Up @@ -107,7 +108,7 @@ func newDashboardCmd() *cobra.Command {

consoleCmd := consoleDashCmd()
consoleCmd.PersistentFlags().IntVar(&consolePort, "ui-port", defaultConsolePort, "The component dashboard UI port.")
consoleCmd.PersistentFlags().BoolVar(&docker, "docker", false, "Search higress console from docker")
consoleCmd.PersistentFlags().BoolVar(&dockerCli, "docker", false, "Search higress console from docker")
dashboardCmd.AddCommand(consoleCmd)

controllerDebugCmd := controllerDebugCmd()
Expand Down Expand Up @@ -165,23 +166,23 @@ func consoleDashCmd() *cobra.Command {
hgctl dash console
hgctl d console`,
RunE: func(cmd *cobra.Command, args []string) error {
if docker {
return accessDocker(cmd)
if dockerCli {
return accessDockerCompose(cmd)
}
client, err := kubernetes.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader())
if err != nil {
fmt.Printf("build kubernetes CLI client fail: %v\ntry to access docker container\n", err)
return accessDocker(cmd)
return accessDockerCompose(cmd)
}
pl, err := client.PodsForSelector(addonNamespace, "app.kubernetes.io/name=higress-console")
if err != nil {
fmt.Printf("build kubernetes CLI client fail: %v\ntry to access docker container\n", err)
return accessDocker(cmd)
return accessDockerCompose(cmd)
}

if len(pl.Items) < 1 {
fmt.Printf("no higress console pods found\ntry to access docker container\n")
return accessDocker(cmd)
return accessDockerCompose(cmd)
}

// only use the first pod in the list
Expand All @@ -193,27 +194,26 @@ func consoleDashCmd() *cobra.Command {
return cmd
}

// accessDocker access docker container
func accessDocker(cmd *cobra.Command) error {
dockerCli, err := command.NewDockerCli(command.WithCombinedStreams(os.Stdout))
// accessDockerCompose access docker compose ps
func accessDockerCompose(cmd *cobra.Command) error {
cli, err := docker.NewCompose(cmd.OutOrStdout())
if err != nil {
return fmt.Errorf("build docker CLI client fail: %w", err)
return errors.Wrap(err, "failed to build the docker compose client")
}
err = dockerCli.Initialize(flags.NewClientOptions())

list, err := cli.Ps(context.TODO(), project)
if err != nil {
return fmt.Errorf("docker client initialize fail: %w", err)
return errors.Wrap(err, "failed to build the docker compose ps")
}
apiClient := dockerCli.Client()
list, err := apiClient.ContainerList(context.Background(), types2.ContainerListOptions{})
for _, container := range list {
for i, name := range container.Names {
if strings.Contains(name, "higress-console") {
port := container.Ports[i].PublicPort
// not support define ip address
url := fmt.Sprintf("http://localhost:%d", port)
if strings.Contains(container.Service, "console") {
// not support define ip address
if container.Publishers != nil {
url := fmt.Sprintf("http://localhost:%d", container.Publishers[0].PublishedPort)
openBrowser(url, cmd.OutOrStdout(), browser)
return nil
}

return nil
}
}
return errors.New("no higress console container found")
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/hgctl/docker/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ func (c Compose) List(ctx context.Context) ([]api.Stack, error) {
func (c Compose) Down(ctx context.Context, name string) error {
return c.client.Down(ctx, name, api.DownOptions{})
}

func (c Compose) Ps(ctx context.Context, name string) ([]api.ContainerSummary, error) {
return c.client.Ps(ctx, name, api.PsOptions{})
}

0 comments on commit f1cadcb

Please sign in to comment.