-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(kind): load images into kind nodes * chore(lint): fixed linting issues * feat(kind): used kubeApi * chore(kind): fixed linter warnings * chore(feedback): fix indentation and return type * chore(docs): updated the docs * chore(error): improve exception handling
- Loading branch information
1 parent
b2b30b1
commit 87a6978
Showing
4 changed files
with
119 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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 { exec } from "../../../util/util" | ||
import { LogEntry } from "../../../logger/log-entry" | ||
import { safeLoad } from "js-yaml" | ||
import { BuildResult } from "../../../types/plugin/module/build" | ||
import { KubernetesConfig, KubernetesProvider } from "../config" | ||
import { RuntimeError } from "../../../exceptions" | ||
import { KubeApi } from "../api" | ||
import { KubernetesResource } from "../types" | ||
|
||
export async function loadLocalImage(buildResult: BuildResult, config: KubernetesConfig): Promise<void> { | ||
try { | ||
const clusterName = await getClusterForContext(config.context) | ||
if (clusterName !== null) { | ||
await exec("kind", ["load", "docker-image", buildResult.details.identifier, `--name=${clusterName}`]) | ||
} | ||
} catch (err) { | ||
throw new RuntimeError( | ||
`An attempt to load image ${buildResult.details.identifier} into the kind cluster failed: ${err.message}`, | ||
{ err } | ||
) | ||
} | ||
} | ||
|
||
export async function isClusterKind(provider: KubernetesProvider, log: LogEntry): Promise<boolean> { | ||
return (await isKindInstalled(log)) && (await isKindContext(log, provider)) | ||
} | ||
|
||
async function isKindInstalled(log: LogEntry): Promise<boolean> { | ||
try { | ||
const kindVersion = (await exec("kind", ["version"])).stdout | ||
log.debug(`Found kind with the following version details ${kindVersion}`) | ||
return true | ||
} catch (err) { | ||
log.debug(`An attempt to get kind version failed with ${err}`) | ||
} | ||
|
||
return false | ||
} | ||
|
||
async function isKindContext(log: LogEntry, provider: KubernetesProvider): Promise<boolean> { | ||
const kubeApi = await KubeApi.factory(log, provider) | ||
const manifest: KubernetesResource = { | ||
apiVersion: "apps/v1", | ||
kind: "DaemonSet", | ||
metadata: { | ||
name: "kindnet", | ||
}, | ||
} | ||
try { | ||
await kubeApi.readBySpec("kube-system", manifest, log) | ||
return true | ||
} catch (err) { | ||
log.debug(`An attempt to get kindnet deamonset failed with ${err}`) | ||
} | ||
|
||
return false | ||
} | ||
|
||
async function getKindClusters(): Promise<Array<string>> { | ||
try { | ||
const clusters = (await exec("kind", ["get", "clusters"])).stdout | ||
if (clusters) { | ||
return clusters.split("\n") | ||
} | ||
return [] | ||
} catch (err) {} | ||
return [] | ||
} | ||
|
||
async function getClusterForContext(context: string) { | ||
for (let cluster of await getKindClusters()) { | ||
if (await isContextAMatch(cluster, context)) { | ||
return cluster | ||
} | ||
} | ||
return null | ||
} | ||
|
||
async function isContextAMatch(cluster: string, context: string): Promise<Boolean> { | ||
try { | ||
const kubeConfigString = (await exec("kind", ["get", "kubeconfig", `--name=${cluster}`])).stdout | ||
const kubeConfig = safeLoad(kubeConfigString) | ||
return kubeConfig["current-context"] === context | ||
} catch (err) {} | ||
return false | ||
} |