gcpruntimeconfig

package
v0.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2024 License: Apache-2.0 Imports: 21 Imported by: 7

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://gocloud.dev/concepts/urls/ for background information.

As

gcpruntimeconfig exposes the following types for As:

  • Snapshot: *pb.Variable
  • Error: *status.Status
Example (OpenVariableFromURL)
package main

import (
	"context"
	"log"

	"gocloud.dev/runtimevar"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/gcpruntimeconfig"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
	// The URL Host+Path are used as the GCP Runtime Configurator Variable key;
	// see https://cloud.google.com/deployment-manager/runtime-configurator/
	// for more details.

	v, err := runtimevar.OpenVariable(ctx, "gcpruntimeconfig://projects/myproject/configs/myconfigid/variables/myvar?decoder=string")
	if err != nil {
		log.Fatal(err)
	}
	defer v.Close()
}
Output:

Index

Examples

Constants

View Source
const Scheme = "gcpruntimeconfig"

Scheme is the URL scheme gcpruntimeconfig registers its URLOpener under on runtimevar.DefaultMux.

Variables

View Source
var Set = wire.NewSet(
	Dial,
	wire.Struct(new(URLOpener), "Client"),
)

Set holds Wire providers for this package.

Functions

func Dial

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 added in v0.13.0

func OpenVariable(client pb.RuntimeConfigManagerClient, variableKey string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

OpenVariable constructs a *runtimevar.Variable backed by variableKey in GCP Cloud Runtime Configurator.

A variableKey will look like:

projects/[project_id]/configs/[CONFIG_ID]/variables/[VARIABLE_NAME]

You can use the full string (e.g., copied from the GCP Console), or construct one from its parts using VariableKey.

See https://cloud.google.com/deployment-manager/runtime-configurator/ for more details.

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.

Example
package main

import (
	"context"
	"log"

	"gocloud.dev/gcp"
	"gocloud.dev/runtimevar"
	"gocloud.dev/runtimevar/gcpruntimeconfig"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// Your GCP credentials.
	// See https://cloud.google.com/docs/authentication/production
	// for more info on alternatives.
	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()

	// You can use the VariableKey helper to construct a Variable key from
	// your project ID, config ID, and the variable name; alternatively,
	// you can construct the full string yourself (e.g.,
	// "projects/gcp-project-id/configs/config-id/variables/variable-name").
	// See https://cloud.google.com/deployment-manager/runtime-configurator/
	// for more details.
	//
	// For this example, the GCP Cloud Runtime Configurator variable being
	// referenced should have a JSON string that decodes into MyConfig.
	variableKey := gcpruntimeconfig.VariableKey("gcp-project-id", "config-id", "variable-name")

	// Construct a *runtimevar.Variable that watches the variable.
	v, err := gcpruntimeconfig.OpenVariable(client, variableKey, runtimevar.StringDecoder, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer v.Close()
}
Output:

func VariableKey added in v0.16.0

func VariableKey(projectID gcp.ProjectID, configID, variableName string) string

VariableKey constructs a GCP Runtime Configurator variable key from component parts. See https://cloud.google.com/deployment-manager/runtime-configurator/ for more details.

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 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://projects/[project_id]/configs/[CONFIG_ID]/variables/[VARIABLE_NAME]".

The URL Host+Path are used as the GCP Runtime Configurator Variable key; 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.
  • wait: The poll interval, in time.ParseDuration formats. Defaults to 30s.

func (*URLOpener) OpenVariableURL

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens a gcpruntimeconfig Variable for u.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL