Skip to content

Commit

Permalink
modified Kubectl.checkBinary() to test the binary for the right versi…
Browse files Browse the repository at this point in the history
…on instead of using the eTag (#510)

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
  • Loading branch information
jim-docker authored Jun 23, 2020
1 parent 6770f05 commit cd66a98
Showing 1 changed file with 18 additions and 29 deletions.
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

0 comments on commit cd66a98

Please # to comment.