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

roachprod: load balancer from pgurl command #123206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/cmd/roachprod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,13 @@ var pgurlCmd = &cobra.Command{
Short: "generate pgurls for the nodes in a cluster",
Long: `Generate pgurls for the nodes in a cluster.

The command generates postgres urls for the specified nodes in a cluster.
Both the nodes or a load balancer can be specified as the target of the pgurl.

Examples of <cluster>:
cluster-name:1-3
cluster-name:lb

--auth-mode specifies the method of authentication unless --insecure is passed.
Defaults to root if not passed. Available auth-modes are:

Expand Down
12 changes: 6 additions & 6 deletions pkg/roachprod/install/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

var parameterRe = regexp.MustCompile(`{[^{}]*}`)
var pgURLRe = regexp.MustCompile(`{pgurl(:[-,0-9]+|:L)?(:[a-z0-9\-]+)?(:[0-9]+)?}`)
var pgHostRe = regexp.MustCompile(`{pghost(:[-,0-9]+|:L)?(:[a-z0-9\-]+)?(:[0-9]+)?}`)
var pgURLRe = regexp.MustCompile(`{pgurl(:[-,0-9]+|:(?i)lb)?(:[a-z0-9\-]+)?(:[0-9]+)?}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

At some point it would be nice to document these with examples, these regexes are getting complicated 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True, these are becoming a bit over-encumbered.

var pgHostRe = regexp.MustCompile(`{pghost(:[-,0-9]+|:(?i)lb)?(:[a-z0-9\-]+)?(:[0-9]+)?}`)
var pgPortRe = regexp.MustCompile(`{pgport(:[-,0-9]+)?(:[a-z0-9\-]+)?(:[0-9]+)?}`)
var uiPortRe = regexp.MustCompile(`{uiport(:[-,0-9]+)}`)
var storeDirRe = regexp.MustCompile(`{store-dir(:[0-9]+)?}`)
Expand Down Expand Up @@ -158,8 +158,8 @@ func (e *expander) maybeExpandPgURL(
if err != nil {
return "", false, err
}
switch m[1] {
case ":L":
switch strings.ToLower(m[1]) {
case ":lb":
url, err := c.loadBalancerURL(ctx, l, virtualClusterName, sqlInstance, DefaultAuthMode)
return url, url != "", err
default:
Expand Down Expand Up @@ -187,8 +187,8 @@ func (e *expander) maybeExpandPgHost(
return "", false, err
}

switch m[1] {
case ":L":
switch strings.ToLower(m[1]) {
case ":lb":
services, err := c.DiscoverServices(ctx, virtualClusterName, ServiceTypeSQL, ServiceInstancePredicate(sqlInstance))
if err != nil {
return "", false, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/roachprod/install/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func ListNodes(s string, numNodesInCluster int) (Nodes, error) {
return nil, errors.AssertionFailedf("invalid number of nodes %d", numNodesInCluster)
}

if s == "all" {
// "lb" is a special value that also returns all nodes, but is used to
// indicate that a load balancer should be used.
if s == "all" || strings.ToLower(s) == "lb" {
return allNodes(numNodesInCluster), nil
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/roachprod/roachprod.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ func newCluster(
return c, nil
}

// shouldUseLoadBalancer determines if the node selector section in the cluster
// name is set to select a load balancer.
func shouldUseLoadBalancer(name string) bool {
parts := strings.Split(name, ":")
return len(parts) == 2 && strings.ToLower(parts[1]) == "lb"
}

// userClusterNameRegexp returns a regexp that matches all clusters owned by the
// current user.
func userClusterNameRegexp(l *logger.Logger) (*regexp.Regexp, error) {
Expand Down Expand Up @@ -929,6 +936,28 @@ func PgURL(
if err != nil {
return nil, err
}

if shouldUseLoadBalancer(clusterName) {
services, err := c.DiscoverServices(ctx, opts.VirtualClusterName, install.ServiceTypeSQL,
install.ServiceInstancePredicate(opts.SQLInstance))
if err != nil {
return nil, err
}
port := config.DefaultSQLPort
serviceMode := install.ServiceModeExternal
if len(services) > 0 {
port = services[0].Port
serviceMode = services[0].ServiceMode
} else {
l.Printf("no services found, searching for load balancer on default port %d", port)
}
addr, err := c.FindLoadBalancer(l, port)
if err != nil {
return nil, err
}
return []string{c.NodeURL(addr.IP, port, opts.VirtualClusterName, serviceMode, opts.Auth)}, nil
}

nodes := c.TargetNodes()
ips := make([]string, len(nodes))

Expand Down