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

[vtadmin] Add cluster protos to discovery and vtsql package constructors #7224

Merged
merged 3 commits into from
Dec 24, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions go/vt/proto/vtadmin/vtadmin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions go/vt/vtadmin/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions go/vt/vtadmin/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtadmin/cluster/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions go/vt/vtadmin/cluster/discovery/discovery_consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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
Expand Down
Loading