| 1 |
--- |
| 2 |
title: "Runtime Configuration" |
| 3 |
date: 2019-07-11T12:00:00-07:00 |
| 4 |
lastmod: 2020-12-23T12:00:00-07:00 |
| 5 |
showInSidenav: true |
| 6 |
toc: true |
| 7 |
--- |
| 8 |
|
| 9 |
The [`runtimevar` package][] provides an easy and portable way to watch runtime |
| 10 |
configuration variables. This guide shows how to work with runtime configuration |
| 11 |
variables using the Go CDK. |
| 12 |
|
| 13 |
<!--more--> |
| 14 |
|
| 15 |
Subpackages contain driver implementations of runtimevar for various services, |
| 16 |
including Cloud and on-prem solutions. You can develop your application locally |
| 17 |
using [`filevar`][] or [`constantvar`][], then deploy it to multiple Cloud |
| 18 |
providers with minimal initialization reconfiguration. |
| 19 |
|
| 20 |
[`runtimevar` package]: https://godoc.org/gocloud.dev/runtimevar |
| 21 |
[`filevar`]: https://godoc.org/gocloud.dev/runtimevar/filevar |
| 22 |
[`constantvar`]: https://godoc.org/gocloud.dev/runtimevar/constantvar |
| 23 |
|
| 24 |
## Opening a Variable {#opening} |
| 25 |
|
| 26 |
The first step in watching a variable is to instantiate a portable |
| 27 |
[`*runtimevar.Variable`][] for your service. |
| 28 |
|
| 29 |
The easiest way to do so is to use [`runtimevar.OpenVariable`][] and a service-specific URL pointing |
| 30 |
to the variable, making sure you ["blank import"][] the driver package to link |
| 31 |
it in. |
| 32 |
|
| 33 |
```go |
| 34 |
import ( |
| 35 |
"gocloud.dev/runtimevar" |
| 36 |
_ "gocloud.dev/runtimevar/<driver>" |
| 37 |
) |
| 38 |
... |
| 39 |
v, err := runtimevar.OpenVariable(context.Background(), "<driver-url>") |
| 40 |
if err != nil { |
| 41 |
return fmt.Errorf("could not open variable: %v", err) |
| 42 |
} |
| 43 |
defer v.Close() |
| 44 |
// v is a *runtimevar.Variable; see usage below |
| 45 |
... |
| 46 |
``` |
| 47 |
|
| 48 |
See [Concepts: URLs][] for general background and the [guide below][] |
| 49 |
for URL usage for each supported service. |
| 50 |
|
| 51 |
Alternatively, if you need fine-grained control |
| 52 |
over the connection settings, you can call the constructor function in the |
| 53 |
driver package directly (like `etcdvar.OpenVariable`). |
| 54 |
|
| 55 |
```go |
| 56 |
import "gocloud.dev/runtimevar/<driver>" |
| 57 |
... |
| 58 |
v, err := <driver>.OpenVariable(...) |
| 59 |
... |
| 60 |
``` |
| 61 |
|
| 62 |
You may find the [`wire` package][] useful for managing your initialization code |
| 63 |
when switching between different backing services. |
| 64 |
|
| 65 |
See the [guide below][] for constructor usage for each supported service. |
| 66 |
|
| 67 |
When opening the variable, you can provide a [decoder][] parameter (either as a |
| 68 |
[query parameter][] for URLs, or explicitly to the constructor) to specify |
| 69 |
whether the raw value stored in the variable is interpreted as a `string`, a |
| 70 |
`[]byte`, or as JSON. Here's an example of using a JSON encoder: |
| 71 |
|
| 72 |
{{< goexample src="gocloud.dev/runtimevar.Example_jsonDecoder" imports="0" >}} |
| 73 |
|
| 74 |
[`*runtimevar.Variable`]: https://godoc.org/gocloud.dev/runtimevar#Variable |
| 75 |
[`runtimevar.OpenVariable`]: https://godoc.org/gocloud.dev/runtimevar#OpenVariable |
| 76 |
["blank import"]: https://golang.org/doc/effective_go.html#blank_import |
| 77 |
[Concepts: URLs]: {{< ref "/concepts/urls.md" >}} |
| 78 |
[decoder]: https://godoc.org/gocloud.dev/runtimevar#Decoder |
| 79 |
[guide below]: {{< ref "#services" >}} |
| 80 |
[query parameter]: https://godoc.org/gocloud.dev/runtimevar#DecoderByName |
| 81 |
[`wire` package]: http://github.com/google/wire |
| 82 |
|
| 83 |
## Using a Variable {#using} |
| 84 |
|
| 85 |
Once you have opened a `runtimevar.Variable` for the provider you want, you can |
| 86 |
use it portably. |
| 87 |
|
| 88 |
### Latest {#latest} |
| 89 |
|
| 90 |
The easiest way to a `Variable` is to use the [`Variable.Latest`][] method. It |
| 91 |
returns the latest good [`Snapshot`][] of the variable value, blocking if no |
| 92 |
good value has *ever* been detected. The dynamic type of `Snapshot.Value` |
| 93 |
depends on the decoder you provided when creating the `Variable`. |
| 94 |
|
| 95 |
{{< goexample src="gocloud.dev/runtimevar.ExampleVariable_Latest" imports="0" >}} |
| 96 |
|
| 97 |
To avoid blocking, you can pass an already-`Done` context. You can also use |
| 98 |
[`Variable.CheckHealth`][], which reports as healthy when `Latest` will |
| 99 |
return a value without blocking. |
| 100 |
|
| 101 |
[`Variable.Latest`]: https://godoc.org/gocloud.dev/runtimevar#Variable.Latest |
| 102 |
[`Variable.CheckHealth`]: https://godoc.org/gocloud.dev/runtimevar#Variable.CheckHealth |
| 103 |
[`Snapshot`]: https://godoc.org/gocloud.dev/runtimevar#Snapshot |
| 104 |
|
| 105 |
### Watch {#watch} |
| 106 |
|
| 107 |
`Variable` also has a [`Watch`][] method for obtaining the value of a variable; |
| 108 |
it has different semantics than `Latest` and may be useful in some scenarios. We |
| 109 |
recommend starting with `Latest` as it's conceptually simpler to work with. |
| 110 |
|
| 111 |
[`Watch`]: https://godoc.org/gocloud.dev/runtimevar#Variable.Watch |
| 112 |
|
| 113 |
## Other Usage Samples |
| 114 |
|
| 115 |
* [CLI Sample](https://github.com/google/go-cloud/tree/master/samples/gocdk-runtimevar) |
| 116 |
* [Guestbook sample](https://gocloud.dev/tutorials/guestbook/) |
| 117 |
* [runtimevar package examples](https://godoc.org/gocloud.dev/runtimevar#pkg-examples) |
| 118 |
|
| 119 |
## Supported Services {#services} |
| 120 |
|
| 121 |
### GCP Runtime Configurator {#gcprc} |
| 122 |
|
| 123 |
To open a variable stored in [GCP Runtime Configurator][] via a URL, you can use |
| 124 |
the `runtimevar.OpenVariable` function as shown in the example below. |
| 125 |
|
| 126 |
[GCP Runtime Configurator]: https://cloud.google.com/deployment-manager/runtime-configurator/ |
| 127 |
|
| 128 |
`runtimevar.OpenVariable` will use Application Default Credentials; if you have |
| 129 |
authenticated via [`gcloud auth application-default login`][], it will use those credentials. See |
| 130 |
[Application Default Credentials][GCP creds] to learn about authentication |
| 131 |
alternatives, including using environment variables. |
| 132 |
|
| 133 |
[GCP creds]: https://cloud.google.com/docs/authentication/production |
| 134 |
[`gcloud auth application-default login`]: https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login |
| 135 |
|
| 136 |
{{< goexample |
| 137 |
"gocloud.dev/runtimevar/gcpruntimeconfig.Example_openVariableFromURL" >}} |
| 138 |
|
| 139 |
#### GCP Constructor {#gcprc-ctor} |
| 140 |
|
| 141 |
The [`gcpruntimeconfig.OpenVariable`][] constructor opens a Runtime Configurator |
| 142 |
variable. |
| 143 |
|
| 144 |
{{< goexample "gocloud.dev/runtimevar/gcpruntimeconfig.ExampleOpenVariable" >}} |
| 145 |
|
| 146 |
[`gcpruntimeconfig.OpenVariable`]: https://godoc.org/gocloud.dev/runtimevar/gcpruntimeconfig#OpenVariable |
| 147 |
|
| 148 |
### GCP Secret Manager {#gcpsm} |
| 149 |
|
| 150 |
To open a variable stored in [GCP Secret Manager][] via a URL, you can use |
| 151 |
the `runtimevar.OpenVariable` function as shown in the example below. |
| 152 |
|
| 153 |
[GCP Secret Manager]: https://cloud.google.com/secret-manager |
| 154 |
|
| 155 |
`runtimevar.OpenVariable` will use Application Default Credentials; if you have |
| 156 |
authenticated via [`gcloud auth application-default login`][], it will use those credentials. See |
| 157 |
[Application Default Credentials][GCP creds] to learn about authentication |
| 158 |
alternatives, including using environment variables. |
| 159 |
|
| 160 |
[GCP creds]: https://cloud.google.com/docs/authentication/production |
| 161 |
[`gcloud auth application-default login`]: https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login |
| 162 |
|
| 163 |
{{< goexample |
| 164 |
"gocloud.dev/runtimevar/gcpsecretmanager.Example_openVariableFromURL" >}} |
| 165 |
|
| 166 |
#### GCP Constructor {#gcpsm-ctor} |
| 167 |
|
| 168 |
The [`gcpsecretmanager.OpenVariable`][] constructor opens a Secret Manager |
| 169 |
variable. |
| 170 |
|
| 171 |
{{< goexample "gocloud.dev/runtimevar/gcpsecretmanager.ExampleOpenVariable" >}} |
| 172 |
|
| 173 |
[`gcpsecretmanager.OpenVariable`]: https://godoc.org/gocloud.dev/runtimevar/gcpsecretmanager#OpenVariable |
| 174 |
|
| 175 |
### AWS Parameter Store {#awsps} |
| 176 |
|
| 177 |
To open a variable stored in [AWS Parameter Store][] via a URL, you can use the |
| 178 |
`runtimevar.OpenVariable` function as shown in the example below. |
| 179 |
|
| 180 |
[AWS Parameter Store]: |
| 181 |
https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html |
| 182 |
|
| 183 |
It will create an AWS Config based on the AWS SDK V2; see [AWS V2 Config][] to learn more. |
| 184 |
|
| 185 |
[AWS V2 Config]: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ |
| 186 |
|
| 187 |
{{< goexample |
| 188 |
"gocloud.dev/runtimevar/awsparamstore.Example_openVariableFromURL" >}} |
| 189 |
|
| 190 |
#### AWS Constructor {#awsps-ctor} |
| 191 |
|
| 192 |
The [`awsparamstore.OpenVariable`][] constructor opens a Parameter Store |
| 193 |
variable. |
| 194 |
|
| 195 |
{{< goexample "gocloud.dev/runtimevar/awsparamstore.ExampleOpenVariable" >}} |
| 196 |
|
| 197 |
[`awsparamstore.OpenVariable`]: |
| 198 |
https://godoc.org/gocloud.dev/runtimevar/awsparamstore#OpenVariable |
| 199 |
|
| 200 |
### AWS Secrets Manager {#awssm} |
| 201 |
|
| 202 |
To open a variable stored in [AWS Secrets Manager][] via a URL, you can use the |
| 203 |
`runtimevar.OpenVariable` function as shown in the example below. |
| 204 |
|
| 205 |
[AWS Secrets Manager]: |
| 206 |
https://aws.amazon.com/secrets-manager |
| 207 |
|
| 208 |
It will create an AWS Config based on the AWS SDK V2; see [AWS V2 Config][] to learn more. |
| 209 |
|
| 210 |
[AWS V2 Config]: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ |
| 211 |
|
| 212 |
{{< goexample |
| 213 |
"gocloud.dev/runtimevar/awssecretsmanager.Example_openVariableFromURL" >}} |
| 214 |
|
| 215 |
#### AWS Constructor {#awssm-ctor} |
| 216 |
|
| 217 |
The [`awssecretsmanager.OpenVariable`][] constructor opens a Secrets Manager |
| 218 |
variable. |
| 219 |
|
| 220 |
{{< goexample "gocloud.dev/runtimevar/awssecretsmanager.ExampleOpenVariable" >}} |
| 221 |
|
| 222 |
[`awssecretsmanager.OpenVariable`]: |
| 223 |
https://godoc.org/gocloud.dev/runtimevar/awssecretsmanager#OpenVariable |
| 224 |
|
| 225 |
Note that both `secretsmanager:GetSecretValue` and `secretsmanager:DescribeSecret` actions must be allowed in |
| 226 |
the caller's IAM policy. |
| 227 |
|
| 228 |
### etcd {#etcd} |
| 229 |
|
| 230 |
*NOTE*: Support for `etcd` has been temporarily dropped due to dependency |
| 231 |
issues. See https://github.com/google/go-cloud/issues/2914. |
| 232 |
|
| 233 |
You can use `runtimevar.etcd` in Go CDK version `v0.20.0` or earlier. |
| 234 |
|
| 235 |
### HTTP {#http} |
| 236 |
|
| 237 |
`httpvar` supports watching a variable via an HTTP request. Use |
| 238 |
`runtimevar.OpenVariable` with a regular URL starting with `http` or `https`. |
| 239 |
`httpvar` will periodically make an HTTP `GET` request to that URL, with the |
| 240 |
`decode` URL parameter removed (if present). |
| 241 |
|
| 242 |
{{< goexample "gocloud.dev/runtimevar/httpvar.Example_openVariableFromURL" >}} |
| 243 |
|
| 244 |
#### HTTP Constructor {#http-ctor} |
| 245 |
|
| 246 |
The [`httpvar.OpenVariable`][] constructor opens a variable with a `http.Client` |
| 247 |
and a URL. |
| 248 |
|
| 249 |
{{< goexample "gocloud.dev/runtimevar/httpvar.ExampleOpenVariable" >}} |
| 250 |
|
| 251 |
[`httpvar.OpenVariable`]: https://godoc.org/gocloud.dev/runtimevar/httpvar#OpenVariable |
| 252 |
|
| 253 |
### HashiCorp Vault {#hashivault} |
| 254 |
|
| 255 |
`hashivault` supports watching a variable stored in [HashiCorp Vault's KV Secrets Engine][]. |
| 256 |
Use `runtimevar.OpenVariable` with a URL starting with `hashivault://`. |
| 257 |
|
| 258 |
The default URL opener will use the environment variables `VAULT_SERVER_URL` (or |
| 259 |
`VAULT_ADDR`) for the server address, and `VAULT_SERVER_TOKEN` (or `VAULT_TOKEN`) |
| 260 |
for authentication. |
| 261 |
|
| 262 |
[HashiCorp Vault's KV Secrets Engine]: https://www.vaultproject.io/docs/secrets/kv |
| 263 |
|
| 264 |
{{< goexample "gocloud.dev/runtimevar/hashivault.Example_openVariableFromURL" >}} |
| 265 |
|
| 266 |
#### HashiCorp Vault Constructor {#hashivault-ctor} |
| 267 |
|
| 268 |
The [`hashivault.OpenVariable`][] constructor opens a variable with a Vault client. |
| 269 |
|
| 270 |
{{< goexample "gocloud.dev/runtimevar/hashivault.ExampleOpenVariable" >}} |
| 271 |
|
| 272 |
[`hashivault.OpenVariable`]: https://godoc.org/gocloud.dev/runtimevar/hashivault#OpenVariable |
| 273 |
|
| 274 |
### Blob {#blob} |
| 275 |
|
| 276 |
`blobvar` supports watching a variable based on the contents of a |
| 277 |
[Go CDK blob][]. Set the environment variable `BLOBVAR_BUCKET_URL` to the URL |
| 278 |
of the bucket, and then use `runtimevar.OpenVariable` as shown below. |
| 279 |
`blobvar` will periodically re-fetch the contents of the blob. |
| 280 |
|
| 281 |
{{< goexample "gocloud.dev/runtimevar/blobvar.Example_openVariableFromURL" >}} |
| 282 |
|
| 283 |
[Go CDK blob]: https://gocloud.dev/howto/blob/ |
| 284 |
|
| 285 |
You can also use [`blobvar.OpenVariable`][]. |
| 286 |
|
| 287 |
[`blobvar.OpenVariable`]: https://godoc.org/gocloud.dev/runtimevar/blobvar#OpenVariable |
| 288 |
|
| 289 |
### Local {#local} |
| 290 |
|
| 291 |
You can create an in-memory variable (useful for testing) using `constantvar`: |
| 292 |
|
| 293 |
{{< goexample "gocloud.dev/runtimevar/constantvar.Example_openVariableFromURL" >}} |
| 294 |
|
| 295 |
Alternatively, you can create a variable based on the contents of a file using |
| 296 |
`filevar`: |
| 297 |
|
| 298 |
{{< goexample "gocloud.dev/runtimevar/filevar.Example_openVariableFromURL" >}} |