Skip to content

Instantly share code, notes, and snippets.

@ajm188
Created April 6, 2022 20:04
Show Gist options
  • Save ajm188/5b5c8ba0cc5660298697e0f762081d45 to your computer and use it in GitHub Desktop.
Save ajm188/5b5c8ba0cc5660298697e0f762081d45 to your computer and use it in GitHub Desktop.
vtadmin grpc dynamic cluster
I0406 16:00:17.342425 5884 resolver.go:269] [vtadmin.cluster.resolver]: resolving vtctlds (cluster dynamic)
I0406 16:00:17.342477 5884 resolver.go:332] [vtadmin.cluster.resolver]: found 1 vtctlds (cluster dynamic)
I0406 16:00:17.342557 5884 proxy.go:141] Established gRPC connection to vtctld
I0406 16:00:17.345089 5884 resolver.go:269] [vtadmin.cluster.resolver]: resolving vtgates (cluster dynamic)
I0406 16:00:17.345144 5884 resolver.go:332] [vtadmin.cluster.resolver]: found 1 vtgates (cluster dynamic)
I0406 16:00:17.345230 5884 vtsql.go:136] Have valid connection to vtgate, reusing it.
➜  vitess git:(andrew/vtadmin-grpc-dynamic-clusters) ✗ ./bin/vtadminclient
{
  "clusters": [
    {
      "id": "dynamic",
      "name": "dynamic"
    }
  ]
}
package main
import (
"context"
"encoding/base64"
"flag"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/vt/log"
vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
)
func fatal(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
addr := flag.String("addr", ":14200", "")
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cc, err := grpc.DialContext(ctx, *addr, grpc.WithInsecure())
fatal(err)
defer cc.Close()
client := vtadminpb.NewVTAdminClient(cc)
clusterJSON := `{
"name": "dynamic",
"vtctlds": [
{
"host": {
"fqdn": "localhost:15000",
"hostname": "localhost:15999"
}
}
],
"vtgates": [
{
"host": {
"hostname": "localhost:15991"
}
}
]
}
`
ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{
"cluster": base64.StdEncoding.EncodeToString([]byte(clusterJSON)),
}))
resp, err := client.GetClusters(ctx, &vtadminpb.GetClustersRequest{})
fatal(err)
data, err := cli.MarshalJSON(resp)
fatal(err)
fmt.Printf("%s\n", data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment