Skip to content

Commit a7dbded

Browse files
committed
roachprod: prepare centralized - use centralized from roachprod lib
This patch starts using the roachprod centralized client from the roachprod library when properly configured via environment variables. Epic: none Release note: None
1 parent 11ab52d commit a7dbded

File tree

8 files changed

+212
-48
lines changed

8 files changed

+212
-48
lines changed

pkg/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,7 @@ GO_TARGETS = [
18181818
"//pkg/roachprod/agents/parca:parca",
18191819
"//pkg/roachprod/blobfixture:blobfixture",
18201820
"//pkg/roachprod/blobfixture:blobfixture_test",
1821+
"//pkg/roachprod/centralizedapi:centralizedapi",
18211822
"//pkg/roachprod/cloud/types:types",
18221823
"//pkg/roachprod/cloud:cloud",
18231824
"//pkg/roachprod/cloud:cloud_test",

pkg/cmd/roachprod/cli/commands.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,12 @@ hosts file.
528528
color.HiGreenString(""))
529529
}
530530
} else {
531-
fmt.Fprintf(tw, "\t(-)")
531+
fmt.Fprintf(tw, "\t%s\t%s\t%s\t%s\t%s\t",
532+
color.HiGreenString(""),
533+
color.HiGreenString(""),
534+
color.HiWhiteString(""),
535+
color.HiWhiteString(""),
536+
color.HiGreenString(""))
532537
}
533538
fmt.Fprintf(tw, "\n")
534539
}

pkg/roachprod/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go_library(
1919
"//pkg/roachprod/agents/fluentbit",
2020
"//pkg/roachprod/agents/opentelemetry",
2121
"//pkg/roachprod/agents/parca",
22+
"//pkg/roachprod/centralizedapi",
2223
"//pkg/roachprod/cloud",
2324
"//pkg/roachprod/cloud/types",
2425
"//pkg/roachprod/config",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "centralizedapi",
5+
srcs = ["centralizedapi.go"],
6+
importpath = "github.com/cockroachdb/cockroach/pkg/roachprod/centralizedapi",
7+
visibility = ["//visibility:public"],
8+
deps = ["//pkg/cmd/roachprod-centralized/client"],
9+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package centralizedapi
7+
8+
import (
9+
"sync"
10+
11+
"github.com/cockroachdb/cockroach/pkg/cmd/roachprod-centralized/client"
12+
)
13+
14+
var (
15+
once sync.Once
16+
centralizedapi *client.Client
17+
)
18+
19+
// GetCentralizedAPIClient returns a singleton instance of the centralized API client.
20+
func GetCentralizedAPIClient() *client.Client {
21+
once.Do(func() {
22+
client, err := client.NewClientWithConfig(client.LoadConfigFromEnv())
23+
if err != nil {
24+
panic(err)
25+
}
26+
centralizedapi = client
27+
})
28+
return centralizedapi
29+
}

pkg/roachprod/cloud/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ go_library(
1111
visibility = ["//visibility:public"],
1212
deps = [
1313
"//pkg/cloud/amazon",
14+
"//pkg/cmd/roachprod-centralized/client",
15+
"//pkg/roachprod/centralizedapi",
1416
"//pkg/roachprod/cloud/types",
1517
"//pkg/roachprod/config",
1618
"//pkg/roachprod/logger",

pkg/roachprod/cloud/cluster_cloud.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"sort"
1212
"time"
1313

14+
roachprodcentralized "github.com/cockroachdb/cockroach/pkg/cmd/roachprod-centralized/client"
15+
"github.com/cockroachdb/cockroach/pkg/roachprod/centralizedapi"
1416
cloudcluster "github.com/cockroachdb/cockroach/pkg/roachprod/cloud/types"
1517
"github.com/cockroachdb/cockroach/pkg/roachprod/logger"
1618
"github.com/cockroachdb/cockroach/pkg/roachprod/ui"
@@ -140,6 +142,22 @@ func (c *Cloud) ListCloud(ctx context.Context, l *logger.Logger, options vm.List
140142

141143
providers := append(c.providers, c.localProviders...)
142144

145+
apiClient := centralizedapi.GetCentralizedAPIClient()
146+
if apiClient.IsEnabled() {
147+
// Use the centralized roachprod service to list clusters.
148+
response, err := apiClient.ListClusters(context.Background(), l, roachprodcentralized.ListClustersOptions{})
149+
if err != nil {
150+
l.Errorf("Error listing clusters using the centralized API: %s", err)
151+
} else {
152+
c.Clusters = response.Clusters
153+
c.BadInstances = response.BadInstances
154+
155+
// Remote providers are already listed by the centralized service,
156+
// so only use local providers in the local listing.
157+
providers = c.localProviders
158+
}
159+
}
160+
143161
// List all VMs across all providers in parallel.
144162
providerVMs := make(map[string]vm.List)
145163
g := ctxgroup.WithContext(ctx)
@@ -347,6 +365,17 @@ func CreateCluster(l *logger.Logger, opts []*ClusterCreateOpts) (*cloudcluster.C
347365
// `roachprod.Start` expects nodes/vms to be in sorted order
348366
sort.Sort(c.VMs)
349367

368+
apiClient := centralizedapi.GetCentralizedAPIClient()
369+
if apiClient.IsEnabled() {
370+
// Use the centralized roachprod service to upsert the cluster registration
371+
// in its final state.
372+
// Upsert is used here because the cluster may already exist in the
373+
// centralized service if the cluster was picked up during a background sync.
374+
if err = apiClient.RegisterClusterUpsert(context.Background(), l, c); err != nil {
375+
return nil, err
376+
}
377+
}
378+
350379
return c, nil
351380
}
352381

@@ -387,6 +416,14 @@ func GrowCluster(l *logger.Logger, c *cloudcluster.Cluster, numNodes int) error
387416
// `roachprod.Start` expects nodes/vms to be in sorted order
388417
sort.Sort(c.VMs)
389418

419+
apiClient := centralizedapi.GetCentralizedAPIClient()
420+
if apiClient.IsEnabled() {
421+
// Use the centralized roachprod service to create the cluster.
422+
if err := apiClient.RegisterClusterUpdate(context.Background(), l, c); err != nil {
423+
return err
424+
}
425+
}
426+
390427
return nil
391428
}
392429

@@ -418,6 +455,15 @@ func ShrinkCluster(l *logger.Logger, c *cloudcluster.Cluster, numNodes int) erro
418455

419456
// Update the list of VMs in the cluster.
420457
c.VMs = c.VMs[:len(c.VMs)-numNodes]
458+
459+
apiClient := centralizedapi.GetCentralizedAPIClient()
460+
if apiClient.IsEnabled() {
461+
// Use the centralized roachprod service to create the cluster.
462+
if err := apiClient.RegisterClusterUpdate(context.Background(), l, c); err != nil {
463+
return err
464+
}
465+
}
466+
421467
return nil
422468
}
423469

@@ -436,7 +482,10 @@ func DestroyCluster(l *logger.Logger, c *cloudcluster.Cluster) error {
436482
publicRecords = append(publicRecords, v.PublicDNS)
437483
}
438484
dnsErr := vm.FanOutDNS(c.VMs, func(p vm.DNSProvider, vms vm.List) error {
439-
publicRecordsErr := p.DeletePublicRecordsByName(context.Background(), publicRecords...)
485+
var publicRecordsErr error
486+
if !centralizedapi.GetCentralizedAPIClient().IsEnabled() {
487+
publicRecordsErr = p.DeletePublicRecordsByName(context.Background(), publicRecords...)
488+
}
440489
srvRecordsErr := p.DeleteSRVRecordsBySubdomain(context.Background(), c.Name)
441490
return errors.CombineErrors(publicRecordsErr, srvRecordsErr)
442491
})
@@ -453,6 +502,15 @@ func DestroyCluster(l *logger.Logger, c *cloudcluster.Cluster) error {
453502
return p.Delete(l, vms)
454503
})
455504
stopSpinner()
505+
506+
if clusterErr == nil {
507+
apiClient := centralizedapi.GetCentralizedAPIClient()
508+
if apiClient.IsEnabled() {
509+
if err := apiClient.RegisterClusterDelete(context.Background(), l, c.Name); err != nil {
510+
l.Printf("WARNING: failed to delete cluster %s from centralized service: %s", c.Name, err)
511+
}
512+
}
513+
}
456514
return errors.CombineErrors(dnsErr, clusterErr)
457515
}
458516

@@ -467,5 +525,13 @@ func ExtendCluster(l *logger.Logger, c *cloudcluster.Cluster, extension time.Dur
467525
return err
468526
}
469527
c.Lifetime = newLifetime
528+
529+
apiClient := centralizedapi.GetCentralizedAPIClient()
530+
if apiClient.IsEnabled() {
531+
// Use the centralized roachprod service to create the cluster.
532+
if err := apiClient.RegisterClusterUpdate(context.Background(), l, c); err != nil {
533+
return err
534+
}
535+
}
470536
return nil
471537
}

0 commit comments

Comments
 (0)