Skip to content

Commit

Permalink
fix(sync): ensure the sync daemon env is configured correctly (#6302)
Browse files Browse the repository at this point in the history
* fix(sync): ensure the sync daemon env is configured correctly

Make sure that `MUTAGEN_SSH_PATH` pointing to the faus ssh is always set
when a native Mutagen daemon is used.

Co-authored-by: Steffen Neubauer <[email protected]>

* refactor(sync): named constant for sync init container name

* refactor(sync): rename sync init container

* chore: remove unused constants

---------

Co-authored-by: Steffen Neubauer <[email protected]>
  • Loading branch information
vvagaytsev and stefreak authored Jul 23, 2024
1 parent f651b2c commit 8e3e02f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
22 changes: 9 additions & 13 deletions core/src/mutagen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,10 @@ class _MutagenMonitor extends TypedEventEmitter<MonitorEvents> {
const mutagenOpts = [mutagenPath, "sync", "monitor", "--template", "{{ json . }}", "--long"]
log.silly(() => `Spawning mutagen using respawn: "${mutagenOpts.join(" ")}"`)

const sshPath = await getMutagenSshPath(log)
if (sshPath) {
log.debug(`Mutagen will be used with the faux SSH transport located in ${sshPath}`)
}
const proc = respawn(mutagenOpts, {
cwd: dataDir,
name: "mutagen",
env: getMutagenEnv({ dataDir, logLevel: "debug", sshPath }),
env: getMutagenEnv({ dataDir, logLevel: "debug" }, log),
maxRestarts,
sleep: 3000,
kill: 500,
Expand Down Expand Up @@ -708,7 +704,7 @@ export class Mutagen {
cwd: this.dataDir,
args,
log: this.log,
env: getMutagenEnv({ dataDir: this.dataDir }),
env: await getMutagenEnv({ dataDir: this.dataDir }, this.log),
})
} catch (err) {
if (!(err instanceof ChildProcessError)) {
Expand Down Expand Up @@ -892,24 +888,24 @@ export function getMutagenDataDir({ ctx, log }: MutagenDaemonParams) {
*/
type MutagenEnv = {
MUTAGEN_DATA_DIRECTORY: string
MUTAGEN_LOG_LEVEL?: string
MUTAGEN_SSH_PATH?: string
MUTAGEN_LOG_LEVEL?: string
}

type MutagenEnvValues = {
dataDir: string
logLevel?: string
sshPath?: string
}

export function getMutagenEnv({ dataDir, logLevel, sshPath }: MutagenEnvValues): MutagenEnv {
const env: MutagenEnv = { MUTAGEN_DATA_DIRECTORY: dataDir }
export async function getMutagenEnv({ dataDir, logLevel }: MutagenEnvValues, log: Log): Promise<MutagenEnv> {
const sshPath = await getMutagenSshPath(log)
if (sshPath) {
log.debug(`Mutagen will be used with the faux SSH transport located in ${sshPath}`)
}
const env: MutagenEnv = { MUTAGEN_DATA_DIRECTORY: dataDir, MUTAGEN_SSH_PATH: sshPath }
if (!!logLevel) {
env.MUTAGEN_LOG_LEVEL = logLevel
}
if (!!sshPath) {
env.MUTAGEN_SSH_PATH = sshPath
}
return env
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/plugins/kubernetes/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const syncPause: PluginCommand = {
await mutagen.exec({
cwd: dataDir,
log,
env: getMutagenEnv({ dataDir }),
env: await getMutagenEnv({ dataDir }, log),
args: ["sync", "pause", sessionName],
})
}
Expand Down Expand Up @@ -133,7 +133,7 @@ export const syncResume: PluginCommand = {
await mutagen.exec({
cwd: dataDir,
log,
env: getMutagenEnv({ dataDir }),
env: await getMutagenEnv({ dataDir }, log),
args: ["sync", "resume", sessionName],
})
}
Expand All @@ -148,7 +148,7 @@ async function getMutagenSyncSessions({ mutagen, dataDir, log }: { mutagen: Plug
const res = await mutagen.exec({
cwd: dataDir,
log,
env: getMutagenEnv({ dataDir }),
env: await getMutagenEnv({ dataDir }, log),
args: ["sync", "list", "--template={{ json . }}"],
})
return parseSyncListResult(res)
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { makeDocsLinkPlain } from "../../docs/common.js"
export const rsyncPortName = "garden-rsync"
export const buildSyncVolumeName = `garden-sync`

export const CLUSTER_REGISTRY_PORT = 5000
export const CLUSTER_REGISTRY_DEPLOYMENT_NAME = "garden-docker-registry"
export const MAX_CONFIGMAP_DATA_SIZE = 1024 * 1024 // max ConfigMap data size is 1MB
// max ConfigMap data size is 1MB but we need to factor in overhead, plus in some cases the log is duplicated in
// the outputs field, so we cap at 250kB.
Expand Down Expand Up @@ -46,6 +44,8 @@ export const k8sSyncUtilImageNameLegacy: DockerImageWithDigest =
export const k8sSyncUtilImageName: DockerImageWithDigest =
"gardendev/k8s-sync:0.2.1@sha256:90a583672c63e61031a036900753cb6a8a6b0b7dc20909e2abcc079a1120127b"

export const k8sSyncUtilContainerName = "garden-sync-init"

export function getK8sSyncUtilImageName(): DockerImageWithDigest {
return gardenEnv.GARDEN_ENABLE_NEW_SYNC ? k8sSyncUtilImageName : k8sSyncUtilImageNameLegacy
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { isConfiguredForSyncMode } from "./status/status.js"
import type { PluginContext } from "../../plugin-context.js"
import type { SyncConfig, SyncSession } from "../../mutagen.js"
import { haltedStatuses, Mutagen, mutagenAgentPath, mutagenStatusDescriptions } from "../../mutagen.js"
import { getK8sSyncUtilImageName, syncGuideLink } from "./constants.js"
import { getK8sSyncUtilImageName, k8sSyncUtilContainerName, syncGuideLink } from "./constants.js"
import { isAbsolute, relative, resolve } from "path"
import type { Resolved } from "../../actions/types.js"
import { joinWithPosix } from "../../util/fs.js"
Expand Down Expand Up @@ -463,7 +463,7 @@ export async function configureSyncMode({
const k8sSyncUtilImageName = getK8sSyncUtilImageName()
if (!podSpec.initContainers.find((c) => c.image === k8sSyncUtilImageName)) {
const initContainer = {
name: "garden-dev-init",
name: k8sSyncUtilContainerName,
image: k8sSyncUtilImageName,
command: [
"/bin/sh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { gardenAnnotationKey } from "../../../../../../src/util/string.js"
import {
getK8sSyncUtilImageName,
k8sReverseProxyImageName,
k8sSyncUtilContainerName,
PROXY_CONTAINER_SSH_TUNNEL_PORT,
PROXY_CONTAINER_SSH_TUNNEL_PORT_NAME,
PROXY_CONTAINER_USER_NAME,
Expand Down Expand Up @@ -444,13 +445,13 @@ describe("kubernetes container deployment handlers", () => {

const initContainer = resource.spec.template?.spec?.initContainers![0]
expect(initContainer).to.exist
expect(initContainer!.name).to.eq("garden-dev-init")
expect(initContainer!.name).to.eq(k8sSyncUtilContainerName)
expect(initContainer!.volumeMounts).to.exist
expect(initContainer!.volumeMounts![0]).to.eql({ name: "garden", mountPath: "/.garden" })

expect(resource.spec.template?.spec?.initContainers).to.eql([
{
name: "garden-dev-init",
name: k8sSyncUtilContainerName,
image: getK8sSyncUtilImageName(),
command: ["/bin/sh", "-c", "'cp' '/usr/local/bin/mutagen-agent' '/.garden/mutagen-agent'"],
imagePullPolicy: "IfNotPresent",
Expand Down

0 comments on commit 8e3e02f

Please sign in to comment.