Documentation ¶
Overview ¶
Package gcpruntimeconfig provides a runtimevar implementation with variables read from GCP Cloud Runtime Configurator (https://cloud.google.com/deployment-manager/runtime-configurator). Use OpenVariable to construct a *runtimevar.Variable.
URLs ¶
For runtimevar.OpenVariable, gcpruntimeconfig registers for the scheme "gcpruntimeconfig". The default URL opener will creating a connection using use default credentials from the environment, as described in https://cloud.google.com/docs/authentication/production. To customize the URL opener, or for more details on the URL format, see URLOpener. See https://github.com/eliben/gocdkx/concepts/urls/ for background information.
As ¶
gcpruntimeconfig exposes the following types for As:
- Snapshot: *pb.Variable
- Error: *status.Status
Example ¶
package main import ( "context" "log" "github.com/eliben/gocdkx/gcp" "github.com/eliben/gocdkx/runtimevar" "github.com/eliben/gocdkx/runtimevar/gcpruntimeconfig" ) // MyConfig is a sample configuration struct. type MyConfig struct { Server string Port int } func main() { // Your GCP credentials. // See https://cloud.google.com/docs/authentication/production // for more info on alternatives. ctx := context.Background() creds, err := gcp.DefaultCredentials(ctx) if err != nil { log.Fatal(err) } // Connect to the Runtime Configurator service. client, cleanup, err := gcpruntimeconfig.Dial(ctx, creds.TokenSource) if err != nil { log.Fatal(err) } defer cleanup() // Create a decoder for decoding JSON strings into MyConfig. decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode) // Fill these in with the values from the Cloud Console. // For this example, the GCP Cloud Runtime Configurator variable being // referenced should have a JSON string that decodes into MyConfig. name := gcpruntimeconfig.ResourceName{ ProjectID: "gcp-project-id", Config: "cfg-name", Variable: "cfg-variable-name", } // Construct a *runtimevar.Variable that watches the variable. v, err := gcpruntimeconfig.OpenVariable(client, name, decoder, nil) if err != nil { log.Fatal(err) } defer v.Close() // We can now read the current value of the variable from v. snapshot, err := v.Latest(context.Background()) if err != nil { log.Fatal(err) } cfg := snapshot.Value.(MyConfig) _ = cfg }
Output:
Example (OpenVariable) ¶
package main import ( "context" "log" "github.com/eliben/gocdkx/runtimevar" ) func main() { // OpenVariable creates a *runtimevar.Variable from a URL. ctx := context.Background() v, err := runtimevar.OpenVariable(ctx, "gcpruntimeconfig://myproject/myconfigid/myvar?decoder=string") if err != nil { log.Fatal(err) } snapshot, err := v.Latest(ctx) _, _ = snapshot, err }
Output:
Index ¶
Examples ¶
Constants ¶
const Scheme = "gcpruntimeconfig"
Scheme is the URL scheme gcpruntimeconfig registers its URLOpener under on runtimevar.DefaultMux.
Variables ¶
var Set = wire.NewSet( Dial, URLOpener{}, )
Set holds Wire providers for this package.
Functions ¶
func Dial ¶
func Dial(ctx context.Context, ts gcp.TokenSource) (pb.RuntimeConfigManagerClient, func(), error)
Dial opens a gRPC connection to the Runtime Configurator API using credentials from ts. It is provided as an optional helper with useful defaults.
The second return value is a function that should be called to clean up the connection opened by Dial.
func OpenVariable ¶
func OpenVariable(client pb.RuntimeConfigManagerClient, name ResourceName, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)
OpenVariable constructs a *runtimevar.Variable backed by the variable name in GCP Cloud Runtime Configurator. Runtime Configurator returns raw bytes; provide a decoder to decode the raw bytes into the appropriate type for runtimevar.Snapshot.Value. See the runtimevar package documentation for examples of decoders.
Types ¶
type Options ¶
type Options struct { // WaitDuration controls the rate at which Parameter Store is polled. // Defaults to 30 seconds. WaitDuration time.Duration }
Options sets options.
type ResourceName ¶
ResourceName identifies a configuration variable.
func (ResourceName) String ¶
func (r ResourceName) String() string
String returns the full configuration variable path.
type URLOpener ¶
type URLOpener struct { // Client must be set to a non-nil client authenticated with // Cloud RuntimeConfigurator scope or equivalent. Client pb.RuntimeConfigManagerClient // Decoder specifies the decoder to use if one is not specified in the URL. // Defaults to runtimevar.BytesDecoder. Decoder *runtimevar.Decoder // Options specifies the options to pass to New. Options Options }
URLOpener opens gcpruntimeconfig URLs like "gcpruntimeconfig://myproject/mycfg/myvar".
- The URL's host holds the GCP projectID.
- The first element of the URL's path holds the GCP RuntimeConfigurator ConfigID.
- The second element of the URL's path holds the GCP RuntimeConfigurator Variable Name.
See https://cloud.google.com/deployment-manager/runtime-configurator/ for more details.
The following query parameters are supported:
- decoder: The decoder to use. Defaults to URLOpener.Decoder, or runtimevar.BytesDecoder if URLOpener.Decoder is nil. See runtimevar.DecoderByName for supported values.
func (*URLOpener) OpenVariableURL ¶
OpenVariableURL opens a gcpruntimeconfig Variable for u.