Skip to content
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
2 changes: 1 addition & 1 deletion docker/local/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

docker run -p 15000:15000 -p 15001:15001 --rm -it vitess/local
docker run -p 15000:15000 -p 15001:15001 -p 15991:15991 --rm -it vitess/local
1 change: 1 addition & 0 deletions go/vt/vtadmin/cluster/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ func New(impl string, cluster *vtadminpb.Cluster, args []string) (Discovery, err

func init() { // nolint:gochecknoinits
Register("consul", NewConsul)
Register("staticFile", NewStaticFile)
}
176 changes: 176 additions & 0 deletions go/vt/vtadmin/cluster/discovery/discovery_static_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
Copyright 2020 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package discovery

import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"math/rand"

"github.com/spf13/pflag"
vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
)

// StaticFileDiscovery implements the Discovery interface for "discovering"
// Vitess components hardcoded in a static JSON file.
//
// As an example, here's a minimal JSON file for a single Vitess cluster running locally
// (such as the one described in https://vitess.io/docs/get-started/local-docker):
//
// {
// "vtgates": [
// {
// "host": {
// "hostname": "127.0.0.1:15991"
// }
// }
// ]
// }
//
// For more examples of various static file configurations, see the unit tests.
type StaticFileDiscovery struct {
cluster *vtadminpb.Cluster
config *StaticFileClusterConfig
gates struct {
byName map[string]*vtadminpb.VTGate
byTag map[string][]*vtadminpb.VTGate
}
}

// StaticFileClusterConfig configures Vitess components for a single cluster.
type StaticFileClusterConfig struct {
VTGates []*StaticFileVTGateConfig `json:"vtgates,omitempty"`
}

// StaticFileVTGateConfig contains host and tag information for a single VTGate in a cluster.
type StaticFileVTGateConfig struct {
Host *vtadminpb.VTGate `json:"host"`
Tags []string `json:"tags"`
}

// NewStaticFile returns a StaticFileDiscovery for the given cluster.
func NewStaticFile(cluster *vtadminpb.Cluster, flags *pflag.FlagSet, args []string) (Discovery, error) {
disco := &StaticFileDiscovery{
cluster: cluster,
}

filePath := flags.String("path", "", "path to the service discovery JSON config file")
if err := flags.Parse(args); err != nil {
return nil, err
}

if filePath == nil || *filePath == "" {
return nil, errors.New("must specify path to the service discovery JSON config file")
}

b, err := ioutil.ReadFile(*filePath)
if err != nil {
return nil, err
}

if err := disco.parseConfig(b); err != nil {
return nil, err
}

return disco, nil
}

func (d *StaticFileDiscovery) parseConfig(bytes []byte) error {
if err := json.Unmarshal(bytes, &d.config); err != nil {
return err
}

d.gates.byName = make(map[string]*vtadminpb.VTGate, len(d.config.VTGates))
d.gates.byTag = make(map[string][]*vtadminpb.VTGate)

// Index the gates by name and by tag for easier lookups
for _, gate := range d.config.VTGates {
d.gates.byName[gate.Host.Hostname] = gate.Host

for _, tag := range gate.Tags {
d.gates.byTag[tag] = append(d.gates.byTag[tag], gate.Host)
}
}
return nil
}

// DiscoverVTGate is part of the Discovery interface.
func (d *StaticFileDiscovery) DiscoverVTGate(ctx context.Context, tags []string) (*vtadminpb.VTGate, error) {
gates, err := d.DiscoverVTGates(ctx, tags)
if err != nil {
return nil, err
}

count := len(gates)
if count == 0 {
return nil, ErrNoVTGates
}

gate := gates[rand.Intn(len(gates))]
return gate, nil
}

// DiscoverVTGateAddr is part of the Discovery interface.
func (d *StaticFileDiscovery) DiscoverVTGateAddr(ctx context.Context, tags []string) (string, error) {
gate, err := d.DiscoverVTGate(ctx, tags)
if err != nil {
return "", err
}

return gate.Hostname, nil
}

// DiscoverVTGates is part of the Discovery interface.
func (d *StaticFileDiscovery) DiscoverVTGates(ctx context.Context, tags []string) ([]*vtadminpb.VTGate, error) {
if len(tags) == 0 {
results := []*vtadminpb.VTGate{}
for _, g := range d.gates.byName {
results = append(results, g)
}

return results, nil
}

set := d.gates.byName

for _, tag := range tags {
intermediate := map[string]*vtadminpb.VTGate{}

gates, ok := d.gates.byTag[tag]
if !ok {
return []*vtadminpb.VTGate{}, nil
}

for _, g := range gates {
if _, ok := set[g.Hostname]; ok {
intermediate[g.Hostname] = g
}
}

set = intermediate
}

results := make([]*vtadminpb.VTGate, 0, len(set))

for _, gate := range set {
results = append(results, gate)
}

return results, nil
}
Loading