• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.circleci/H29-Oct-2020-

cmd/discover/H29-Oct-2020-

provider/H29-Oct-2020-

test/tf/H29-Oct-2020-

.gitignoreH A D29-Oct-2020123

.travis.ymlH A D29-Oct-202081

LICENSEH A D29-Oct-202015.6 KiB

README.mdH A D29-Oct-202010.3 KiB

config.goH A D29-Oct-20204.8 KiB

config_test.goH A D29-Oct-20202.4 KiB

discover.goH A D29-Oct-20205.1 KiB

go.modH A D29-Oct-20202 KiB

go.sumH A D29-Oct-202030.2 KiB

README.md

1# Go Discover Nodes for Cloud Providers [![Build Status](https://travis-ci.org/hashicorp/go-discover.svg?branch=master)](https://travis-ci.org/hashicorp/go-discover) [![GoDoc](https://godoc.org/github.com/hashicorp/go-discover?status.svg)](https://godoc.org/github.com/hashicorp/go-discover)
2
3
4`go-discover` is a Go (golang) library and command line tool to discover
5ip addresses of nodes in cloud environments based on meta information
6like tags provided by the environment.
7
8The configuration for the providers is provided as a list of `key=val key=val
9...` tuples. If either the key or the value contains a space (` `), a backslash
10(`\`) or double quotes (`"`) then it needs to be quoted with double quotes.
11Within a quoted string you can use the backslash to escape double quotes or the
12backslash itself, e.g. `key=val "some key"="some value"`
13
14Duplicate keys are reported as error and the provider is determined through the
15`provider` key.
16
17### Supported Providers
18
19The following cloud providers have implementations in the go-discover/provider
20sub packages. Additional providers can be added through the
21[Register](https://godoc.org/github.com/hashicorp/go-discover#Register)
22function.
23
24 * Aliyun (Alibaba) Cloud [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/aliyun/aliyun_discover.go#L15-L28)
25 * Amazon AWS [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/aws/aws_discover.go#L19-L33)
26 * DigitalOcean [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/digitalocean/digitalocean_discover.go#L16-L24)
27 * Google Cloud [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/gce/gce_discover.go#L17-L37)
28 * Linode [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/linode/linode_discover.go#L30-L41)
29 * mDNS [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/mdns/mdns_provider.go#L19-L31)
30 * Microsoft Azure [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/azure/azure_discover.go#L16-L37)
31 * Openstack [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/os/os_discover.go#L23-L38)
32 * Scaleway [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/scaleway/scaleway_discover.go#L14-L22)
33 * SoftLayer [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/softlayer/softlayer_discover.go#L16-L25)
34 * TencentCloud [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/tencentcloud/tencentcloud_discover.go#L23-L37)
35 * Triton [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/triton/triton_discover.go#L17-L27)
36 * vSphere [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/vsphere/vsphere_discover.go#L148-L155)
37 * Packet [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/packet/packet_discover.go#L25-L35)
38
39The following providers are implemented in the go-discover/provider subdirectory
40but aren't automatically registered. If you want to support these providers,
41register them manually:
42
43 * Kubernetes [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/k8s/k8s_discover.go#L32-L51)
44
45HashiCorp maintains acceptance tests that regularly allocate and run tests with
46real resources to verify the behavior of several of these providers. Those
47currently are: Amazon AWS, Microsoft Azure, Google Cloud, DigitalOcean, Triton, Scaleway, AliBaba Cloud, vSphere, and Packet.net.
48
49### Config Example
50
51```
52# Aliyun (Alibaba) Cloud
53provider=aliyun region=... tag_key=consul tag_value=... access_key_id=... access_key_secret=...
54
55# Amazon AWS
56provider=aws region=eu-west-1 tag_key=consul tag_value=... access_key_id=... secret_access_key=...
57
58# DigitalOcean
59provider=digitalocean region=... tag_name=... api_token=...
60
61# Google Cloud
62provider=gce project_name=... zone_pattern=eu-west-* tag_value=consul credentials_file=...
63
64# Linode
65provider=linode tag_name=... region=us-east address_type=private_v4 api_token=...
66
67# mDNS
68provider=mdns service=consul domain=local
69
70# Microsoft Azure
71provider=azure tag_name=consul tag_value=... tenant_id=... client_id=... subscription_id=... secret_access_key=...
72
73# Openstack
74provider=os tag_key=consul tag_value=server username=... password=... auth_url=...
75
76# Scaleway
77provider=scaleway organization=my-org tag_name=consul-server token=... region=...
78
79# SoftLayer
80provider=softlayer datacenter=dal06 tag_value=consul username=... api_key=...
81
82# TencentCloud
83provider=tencentcloud region=ap-guangzhou tag_key=consul tag_value=... access_key_id=... access_key_secret=...
84
85# Triton
86provider=triton account=testaccount url=https://us-sw-1.api.joyentcloud.com key_id=... tag_key=consul-role tag_value=server
87
88# vSphere
89provider=vsphere category_name=consul-role tag_name=consul-server host=... user=... password=... insecure_ssl=[true|false]
90
91# Packet
92provider=packet auth_token=token project=uuid url=... address_type=...
93
94# Kubernetes
95provider=k8s label_selector="app = consul-server"
96```
97
98## Command Line Tool Usage
99
100Install the command line tool with:
101
102```
103go get -u github.com/hashicorp/go-discover/cmd/discover
104```
105
106Then run it with:
107
108```
109$ discover addrs provider=aws region=eu-west-1 ...
110```
111
112## Library Usage
113
114Install the library with:
115
116```
117go get -u github.com/hashicorp/go-discover
118```
119
120You can then either support discovery for all available providers
121or only for some of them.
122
123```go
124// support discovery for all supported providers
125d := discover.Discover{}
126
127// support discovery for AWS and GCE only
128d := discover.Discover{
129	Providers : map[string]discover.Provider{
130		"aws": discover.Providers["aws"],
131		"gce": discover.Providers["gce"],
132	}
133}
134
135// use ioutil.Discard for no log output
136l := log.New(os.Stderr, "", log.LstdFlags)
137
138cfg := "provider=aws region=eu-west-1 ..."
139addrs, err := d.Addrs(cfg, l)
140```
141
142You can also add support for providers that aren't registered by default:
143
144```go
145// Imports at top of file
146import "github.com/hashicorp/go-discover/provider/k8s"
147
148// support discovery for all supported providers
149d := discover.Discover{}
150
151// support discovery for AWS and GCE only
152d := discover.Discover{
153	Providers : map[string]discover.Provider{
154		"k8s": &k8s.Provider{},
155	}
156}
157
158// ...
159```
160
161For complete API documentation, see
162[GoDoc](https://godoc.org/github.com/hashicorp/go-discover). The configuration
163for the supported providers is documented in the
164[providers](https://godoc.org/github.com/hashicorp/go-discover/provider)
165sub-package.
166
167## Testing
168
169**Note: Due to the `go.sum` checksum errors referenced in [#68](https://github.com/hashicorp/go-discover/issues/68),
170you will need Go 1.11.4+ to build/test go-discover.**
171
172Configuration tests can be run with Go:
173
174```
175$ go test ./...
176```
177
178By default tests that communicate with providers do not run unless credentials
179are set for that provider. To run provider tests you must set the necessary
180environment variables.
181
182**Note: This will make real API calls to the account provided by the credentials.**
183
184```
185$ AWS_ACCESS_KEY_ID=... AWS_ACCESS_KEY_SECRET=... AWS_REGION=... go test -v ./provider/aws
186```
187
188This requires resources to exist that match those specified in tests
189(eg instance tags in the case of AWS). To create these resources,
190there are sets of [Terraform](https://www.terraform.io) configuration
191in the `test/tf` directory for supported providers.
192
193You must use the same account and access credentials above. The same
194environment variables should be applicable and read by Terraform.
195
196```
197$ cd test/tf/aws
198$ export AWS_ACCESS_KEY_ID=... AWS_ACCESS_KEY_SECRET=... AWS_REGION=...
199$ terraform init
200...
201$ terraform apply
202...
203```
204
205After Terraform successfully runs, you should be able to successfully
206run the tests, assuming you have exported credentials into
207your environment:
208
209```
210$ go test -v ./provider/aws
211```
212
213To destroy the resources you need to use Terraform again:
214
215```
216$ cd test/tf/aws
217$ terraform destroy
218...
219```
220
221**Note: There should be no requirements to create and test these resources other
222than credentials and Terraform. This is to ensure tests can run in development
223and CI environments consistently across all providers.**
224
225## Retrieving Test Credentials
226
227Below are instructions for retrieving credentials in order to run
228tests for some of the providers.
229
230<details>
231  <summary>Google Cloud</summary>
232
2331. Go to https://console.cloud.google.com/
2341. IAM &amp; Admin / Settings:
235    * Create Project, e.g. `discover`
236    * Write down the `Project ID`, e.g. `discover-xxx`
2371. Billing: Ensure that the project is linked to a billing account
2381. API Manager / Dashboard: Enable the following APIs
239    * Google Compute Engine API
2401. IAM &amp; Admin / Service Accounts: Create Service Account
241    * Service account name: `admin`
242    * Roles:
243        * `Project/Service Account Actor`
244        * `Compute Engine/Compute Instance Admin (v1)`
245        * `Compute Engine/Compute Security Admin`
246    * Furnish a new private key: `yes`
247    * Key type: `JSON`
2481. The credentials file `discover-xxx.json` will have been downloaded
249   automatically to your machine
2501. Source the contents of the credentials file into the `GOOGLE_CREDENTIALS`
251   environment variable
252
253</details>
254
255<details>
256  <summary>Azure</summary>
257See also the [Terraform provider documentation](https://www.terraform.io/docs/providers/azurerm/index.html#creating-credentials).
258
259```shell
260# Install Azure CLI (https://github.com/Azure/azure-cli)
261curl -L https://aka.ms/InstallAzureCli | bash
262
263# 1. Login
264$ az login
265
266# 2. Get SubscriptionID
267$ az account list
268[
269  {
270    "cloudName": "AzureCloud",
271    "id": "subscription_id",
272    "isDefault": true,
273    "name": "Gratis versie",
274    "state": "Enabled",
275    "tenantId": "tenant_id",
276    "user": {
277      "name": "user@email.com",
278      "type": "user"
279    }
280  }
281]
282
283# 3. Switch to subscription
284$ az account set --subscription="subscription_id"
285
286# 4. Create ClientID and Secret
287$ az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/subscription_id"
288{
289  "appId": "client_id",
290  "displayName": "azure-cli-2017-07-18-16-51-43",
291  "name": "http://azure-cli-2017-07-18-16-51-43",
292  "password": "client_secret",
293  "tenant": "tenant_id"
294}
295
296# 5. Export the Credentials for the client
297export ARM_CLIENT_ID=client_id
298export ARM_CLIENT_SECRET=client_secret
299export ARM_TENANT_ID=tenant_id
300export ARM_SUBSCRIPTION_ID=subscription_id
301
302# 6. Test the credentials
303$ az vm list-sizes --location 'West Europe'
304```
305</details>
306