Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix kubectl binary version check #510

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions src/main/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, remote } from "electron"
import * as path from "path"
import * as fs from "fs"
import * as request from "request"
import * as requestPromise from "request-promise-native"
import { promiseExec} from "./promise-exec"
import logger from "./logger"
import { ensureDir, pathExists } from "fs-extra"
import * as md5File from "md5-file"
Expand Down Expand Up @@ -117,42 +117,31 @@ export class Kubectl {
}
}

protected async urlEtag() {
const response = await requestPromise({
method: "HEAD",
uri: this.url,
resolveWithFullResponse: true,
timeout: 4000,
...this.getRequestOpts()
}).catch((error) => { logger.error(error) })

if (response && response.headers["etag"]) {
return response.headers["etag"].replace(/"/g, "")
}
return ""
}

public async checkBinary(checkTag = true) {
public async checkBinary(checkVersion = true) {
const exists = await pathExists(this.path)
if (exists) {
if (!checkTag) {
if (!checkVersion) {
return true
}
const hash = md5File.sync(this.path)
const etag = await this.urlEtag()
if (etag === "") {
logger.debug("Cannot resolve kubectl remote etag")
return true

try {
const { stdout, stderr } = await promiseExec(`"${this.path}" version --client=true -o json`)
const output = JSON.parse(stdout)
let version: string = output.clientVersion.gitVersion
if (version[0] === 'v') {
version = version.slice(1)
}
if (version === this.kubectlVersion) {
logger.debug(`Local kubectl is version ${this.kubectlVersion}`)
return true
}
logger.error(`Local kubectl is version ${version}, expected ${this.kubectlVersion}, unlinking`)
}
if (hash == etag) {
logger.debug("Kubectl md5sum matches the remote etag")
return true
catch(err) {
logger.error(`Local kubectl failed to run properly (${err.message}), unlinking`)
}

logger.error("Kubectl md5sum " + hash + " does not match the remote etag " + etag + ", unlinking and downloading again")
await fs.promises.unlink(this.path)
}

return false
}

Expand Down