Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance Cloud Run deploy command with advanced configuration options #40

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: Add no-wait option for Cloud Run service deployment
- Introduce `--no-wait` flag to skip waiting for service readiness
- Enhance deployment flexibility by allowing immediate return after service creation
- Improve WaitForServiceReadyV2 with more detailed logging and robust readiness checks
- Add interval and timeout configuration for service readiness polling
  • Loading branch information
dijarvrella committed Feb 27, 2025
commit b79b7c29f7edcb42626624f1d6b2bc0fb2e6f9c4
14 changes: 13 additions & 1 deletion cmd/cloud-run/deploy.go
Original file line number Diff line number Diff line change
@@ -150,6 +150,9 @@ Examples:
deployCmd.MarkFlagsMutuallyExclusive("set-secrets", "update-secrets", "clear-secrets")
}

// Add no-wait flag
deployCmd.Flags().Bool("no-wait", false, "Skip waiting for service to be ready")

_ = deployCmd.MarkFlagRequired("service")
deployCmd.MarkFlagsOneRequired(
"image",
@@ -204,7 +207,16 @@ func deployService(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
return deploy.WaitForServiceReadyV2(ctx, servicesClient, projectID, region, opts.Service)

// Check for no-wait flag
noWait, _ := cmd.Flags().GetBool("no-wait")
if !noWait {
// Wait for service to be ready
if err := deploy.WaitForServiceReadyV2(ctx, servicesClient, projectID, region, opts.Service); err != nil {
return err
}
}
return nil
}

// isValidServiceName validates that a service name meets Cloud Run requirements:
47 changes: 20 additions & 27 deletions cmd/cloud-run/pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
@@ -798,54 +798,47 @@ func SetIAMPolicyV2(servicesClient *run.ProjectsLocationsServicesService, servic
func WaitForServiceReadyV2(ctx context.Context, servicesClient *run.ProjectsLocationsServicesService, projectID, region, service string) error {
name := fmt.Sprintf("projects/%s/locations/%s/services/%s", projectID, region, service)

if err := utils.PollWithInterval(ctx, time.Minute*2, time.Second, func() (bool, error) {
log.Println("Waiting for service to be ready (timeout: 2 minutes)...")

if err := utils.PollWithInterval(ctx, time.Minute*2, time.Second*5, func() (bool, error) {
getCall := servicesClient.Get(name)
runService, err := getCall.Do()

if err != nil {
return false, err
}

// Log all conditions for debugging
log.Println("Current service conditions:")
for _, cond := range runService.Conditions {
log.Printf("- Type: %s, State: %s, Message: %s", cond.Type, cond.State, cond.Message)

if cond.Type == "Ready" {
log.Println(cond.Message)
if cond.State == "CONDITION_SUCCEEDED" {
// Check for multiple success states
if cond.State == "CONDITION_SUCCEEDED" ||
strings.Contains(strings.ToLower(cond.Message), "ready") {
return true, nil
}

// Only fail for explicit failure
if cond.State == "CONDITION_FAILED" {
return false, fmt.Errorf("failed to deploy the latest revision of the service %s: %s", service, cond.Message)
return false, fmt.Errorf("failed to deploy: %s", cond.Message)
}
}
}

// Also check other indicators of readiness
if runService.LatestReadyRevision != "" && runService.ObservedGeneration > 0 {
log.Println("Service has a latest ready revision and observed generation > 0")
return true, nil
}

return false, nil
}); err != nil {
return err
}

getCall := servicesClient.Get(name)
runService, err := getCall.Do()

if err != nil {
return err
}

// Get traffic percentage
var trafficPercent int64

if len(runService.Traffic) > 0 {
trafficPercent = runService.Traffic[0].Percent
}

log.Printf("Service [%s] is deployed successfully, serving %d percent of traffic.\n",
service, trafficPercent)

if runService.Uri != "" {
log.Printf("Service URL: %s \n", runService.Uri)
} else {
log.Println("Service has no URL (default URL is disabled)")
}

log.Println("Service is ready!")
return nil
}