diff --git a/go/vt/proto/vtadmin/vtadmin.pb.go b/go/vt/proto/vtadmin/vtadmin.pb.go index aa926bba944..404321e4e11 100644 --- a/go/vt/proto/vtadmin/vtadmin.pb.go +++ b/go/vt/proto/vtadmin/vtadmin.pb.go @@ -169,8 +169,8 @@ type VTGate struct { Pool string `protobuf:"bytes,2,opt,name=pool,proto3" json:"pool,omitempty"` // Cell is the topology cell the VTGate is in. Cell string `protobuf:"bytes,3,opt,name=cell,proto3" json:"cell,omitempty"` - // Cluster is the name of the cluster the VTGate serves. - Cluster string `protobuf:"bytes,4,opt,name=cluster,proto3" json:"cluster,omitempty"` + // Cluster is the cluster the VTGate serves. + Cluster *Cluster `protobuf:"bytes,4,opt,name=cluster,proto3" json:"cluster,omitempty"` // Keyspaces is the list of keyspaces-to-watch for the VTGate. Keyspaces []string `protobuf:"bytes,5,rep,name=keyspaces,proto3" json:"keyspaces,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` @@ -224,11 +224,11 @@ func (m *VTGate) GetCell() string { return "" } -func (m *VTGate) GetCluster() string { +func (m *VTGate) GetCluster() *Cluster { if m != nil { return m.Cluster } - return "" + return nil } func (m *VTGate) GetKeyspaces() []string { @@ -459,37 +459,37 @@ func init() { func init() { proto.RegisterFile("vtadmin.proto", fileDescriptor_609739e22a0a50b3) } var fileDescriptor_609739e22a0a50b3 = []byte{ - // 473 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x5d, 0x8b, 0xd3, 0x40, - 0x14, 0x6d, 0xb2, 0xdb, 0x66, 0x73, 0xa3, 0x6d, 0xbd, 0x4f, 0x31, 0x2e, 0x58, 0x82, 0x4a, 0x15, - 0x6c, 0x20, 0xfa, 0xd2, 0x27, 0x59, 0x45, 0xca, 0x22, 0xa4, 0x30, 0xad, 0x15, 0x7c, 0x59, 0xb2, - 0xcd, 0x50, 0x83, 0xd9, 0x4e, 0x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 0x16, 0x4e, 0x00, 0x76, 0x56, 0x30, 0x3a, 0xe4, 0x5a, 0x05, 0x4f, 0x8e, 0xce, 0x1a, 0x0d, - 0xef, 0x5f, 0x7c, 0x7d, 0xb6, 0x2d, 0x05, 0xe5, 0x7c, 0x54, 0xb2, 0x44, 0x57, 0xc9, 0x8a, 0x25, - 0x5b, 0x91, 0xa8, 0xff, 0x35, 0x31, 0xe4, 0xeb, 0x8e, 0x6a, 0xdf, 0xfc, 0x0d, 0x00, 0x00, 0xff, - 0xff, 0x51, 0x4b, 0xaa, 0x23, 0xe8, 0x03, 0x00, 0x00, + // 474 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x5f, 0x8b, 0xd3, 0x4e, + 0x14, 0x6d, 0xb2, 0xdb, 0x66, 0x73, 0xf3, 0xfb, 0xb5, 0xf5, 0x3e, 0xc5, 0xb8, 0x60, 0x19, 0x54, + 0xaa, 0x60, 0x03, 0xd1, 0x97, 0x3e, 0xc9, 0x2a, 0x52, 0x16, 0x21, 0x85, 0x69, 0xad, 0xe0, 0xcb, + 0x92, 0x6d, 0x86, 0x1a, 0xcc, 0x76, 0x62, 0x67, 0xb6, 0xe0, 0x17, 0xf1, 0x5b, 0x09, 0x7e, 0x24, + 0x99, 0x3f, 0x49, 0xbb, 0xed, 0xa2, 0xbe, 0xdd, 0x7b, 0xcf, 0x39, 0x77, 0xce, 0x3d, 0x6d, 0xe0, + 0xff, 0xad, 0xcc, 0xf2, 0x9b, 0x62, 0x3d, 0xaa, 0x36, 0x5c, 0x72, 0xf4, 0x6c, 0x1b, 0x75, 0x25, + 0xaf, 0x78, 0x9e, 0xc9, 0xcc, 0x00, 0xe4, 0x25, 0x78, 0xef, 0xca, 0x5b, 0x21, 0xd9, 0x06, 0xbb, + 0xe0, 0x16, 0x79, 0xe8, 0x0c, 0x9c, 0xa1, 0x4f, 0xdd, 0x22, 0x47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, 0xac, 0xf6, 0xaa, 0xc8, 0x45, 0xe8, 0x68, 0x09, 0xd8, 0xd1, 0x65, 0x2e, 0xc8, 0x18, 0xfa, + 0x3b, 0x8d, 0xa8, 0xf8, 0x5a, 0x30, 0x7c, 0x0a, 0xed, 0x95, 0x1a, 0x68, 0x7a, 0x90, 0xf4, 0x1a, + 0x3f, 0xe6, 0x6a, 0x6a, 0x50, 0x32, 0xd5, 0x52, 0xfb, 0x5b, 0xd8, 0xf7, 0xfe, 0x14, 0xc8, 0x81, + 0x17, 0xf7, 0xc8, 0xcb, 0x6b, 0x78, 0xd0, 0x2c, 0xfc, 0xf7, 0x0b, 0xde, 0x00, 0xee, 0xab, 0xec, + 0x0d, 0xcf, 0xc1, 0x33, 0xff, 0x8e, 0xe3, 0x2b, 0xac, 0xe3, 0x1a, 0x4f, 0x7e, 0x39, 0xe0, 0x2d, + 0xe6, 0x17, 0x0a, 0xc3, 0x0b, 0x38, 0xab, 0xe3, 0xc0, 0xb0, 0x51, 0x1c, 0xa4, 0x1a, 0x3d, 0xbc, + 0x07, 0x31, 0xef, 0x92, 0x16, 0x8e, 0xc1, 0x6f, 0xfc, 0xe0, 0x1d, 0xe6, 0x9d, 0xa8, 0xa2, 0x43, + 0x43, 0xa4, 0x85, 0x13, 0x80, 0xdd, 0x29, 0x18, 0x1d, 0x6b, 0x1b, 0x07, 0x8f, 0xee, 0xc5, 0x6a, + 0x0f, 0x6f, 0x9f, 0x7d, 0x7e, 0xb2, 0x2d, 0x24, 0x13, 0x62, 0x54, 0xf0, 0xd8, 0x54, 0xf1, 0x8a, + 0xc7, 0x5b, 0x19, 0xeb, 0x2f, 0x38, 0xb6, 0xe2, 0xeb, 0x8e, 0x6e, 0x5f, 0xfd, 0x0e, 0x00, 0x00, + 0xff, 0xff, 0x34, 0xe6, 0x6b, 0x4d, 0xfa, 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/go/vt/vtadmin/api_test.go b/go/vt/vtadmin/api_test.go index a5b2f898eda..3c1a4574023 100644 --- a/go/vt/vtadmin/api_test.go +++ b/go/vt/vtadmin/api_test.go @@ -273,7 +273,11 @@ func Test_getTablets(t *testing.T) { disco := fakediscovery.New() disco.AddTaggedGates(nil, &vtadminpb.VTGate{Hostname: "gate"}) - db := vtsql.New("one", &vtsql.Config{ + db := vtsql.New(&vtsql.Config{ + Cluster: &vtadminpb.Cluster{ + Id: "c1", + Name: "one", + }, Discovery: disco, }) db.DialFunc = func(cfg vitessdriver.Configuration) (*sql.DB, error) { @@ -545,7 +549,8 @@ func buildCluster(i int, tablets []*vtadminpb.Tablet, dbconfigs map[string]*dbcf dbconfig = &dbcfg{shouldErr: false} } - db := vtsql.New(cluster.ID, &vtsql.Config{ + db := vtsql.New(&vtsql.Config{ + Cluster: cluster.ToProto(), Discovery: disco, }) db.DialFunc = func(cfg vitessdriver.Configuration) (*sql.DB, error) { diff --git a/go/vt/vtadmin/cluster/cluster.go b/go/vt/vtadmin/cluster/cluster.go index da6375fc8eb..3f5b0af7e5d 100644 --- a/go/vt/vtadmin/cluster/cluster.go +++ b/go/vt/vtadmin/cluster/cluster.go @@ -21,6 +21,8 @@ import ( "vitess.io/vitess/go/vt/vtadmin/cluster/discovery" "vitess.io/vitess/go/vt/vtadmin/vtsql" + + vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" ) // Cluster is the self-contained unit of services required for vtadmin to talk @@ -51,7 +53,7 @@ func New(cfg Config) (*Cluster, error) { discoargs := buildPFlagSlice(cfg.DiscoveryFlagsByImpl[cfg.DiscoveryImpl]) - disco, err := discovery.New(cfg.DiscoveryImpl, cluster.Name, discoargs) + disco, err := discovery.New(cfg.DiscoveryImpl, cluster.ToProto(), discoargs) if err != nil { return nil, fmt.Errorf("error while creating discovery impl (%s): %w", cfg.DiscoveryImpl, err) } @@ -60,16 +62,24 @@ func New(cfg Config) (*Cluster, error) { vtsqlargs := buildPFlagSlice(cfg.VtSQLFlags) - vtsqlCfg, err := vtsql.Parse(cluster.ID, cluster.Name, disco, vtsqlargs) + vtsqlCfg, err := vtsql.Parse(cluster.ToProto(), disco, vtsqlargs) if err != nil { return nil, fmt.Errorf("error while creating vtsql connection: %w", err) } - cluster.DB = vtsql.New(cluster.Name, vtsqlCfg) + cluster.DB = vtsql.New(vtsqlCfg) return cluster, nil } +// ToProto returns a value-copy protobuf equivalent of the cluster. +func (c Cluster) ToProto() *vtadminpb.Cluster { + return &vtadminpb.Cluster{ + Id: c.ID, + Name: c.Name, + } +} + func buildPFlagSlice(flags map[string]string) []string { args := make([]string, 0, len(flags)) for k, v := range flags { diff --git a/go/vt/vtadmin/cluster/discovery/discovery.go b/go/vt/vtadmin/cluster/discovery/discovery.go index 2aaf53e194e..25cb4fac8fe 100644 --- a/go/vt/vtadmin/cluster/discovery/discovery.go +++ b/go/vt/vtadmin/cluster/discovery/discovery.go @@ -60,7 +60,7 @@ type Discovery interface { // The flags FlagSet is provided for convenience, but also to hint to plugin // developers that they should expect the args to be in a format compatible with // pflag. -type Factory func(cluster string, flags *pflag.FlagSet, args []string) (Discovery, error) +type Factory func(cluster *vtadminpb.Cluster, flags *pflag.FlagSet, args []string) (Discovery, error) // nolint:gochecknoglobals var registry = map[string]Factory{} @@ -80,7 +80,7 @@ func Register(name string, factory Factory) { // New returns a Discovery implementation using the registered factory for the // implementation. Usage of the args slice is dependent on the implementation's // factory. -func New(impl string, cluster string, args []string) (Discovery, error) { +func New(impl string, cluster *vtadminpb.Cluster, args []string) (Discovery, error) { factory, ok := registry[impl] if !ok { return nil, fmt.Errorf("%w %s", ErrImplementationNotRegistered, impl) diff --git a/go/vt/vtadmin/cluster/discovery/discovery_consul.go b/go/vt/vtadmin/cluster/discovery/discovery_consul.go index dbeea6b3302..e560b7dce3b 100644 --- a/go/vt/vtadmin/cluster/discovery/discovery_consul.go +++ b/go/vt/vtadmin/cluster/discovery/discovery_consul.go @@ -33,7 +33,7 @@ import ( // ConsulDiscovery implements the Discovery interface for consul. type ConsulDiscovery struct { - cluster string + cluster *vtadminpb.Cluster client ConsulClient queryOptions *consul.QueryOptions @@ -52,7 +52,7 @@ type ConsulDiscovery struct { // NewConsul returns a ConsulDiscovery for the given cluster. Args are a slice // of command-line flags (e.g. "-key=value") that are parsed by a consul- // specific flag set. -func NewConsul(cluster string, flags *pflag.FlagSet, args []string) (Discovery, error) { // nolint:funlen +func NewConsul(cluster *vtadminpb.Cluster, flags *pflag.FlagSet, args []string) (Discovery, error) { // nolint:funlen c, err := consul.NewClient(consul.DefaultConfig()) if err != nil { return nil, err @@ -86,21 +86,22 @@ func NewConsul(cluster string, flags *pflag.FlagSet, args []string) (Discovery, "Go template string to produce a dialable address from a *vtadminpb.VTGate") vtgateDatacenterTmplStr := flags.String("vtgate-datacenter-tmpl", "", "Go template string to generate the datacenter for vtgate consul queries. "+ - "The cluster name is provided to the template via {{ .Cluster }}. Used once during initialization.") + "The meta information about the cluster is provided to the template via {{ .Cluster }}. "+ + "Used once during initialization.") if err := flags.Parse(args); err != nil { return nil, err } if *vtgateDatacenterTmplStr != "" { - tmpl, err := template.New("consul-vtgate-datacenter-" + cluster).Parse(*vtgateDatacenterTmplStr) + tmpl, err := template.New("consul-vtgate-datacenter-" + cluster.Id).Parse(*vtgateDatacenterTmplStr) if err != nil { return nil, err } buf := bytes.NewBuffer(nil) err = tmpl.Execute(buf, &struct { - Cluster string + Cluster *vtadminpb.Cluster }{ Cluster: cluster, }) @@ -181,7 +182,10 @@ func (c *ConsulDiscovery) discoverVTGates(_ context.Context, tags []string) ([]* for i, entry := range entries { vtgate := &vtadminpb.VTGate{ Hostname: entry.Node.Node, - Cluster: c.cluster, + Cluster: &vtadminpb.Cluster{ + Id: c.cluster.Id, + Name: c.cluster.Name, + }, } var cell, pool string diff --git a/go/vt/vtadmin/cluster/discovery/discovery_consul_test.go b/go/vt/vtadmin/cluster/discovery/discovery_consul_test.go index 88e73a07864..8d8e67722c3 100644 --- a/go/vt/vtadmin/cluster/discovery/discovery_consul_test.go +++ b/go/vt/vtadmin/cluster/discovery/discovery_consul_test.go @@ -98,7 +98,10 @@ func TestConsulDiscoverVTGates(t *testing.T) { { name: "all gates", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -113,19 +116,28 @@ func TestConsulDiscoverVTGates(t *testing.T) { }, expected: []*vtadminpb.VTGate{ { - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate1", Cell: "zone1", Pool: "pool1", }, { - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate2", Cell: "zone2", Pool: "pool1", }, { - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate3", Cell: "zone3", Pool: "pool1", @@ -136,7 +148,10 @@ func TestConsulDiscoverVTGates(t *testing.T) { { name: "one cell", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -151,7 +166,10 @@ func TestConsulDiscoverVTGates(t *testing.T) { }, expected: []*vtadminpb.VTGate{ { - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate1", Cell: "zone1", Pool: "pool1", @@ -162,7 +180,10 @@ func TestConsulDiscoverVTGates(t *testing.T) { { name: "keyspaces to watch", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -176,7 +197,10 @@ func TestConsulDiscoverVTGates(t *testing.T) { }, expected: []*vtadminpb.VTGate{ { - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate1", Cell: "zone1", Pool: "pool1", @@ -188,7 +212,10 @@ func TestConsulDiscoverVTGates(t *testing.T) { { name: "error", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -233,7 +260,10 @@ func TestConsulDiscoverVTGate(t *testing.T) { { name: "success", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -247,7 +277,10 @@ func TestConsulDiscoverVTGate(t *testing.T) { }, }, expected: &vtadminpb.VTGate{ - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate1", Cell: "zone1", Pool: "pool1", @@ -257,7 +290,10 @@ func TestConsulDiscoverVTGate(t *testing.T) { { name: "no gates", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -267,7 +303,10 @@ func TestConsulDiscoverVTGate(t *testing.T) { "vtgate": {}, }, expected: &vtadminpb.VTGate{ - Cluster: "cluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, Hostname: "vtgate1", Cell: "zone1", Pool: "pool1", @@ -277,7 +316,10 @@ func TestConsulDiscoverVTGate(t *testing.T) { { name: "error", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -321,7 +363,10 @@ func TestConsulDiscoverVTGateAddr(t *testing.T) { { name: "default template", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", @@ -339,11 +384,14 @@ func TestConsulDiscoverVTGateAddr(t *testing.T) { { name: "custom template", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", - vtgateAddrTmpl: template.Must(template.New("").Parse("{{ .Cluster }}-{{ .Pool }}-{{ .Cell }}-{{ .Hostname }}.example.com:15000")), // nolint:lll + vtgateAddrTmpl: template.Must(template.New("").Parse("{{ .Cluster.Name }}-{{ .Pool }}-{{ .Cell }}-{{ .Hostname }}.example.com:15000")), // nolint:lll }, tags: []string{}, entries: map[string][]*consul.ServiceEntry{ @@ -357,7 +405,10 @@ func TestConsulDiscoverVTGateAddr(t *testing.T) { { name: "error", disco: &ConsulDiscovery{ - cluster: "cluster", + cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "cluster", + }, vtgateService: "vtgate", vtgateCellTag: "cell", vtgatePoolTag: "pool", diff --git a/go/vt/vtadmin/cluster/discovery/discovery_test.go b/go/vt/vtadmin/cluster/discovery/discovery_test.go index 6c2b4514a2a..b00071288c7 100644 --- a/go/vt/vtadmin/cluster/discovery/discovery_test.go +++ b/go/vt/vtadmin/cluster/discovery/discovery_test.go @@ -20,6 +20,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" ) func TestNew(t *testing.T) { @@ -45,7 +47,7 @@ func TestNew(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - disco, err := New(tt.impl, "testcluster", []string{}) + disco, err := New(tt.impl, &vtadminpb.Cluster{Id: "testid", Name: "testcluster"}, []string{}) if tt.err != nil { assert.Error(t, err, tt.err.Error()) return diff --git a/go/vt/vtadmin/vtsql/config.go b/go/vt/vtadmin/vtsql/config.go index e33ae4dd55f..bb4d247a585 100644 --- a/go/vt/vtadmin/vtsql/config.go +++ b/go/vt/vtadmin/vtsql/config.go @@ -24,6 +24,8 @@ import ( "github.com/spf13/pflag" "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/vtadmin/cluster/discovery" + + vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" ) // Config represents the options that modify the behavior of a vtqsl.VTGateProxy. @@ -37,18 +39,16 @@ type Config struct { // it's not really an "option" in normal use. CredentialsPath string - ClusterID string - ClusterName string + Cluster *vtadminpb.Cluster } // Parse returns a new config with the given cluster ID and name, after // attempting to parse the command-line pflags into that Config. See // (*Config).Parse() for more details. -func Parse(clusterID string, clusterName string, disco discovery.Discovery, args []string) (*Config, error) { +func Parse(cluster *vtadminpb.Cluster, disco discovery.Discovery, args []string) (*Config, error) { cfg := &Config{ - ClusterID: clusterID, - ClusterName: clusterName, - Discovery: disco, + Cluster: cluster, + Discovery: disco, } err := cfg.Parse(args) diff --git a/go/vt/vtadmin/vtsql/config_test.go b/go/vt/vtadmin/vtsql/config_test.go index 4bbb1aa09a3..1a3a877d141 100644 --- a/go/vt/vtadmin/vtsql/config_test.go +++ b/go/vt/vtadmin/vtsql/config_test.go @@ -27,6 +27,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "vitess.io/vitess/go/vt/grpcclient" + + vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" ) func TestConfigParse(t *testing.T) { @@ -53,10 +55,12 @@ func TestConfigParse(t *testing.T) { dir := filepath.Dir(path) baseParts := strings.Split(filepath.Base(path), "-") - tmplParts := append(baseParts[:3], "{{ .ClusterName }}", baseParts[4]) + tmplParts := append(baseParts[:3], "{{ .Cluster.Name }}", baseParts[4]) cfg := &Config{ - ClusterName: "testcluster", + Cluster: &vtadminpb.Cluster{ + Name: "testcluster", + }, } credsTmplStr := filepath.Join(dir, strings.Join(tmplParts, "-")) @@ -103,7 +107,7 @@ func TestConfigParse(t *testing.T) { dir := filepath.Dir(path) baseParts := strings.Split(filepath.Base(path), "-") - tmplParts := append(baseParts[:3], "{{ .ClusterName }}", baseParts[4]) + tmplParts := append(baseParts[:3], "{{ .Cluster.Name }}", baseParts[4]) credsTmplStr := filepath.Join(dir, strings.Join(tmplParts, "-")) @@ -128,14 +132,16 @@ func TestConfigParse(t *testing.T) { } expected := &Config{ - ClusterID: "cid", - ClusterName: "testcluster", + Cluster: &vtadminpb.Cluster{ + Id: "cid", + Name: "testcluster", + }, DiscoveryTags: expectedTags, Credentials: expectedCreds, CredentialsPath: path, } - cfg, err := Parse("cid", "testcluster", nil, args) + cfg, err := Parse(&vtadminpb.Cluster{Id: "cid", Name: "testcluster"}, nil, args) assert.NoError(t, err) assert.Equal(t, expected, cfg) }) diff --git a/go/vt/vtadmin/vtsql/fakevtsql/doc.go b/go/vt/vtadmin/vtsql/fakevtsql/doc.go index 34e9b67641b..904e60c5df7 100644 --- a/go/vt/vtadmin/vtsql/fakevtsql/doc.go +++ b/go/vt/vtadmin/vtsql/fakevtsql/doc.go @@ -26,7 +26,8 @@ limitations under the License. // Then, you will call vtsql.New(), passing the faked discovery implementation // into the config: // -// db := vtsql.New("clusterID", &vtsql.Config{ +// db := vtsql.New(&vtsql.Config{ +// Cluster: &vtadminpb.Cluster{Id: "cid", Name: "cluster"}, // Discovery: disco, // }) // diff --git a/go/vt/vtadmin/vtsql/vtsql.go b/go/vt/vtadmin/vtsql/vtsql.go index abd8b7e7784..bebceeb45f2 100644 --- a/go/vt/vtadmin/vtsql/vtsql.go +++ b/go/vt/vtadmin/vtsql/vtsql.go @@ -27,6 +27,8 @@ import ( "vitess.io/vitess/go/vt/callerid" "vitess.io/vitess/go/vt/vitessdriver" "vitess.io/vitess/go/vt/vtadmin/cluster/discovery" + + vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" ) // DB defines the connection and query interface of vitess SQL queries used by @@ -59,7 +61,7 @@ type DB interface { // VTGateProxy is a proxy for creating and using database connections to vtgates // in a Vitess cluster. type VTGateProxy struct { - cluster string + cluster *vtadminpb.Cluster discovery discovery.Discovery discoveryTags []string creds Credentials @@ -85,14 +87,14 @@ var ErrConnClosed = errors.New("use of closed connection") // // It does not open a connection to a vtgate; users must call Dial before first // use. -func New(cluster string, cfg *Config) *VTGateProxy { +func New(cfg *Config) *VTGateProxy { discoveryTags := cfg.DiscoveryTags if discoveryTags == nil { discoveryTags = []string{} } return &VTGateProxy{ - cluster: cluster, + cluster: cfg.Cluster, discovery: cfg.Discovery, discoveryTags: discoveryTags, creds: cfg.Credentials, @@ -145,7 +147,7 @@ func (vtgate *VTGateProxy) Dial(ctx context.Context, target string, opts ...grpc } conf := vitessdriver.Configuration{ - Protocol: fmt.Sprintf("grpc_%s", vtgate.cluster), + Protocol: fmt.Sprintf("grpc_%s", vtgate.cluster.Id), Address: vtgate.host, Target: target, GRPCDialOptions: opts, @@ -216,7 +218,8 @@ func (vtgate *VTGateProxy) Hostname() string { } func (vtgate *VTGateProxy) annotateSpan(span trace.Span) { - span.Annotate("cluster", vtgate.cluster) + span.Annotate("cluster_id", vtgate.cluster.Id) + span.Annotate("cluster_name", vtgate.cluster.Name) if vtgate.host != "" { span.Annotate("vtgate_host", vtgate.host) diff --git a/go/vt/vtadmin/vtsql/vtsql_test.go b/go/vt/vtadmin/vtsql/vtsql_test.go index 12c7509d1d0..b5265ee1f76 100644 --- a/go/vt/vtadmin/vtsql/vtsql_test.go +++ b/go/vt/vtadmin/vtsql/vtsql_test.go @@ -91,14 +91,17 @@ func TestDial(t *testing.T) { { name: "existing conn", proxy: &VTGateProxy{ - conn: sql.OpenDB(&fakevtsql.Connector{}), + cluster: &vtadminpb.Cluster{}, + conn: sql.OpenDB(&fakevtsql.Connector{}), }, shouldErr: false, }, { - name: "discovery error", - disco: fakediscovery.New(), - proxy: &VTGateProxy{}, + name: "discovery error", + disco: fakediscovery.New(), + proxy: &VTGateProxy{ + cluster: &vtadminpb.Cluster{}, + }, shouldErr: true, }, { @@ -110,6 +113,7 @@ func TestDial(t *testing.T) { }, }, proxy: &VTGateProxy{ + cluster: &vtadminpb.Cluster{}, DialFunc: func(cfg vitessdriver.Configuration) (*sql.DB, error) { return nil, assert.AnError }, @@ -125,6 +129,7 @@ func TestDial(t *testing.T) { }, }, proxy: &VTGateProxy{ + cluster: &vtadminpb.Cluster{}, creds: &StaticAuthCredentials{ StaticAuthClientCreds: &grpcclient.StaticAuthClientCreds{ Username: "user", diff --git a/proto/vtadmin.proto b/proto/vtadmin.proto index c1f3fb79c34..bf9cc73bfe5 100644 --- a/proto/vtadmin.proto +++ b/proto/vtadmin.proto @@ -70,8 +70,8 @@ message VTGate { string pool = 2; // Cell is the topology cell the VTGate is in. string cell = 3; - // Cluster is the name of the cluster the VTGate serves. - string cluster = 4; + // Cluster is the cluster the VTGate serves. + Cluster cluster = 4; // Keyspaces is the list of keyspaces-to-watch for the VTGate. repeated string keyspaces = 5; }