Skip to content

Commit 6c04951

Browse files
committed
initial commit
1 parent 843d488 commit 6c04951

File tree

3 files changed

+563
-7
lines changed

3 files changed

+563
-7
lines changed

pkg/cmd/roachprod/cli/commands.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ func (cr *commandRegistry) buildLoadBalancerCmd() *cobra.Command {
273273
}
274274
loadBalancerCmd.AddCommand(
275275
buildCreateLoadBalancerCmd(),
276+
buildDeleteLoadBalancerCmd(),
276277
buildLoadBalancerPGUrl(),
277278
buildLoadBalancerIP(),
279+
buildLoadBalancerList(),
278280
)
279281
return loadBalancerCmd
280282
}
@@ -304,6 +306,26 @@ These resources will automatically be destroyed when the cluster is destroyed.
304306
return createLoadBalancerCmd
305307
}
306308

309+
func buildDeleteLoadBalancerCmd() *cobra.Command {
310+
deleteLoadBalancerCmd := &cobra.Command{
311+
Use: "destroy <cluster>",
312+
Short: "destroy a load balancer for a cluster",
313+
Long: `Destroy a load balancer for a specific service (port), system by default, for the given cluster.
314+
315+
The load balancer is deleted using the cloud provider's API.
316+
`,
317+
Args: cobra.ExactArgs(1),
318+
Run: Wrap(func(cmd *cobra.Command, args []string) error {
319+
return roachprod.DeleteLoadBalancer(context.Background(), config.Logger,
320+
args[0], isSecure, virtualClusterName, sqlInstance,
321+
)
322+
}),
323+
}
324+
initFlagInsecureForCmd(deleteLoadBalancerCmd)
325+
initFlagsClusterNSQLForCmd(deleteLoadBalancerCmd)
326+
return deleteLoadBalancerCmd
327+
}
328+
307329
func buildLoadBalancerPGUrl() *cobra.Command {
308330
loadBalancerPGUrl := &cobra.Command{
309331
Use: "pgurl <cluster>",
@@ -357,6 +379,30 @@ func buildLoadBalancerIP() *cobra.Command {
357379
return loadBalancerIP
358380
}
359381

382+
func buildLoadBalancerList() *cobra.Command {
383+
loadBalancerList := &cobra.Command{
384+
Use: "list <cluster>",
385+
Short: "list all load balancers for a cluster",
386+
Long: "List all load balancers and their addresses for the given cluster.",
387+
Args: cobra.ExactArgs(1),
388+
Run: Wrap(func(cmd *cobra.Command, args []string) error {
389+
addresses, err := roachprod.ListLoadBalancers(config.Logger, args[0])
390+
if err != nil {
391+
return err
392+
}
393+
if len(addresses) == 0 {
394+
fmt.Println("No load balancers found for cluster.")
395+
return nil
396+
}
397+
for _, addr := range addresses {
398+
fmt.Printf("%s:%d\n", addr.IP, addr.Port)
399+
}
400+
return nil
401+
}),
402+
}
403+
return loadBalancerList
404+
}
405+
360406
func (cr *commandRegistry) buildListCmd() *cobra.Command {
361407
listCmd := &cobra.Command{
362408
Use: "list [--details | --json] [ --mine | --pattern ]",

pkg/roachprod/roachprod.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,66 @@ func LoadBalancerIP(
31083108
return addr.IP, nil
31093109
}
31103110

3111+
// ListLoadBalancers returns all load balancers for the given cluster.
3112+
func ListLoadBalancers(
3113+
l *logger.Logger, clusterName string,
3114+
) ([]vm.ServiceAddress, error) {
3115+
c, err := GetClusterFromCache(l, clusterName)
3116+
if err != nil {
3117+
return nil, err
3118+
}
3119+
return c.ListLoadBalancers(l)
3120+
}
3121+
3122+
// DeleteLoadBalancer deletes the load balancer for the SQL service on the given
3123+
// cluster.
3124+
func DeleteLoadBalancer(
3125+
ctx context.Context,
3126+
l *logger.Logger,
3127+
clusterName string,
3128+
secure install.ComplexSecureOption,
3129+
virtualClusterName string,
3130+
sqlInstance int,
3131+
) error {
3132+
c, err := GetClusterFromCache(l, clusterName, secure)
3133+
if err != nil {
3134+
return err
3135+
}
3136+
3137+
// If virtualClusterName is not provided, use the system interface name.
3138+
if virtualClusterName == "" {
3139+
virtualClusterName = install.SystemInterfaceName
3140+
}
3141+
3142+
// Find the SQL ports for the service on all nodes.
3143+
services, err := c.ServiceDescriptors(
3144+
ctx, c.Nodes, virtualClusterName, install.ServiceTypeSQL, sqlInstance,
3145+
)
3146+
if err != nil {
3147+
return err
3148+
}
3149+
3150+
port := config.DefaultSQLPort
3151+
if len(services) == 0 {
3152+
l.Errorf("WARNING: %s SQL service not found on cluster %s, using default SQL port %d",
3153+
virtualClusterName, clusterName, port)
3154+
} else {
3155+
port = services[0].Port
3156+
// Confirm that the service has the same port on all nodes.
3157+
for _, service := range services[1:] {
3158+
if port != service.Port {
3159+
return errors.Errorf("service %s must share the same port on all nodes, different ports found %d and %d",
3160+
virtualClusterName, port, service.Port)
3161+
}
3162+
}
3163+
}
3164+
3165+
// Delete the load balancer for the service's port.
3166+
return vm.FanOut(c.VMs, func(provider vm.Provider, vms vm.List) error {
3167+
return provider.DeleteLoadBalancer(l, vms, port)
3168+
})
3169+
}
3170+
31113171
// Deploy deploys a new version of cockroach to the given cluster. It currently
31123172
// does not support clusters running external SQL instances.
31133173
// TODO(herko): Add support for virtual clusters (external SQL processes)

0 commit comments

Comments
 (0)