Skip to content

Commit

Permalink
refactor(k8s): nest system garden dir under project garden dir
Browse files Browse the repository at this point in the history
With this change, system module build are stored at the project level. This way we
can run several Garden processes at the same time without them all modifying the same
system build directory, which can cause unexpected issues.
  • Loading branch information
eysi09 committed Jun 11, 2019
1 parent 43f2ad6 commit a57a611
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
9 changes: 8 additions & 1 deletion garden-service/src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import { projectNameSchema, projectSourcesSchema, environmentNameSchema } from "
import { PluginError } from "./exceptions"
import { defaultProvider, Provider, providerSchema, ProviderConfig } from "./config/provider"
import { configStoreSchema } from "./config-store"
import { deline } from "./util/string"

type WrappedFromGarden = Pick<Garden,
"projectName" |
"projectRoot" |
"projectSources" |
"gardenDirPath" |
// TODO: remove this from the interface
"configStore" |
"environmentName"
Expand All @@ -34,8 +36,12 @@ export const pluginContextSchema = Joi.object()
.keys({
projectName: projectNameSchema,
projectRoot: Joi.string()
.uri(<any>{ relativeOnly: true })
.description("The absolute path of the project root."),
gardenDirPath: Joi.string()
.description(deline`
The absolute path of the project's Garden dir. This is the directory the contains builds, logs and
other meta data. A custom path can be set when initialising the Garden class. Defaults to \`.garden\`.
`),
projectSources: projectSourcesSchema,
configStore: configStoreSchema,
environmentName: environmentNameSchema,
Expand All @@ -59,6 +65,7 @@ export async function createPluginContext(garden: Garden, providerName: string):
environmentName: garden.environmentName,
projectName: garden.projectName,
projectRoot: garden.projectRoot,
gardenDirPath: garden.gardenDirPath,
projectSources: cloneDeep(garden.projectSources),
configStore: garden.configStore,
provider,
Expand Down
8 changes: 4 additions & 4 deletions garden-service/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getEnvironmentStatus({ ctx, log }: GetEnvironmentStatusPar
const k8sCtx = <KubernetesPluginContext>ctx
const variables = getVariables(k8sCtx.provider.config)

const sysGarden = await getSystemGarden(k8sCtx.provider, variables || {})
const sysGarden = await getSystemGarden(k8sCtx, variables || {})
const sysCtx = <KubernetesPluginContext>await sysGarden.getPluginContext(k8sCtx.provider.name)

let systemReady = true
Expand Down Expand Up @@ -74,9 +74,9 @@ export async function getEnvironmentStatus({ ctx, log }: GetEnvironmentStatusPar
const systemServiceStatus = await getSystemServiceStatus({
ctx: k8sCtx,
log,
sysGarden,
namespace,
serviceNames: systemServiceNames,
variables: variables || {},
})

// We require manual init if we're installing any system services to remote clusters, to avoid conflicts
Expand Down Expand Up @@ -140,7 +140,7 @@ export async function prepareEnvironment({ ctx, log, force, manualInit, status }
}

// Install Tiller to system namespace
const sysGarden = await getSystemGarden(k8sCtx.provider, variables || {})
const sysGarden = await getSystemGarden(k8sCtx, variables || {})
const sysCtx = <KubernetesPluginContext>await sysGarden.getPluginContext(k8sCtx.provider.name)
await installTiller({ ctx: sysCtx, provider: sysCtx.provider, log, force })

Expand All @@ -149,11 +149,11 @@ export async function prepareEnvironment({ ctx, log, force, manualInit, status }
// Install system services
await prepareSystemServices({
log,
sysGarden,
namespace,
force,
ctx: k8sCtx,
serviceNames: systemServiceNames,
variables: variables || {},
})
}

Expand Down
24 changes: 14 additions & 10 deletions garden-service/src/plugins/kubernetes/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as semver from "semver"

import { STATIC_DIR, DEFAULT_API_VERSION } from "../../constants"
import { Garden } from "../../garden"
import { KubernetesProvider, KubernetesPluginContext, KubernetesConfig } from "./config"
import { KubernetesPluginContext, KubernetesConfig } from "./config"
import { LogEntry } from "../../logger/log-entry"
import { KubeApi } from "./api"
import { createNamespace } from "./namespace"
Expand All @@ -33,17 +33,23 @@ const systemProjectPath = join(STATIC_DIR, "kubernetes", "system")
export const systemNamespace = "garden-system"
export const systemMetadataNamespace = "garden-system--metadata"

export async function getSystemGarden(provider: KubernetesProvider, variables: PrimitiveMap): Promise<Garden> {
/**
* Note that we initialise system Garden with a custom Garden dir path. This is because
* the system modules are stored in the static directory but we want their build products
* stored at the project level. This way we can run several Garden processes at the same time
* without them all modifying the same system build directory, which can cause unexpected issues.
*/
export async function getSystemGarden(ctx: KubernetesPluginContext, variables: PrimitiveMap): Promise<Garden> {
const sysProvider: KubernetesConfig = {
...provider.config,

...ctx.provider.config,
environments: ["default"],
name: provider.name,
name: ctx.provider.name,
namespace: systemNamespace,
_systemServices: [],
}

return Garden.factory(systemProjectPath, {
gardenDirPath: join(ctx.gardenDirPath, "kubernetes.garden"),
environmentName: "default",
config: {
path: systemProjectPath,
Expand Down Expand Up @@ -114,18 +120,17 @@ export async function recreateSystemNamespaces(api: KubeApi, log: LogEntry, name

interface GetSystemServicesStatusParams {
ctx: KubernetesPluginContext,
sysGarden: Garden,
log: LogEntry,
namespace: string,
serviceNames: string[],
variables: PrimitiveMap,
}

export async function getSystemServiceStatus(
{ ctx, log, namespace, serviceNames, variables }: GetSystemServicesStatusParams,
{ ctx, sysGarden, log, namespace, serviceNames }: GetSystemServicesStatusParams,
) {
let dashboardPages: DashboardPage[] = []

const sysGarden = await getSystemGarden(ctx.provider, variables)
const actions = await sysGarden.getActionHelper()

const serviceStatuses = await actions.getServiceStatuses({ log, serviceNames })
Expand Down Expand Up @@ -167,7 +172,7 @@ interface PrepareSystemServicesParams extends GetSystemServicesStatusParams {
}

export async function prepareSystemServices(
{ ctx, log, namespace, serviceNames, force, variables }: PrepareSystemServicesParams,
{ ctx, sysGarden, log, namespace, serviceNames, force }: PrepareSystemServicesParams,
) {
const api = await KubeApi.factory(log, ctx.provider.config.context)

Expand All @@ -180,7 +185,6 @@ export async function prepareSystemServices(

const k8sCtx = <KubernetesPluginContext>ctx
const provider = k8sCtx.provider
const sysGarden = await getSystemGarden(provider, variables)

// Deploy enabled system services
if (serviceNames.length > 0) {
Expand Down

0 comments on commit a57a611

Please sign in to comment.