vitess.io/vitess@v0.16.2/go/vt/vtadmin/cluster/discovery/discovery_dynamic.go (about)

     1  /*
     2  Copyright 2022 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  
    22  	"github.com/spf13/pflag"
    23  
    24  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    25  )
    26  
    27  // DynamicDiscovery implements the Discovery interface for "discovering"
    28  // Vitess components hardcoded in a json string. It inherits from JSONDiscovery.
    29  //
    30  // As an example, here's a minimal Dynamic object for a single Vitess cluster running locally
    31  // (such as the one described in https://vitess.io/docs/get-started/local-docker):
    32  //
    33  // 		{
    34  // 			"vtgates": [
    35  // 				{
    36  // 					"host": {
    37  // 						"hostname": "127.0.0.1:15991"
    38  // 					}
    39  // 				}
    40  // 			]
    41  // 		}
    42  //
    43  // For more examples of various static file configurations, see the unit tests.
    44  // Discovery Dynamic is very similar to static file discovery, but removes the need for a static file in memory.
    45  // This allows for dynamic cluster discovery after initial vtadmin deploy without a topo.
    46  
    47  type DynamicDiscovery struct {
    48  	JSONDiscovery
    49  }
    50  
    51  // NewDynamic returns a DynamicDiscovery for the given cluster.
    52  func NewDynamic(cluster *vtadminpb.Cluster, flags *pflag.FlagSet, args []string) (Discovery, error) {
    53  	disco := &DynamicDiscovery{
    54  		JSONDiscovery: JSONDiscovery{
    55  			cluster: cluster,
    56  		},
    57  	}
    58  
    59  	json := flags.String("discovery", "", "the json config object")
    60  	if err := flags.Parse(args); err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	if json == nil || *json == "" {
    65  		return nil, errors.New("must pass service discovery json config object")
    66  	}
    67  
    68  	bytes := []byte(*json)
    69  	if err := disco.parseConfig(bytes); err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return disco, nil
    74  }