README

The Go Cloud Development Kit (Go CDK)

Write once, run on any cloud ☁️

Build Status godoc Coverage

The Go Cloud Development Kit (Go CDK) allows Go application developers to seamlessly deploy cloud applications on any combination of cloud providers. It does this by providing stable, idiomatic interfaces for common uses like storage and databases. Think database/sql for cloud products.

Imagine writing this to read from blob storage (like Google Cloud Storage or S3):

ctx := context.Background()
bucket, err := blob.OpenBucket(ctx, "s3://my-bucket")
if err != nil {
    return err
}
defer bucket.Close()
blobReader, err := bucket.NewReader(ctx, "my-blob", nil)
if err != nil {
    return err
}

and being able to run that code on any cloud you want, avoiding all the ceremony of cloud-specific authorization, tracing, SDKs and all the other code required to make an application portable across cloud platforms.

The project works well with a code generator called Wire. It creates human-readable code that only imports the cloud SDKs for providers you use. This allows the Go CDK to grow to support any number of cloud providers, without increasing compile times or binary sizes, and avoiding any side effects from init() functions.

You can learn more about the project from our announcement blog post, or our talk at Next 2018:

Installation

# First "cd" into your project directory if you have one to ensure "go get" uses
# Go modules (or not) appropriately. See "go help modules" for more info.
go get gocloud.dev

The Go CDK builds at the latest stable release of Go. Previous Go versions may compile but are not supported.

Documentation

Documentation for the project lives primarily on https://gocloud.dev/, including tutorials.

You can also browse Go package reference on godoc.org.

Project status

The APIs are still in alpha, but we think they are production-ready and are actively looking for feedback from early adopters. If you have comments or questions, you can post to the go-cloud mailing list or email us at go-cdk-feedback@google.com.

Current features

The Go CDK provides generic APIs for:

  • Unstructured binary (blob) storage
  • Publish/Subscribe (pubsub)
  • Variables that change at runtime (runtimevar)
  • Connecting to MySQL and PostgreSQL databases (mysql, postgres)
  • Server startup and diagnostics: request logging, tracing, and health checking (server)

Contributing

Thank you for your interest in contributing to the Go Cloud Development Kit! ❤️

Everyone is welcome to contribute, whether it's in the form of code, documentation, bug reports, feature requests, or anything else. We encourage you to experiment with the Go CDK and make contributions to help evolve it to meet your needs!

The GitHub repository at google/go-cloud contains some provider implementations for each portable API. We intend to include Google Cloud Platform, Amazon Web Services, and Azure implementations, as well as prominent open source providers and at least one implementation suitable for use in local testing. Unfortunately, we cannot support every cloud provider directly from the project; however, we encourage contributions in separate repositories.

If you create a repository that implements the Go CDK interfaces for other providers, let us know! We would be happy to link to it here and give you a heads-up before making any breaking changes.

See the contributing guide for more details.

Community

You can contact us on the go-cloud mailing list.

This project is covered by the Go Code of Conduct.

Expand ▾ Collapse ▴

Documentation

Overview

Package cloud contains a library and tools for open cloud development in Go.

The Go Cloud Development Kit (Go CDK) allows application developers to seamlessly deploy cloud applications on any combination of cloud providers. It does this by providing stable, idiomatic interfaces for common uses like storage and databases. Think `database/sql` for cloud products.

At the core of the Go CDK are common "portable types" implemented by cloud providers. For example, objects of the blob.Bucket portable type can be created using gcsblob.OpenBucket, s3blob.OpenBucket, or any other provider. Then, the blob.Bucket can be used throughout your application without worrying about the underlying implementation.

The Go CDK works well with a code generator called Wire (https://github.com/google/wire/blob/master/README.md). It creates human-readable code that only imports the cloud SDKs for providers you use. This allows the Go CDK to grow to support any number of cloud providers, without increasing compile times or binary sizes, and avoiding any side effects from `init()` functions.

For non-reference documentation, see https://gocloud.dev/

URLs

In addition to creating portable types via provider-specific constructors (e.g., creating a blob.Bucket using s3blob.OpenBucket), many portable types can also be created using a URL. The scheme of the URL specifies the provider, and each provider implementation has code to convert the URL into the data needed to call its constructor. For example, calling blob.OpenBucket with s3blob://my-bucket will return a blob.Bucket created using s3blob.OpenBucket.

Each portable API package will document the types that it supports opening by URL; for example, the blob package supports Buckets, while the pubsub package supports Topics and Subscriptions. Each provider implementation will document what scheme(s) it registers for, and what format of URL it expects.

Each portable type URL opener will accept URL schemes with an <api>+ prefix (e.g., blob+file:///dir" instead of "file:///dir", as well as schemes with an <api>+<type>+ prefix (e.g., blob+bucket+file:///dir).

Each portable API package should include an example using a URL, and many providers will include provider-specific examples as well.

URL Muxes

Each portable type that's openable via URL will have a top-level function you can call, like blob.OpenBucket. This top-level function uses a default instance of an URLMux multiplexer to map schemes to a provider-specific opener for the type. For example, blob has a BucketURLOpener interface that providers implement and then register using RegisterBucket.

Many applications will work just fine using the default mux through the top-level Open functions. However, if you want more control, you can create your own URLMux and register the provider URLOpeners you need. Most providers will export URLOpeners that give you more fine grained control over the arguments needed by the constructor. In particular, portable types opened via URL will often use default credentials from the environment. For example, the AWS URL openers use the credentials saved by "aws login" (we don't want to include credentials in the URL itself, since they are likely to be sensitive).

- Instantiate the provider's URLOpener with the specific fields you need.
  For example, s3blob.URLOpener{ConfigProvider: myAWSProvider} using a
  ConfigProvider that holds explicit AWS credentials.
- Create your own instance of the URLMux, e.g., mymux := new(blob.URLMux).
- Register your custom URLOpener on your mux, e.g.,
  mymux.RegisterBucket(s3blob.Scheme, myS3URLOpener).
- Now use your mux to open URLs, e.g. mymux.OpenBucket('s3://my-bucket').

Escaping the abstraction

It is not feasible or desirable for APIs like blob.Bucket to encompass the full functionality of every provider. Rather, we intend to provide a subset of the most commonly used functionality. There will be cases where a developer wants to access provider-specific functionality, such as unexposed APIs or data fields, errors or options. This can be accomplished using As functions.

As

As functions in the APIs provide the user a way to escape the Go CDK abstraction to access provider-specific types. They might be used as an interim solution until a feature request to the Go CDK is implemented. Or, the Go CDK may choose not to support specific features, and the use of As will be permanent.

Using As implies that the resulting code is no longer portable; the provider-specific code will need to be ported in order to switch providers. Therefore, it should be avoided if possible.

Each API will include examples demonstrating how to use its various As functions, and each provider implementation will document what types it supports for each.

Usage:

1. Declare a variable of the provider-specific type you want to access.

2. Pass a pointer to it to As.

3. If the type is supported, As will return true and copy the provider-specific type into your variable. Otherwise, it will return false.

Provider-specific types that are intended to be mutable will be exposed as a pointer to the underlying type.

Source Files

Directories

Path Synopsis
aws Package aws provides fundamental Wire providers for Amazon Web Services (AWS).
aws/awscloud Package awscloud contains Wire providers for AWS services.
aws/rds Package rds contains Wire providers that are common across RDS.
azure/azurecloud Package azurecloud contains Wire providers for Azure services.
azure/azuredb Package azuredb contains Wire providers that are common across Azure Database.
blob Package blob provides an easy and portable way to interact with blobs within a storage location, hereafter called a "bucket".
blob/azureblob Package azureblob provides a blob implementation that uses Azure Storage’s BlockBlob.
blob/driver Package driver defines a set of interfaces that the blob package uses to interact with the underlying blob services.
blob/drivertest Package drivertest provides a conformance test for implementations of driver.
blob/fileblob Package fileblob provides a blob implementation that uses the filesystem.
blob/gcsblob Package gcsblob provides a blob implementation that uses GCS.
blob/memblob Package memblob provides an in-memory blob implementation.
blob/s3blob Package s3blob provides a blob implementation that uses S3.
gcerrors Package gcerrors provides support for getting error codes from errors returned by Go CDK APIs.
gcp Package gcp provides fundamental Wire providers and types for Google Cloud Platform (GCP).
gcp/cloudsql Package cloudsql contains Wire providers that are common across Google Cloud SQL.
gcp/gcpcloud Package gcpcloud contains Wire providers for GCP services.
health Package health provides health check handlers.
health/sqlhealth Package sqlhealth provides a health check for a SQL database connection.
internal/batcher Package batcher supports batching of items.
internal/docstore Package docstore provides a portable implementation of a document store.
internal/docstore/driver Package driver defines a set of interfaces that the docstore package uses to interact with the underlying services.
internal/docstore/drivertest Package drivertest provides a conformance test for implementations of driver.
internal/docstore/dynamodocstore Package dynamodocstore provides a docstore implementation backed by AWS DynamoDB.
internal/docstore/firedocstore Package firedocstore provides an implementation of the docstore API for Google Cloud Firestore.
internal/docstore/internal/fields Package fields provides a view of the fields of a struct that follows the Go rules, amended to consider tags and case insensitivity.
internal/docstore/memdocstore Package memdocstore provides an in-memory implementation of the docstore API.
internal/docstore/mongodocstore Package mongodocstore provides an implementation of the docstore API for MongoDB.
internal/escape Package escape includes helpers for escaping and unescaping strings.
internal/gcerr Package gcerr provides an error type for Go CDK APIs.
internal/oc Package oc supports OpenCensus tracing and metrics for the Go Cloud Development Kit.
internal/openurl Package openurl provides helpers for URLMux and URLOpeners in portable APIs.
internal/retry Package retry provides retry logic.
internal/testing/octest Package octest supports testing of OpenCensus integrations.
internal/testing/replay Package replay provides the ability to record and replay HTTP requests.
internal/testing/setup
internal/testing/terraform Package terraform provides a function to read Terraform output.
internal/trace Package trace provides support for OpenCensus tracing.
internal/useragent Package useragent includes constants and utilitiesfor setting the User-Agent for Go CDK connections to GCP.
mysql Package mysql provides functions to open MySQL databases with OpenCensus instrumentation.
mysql/azuremysql Package azuremysql provides connections to Azure Database for MySQL.
mysql/cloudmysql Package cloudmysql provides connections to managed MySQL Cloud SQL instances.
mysql/rdsmysql Package rdsmysql provides connections to AWS RDS MySQL instances.
postgres Package postgres provides functions to open PostgreSQL databases with OpenCensus instrumentation.
postgres/cloudpostgres Package cloudpostgres provides connections to managed PostgreSQL Cloud SQL instances.
postgres/rdspostgres Package rdspostgres provides connections to AWS RDS PostgreSQL instances.
pubsub Package pubsub provides an easy and portable way to interact with publish/ subscribe systems.
pubsub/awssnssqs Package awssnssqs provides an implementation of pubsub that uses AWS SNS (Simple Notification Service) and SQS (Simple Queueing Service).
pubsub/azuresb Package azuresb provides an implementation of pubsub using Azure Service Bus Topic and Subscription.
pubsub/driver Package driver defines a set of interfaces that the pubsub package uses to interact with the underlying pubsub services.
pubsub/drivertest Package drivertest provides a conformance test for implementations of driver.
pubsub/gcppubsub Package gcppubsub provides a pubsub implementation that uses GCP PubSub.
pubsub/mempubsub Package mempubsub provides an in-memory pubsub implementation.
pubsub/natspubsub Package natspubsub provides a pubsub implementation for NATS.io.
pubsub/rabbitpubsub Package rabbitpubsub provides an pubsub implementation for RabbitMQ.
requestlog Package requestlog provides an http.Handler that logs information about requests.
runtimevar Package runtimevar provides an easy and portable way to watch runtime configuration variables.
runtimevar/awsparamstore Package awsparamstore provides a runtimevar implementation with variables read from AWS Systems Manager Parameter Store (https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html) Use OpenVariable to construct a *runtimevar.Variable.
runtimevar/blobvar Package blobvar provides a runtimevar implementation with variables read from a blob.Bucket.
runtimevar/constantvar Package constantvar provides a runtimevar implementation with Variables that never change.
runtimevar/driver Package driver provides the interface for providers of runtimevar.
runtimevar/drivertest Package drivertest provides a conformance test for implementations of runtimevar.
runtimevar/etcdvar Package etcdvar provides a runtimevar implementation with variables backed by etcd.
runtimevar/filevar Package filevar provides a runtimevar implementation with variables backed by the filesystem.
runtimevar/gcpruntimeconfig Package gcpruntimeconfig provides a runtimevar implementation with variables read from GCP Cloud Runtime Configurator (https://cloud.google.com/deployment-manager/runtime-configurator).
runtimevar/httpvar Package httpvar provides a runtimevar implementation with variables backed by http endpoint.
samples/gocdk-blob gocdk-blob demonstrates the use of the Go CDK blob package in a simple command-line application.
samples/gocdk-pubsub gocdk-pubsub demonstrates the use of the Go CDK pubsub package in a simple command-line application.
samples/gocdk-runtimevar gocdk-runtimevar demonstrates the use of the Go CDK runtimevar package in a simple command-line application.
samples/gocdk-secrets gocdk-secrets demonstrates the use of the Go CDK secrets package in a simple command-line application.
samples/guestbook guestbook is a sample application that records visitors' messages, displays a cloud banner, and an administrative message.
samples/guestbook/aws/provision_db The provision_db program connects to an RDS database and initializes it with SQL from stdin.
samples/guestbook/gcp/deploy The deploy program builds the Guestbook server locally and deploys it to GKE.
samples/guestbook/gcp/provision_db The provision_db program connects to a Cloud SQL database and initializes it with SQL from a file.
samples/guestbook/localdb
samples/server Command server runs a simple HTTP server with integrated Stackdriver tracing and health checks.
samples/tutorial Command upload saves files to blob storage on GCP, AWS, and Azure.
secrets Package secrets provides an easy and portable way to encrypt and decrypt messages.
secrets/awskms Package awskms provides a secrets implementation backed by AWS KMS.
secrets/azurekeyvault Package azurekeyvault provides a secrets implementation backed by Azure KeyVault.
secrets/driver Package driver defines interfaces to be implemented for providers of the secrets package.
secrets/drivertest Package drivertest provides a conformance test for implementations of the secrets driver.
secrets/gcpkms Package gcpkms provides a secrets implementation backed by Google Cloud KMS.
secrets/localsecrets Package localsecrets provides a secrets implementation using a locally locally provided symmetric key.
secrets/vault Package vault provides a secrets implementation using the Transit Secrets Engine of Vault by Hashicorp.
server Package server provides a preconfigured HTTP server with diagnostic hooks.
server/driver Package driver defines an interface for custom HTTP listeners.
server/sdserver Package sdserver provides the diagnostic hooks for a server using Stackdriver.
server/xrayserver Package xrayserver provides the diagnostic hooks for a server using AWS X-Ray.
tests/aws/app
tests/gcp/app The app command is a test app that is initialized with the GCP SDK.
tests/internal/testutil Package testutil contains utility functions used by server tests against different platforms.
MODULE docstore/mongodocstore
MODULE internal/cmd/gocdk
MODULE internal/contributebot
MODULE pubsub/kafkapubsub
MODULE pubsub/natspubsub
MODULE pubsub/rabbitpubsub
MODULE runtimevar/etcdvar
MODULE secrets/hashivault
MODULE secrets/vault