github.com/google/cloudprober@v0.11.3/rds/client/cmd/client.go (about)

     1  // Copyright 2018 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // This binary implements a standalone ResourceDiscovery service (RDS) client.
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"flag"
    24  	"github.com/golang/glog"
    25  	"github.com/golang/protobuf/proto"
    26  
    27  	tlsconfigpb "github.com/google/cloudprober/common/tlsconfig/proto"
    28  	"github.com/google/cloudprober/logger"
    29  	"github.com/google/cloudprober/rds/client"
    30  	configpb "github.com/google/cloudprober/rds/client/proto"
    31  	pb "github.com/google/cloudprober/rds/proto"
    32  )
    33  
    34  var (
    35  	rdsServer      = flag.String("rds_server", "", "gRPC server address")
    36  	rdsServerCert  = flag.String("rds_server_cert", "", "gRPC server cert to use.")
    37  	certServerName = flag.String("cert_server_name", "", "Server name for the cert, defaults to project name.")
    38  	provider       = flag.String("provider", "gcp", "Resource provider")
    39  	resType        = flag.String("resource_type", "gce_instances", "Resource type")
    40  	publicIP       = flag.Bool("public_ip", false, "Public IP instead of private")
    41  	project        = flag.String("project", "", "GCP project")
    42  	filtersF       = flag.String("filters", "", "Comma separated list of filters, e.g. name=ig-us-central1-a-.*")
    43  )
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	if *project == "" {
    49  		glog.Exit("--project is a required paramater")
    50  	}
    51  
    52  	serverName := *certServerName
    53  	if serverName == "" {
    54  		serverName = *project
    55  
    56  		if strings.Contains(serverName, ":") {
    57  			parts := strings.SplitN(serverName, ":", 2)
    58  			serverName = parts[1] + "." + parts[0]
    59  		}
    60  	}
    61  
    62  	c := &configpb.ClientConf{
    63  		ServerOptions: &configpb.ClientConf_ServerOptions{
    64  			ServerAddress: rdsServer,
    65  		},
    66  	}
    67  
    68  	if *rdsServerCert != "" {
    69  		c.ServerOptions.TlsConfig = &tlsconfigpb.TLSConfig{
    70  			CaCertFile: rdsServerCert,
    71  			ServerName: proto.String(serverName),
    72  		}
    73  	}
    74  
    75  	c.Request = &pb.ListResourcesRequest{
    76  		Provider:     proto.String(*provider),
    77  		ResourcePath: proto.String(fmt.Sprintf("%s/%s", *resType, *project)),
    78  	}
    79  
    80  	if *publicIP {
    81  		c.Request.IpConfig = &pb.IPConfig{
    82  			IpType: pb.IPConfig_PUBLIC.Enum(),
    83  		}
    84  	}
    85  
    86  	for _, f := range strings.Split(*filtersF, ",") {
    87  		if f == "" {
    88  			continue
    89  		}
    90  		fParts := strings.SplitN(f, "=", 2)
    91  		if len(fParts) != 2 {
    92  			glog.Exitf("bad filter in --filters flag (%s): %s", *filtersF, f)
    93  		}
    94  		c.Request.Filter = append(c.Request.Filter, &pb.Filter{
    95  			Key:   proto.String(fParts[0]),
    96  			Value: proto.String(fParts[1]),
    97  		})
    98  	}
    99  
   100  	tgts, err := client.New(c, nil, &logger.Logger{})
   101  	if err != nil {
   102  		glog.Exit(err)
   103  	}
   104  	for {
   105  		fmt.Printf("%s\n", time.Now())
   106  
   107  		for _, ep := range tgts.ListEndpoints() {
   108  			ip, _ := tgts.Resolve(ep.Name, 4)
   109  			fmt.Printf("%s\t%s\n", ep.Name, ip.String())
   110  		}
   111  		time.Sleep(5 * time.Second)
   112  	}
   113  }