Go Cloud Development Kit (Go CDK)

repository·master·Indexed 27 days ago

https://github.com/google/go-cloud

A toolkit that allows Go developers to write cloud-agnostic applications by providing stable, idiomatic interfaces for common services. Supported generic APIs include unstructured binary (blob) storage, publish/subscribe (pubsub), runtime variables (runtimevar), databases (MySQL, PostgreSQL), and server diagnostics. It uses portable types implemented on top of service-specific drivers for providers like GCP, AWS, and Azure.

Tokens
24.9K
Snippets
43
Records
191
Agent score
94%

What's inside Go CDK

  1. Overview of Go CDK Portable Cloud APIs

    master

    The Go Cloud Development Kit (Go CDK) provides vendor-neutral, idiomatic Go APIs for common cloud use cases such as storage, events, and databases.

    By using these portable APIs, you can:

    1. Write application code once.
    2. Test locally using on-prem/local implementations.
    3. Deploy to major cloud providers (AWS, GCP, Azure) with minimal setup-time changes.
  2. Overview of Go CDK portable APIs

    master

    The Go Cloud Development Kit (Go CDK) provides stable, idiomatic interfaces for common cloud services, allowing you to write code that is portable across different cloud providers (like GCP, AWS, and Azure).

    Supported generic APIs include:

    • Unstructured binary (blob) storage: For reading/writing blobs (e.g., S3, GCS).
    • Publish/Subscribe (pubsub): For messaging patterns.
    • Variables (runtimevar): For values that change at runtime.
    • Databases (mysql, postgres): For connecting to MySQL (including MariaDB) and PostgreSQL.
    • Server (server): For server startup and diagnostics, including request logging, tracing, and health checking.
  3. Minimize Global State usage

    master

    The Go CDK avoids introducing global state to ensure applications can reason about different states in large codebases. Responsibility for state is pushed to the application via dependency injection.

    Exception: A global registry is permitted specifically for URL scheme registration. This is allowed to reduce the boilerplate required for URL muxes when using multiple drivers without dependency injection tools like Wire.

  4. Understand Portable Type Constructors

    master

    In Go CDK, portable type constructors are top-level functions in driver packages used to obtain an instance of a portable type.

    Key characteristics:

    • They return the portable type directly (e.g., gcsblob.OpenBucket returns *blob.Bucket).
    • They avoid using helper structs or wrappers to ensure ease of use.
    • Argument ordering: Arguments less likely to change (like connection or authorization details) are placed before arguments likely to change (like resource names). Example: OpenBucket(ctx, client, "mybucket").
  5. Understand the Portable Type and Driver pattern

    master

    The Go CDK uses a pattern similar to database/sql to separate implementation-agnostic logic from service-specific implementations.

    • Portable Types: These are concrete types (e.g., blob.Bucket or runtimevar.Variable) that you use in your application logic. They contain high-level, implementation-agnostic logic and provide a consistent API regardless of the underlying service.
    • Drivers: These are the interfaces implemented by specific providers (e.g., AWS S3, Google Cloud Storage). The portable type wraps the driver to provide advanced functionality (like content-type detection in blob.Bucket.NewWriter) without requiring the driver to implement that complex logic itself.

    This structure allows you to write your core logic once and swap providers by simply changing the driver used at startup.

  6. Explore the Guestbook Sample application

    master

    The Guestbook sample application demonstrates how to write cloud-agnostic business logic using the Go CDK. It records visitor messages, displays a cloud banner, and shows an administrative message.

    Key Go CDK APIs used in this sample:

    • MySQL driver: For data persistence.
    • Generic blob API: For handling unstructured data.
    • Generic runtimevar API: For managing dynamic configuration/runtime variables.

    Platform-specific code and dependency injection are managed using Wire.

  7. Understand Go CDK pubsub design trade-offs

    master

    The Go CDK pubsub package is designed to provide a consistent interface across different messaging services (like GCP PubSub, Azure Service Bus, RabbitMQ, and Redis Streams) while avoiding the complexity of forcing developers to manage manual batching or callback-based subscription models.

    Key Design Decisions

    • Avoids Batch-only APIs: Unlike some designs that force the application to send/receive slices of messages, Go CDK allows for single-message operations while handling batching efficiency internally or via driver implementations.
    • Avoids Callback-based Subscriptions: Unlike go-micro, Go CDK does not use a callback function within the subscription to handle messages. This ensures compatibility with the inverted worker pool pattern.
    • Explicit Acknowledgement: The design supports message acknowledgement (ack), which is critical for reliable systems. For systems that do not natively support acks, the behavior is currently an open design question (potential options include simulating queues or making Ack a no-op).
    • No Auto-provisioning: In line with Go CDK principles, the library does not automatically create topics or subscriptions; these must be managed by the user or the underlying infrastructure.
  8. Understand the Developer and Operator persona separation

    master

    The Go CDK is designed to separate concerns between two roles:

    • Developer: Focuses on writing business logic that is agnostic of the underlying cloud provider. Developers should use the provided portable types (interfaces/concrete types) to ensure code portability.
    • Operator: Focuses on provisioning resources and managing platform-specific configurations (like IAM roles, ACLs, or resource creation).

    Key Principle: The Go CDK avoids including platform-specific resource management (like creating a new blob.Bucket) to prevent leaky abstractions. Operators are expected to manage these non-portable resources externally and then provide the implementation to the application.

  9. Deploy Guestbook to Azure

    master

    Since Go CDK does not currently support SQL on Azure for this sample, you must run a local MySQL database while using Azure Storage for the blob and runtimevar APIs.

    1. Authenticate: Log in via az login.
    2. Provision: Use Terraform in the azure directory to create storage resources.
    3. Run locally with Azure storage: Start the local MySQL database, then run the guestbook binary locally, providing Azure credentials via environment variables (AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_KEY) and the storage container name via the -bucket flag.
    4. Cleanup: Run terraform destroy in the azure directory.
    # Provisioning
    az login
    cd azure
    terraform init
    terraform apply -var location="West US"
    
    # Running with Azure Storage
    # (In a separate terminal, start local MySQL)
    go run localdb/main.go
    
    # In the azure directory
    export AZURE_STORAGE_ACCOUNT=<your storage_account>
    export AZURE_STORAGE_KEY=<your access_key>
    ./guestbook -env=azure -bucket=<your storage_container> -motd_var=motd
  10. Configure custom URL multiplexers (URLMux)

    master

    While top-level functions like blob.OpenBucket use a DefaultURLMux to map schemes to providers, you can create a custom URLMux for more control. This is useful when you need to provide explicit configuration or credentials (e.g., via a ConfigProvider) rather than relying on default environment credentials.

    To use a custom mux, follow these steps:

    1. Instantiate the provider's URLOpener with specific fields (e.g., s3blob.URLOpener{ConfigProvider: myAWSProvider}).
    2. Create a new instance of the URLMux (e.g., mymux := new(blob.URLMux)).
    3. Register your custom URLOpener on the mux using the provider's scheme (e.g., mymux.RegisterBucket(s3blob.Scheme, myS3URLOpener)).
    4. Use the mux to open URLs (e.g., mymux.OpenBucket("s3://my-bucket")).
  11. Run tests in Record mode to capture network interactions

    master

    Tests in Go CDK can be run in -record mode to perform live integration tests against backend servers and record the requests/responses for later use in replay mode.

    To use -record mode:

    1. Provision resources: Manually provision the required resources (e.g., an AWS S3 bucket for blob tests) or use the provided test-specific flags to pass resource information.
    2. Run the test: Execute the test using the -record flag. When adding or changing tests, use the -run flag with go test to record only the specific tests that were affected to minimize noise.
    3. Commit replay files: The test will save network interactions. You must commit these new replay files along with your code changes.