Skip to content

Commit

Permalink
feat(k8s): allow specifying default username in k8s provider config
Browse files Browse the repository at this point in the history
Also, fix it to "default" in local-kubernetes plugin.
  • Loading branch information
edvald committed Jul 10, 2018
1 parent ad0a54f commit 1e42cfb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/plugins/kubernetes/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
*/

import * as Joi from "joi"
import { validate } from "../../types/common"
import {
joiIdentifier,
validate
} from "../../types/common"
import {
GardenPlugin,
Provider,
Expand Down Expand Up @@ -47,6 +50,7 @@ export interface KubernetesConfig extends ProviderConfig {
ingressPort: number
ingressClass: string
forceSsl: boolean
defaultUsername?: string
}

export interface KubernetesProvider extends Provider<KubernetesConfig> { }
Expand All @@ -60,6 +64,8 @@ const kubernetesConfigBase = providerConfigBase
.hostname()
.required()
.description("The external hostname of the cluster's ingress controller."),
defaultUsername: joiIdentifier()
.description("Set a default username (used for namespacing within a cluster)."),
})

const configSchema = kubernetesConfigBase
Expand Down
1 change: 1 addition & 0 deletions src/plugins/kubernetes/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export async function gardenPlugin({ config, logEntry }): Promise<GardenPlugin>
ingressClass: "nginx",
// TODO: support SSL on local deployments
forceSsl: false,
defaultUsername: "default",
_system: config._system,
_systemServices: systemServices,
}
Expand Down
31 changes: 20 additions & 11 deletions src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,31 @@ export async function createNamespace(context: string, namespace: string) {
}

export async function getAppNamespace(ctx: PluginContext, provider: KubernetesProvider) {
let namespace

if (isSystemGarden(provider)) {
return GARDEN_SYSTEM_NAMESPACE
}
namespace = GARDEN_SYSTEM_NAMESPACE
} else {
const localConfig = await ctx.localConfigStore.get()
const k8sConfig = localConfig.kubernetes || {}
let { username, ["previous-usernames"]: previousUsernames } = k8sConfig

if (!username) {
username = provider.config.defaultUsername
}

const localConfig = await ctx.localConfigStore.get()
const k8sConfig = localConfig.kubernetes || {}
const { username, ["previous-usernames"]: previousUsernames } = k8sConfig
if (!username) {
throw new AuthenticationError(
`User not logged into provider ${providerName}. Please specify defaultUsername in provider ` +
`config or run garden login.`,
{ previousUsernames, provider: providerName },
)
}

if (!username) {
throw new AuthenticationError(
`User not logged into provider ${providerName}. Please run garden login.`,
{ previousUsernames, provider: providerName },
)
namespace = `garden--${username}--${ctx.projectName}`
}

return `garden--${username}--${ctx.projectName}`
return namespace
}

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

0 comments on commit 1e42cfb

Please sign in to comment.