runtimeconfigurator

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package runtimeconfigurator provides a runtimevar implementation with variables read from GCP Cloud Runtime Configurator (https://cloud.google.com/deployment-manager/runtime-configurator). Use NewVariable to construct a *runtimevar.Variable.

As

runtimeconfigurator exposes the following types for As:

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

import (
	"context"
	"log"

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

// 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 := runtimeconfigurator.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 := runtimeconfigurator.ResourceName{
		ProjectID: "gcp-project-id",
		Config:    "cfg-name",
		Variable:  "cfg-variable-name",
	}

	// Construct a *runtimevar.Variable that watches the variable.
	v, err := runtimeconfigurator.NewVariable(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.Watch(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	cfg := snapshot.Value.(MyConfig)
	_ = cfg
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

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

func NewVariable(client pb.RuntimeConfigManagerClient, name ResourceName, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

NewVariable 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

type ResourceName struct {
	ProjectID string
	Config    string
	Variable  string
}

ResourceName identifies a configuration variable.

func (ResourceName) String

func (r ResourceName) String() string

String returns the full configuration variable path.

Jump to

Keyboard shortcuts

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