1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

first crack at updating Kubectl.ts

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2020-06-19 14:13:35 -04:00
parent b5b8cde617
commit afd7a97030
2 changed files with 28 additions and 7 deletions

View File

@ -43,7 +43,7 @@ class KubectlDownloader {
if (exists) { if (exists) {
let oldEtag = "" let oldEtag = ""
try { try {
oldEtag = fs.readFileSync(this.pathEtag, "utf8") oldEtag = await fs.promises.readFile(this.pathEtag, "utf8")
} }
catch(err) { catch(err) {
// treat any error here as a bad check // treat any error here as a bad check
@ -74,7 +74,7 @@ class KubectlDownloader {
try { try {
const etag = await this.urlEtag() const etag = await this.urlEtag()
fs.writeFileSync(this.pathEtag, etag, 'utf8') await fs.promises.writeFile(this.pathEtag, etag, 'utf8')
} }
catch(err) { catch(err) {
console.log(`error (${err.message}) saving kubectl etag, this may incur further downloads`) console.log(`error (${err.message}) saving kubectl etag, this may incur further downloads`)

View File

@ -5,7 +5,6 @@ import * as request from "request"
import * as requestPromise from "request-promise-native" import * as requestPromise from "request-promise-native"
import logger from "./logger" import logger from "./logger"
import { ensureDir, pathExists } from "fs-extra" import { ensureDir, pathExists } from "fs-extra"
import * as md5File from "md5-file"
import { globalRequestOpts } from "../common/request" import { globalRequestOpts } from "../common/request"
import * as lockFile from "proper-lockfile" import * as lockFile from "proper-lockfile"
import { helmCli } from "./helm-cli" import { helmCli } from "./helm-cli"
@ -51,6 +50,8 @@ export class Kubectl {
protected directory: string protected directory: string
protected url: string protected url: string
protected path: string protected path: string
protected pathEtag: string;
protected etag: string;
protected dirname: string protected dirname: string
public static readonly kubectlDir = path.join((app || remote.app).getPath("userData"), "binaries", "kubectl") public static readonly kubectlDir = path.join((app || remote.app).getPath("userData"), "binaries", "kubectl")
@ -94,6 +95,8 @@ export class Kubectl {
this.dirname = path.normalize(path.join(Kubectl.kubectlDir, this.kubectlVersion)) this.dirname = path.normalize(path.join(Kubectl.kubectlDir, this.kubectlVersion))
this.path = path.join(this.dirname, binaryName) this.path = path.join(this.dirname, binaryName)
this.pathEtag = this.path + ".etag";
this.etag = "";
} }
public async kubectlPath(): Promise<string> { public async kubectlPath(): Promise<string> {
@ -138,19 +141,29 @@ export class Kubectl {
if (!checkTag) { if (!checkTag) {
return true return true
} }
const hash = md5File.sync(this.path) let oldEtag = ""
try {
oldEtag = await fs.promises.readFile(this.pathEtag, "utf8")
}
catch(err) {
// treat any error here as a bad check
logger.debug(`Error reading saved etag for local kubectl (${err.message})`)
return false
}
const etag = await this.urlEtag() const etag = await this.urlEtag()
if (etag === "") { if (etag === "") {
logger.debug("Cannot resolve kubectl remote etag") logger.debug("Cannot resolve kubectl remote etag")
return true return true
} }
if (hash == etag) { if(oldEtag == etag) {
logger.debug("Kubectl md5sum matches the remote etag") logger.debug("Local kubectl etag matches the remote etag")
return true return true
} }
logger.error("Kubectl md5sum " + hash + " does not match the remote etag " + etag + ", unlinking and downloading again") logger.error(`Local kubectl etag ${oldEtag} does not match the remote etag ${etag}, unlinking`)
await fs.promises.unlink(this.path) await fs.promises.unlink(this.path)
await fs.promises.unlink(this.pathEtag)
} }
return false return false
@ -163,6 +176,7 @@ export class Kubectl {
if (!exist) { if (!exist) {
await fs.promises.copyFile(Kubectl.bundledKubectlPath, this.path) await fs.promises.copyFile(Kubectl.bundledKubectlPath, this.path)
await fs.promises.chmod(this.path, 0o755) await fs.promises.chmod(this.path, 0o755)
await fs.promises.copyFile(Kubectl.bundledKubectlPath + ".etag", this.pathEtag)
} }
return true return true
} catch(err) { } catch(err) {
@ -199,6 +213,13 @@ export class Kubectl {
logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`) logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try {
const etag = this.urlEtag()
fs.writeFileSync(this.pathEtag, etag)
}
catch(err) {
logger.debug(`error (${err.message}) saving kubectl etag, this may incur further downloads`)
}
const stream = request({ const stream = request({
gzip: true, gzip: true,
...this.getRequestOpts() ...this.getRequestOpts()