cloudrun.go

Overview

The cloudrun.go file implements the command-line interface and deployment logic for deploying an application to Google Cloud Run. It automates the process of compiling a Go server executable, preparing a Dockerfile, deploying the containerized application to Cloud Run, and running a local proxy that adds authentication headers for secure access. This file uses the cobra package for CLI commands and integrates tightly with Google Cloud SDK commands (gcloud) for deployment and proxy management.

The deployment workflow includes:

Types and Structures

gCloudFlags

Holds Google Cloud Platform specific configuration flags.

Field

Type

Description

region

string

GCP region for deployment.

projectName

string

GCP project name.

cloudRunServiceFlags

Contains flags specifying the Cloud Run service configuration.

Field

Type

Description

serviceName

string

Name of the Cloud Run service to deploy.

serverPort

int

Port on which the server listens inside Cloud Run.

a2aAgentCardURL

string

URL for Agent-to-Agent (A2A) agent card.

a2a

bool

Enable A2A communication (true/false).

api

bool

Enable API functionality (true/false).

webui

bool

Enable Web UI functionality (true/false).

localProxyFlags

Specifies configuration for the local proxy that adds authentication headers.

Field

Type

Description

port

int

Port on which the local proxy listens.

buildFlags

Contains build-related flags and paths.

Field

Type

Description

tempDir

string

Temporary directory for the build process. Defaults to OS temp dir if empty.

execPath

string

Full path to the compiled executable output.

execFile

string

Executable filename derived from entry point.

dockerfileBuildPath

string

Path to the temporary Dockerfile created for deployment.

sourceFlags

Holds source code location flags.

Field

Type

Description

srcBasePath

string

Directory of the entry point source file.

entryPointPath

string

Entry point file path for the Go server.

deployCloudRunFlags

Aggregates all the above flag groups for complete deployment configuration.

Field

Type

Description

gcloud

gCloudFlags

Google Cloud deployment flags.

cloudRun

cloudRunServiceFlags

Cloud Run service configuration.

proxy

localProxyFlags

Local proxy configuration.

build

buildFlags

Build process configuration.

source

sourceFlags

Source code location flags.


Variables


Functions and Methods

init()

Registers the cloudrun subcommand under the parent deploy.DeployCmd and binds all persistent flags to the flags structure.


(f *deployCloudRunFlags) computeFlags() error

Processes and validates CLI flag values to prepare for deployment.

Usage example:

err := flags.computeFlags()
if err != nil {
    // handle error
}

(f *deployCloudRunFlags) cleanTemp() error

Removes the temporary directory created during build and deployment.


(f *deployCloudRunFlags) compileEntryPoint() error

Compiles the Go entry point into a statically linked Linux executable targeted for Cloud Run.


(f *deployCloudRunFlags) prepareDockerfile() error

Generates a Dockerfile inside the temporary directory for building the container image.


(f *deployCloudRunFlags) gcloudDeployToCloudRun() error

Executes gcloud run deploy command to deploy the containerized application to Cloud Run.


(f *deployCloudRunFlags) runGcloudProxy() error

Runs a local gcloud run services proxy command.


(f *deployCloudRunFlags) deployOnCloudRun() error

Orchestrates the entire deployment sequence:

  1. Computes flags and prepares environment.

  2. Compiles the Go server executable.

  3. Prepares the Dockerfile.

  4. Deploys the container to Cloud Run using gcloud.

  5. Cleans up temporary build files.

  6. Launches the local proxy for authenticated access.

Each step logs its status and returns errors on failure.


Interactions with Other System Components


Implementation Details and Algorithms


Usage Example

Deploy an application to Cloud Run with all features enabled and custom parameters:

cloudrun \
  --project_name=my-gcp-project \
  --region=us-central1 \
  --service_name=my-cloudrun-service \
  --entry_point_path=./cmd/server/main.go \
  --proxy_port=8081 \
  --server_port=8080 \
  --a2a=true \
  --api=true \
  --webui=true

Visual Diagram

flowchart TD
A[cloudrunCmd] --> B[deployOnCloudRun]
B --> C[computeFlags]
B --> D[compileEntryPoint]
B --> E[prepareDockerfile]
B --> F[gcloudDeployToCloudRun]
B --> G[cleanTemp]
B --> H[runGcloudProxy]
style A fill:#f9f,stroke:#333,stroke-width:1px
style B fill:#bbf,stroke:#333,stroke-width:1px
classDef success fill:#bbf,stroke:#333,stroke-width:1px

This diagram illustrates the main workflow triggered by the cloudrun CLI command, showing the sequential execution of key methods responsible for deployment and running the local proxy.