Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dashboard use ip instead of localhost #212

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cluster/baremetal/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Cluster struct {
type ClusterComponents struct {
MetaSrv components.ClusterComponent
Datanode components.ClusterComponent
Frontend components.ClusterComponent
Frontend components.Frontend
Etcd components.ClusterComponent
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/baremetal/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (c *Cluster) Wait(ctx context.Context, close bool) error {
csd := c.mm.GetClusterScopeDirs()
if !close {
c.logger.V(0).Infof("The cluster(pid=%d, version=%s) is running in bare-metal mode now...", os.Getpid(), v)
c.logger.V(0).Infof("To view dashboard by accessing: %s", logger.Bold("http://localhost:4000/dashboard/"))
c.cc.Frontend.PrintDashboardLog()
} else {
c.logger.Warnf("The cluster(pid=%d, version=%s) run in bare-metal has been shutting down...", os.Getpid(), v)
c.logger.Warnf("To view the failure by browsing logs in: %s", logger.Bold(csd.LogsDir))
Expand Down
46 changes: 39 additions & 7 deletions pkg/components/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package components
import (
"context"
"fmt"
"net"
"net/http"
"path"
"sync"
Expand All @@ -30,7 +31,7 @@ import (
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

type frontend struct {
type Frontend struct {
config *config.Frontend
metaSrvAddr string

Expand All @@ -42,8 +43,8 @@ type frontend struct {
}

func NewFrontend(config *config.Frontend, metaSrvAddr string, workingDirs WorkingDirs,
wg *sync.WaitGroup, logger logger.Logger) ClusterComponent {
return &frontend{
wg *sync.WaitGroup, logger logger.Logger) Frontend {
return Frontend{
config: config,
metaSrvAddr: metaSrvAddr,
workingDirs: workingDirs,
Expand All @@ -52,11 +53,11 @@ func NewFrontend(config *config.Frontend, metaSrvAddr string, workingDirs Workin
}
}

func (f *frontend) Name() string {
func (f *Frontend) Name() string {
return string(greptimedbclusterv1alpha1.FrontendComponentKind)
}

func (f *frontend) Start(ctx context.Context, stop context.CancelFunc, binary string) error {
func (f *Frontend) Start(ctx context.Context, stop context.CancelFunc, binary string) error {
for i := 0; i < f.config.Replicas; i++ {
dirName := fmt.Sprintf("%s.%d", f.Name(), i)

Expand Down Expand Up @@ -87,7 +88,7 @@ func (f *frontend) Start(ctx context.Context, stop context.CancelFunc, binary st
return nil
}

func (f *frontend) BuildArgs(params ...interface{}) []string {
func (f *Frontend) BuildArgs(params ...interface{}) []string {
logLevel := f.config.LogLevel
if logLevel == "" {
logLevel = DefaultLogLevel
Expand Down Expand Up @@ -115,7 +116,7 @@ func (f *frontend) BuildArgs(params ...interface{}) []string {
return args
}

func (f *frontend) IsRunning(_ context.Context) bool {
func (f *Frontend) IsRunning(_ context.Context) bool {
for i := 0; i < f.config.Replicas; i++ {
addr := FormatAddrArg(f.config.HTTPAddr, i)
healthy := fmt.Sprintf("http://%s/health", addr)
Expand All @@ -138,3 +139,34 @@ func (f *frontend) IsRunning(_ context.Context) bool {
}
return true
}

func (f *Frontend) PrintDashboardLog() {
Copy link
Collaborator

@zyy17 zyy17 Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I think it's better to add a util function, for example, HostIP(), and use it in the logging:

...
// get the port from c.config.Cluster.Frontend.HTTPAddr
port := getPort(c.config.Cluster.Frontend.HTTPAddr)

c.logger.V(0).Infof("To view dashboard by accessing: %s", logger.Bold(fmt.Sprintf("http://%s:%d", HostIP(), port)))
...

format := "http://%s:%s/dashboard/"

// Split addr to host:ip
http_addr := f.config.HTTPAddr
host, port, err := net.SplitHostPort(http_addr)
if len(http_addr) > 0 && err != nil {
f.logger.Warnf("%s use incorrect http_addr: %s err: %s", f.Name(), http_addr, err)
return
}

var log string
if len(http_addr) == 0 || host == "0.0.0.0" {
ip, err := HostnameIP()

if err != nil {
f.logger.Warnf("%s hostname local resolution failed: %s", f.Name(), err)
return
}
if len(port) == 0 {
port = "4000"
}

log = fmt.Sprintf(format, ip, port)
} else {
log = fmt.Sprintf(format, host, port)
}

f.logger.V(0).Infof("If enabled dashboard, view it by accessing: %s", logger.Bold(log))
}
18 changes: 18 additions & 0 deletions pkg/components/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package components
import (
"fmt"
"net"
"os"
"strconv"
)

Expand Down Expand Up @@ -48,3 +49,20 @@ func GenerateAddrArg(config string, addr string, nodeId int, args []string) []st

return append(args, fmt.Sprintf("%s=%s", config, socketAddr))
}

func HostnameIP() (ip string, err error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}

ips, err := net.LookupHost(hostname)
if err != nil {
return "", err
}

if len(ips) == 0 {
return "", nil
}
return ips[0], nil
}
Loading