Skip to content

Commit

Permalink
refactor: split Kubernetes plugin into more modules
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 13, 2018
1 parent 21ecc6c commit e6d84e1
Show file tree
Hide file tree
Showing 13 changed files with 677 additions and 522 deletions.
61 changes: 61 additions & 0 deletions src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import * as K8s from "kubernetes-client"

import { DEFAULT_CONTEXT } from "./kubectl"

const cachedParams = {}

function getParams(namespace?: string) {
let params = cachedParams[namespace || ""]

if (!params) {
const config = K8s.config.loadKubeconfig()
params = <any>K8s.config.fromKubeconfig(config, DEFAULT_CONTEXT)

params.promises = true
params.namespace = namespace

cachedParams[namespace || ""] = params
}

return params
}

export function coreApi(namespace?: string): any {
return new K8s.Core(getParams(namespace))
}

export function extensionsApi(namespace?: string): any {
return new K8s.Extensions(getParams(namespace))
}

export async function apiPostOrPut(api: any, name: string, body: object) {
try {
await api.post(body)
} catch (err) {
if (err.code === 409) {
await api(name).put(body)
} else {
throw err
}
}
}

export async function apiGetOrNull(api: any, name: string) {
try {
return await api(name).get()
} catch (err) {
if (err.code === 404) {
return null
} else {
throw err
}
}
}
50 changes: 48 additions & 2 deletions src/plugins/kubernetes/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { ContainerService, ContainerServiceConfig } from "../container"
import { DeployServiceParams } from "../../types/plugin"
import {
ContainerModule,
ContainerService,
ContainerServiceConfig,
} from "../container"
import { toPairs, extend } from "lodash"
import { ServiceContext } from "../../types/service"
import {
ServiceContext,
ServiceStatus,
} from "../../types/service"
import {
createIngress,
getServiceHostname,
} from "./ingress"
import { apply } from "./kubectl"
import { getAppNamespace } from "./namespace"
import { createServices } from "./service"
import {
checkDeploymentStatus,
waitForDeployment,
} from "./status"

const DEFAULT_CPU_REQUEST = 0.01
const DEFAULT_CPU_LIMIT = 0.5
Expand All @@ -21,6 +40,33 @@ interface KubeEnvVar {
valueFrom?: { fieldRef: { fieldPath: string } }
}

export async function deployService(
{ ctx, service, env, serviceContext, exposePorts = false, logEntry }: DeployServiceParams<ContainerModule>,
): Promise<ServiceStatus> {
const namespace = getAppNamespace(ctx, env)

const deployment = await createDeployment(service, serviceContext, exposePorts)
await apply(deployment, { namespace })

// TODO: automatically clean up Services and Ingresses if they should no longer exist

const kubeservices = await createServices(service, exposePorts)

for (let kubeservice of kubeservices) {
await apply(kubeservice, { namespace })
}

const ingress = await createIngress(service, getServiceHostname(ctx, service))

if (ingress !== null) {
await apply(ingress, { namespace })
}

await waitForDeployment({ ctx, service, logEntry, env })

return checkDeploymentStatus({ ctx, service, env })
}

export async function createDeployment(
service: ContainerService, serviceContext: ServiceContext, exposePorts: boolean,
) {
Expand Down
Loading

0 comments on commit e6d84e1

Please sign in to comment.