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

Enable minimal functionality of private worker pools with Cloud Build & Cloud Scheduler #211

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 20 additions & 7 deletions R/build_admin.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ cr_build_status <- function(id = .Last.value,
projectId = cr_project_get()) {
the_id <- extract_build_id(id)

url <- sprintf(
"https://cloudbuild.googleapis.com/v1/projects/%s/builds/%s",
projectId, the_id
)
if (has_private_worker_pool(id)){
url <- sprintf(
"https://cloudbuild.googleapis.com/v1/%s",
id[["metadata"]][["build"]][["name"]]
)
} else{
url <- sprintf(
"https://cloudbuild.googleapis.com/v1/projects/%s/builds/%s",
projectId, the_id
)
}

# cloudbuild.projects.builds.get
f <- gar_api_generator(url, "GET", data_parse_function = as.gar_Build)
Expand Down Expand Up @@ -273,9 +280,15 @@ parse_build_meta_to_obj <- function(o) {

as.gar_Build <- function(x) {
if (is.BuildOperationMetadata(x)) {
bb <- cr_build_status(extract_build_id(x),
projectId = x$metadata$build$projectId
)
# This may be excessively defensive. It's not clear to me why
# we can't just call cr_build_status(x) for both cases.
if (has_private_worker_pool(x)){
bb <- cr_build_status(x)
} else {
bb <- cr_build_status(extract_build_id(x),
projectId = x$metadata$build$projectId
)
}
o <- parse_build_meta_to_obj(bb)
} else if (is.gar_Build(x)) {
o <- x # maybe more here later...
Expand Down
30 changes: 25 additions & 5 deletions R/build_do.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ cr_build <- function(x,
artifacts = NULL,
options = NULL,
projectId = cr_project_get(),
region = cr_region_get(),
launch_browser = interactive()) {
assert_that(
is.flag(launch_browser),
Expand All @@ -76,10 +77,20 @@ cr_build <- function(x,

timeout <- check_timeout(timeout)

url <- sprintf(
"https://cloudbuild.googleapis.com/v1/projects/%s/builds",
projectId
)
# If options$pool$name exists, use different API endpoint that specifies
# the location; region has to match the region of the worker
if (has_private_worker_pool(x)){
url <- sprintf(
"https://cloudbuild.googleapis.com/v1/projects/%s/locations/%s/builds",
projectId,
region
)
} else {
url <- sprintf(
"https://cloudbuild.googleapis.com/v1/projects/%s/builds",
projectId
)
}

if (is.gar_Build(x)) {
# turn existing build into a valid new build
Expand Down Expand Up @@ -127,7 +138,16 @@ is.BuildOperationMetadata <- function(x) {
inherits(x, "BuildOperationMetadata")
}


has_private_worker_pool <- function(x){
has_pool_name <- FALSE
if (is.Yaml(x)){
has_pool_name <- length(x[["options"]][["pool"]][["name"]]) > 0
}
if (is.BuildOperationMetadata(x)){
has_pool_name <- length(x[["metadata"]][["build"]][["options"]][["pool"]][["name"]]) > 0
}
has_pool_name
}

extract_logs <- function(o) {
if (is.BuildOperationMetadata(o)) {
Expand Down
14 changes: 10 additions & 4 deletions R/cloudbuild_schedule.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@ cr_schedule_http <- function(build,
build <- as.gar_Build(build)
build <- safe_set(build, "status", "QUEUED")

HttpTarget(
httpMethod = "POST",
uri = sprintf(
if (has_private_worker_pool(build)){
uri <- paste0("https://cloudbuild.googleapis.com/v1/",build[["options"]][["pool"]][["name"]])
} else {
uri <- sprintf(
"https://cloudbuild.googleapis.com/v1/projects/%s/builds",
projectId
),
)
}

HttpTarget(
httpMethod = "POST",
uri = uri,
body = build,
oauthToken = list(serviceAccountEmail = email)
)
Expand Down