Skip to content

Commit

Permalink
refactor(k8s): ensure namespaces are created when needed
Browse files Browse the repository at this point in the history
This simplifies configuration a bit, and will be helpful for future
additions.
  • Loading branch information
edvald committed Jul 10, 2018
1 parent 6f73299 commit 67946eb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
48 changes: 7 additions & 41 deletions src/plugins/kubernetes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ import {
ContainerModule,
helpers,
} from "../container"
import { values, every, uniq } from "lodash"
import { uniq } from "lodash"
import { deserializeValues, serializeValues, splitFirst, sleep } from "../../util/util"
import { ServiceStatus } from "../../types/service"
import { joiIdentifier } from "../../types/common"
import {
coreApi,
} from "./api"
import {
createNamespace,
getAppNamespace,
getMetadataNamespace,
getAllGardenNamespaces,
Expand Down Expand Up @@ -89,50 +88,17 @@ export async function getEnvironmentStatus({ ctx, provider }: GetEnvironmentStat
throw err
}

const statusDetail: { [key: string]: boolean } = {
namespaceReady: false,
metadataNamespaceReady: false,
}

const metadataNamespace = await getMetadataNamespace(ctx, provider)
const namespacesStatus = await coreApi(context).listNamespace()
const namespace = await getAppNamespace(ctx, provider)

for (const n of namespacesStatus.body.items) {
if (n.metadata.name === namespace && n.status.phase === "Active") {
statusDetail.namespaceReady = true
}

if (n.metadata.name === metadataNamespace && n.status.phase === "Active") {
statusDetail.metadataNamespaceReady = true
}
}

let configured = every(values(statusDetail))
await getMetadataNamespace(ctx, provider)
await getAppNamespace(ctx, provider)

return {
configured,
detail: statusDetail,
configured: true,
detail: <any>{},
}
}

export async function configureEnvironment(
{ ctx, provider, status, logEntry }: ConfigureEnvironmentParams,
) {
const context = provider.config.context

if (!status.detail.namespaceReady) {
const ns = await getAppNamespace(ctx, provider)
logEntry && logEntry.setState({ section: "kubernetes", msg: `Creating namespace ${ns}` })
await createNamespace(context, ns)
}

if (!status.detail.metadataNamespaceReady) {
const ns = await getMetadataNamespace(ctx, provider)
logEntry && logEntry.setState({ section: "kubernetes", msg: `Creating namespace ${ns}` })
await createNamespace(context, ns)
}

export async function configureEnvironment({ }: ConfigureEnvironmentParams) {
// this happens implicitly in the `getEnvironmentStatus()` function
return {}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/kubernetes/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as Joi from "joi"
import {
joiIdentifier,
validate
validate,
} from "../../types/common"
import {
GardenPlugin,
Expand Down
47 changes: 34 additions & 13 deletions src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,33 @@ import {
import { name as providerName } from "./kubernetes"
import { AuthenticationError } from "../../exceptions"

export async function createNamespace(context: string, namespace: string) {
// TODO: the types for all the create functions in the library are currently broken
await coreApi(context).createNamespace(<any>{
apiVersion: "v1",
kind: "Namespace",
metadata: {
name: namespace,
annotations: {
"garden.io/generated": "true",
},
},
})
const created: { [name: string]: boolean } = {}

export async function ensureNamespace(context: string, namespace: string) {
if (!created[namespace]) {
const namespacesStatus = await coreApi(context).listNamespace()

for (const n of namespacesStatus.body.items) {
if (n.status.phase === "Active") {
created[n.metadata.name] = true
}
}

if (!created[namespace]) {
// TODO: the types for all the create functions in the library are currently broken
await coreApi(context).createNamespace(<any>{
apiVersion: "v1",
kind: "Namespace",
metadata: {
name: namespace,
annotations: {
"garden.io/generated": "true",
},
},
})
created[namespace] = true
}
}
}

export async function getNamespace(ctx: PluginContext, provider: KubernetesProvider, suffix?: string) {
Expand Down Expand Up @@ -57,7 +72,13 @@ export async function getNamespace(ctx: PluginContext, provider: KubernetesProvi
namespace = `garden--${username}--${ctx.projectName}`
}

return suffix ? `${namespace}--${suffix}` : namespace
if (suffix) {
namespace = `${namespace}--${suffix}`
}

await ensureNamespace(provider.config.context, namespace)

return namespace
}

export async function getAppNamespace(ctx: PluginContext, provider: KubernetesProvider) {
Expand Down

0 comments on commit 67946eb

Please sign in to comment.