Skip to content

Commit

Permalink
feat(gcp-cloud-run): Added support for volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed May 4, 2024
1 parent 8c9a181 commit 14c7190
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
23 changes: 17 additions & 6 deletions packages/gcp-cloud-run/src/executors/deploy/deploy.impl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExecutorContext, readJsonFile } from '@nx/devkit'
import { ExecutorContext, logger, readJsonFile } from '@nx/devkit'
import { buildCommand, execCommand, getOutputDirectoryFromBuildTarget } from '@nx-extend/core'
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
Expand All @@ -23,7 +23,6 @@ export interface ExecutorSchema extends ContainerFlags {
tagWithVersion?: string
revisionSuffix?: string
buildWith?: 'artifact-registry'
autoCreateArtifactsRepo?: boolean
noTraffic?: boolean
timeout?: number
cpuBoost?: boolean
Expand All @@ -32,6 +31,10 @@ export interface ExecutorSchema extends ContainerFlags {
vpcConnector?: string
vpcEgress?: 'all-traffic' | 'private-ranges-only'

// VOLUME_NAME,type=cloud-storage,bucket=BUCKET_NAME
// VOLUME_NAME,type=in-memory,size=SIZE_LIMIT
volumeName?: string

sidecars?: ContainerFlags[]
}

Expand Down Expand Up @@ -66,11 +69,12 @@ export async function deployExecutor(
vpcEgress,

revisionSuffix = false,
autoCreateArtifactsRepo = true,
timeout,

cpuBoost,
ingress,
// VOLUME_NAME,type=VOLUME_TYPE,size=SIZE_LIMIT'
volumeName,

sidecars = []
} = options
Expand Down Expand Up @@ -102,8 +106,15 @@ export async function deployExecutor(
}
}

let gcloudDeploy = 'gcloud run deploy'
if (options.volumeName) {
logger.warn('Volumes are still in beta, using "gcloud beta" to deploy.\n')

gcloudDeploy = 'gcloud beta run deploy'
}

const deployCommand = buildCommand([
`gcloud run deploy ${name}`,
`${gcloudDeploy} ${name}`,
`--project=${project}`,
'--platform=managed',
`--region=${region}`,
Expand All @@ -123,15 +134,15 @@ export async function deployExecutor(
typeof cpuBoost === 'boolean' && !cpuBoost && '--no-cpu-boost',
noTraffic && '--no-traffic',
allowUnauthenticated && '--allow-unauthenticated',
volumeName && `--add-volume=name=${volumeName}`,

// Add the primary container
...getContainerFlags(options, sidecars.length > 0),

// Add all sidecars
...sidecars.flatMap((sidecarOptions) => getContainerFlags(sidecarOptions, true)),

// There can be a question if a repo should be created
autoCreateArtifactsRepo && '--quiet'
'--quiet'
])

return execCommand(deployCommand, {
Expand Down
8 changes: 8 additions & 0 deletions packages/gcp-cloud-run/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
"internal",
"internal-and-cloud-load-balancing"
]
},
"volumeName": {
"type": "string",
"description": "Should be in format 'VOLUME_NAME,type=VOLUME_TYPE,<specific type options>'"
},
"volumeMount": {
"type": "string",
"description": "Should be in format 'VOLUME_NAME,mount-path=MOUNT_PATH'"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ export interface ContainerFlags {
// removeLabels?: string[] // List of label keys to remove.
labels?: string[] // List of label KEY=VALUE pairs to add.
// updateLabels?: Record<string, string> // List of label KEY=VALUE pairs to update.
volumeMount?: string // VOLUME_NAME,mount-path=MOUNT_PATH
}

export function getContainerFlags(options: ContainerFlags, containerRequired: boolean): string[] {
if (containerRequired && !options.container) {
throw new Error('Option "container" is not set!')
throw new Error('Option "container" is not set! This is required when using sidecars.')
}

const setEnvVars = Object.keys(options.envVars).reduce((env, envVar) => {
const setEnvVars = Object.keys(options.envVars || {}).reduce((env, envVar) => {
env.push(`${envVar}=${options.envVars[envVar]}`)

return env
Expand Down Expand Up @@ -76,7 +77,8 @@ export function getContainerFlags(options: ContainerFlags, containerRequired: bo
// options.clearSecrets && `--clear-secrets=${options.clearSecrets}`,
// options.clearLabels && `--clear-labels=${options.clearLabels}`,
// options.removeLabels && `--remove-labels=${options.removeLabels}`,
options.labels && `--clear-labels --labels=${options.labels.join(',')}`
// options.updateLabels && `--update-labels=${options.updateLabels}`
options.labels && `--clear-labels --labels=${options.labels.join(',')}`,
// options.updateLabels && `--update-labels=${options.updateLabels}`,
options.volumeMount && `--add-volume-mount=volume=${options.volumeMount}`,
]
}

0 comments on commit 14c7190

Please sign in to comment.