vitess.io/vitess@v0.16.2/go/vt/vtadmin/cluster/discovery/discovery_static_file.go (about) 1 /* 2 Copyright 2020 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package discovery 18 19 import ( 20 "errors" 21 "os" 22 23 "github.com/spf13/pflag" 24 25 vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" 26 ) 27 28 // StaticFileDiscovery implements the Discovery interface for "discovering" 29 // Vitess components hardcoded in a static JSON file. It inherits from JSONDiscovery. 30 // 31 // As an example, here's a minimal JSON file for a single Vitess cluster running locally 32 // (such as the one described in https://vitess.io/docs/get-started/local-docker): 33 // 34 // { 35 // "vtgates": [ 36 // { 37 // "host": { 38 // "hostname": "127.0.0.1:15991" 39 // } 40 // } 41 // ] 42 // } 43 // 44 // For more examples of various static file configurations, see the unit tests. 45 type StaticFileDiscovery struct { 46 JSONDiscovery 47 } 48 49 // NewStaticFile returns a StaticFileDiscovery for the given cluster. 50 func NewStaticFile(cluster *vtadminpb.Cluster, flags *pflag.FlagSet, args []string) (Discovery, error) { 51 disco := &StaticFileDiscovery{ 52 JSONDiscovery: JSONDiscovery{ 53 cluster: cluster, 54 }, 55 } 56 57 filePath := flags.String("path", "", "path to the service discovery JSON config file") 58 if err := flags.Parse(args); err != nil { 59 return nil, err 60 } 61 62 if filePath == nil || *filePath == "" { 63 return nil, errors.New("must specify path to the service discovery JSON config file") 64 } 65 66 b, err := os.ReadFile(*filePath) 67 if err != nil { 68 return nil, err 69 } 70 71 if err := disco.parseConfig(b); err != nil { 72 return nil, err 73 } 74 75 return disco, nil 76 }