Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract helper function to reduce code duplication
Browse files Browse the repository at this point in the history
The only diff between `getContainerBuildActionOutputs` and `k8sGetContainerBuildActionOutputs`
was in the deploymentImageId calculation that depended
on the presence of deployment registry config.
vvagaytsev committed May 17, 2024
1 parent ec39ab2 commit 8b00ed8
Showing 3 changed files with 58 additions and 59 deletions.
36 changes: 4 additions & 32 deletions core/src/plugins/container/build.ts
Original file line number Diff line number Diff line change
@@ -209,38 +209,10 @@ async function buildContainerInCloudBuilder(params: {
}

export function getContainerBuildActionOutputs(action: Resolved<ContainerBuildAction>): ContainerBuildOutputs {
const localId = action.getSpec("localId")
const publishImageId = action.getSpec("publishId")
let imageId = localId
if (publishImageId) {
// override imageId if publishId is set
const parsedPublishImage = containerHelpers.parseImageId(publishImageId)
// use internal version tag if publishId doesn't have its own
const tag = parsedPublishImage.tag || action.versionString()
imageId = containerHelpers.unparseImageId({ ...parsedPublishImage, tag })
}

const version = action.moduleVersion()
const buildName = action.name

const localImageName = containerHelpers.getLocalImageName(buildName, localId)
const localImageId = containerHelpers.getLocalImageId(buildName, localId, version)

// Note: The deployment image name/ID outputs are overridden by the kubernetes provider, these defaults are
// generally not used.
const deploymentImageName = containerHelpers.getDeploymentImageName(buildName, imageId, undefined)
const deploymentImageId = containerHelpers.getBuildDeploymentImageId(buildName, imageId, version, undefined)

return {
localImageName,
localImageId,
deploymentImageName,
deploymentImageId,
"local-image-name": localImageName,
"local-image-id": localImageId,
"deployment-image-name": deploymentImageName,
"deployment-image-id": deploymentImageId,
}
return containerHelpers.getBuildActionOutputs({
action,
deploymentRegistryConfig: undefined,
})
}

export function getDockerBuildFlags(
51 changes: 50 additions & 1 deletion core/src/plugins/container/helpers.ts
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import isGlob from "is-glob"
import { ConfigurationError, GardenError, RuntimeError } from "../../exceptions.js"
import type { SpawnOutput } from "../../util/util.js"
import { spawn } from "../../util/util.js"
import type { ContainerModuleConfig, ContainerRegistryConfig } from "./moduleConfig.js"
import type { ContainerBuildOutputs, ContainerModuleConfig, ContainerRegistryConfig } from "./moduleConfig.js"
import { defaultImageNamespace, defaultTag as _defaultTag } from "./moduleConfig.js"
import type { Writable } from "stream"
import { flatten, fromPairs, reduce, uniq } from "lodash-es"
@@ -182,6 +182,55 @@ const helpers = {
}
},

/**
* Serves build action outputs in container and kubernetes plugins.
*/
getBuildActionOutputs({
action,
deploymentRegistryConfig,
}: {
action: Resolved<ContainerBuildAction>
deploymentRegistryConfig: ContainerRegistryConfig | undefined
}): ContainerBuildOutputs {
const localId = action.getSpec("localId")
const publishImageId = action.getSpec("publishId")
let imageId = localId
if (publishImageId) {
// override imageId if publishId is set
const parsedPublishImage = containerHelpers.parseImageId(publishImageId)
// use internal version tag if publishId doesn't have its own
const tag = parsedPublishImage.tag || action.versionString()
imageId = containerHelpers.unparseImageId({ ...parsedPublishImage, tag })
}

const version = action.moduleVersion()
const buildName = action.name

const localImageName = containerHelpers.getLocalImageName(buildName, localId)
const localImageId = containerHelpers.getLocalImageId(buildName, localId, version)

// Note: The deployment image name/ID outputs are overridden by the kubernetes provider, these defaults are
// generally not used.
const deploymentImageName = containerHelpers.getDeploymentImageName(buildName, imageId, deploymentRegistryConfig)
const deploymentImageId = containerHelpers.getBuildDeploymentImageId(
buildName,
imageId,
version,
deploymentRegistryConfig
)

return {
localImageName,
localImageId,
deploymentImageName,
deploymentImageId,
"local-image-name": localImageName,
"local-image-id": localImageId,
"deployment-image-name": deploymentImageName,
"deployment-image-id": deploymentImageId,
}
},

parseImageId(imageId: string, defaultTag = _defaultTag): ParsedImageId {
let [name, tag] = splitLast(imageId, ":")

30 changes: 4 additions & 26 deletions core/src/plugins/kubernetes/container/handlers.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ import type {
import type { GetModuleOutputsParams } from "../../../plugin/handlers/Module/get-outputs.js"
import { containerHelpers } from "../../container/helpers.js"
import { getContainerModuleOutputs } from "../../container/container.js"
import { getContainerBuildActionOutputs } from "../../container/build.js"
import type { Resolved } from "../../../actions/types.js"

async function configure(params: ConfigureModuleParams<ContainerModule>) {
@@ -60,31 +59,10 @@ export function k8sGetContainerBuildActionOutputs({
provider: KubernetesProvider
action: Resolved<ContainerBuildAction>
}): ContainerBuildOutputs {
const localImageId = action.getSpec("localId")
const publishImageId = action.getSpec("publishId")
let imageId = localImageId
if (publishImageId) {
// override imageId if publishId is set
const parsedPublishImage = containerHelpers.parseImageId(publishImageId)
// use internal version tag if publishId doesn't have its own
const tag = parsedPublishImage.tag || action.versionString()
imageId = containerHelpers.unparseImageId({ ...parsedPublishImage, tag })
}

const outputs = getContainerBuildActionOutputs(action)
outputs.deploymentImageName = outputs["deployment-image-name"] = containerHelpers.getDeploymentImageName(
action.name,
imageId,
provider.config.deploymentRegistry
)
outputs.deploymentImageId = outputs["deployment-image-id"] = containerHelpers.getBuildDeploymentImageId(
action.name,
imageId,
action.moduleVersion(),
provider.config.deploymentRegistry
)

return outputs
return containerHelpers.getBuildActionOutputs({
action,
deploymentRegistryConfig: provider.config.deploymentRegistry,
})
}

function validateConfig<T extends ContainerModule>(params: ConfigureModuleParams<T>) {

0 comments on commit 8b00ed8

Please sign in to comment.