Skip to content

Commit

Permalink
NEEDS SQUASH: addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thsig committed Feb 8, 2019
1 parent 30eb3e5 commit 5f9dd63
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 128 deletions.
132 changes: 66 additions & 66 deletions garden-service/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions garden-service/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ export class ActionHelper implements TypeGuard {

// sequentially go through the preparation steps, to allow plugins to request user input
for (const [name, handler] of Object.entries(handlers)) {
const status = statuses[name] || { ready: false, needForce: false }
const forcePrep = force || !!status.needForce
log.info(`handler for ${name}: force ${force} needForce ${!!status.needForce} forcePrep ${forcePrep}`)
const status = statuses[name] || { ready: false }
const needForce = status.detail && !!status.detail.needForce
const forcePrep = force || needForce

if (status.ready && !forcePrep) {
continue
Expand Down
3 changes: 1 addition & 2 deletions garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { resolve } from "path"
import { safeDump } from "js-yaml"
import { coreCommands } from "../commands/commands"
import { DeepPrimitiveMap } from "../config/common"
import { shutdown, sleep } from "../util/util"
import { shutdown, sleep, getPackageVersion } from "../util/util"
import {
BooleanParameter,
ChoicesParameter,
Expand Down Expand Up @@ -42,7 +42,6 @@ import {
prepareArgConfig,
prepareOptionConfig,
styleConfig,
getPackageVersion,
getLogLevelChoices,
parseLogLevel,
} from "./helpers"
Expand Down
5 changes: 0 additions & 5 deletions garden-service/src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,3 @@ export function failOnInvalidOptions(argv, ctx) {
ctx.cliMessage(`Received invalid flag(s): ${invalid.join(", ")}`)
}
}

export function getPackageVersion(): String {
const version = require("../../package.json").version
return version
}
2 changes: 1 addition & 1 deletion garden-service/src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
CommandResult,
CommandParams,
} from "./base"
import { getPackageVersion } from "../cli/helpers"
import chalk from "chalk"
import { getPackageVersion } from "../util/util"

export class VersionCommand extends Command {
name = "version"
Expand Down
1 change: 0 additions & 1 deletion garden-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { resolve, join } from "path"

export const GARDEN_VERSION = "0.9.0"
export const isPkg = !!(<any>process).pkg

export const MODULE_CONFIG_FILENAME = "garden.yml"
Expand Down
87 changes: 41 additions & 46 deletions garden-service/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import * as Bluebird from "bluebird"
import * as inquirer from "inquirer"
import * as Joi from "joi"
import * as semver from "semver"
import { every, find, intersection, pick, uniq, values } from "lodash"

import { DeploymentError, NotFoundError, TimeoutError, PluginError } from "../../exceptions"
Expand All @@ -18,7 +19,8 @@ import {
GetEnvironmentStatusParams,
PluginActionParamsBase,
} from "../../types/plugin/params"
import { sleep } from "../../util/util"
import { deline } from "../../util/string"
import { sleep, getPackageVersion } from "../../util/util"
import { joiUserIdentifier } from "../../config/common"
import { KubeApi } from "./api"
import {
Expand All @@ -34,9 +36,10 @@ import { PluginContext } from "../../plugin-context"
import { LogEntry } from "../../logger/log-entry"
import { DashboardPage } from "../../config/dashboard"
import { checkTillerStatus, installTiller } from "./helm/tiller"
import { GARDEN_VERSION } from "../../constants"

const MAX_STORED_USERNAMES = 5
const GARDEN_VERSION = getPackageVersion()
const SYSTEM_NAMESPACE_MIN_VERSION = "0.9.0"

/**
* Used by both the remote and local plugin
Expand Down Expand Up @@ -83,15 +86,16 @@ export async function getRemoteEnvironmentStatus({ ctx, log }: GetEnvironmentSta
let ready = (await checkTillerStatus(ctx, ctx.provider, log)) === "ready"

const api = new KubeApi(ctx.provider)
const sysNamespaceUpToDate = await systemNamespaceUpToDate(api, log)
const contextForLog = `Checking environment status for plugin "kubernetes"`
const sysNamespaceUpToDate = await systemNamespaceUpToDate(api, log, contextForLog)
if (!sysNamespaceUpToDate) {
ready = false
}

return {
ready,
needForce: !sysNamespaceUpToDate,
needUserInput: false,
detail: { needForce: !sysNamespaceUpToDate },
}
}

Expand All @@ -114,7 +118,8 @@ export async function getLocalEnvironmentStatus({ ctx, log }: GetEnvironmentStat
const api = new KubeApi(ctx.provider)

const servicesReady = every(values(serviceStatuses).map(s => s.state === "ready"))
sysNamespaceUpToDate = await systemNamespaceUpToDate(api, log)
const contextForLog = `Checking environment status for plugin "local-kubernetes"`
sysNamespaceUpToDate = await systemNamespaceUpToDate(api, log, contextForLog)
const systemReady = sysStatus.providers[ctx.provider.config.name].ready
&& servicesReady
&& sysNamespaceUpToDate
Expand Down Expand Up @@ -160,7 +165,7 @@ export async function getLocalEnvironmentStatus({ ctx, log }: GetEnvironmentStat
ready,
needUserInput,
dashboardPages,
needForce: !sysNamespaceUpToDate,
detail: { needForce: !sysNamespaceUpToDate },
}
}

Expand All @@ -172,8 +177,9 @@ export async function prepareRemoteEnvironment({ ctx, log }: PrepareEnvironmentP
}

const api = new KubeApi(ctx.provider)
if (!await systemNamespaceUpToDate(api, log)) {
await cleanupOudatedSystemNamespaces(api, log)
const contextForLog = `Preparing environment for plugin "kubernetes"`
if (!await systemNamespaceUpToDate(api, log, contextForLog)) {
await recreateSystemNamespaces(api, log)
}
await installTiller(ctx, ctx.provider, log)

Expand All @@ -184,9 +190,10 @@ export async function prepareLocalEnvironment({ ctx, force, log }: PrepareEnviro
// make sure system services are deployed
if (!isSystemGarden(ctx.provider)) {
const api = new KubeApi(ctx.provider)
const outdated = !(await systemNamespaceUpToDate(api, log))
const contextForLog = `Preparing environment for plugin "local-kubernetes"`
const outdated = !(await systemNamespaceUpToDate(api, log, contextForLog))
if (outdated) {
await cleanupOudatedSystemNamespaces(api, log)
await recreateSystemNamespaces(api, log)
}
await configureSystemServices({ ctx, log, force: force || outdated })
await installTiller(ctx, ctx.provider, log)
Expand All @@ -197,9 +204,8 @@ export async function prepareLocalEnvironment({ ctx, force, log }: PrepareEnviro

/**
* Returns true if the garden-system namespace exists and has the version
* GARDEN_VERSION.
*/
export async function systemNamespaceUpToDate(api: KubeApi, log: LogEntry): Promise<boolean> {
export async function systemNamespaceUpToDate(api: KubeApi, log: LogEntry, contextForLog: string): Promise<boolean> {
let systemNamespace
try {
systemNamespace = await api.core.readNamespace("garden-system")
Expand All @@ -213,42 +219,31 @@ export async function systemNamespaceUpToDate(api: KubeApi, log: LogEntry): Prom

const versionInCluster = systemNamespace.body.metadata.annotations["garden.io/version"]

log.debug(`current version ${GARDEN_VERSION}, version in cluster: ${versionInCluster}`)
const upToDate = !!versionInCluster && semver.gte(semver.coerce(versionInCluster)!, SYSTEM_NAMESPACE_MIN_VERSION)

return versionInCluster === GARDEN_VERSION
}

// Returns true if the garden-system namespace was outdated
export async function cleanupOudatedSystemNamespaces(api: KubeApi, log: LogEntry) {
let systemNamespace
try {
systemNamespace = await api.core.readNamespace("garden-system")
} catch (err) {
if (err.code === 404) {
return false
} else {
throw err
}
}

const versionInCluster = systemNamespace.body.metadata.annotations["garden.io/version"]
log.debug(deline`
${contextForLog}: current version ${GARDEN_VERSION}, version in cluster: ${versionInCluster},
oldest permitted version: ${SYSTEM_NAMESPACE_MIN_VERSION}, up to date: ${upToDate}
`)

log.info(`current version ${GARDEN_VERSION}, version in cluster: ${versionInCluster}`)
return upToDate
}

if (versionInCluster !== GARDEN_VERSION) {
const entry = log.info({
section: "cleanup",
msg: "Deleting outdated system namespaces...",
status: "active",
})
await deleteNamespaces(["garden-system", "garden-system--metadata"], api, log)
await createNamespace(api, "garden-system")
await createNamespace(api, "garden-system--metadata")
entry.setSuccess()
return true
} else {
return false
}
/**
* Returns true if the garden-system namespace was outdated.
*/
export async function recreateSystemNamespaces(api: KubeApi, log: LogEntry) {
const entry = log.debug({
section: "cleanup",
msg: "Deleting outdated system namespaces...",
status: "active",
})
await deleteNamespaces(["garden-system", "garden-system--metadata"], api, log)
entry.setState({ msg: "Creating system namespaces..." })
await createNamespace(api, "garden-system")
await createNamespace(api, "garden-system--metadata")
entry.setState({ msg: "System namespaces up to date" })
entry.setSuccess()
}

export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams) {
Expand All @@ -260,7 +255,7 @@ export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams)
status: "active",
})

await deleteNamespaces([namespace], api, log)
await deleteNamespaces([namespace], api, entry)
await logout({ ctx, log })

return {}
Expand Down
3 changes: 2 additions & 1 deletion garden-service/src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { KubeApi } from "./api"
import { KubernetesProvider } from "./kubernetes"
import { name as providerName } from "./kubernetes"
import { AuthenticationError } from "../../exceptions"
import { GARDEN_VERSION } from "../../constants";
import { getPackageVersion } from "../../util/util"

const GARDEN_VERSION = getPackageVersion()
const created: { [name: string]: boolean } = {}

export async function ensureNamespace(api: KubeApi, namespace: string) {
Expand Down
2 changes: 0 additions & 2 deletions garden-service/src/types/plugin/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const configureProviderResultSchema = Joi.object()
export interface EnvironmentStatus {
ready: boolean
needUserInput?: boolean
needForce?: boolean
dashboardPages?: DashboardPage[]
detail?: any
}
Expand All @@ -38,7 +37,6 @@ export const environmentStatusSchema = Joi.object()
"Set to true if the environment needs user input to be initialized, " +
"and thus needs to be initialized via `garden init`.",
),
needForce: Joi.boolean(), // TODO: Add descriptiopn
dashboardPages: dashboardPagesSchema,
detail: Joi.object()
.meta({ extendable: true })
Expand Down
5 changes: 5 additions & 0 deletions garden-service/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export function registerCleanupFunction(name: string, func: HookCallback) {
exitHook(func)
}

export function getPackageVersion(): String {
const version = require("../../package.json").version
return version
}

/*
Warning: Don't make any async calls in the loop body when using this function, since this may cause
funky concurrency behavior.
Expand Down
3 changes: 2 additions & 1 deletion garden-service/test/src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/

import { expect } from "chai"
import { getPackageVersion, parseLogLevel, getLogLevelChoices } from "../../../src/cli/helpers"
import { parseLogLevel, getLogLevelChoices } from "../../../src/cli/helpers"
import { expectError } from "../../helpers"
import { getPackageVersion } from "../../../src/util/util"

describe("helpers", () => {
const validLogLevels = ["error", "warn", "info", "verbose", "debug", "silly", "0", "1", "2", "3", "4", "5"]
Expand Down

0 comments on commit 5f9dd63

Please sign in to comment.