Skip to content

Commit

Permalink
improvement(k8s): cache DNS lookups for cluster hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald authored and thsig committed Mar 15, 2021
1 parent d8481b2 commit 667646b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"deline": "^1.0.4",
"dependency-graph": "^0.9.0",
"directory-tree": "^2.2.4",
"dns-lookup-cache": "^1.0.4",
"docker-file-parser": "^1.0.4",
"dockerode": "^3.2.0",
"dotenv": "^8.2.0",
Expand Down Expand Up @@ -255,4 +256,4 @@
]
},
"gitHead": "b0647221a4d2ff06952bae58000b104215aed922"
}
}
11 changes: 10 additions & 1 deletion core/src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// No idea why tslint complains over this line
// tslint:disable-next-line:no-unused
import { IncomingMessage } from "http"
import { Agent } from "https"
import { ReadStream } from "tty"
import {
KubeConfig,
Expand All @@ -35,6 +36,7 @@ import request = require("request-promise")
import requestErrors = require("request-promise/errors")
import { safeLoad } from "js-yaml"
import { readFile } from "fs-extra"
import { lookup } from "dns-lookup-cache"

import { Omit, safeDumpYaml, StringCollector, sleep } from "../../util/util"
import { omitBy, isObject, isPlainObject, keyBy } from "lodash"
Expand Down Expand Up @@ -76,6 +78,8 @@ const cachedApiInfo: { [context: string]: ApiInfo } = {}
const cachedApiResourceInfo: { [context: string]: ApiResourceMap } = {}
const apiInfoLock = new AsyncLock()

const requestAgent = new Agent({ lookup })

// NOTE: be warned, the API of the client library is very likely to change

type K8sApi =
Expand Down Expand Up @@ -318,6 +322,7 @@ export class KubeApi {
method: "get",
json: true,
resolveWithFullResponse: true,
agent: requestAgent,
...opts,
}

Expand Down Expand Up @@ -514,7 +519,7 @@ export class KubeApi {
try {
await api[crudMap[kind].read](name, namespace)
await api[crudMap[kind].replace](name, namespace, obj)
log.debug(`Patched ${kind} ${namespace}/${name}`)
log.debug(`Replaced ${kind} ${namespace}/${name}`)
} catch (err) {
if (err.statusCode === 404) {
try {
Expand All @@ -540,6 +545,10 @@ export class KubeApi {
private wrapApi<T extends K8sApi>(api: T, config: KubeConfig): T {
api.setDefaultAuthentication(config)

api.addInterceptor((opts) => {
opts.agent = requestAgent
})

return new Proxy(api, {
get: (target: T, name: string, receiver) => {
if (!(name in Object.getPrototypeOf(target))) {
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6719,6 +6719,14 @@ dns-equal@^1.0.0:
resolved "https://registry.yarnpkg.com/dns-equal/-/dns-equal-1.0.0.tgz#b39e7f1da6eb0a75ba9c17324b34753c47e0654d"
integrity sha1-s55/HabrCnW6nBcySzR1PEfgZU0=

dns-lookup-cache@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/dns-lookup-cache/-/dns-lookup-cache-1.0.4.tgz#fab28a97ef320215d9255ce866b7dc769acf1762"
integrity sha512-528ydOnvZQ+a0f+BfmBU4A5zGb05uvwxN/u96/yQdSA7Z8uwDXHXJ+FEPl5UI91Ga7SNyDT4csL9x4pWMHMCyg==
dependencies:
lodash "^4.17.11"
rr "^0.1.0"

dns-packet@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/dns-packet/-/dns-packet-1.3.1.tgz#12aa426981075be500b910eedcd0b47dd7deda5a"
Expand Down Expand Up @@ -16188,6 +16196,11 @@ roarr@^2.15.3:
semver-compare "^1.0.0"
sprintf-js "^1.1.2"

rr@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/rr/-/rr-0.1.0.tgz#a18ec25ec94a67c35f210bb3a85d17914e79cd1e"
integrity sha1-oY7CXslKZ8NfIQuzqF0XkU55zR4=

rsvp@^4.8.4:
version "4.8.5"
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734"
Expand Down

0 comments on commit 667646b

Please sign in to comment.