-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(k8s): enable publishing container modules when using remote builders
This has the caveat that you need to have a local Docker daemon running. Avoiding that requirement would take much more work, since we'd need to tackle all manner of authentication/key management issues.
- Loading branch information
Showing
14 changed files
with
165 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { ContainerModule } from "./config" | ||
import { PublishModuleParams } from "../../types/plugin/module/publishModule" | ||
import { containerHelpers } from "./helpers" | ||
|
||
export async function publishContainerModule({ module, log }: PublishModuleParams<ContainerModule>) { | ||
if (!(await containerHelpers.hasDockerfile(module))) { | ||
log.setState({ msg: `Nothing to publish` }) | ||
return { published: false } | ||
} | ||
|
||
const localId = await containerHelpers.getLocalImageId(module) | ||
const remoteId = await containerHelpers.getPublicImageId(module) | ||
|
||
log.setState({ msg: `Publishing image ${remoteId}...` }) | ||
|
||
if (localId !== remoteId) { | ||
await containerHelpers.dockerCli(module, ["tag", localId, remoteId]) | ||
} | ||
|
||
// TODO: stream output to log if at debug log level | ||
await containerHelpers.dockerCli(module, ["push", remoteId]) | ||
|
||
return { published: true, message: `Published ${remoteId}` } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
garden-service/src/plugins/kubernetes/container/publish.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { ContainerModule } from "../../container/config" | ||
import { PublishModuleParams } from "../../../types/plugin/module/publishModule" | ||
import { containerHelpers } from "../../container/helpers" | ||
import { KubernetesPluginContext } from "../config" | ||
import { publishContainerModule } from "../../container/publish" | ||
import { getRegistryPortForward } from "./util" | ||
import execa = require("execa") | ||
|
||
export async function k8sPublishContainerModule(params: PublishModuleParams<ContainerModule>) { | ||
const { ctx, module, log } = params | ||
const k8sCtx = ctx as KubernetesPluginContext | ||
const provider = k8sCtx.provider | ||
|
||
if (!(await containerHelpers.hasDockerfile(module))) { | ||
log.setState({ msg: `Nothing to publish` }) | ||
return { published: false } | ||
} | ||
|
||
if (provider.config.buildMode !== "local-docker") { | ||
// First pull from the in-cluster registry, then resume standard publish flow. | ||
// This does mean we require a local docker as a go-between, but the upside is that we can rely on the user's | ||
// standard authentication setup, instead of having to re-implement or account for all the different ways the | ||
// user might be authenticating with their registries. | ||
log.setState(`Pulling from cluster container registry...`) | ||
|
||
const fwd = await getRegistryPortForward(k8sCtx, log) | ||
|
||
const imageId = await containerHelpers.getDeploymentImageId(module, ctx.provider.config.deploymentRegistry) | ||
const pullImageName = containerHelpers.unparseImageId({ | ||
...containerHelpers.parseImageId(imageId), | ||
// Note: using localhost directly here has issues with Docker for Mac. | ||
// https://github.com/docker/for-mac/issues/3611 | ||
host: `local.app.garden:${fwd.localPort}`, | ||
}) | ||
|
||
await execa("docker", ["pull", pullImageName]) | ||
} | ||
|
||
return publishContainerModule(params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { resolve } from "url" | ||
import { ContainerModule } from "../../container/config" | ||
import { getPortForward } from "../util" | ||
import { systemNamespace } from "../system" | ||
import { CLUSTER_REGISTRY_DEPLOYMENT_NAME, CLUSTER_REGISTRY_PORT } from "../constants" | ||
import { containerHelpers } from "../../container/helpers" | ||
import { PluginError } from "../../../exceptions" | ||
import { PluginContext } from "../../../plugin-context" | ||
import { LogEntry } from "../../../logger/log-entry" | ||
import { KubernetesPluginContext } from "../config" | ||
import axios from "axios" | ||
|
||
export async function getRegistryPortForward(ctx: PluginContext, log: LogEntry) { | ||
return getPortForward({ | ||
ctx, | ||
log, | ||
namespace: systemNamespace, | ||
targetDeployment: `Deployment/${CLUSTER_REGISTRY_DEPLOYMENT_NAME}`, | ||
port: CLUSTER_REGISTRY_PORT, | ||
}) | ||
} | ||
|
||
export async function getManifestFromRegistry( | ||
ctx: KubernetesPluginContext, module: ContainerModule, log: LogEntry, | ||
) { | ||
const url = await getImageRegistryUrl(ctx, module, log, `manifests/${module.version.versionString}`) | ||
|
||
try { | ||
const res = await axios({ url }) | ||
log.silly(res.data) | ||
return res.data | ||
} catch (err) { | ||
if (err.response && err.response.status === 404) { | ||
return null | ||
} else { | ||
throw new PluginError(`Could not query in-cluster registry: ${err}`, { | ||
message: err.message, | ||
response: err.response, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
async function getImageRegistryUrl(ctx: KubernetesPluginContext, module: ContainerModule, log: LogEntry, path: string) { | ||
const registryFwd = await getRegistryPortForward(ctx, log) | ||
const imageId = await containerHelpers.getDeploymentImageId(module, ctx.provider.config.deploymentRegistry) | ||
const imageName = containerHelpers.unparseImageId({ | ||
...containerHelpers.parseImageId(imageId), | ||
host: undefined, | ||
tag: undefined, | ||
}) | ||
|
||
const baseUrl = `http://localhost:${registryFwd.localPort}/v2/${imageName}/` | ||
|
||
return resolve(baseUrl, path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters