reduce memory footprint by reusing object instances

Signed-off-by: Travis Glenn Hansen <travisghansen@yahoo.com>
This commit is contained in:
Travis Glenn Hansen 2022-04-06 11:14:39 -06:00
parent 1783aebb75
commit 1f2c8cf58d
19 changed files with 412 additions and 229 deletions

View File

@ -1,3 +1,10 @@
# v1.6.2
Released 2022-04-06
- dep bumps
- optimize via object instance reuse of various clients etc
# v1.6.1
Released 2022-03-23

View File

@ -134,6 +134,7 @@ async function requestHandlerProxy(call, callback, serviceMethodName) {
cleansedCall.request[key] = "redacted";
}
}
try {
logger.info(
"new request - driver: %s method: %s call: %j",
@ -337,17 +338,38 @@ logger.info(
[`SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach(
(eventType) => {
process.on(eventType, (code) => {
process.on(eventType, async (code) => {
console.log(`running server shutdown, exit code: ${code}`);
// attempt clean shutdown of in-flight requests
try {
await new Promise((resolve, reject) => {
try {
csiServer.tryShutdown(() => {
resolve();
});
} catch (e) {
reject(e);
}
});
console.log(`grpc server gracefully closed all connections`);
} catch (e) {
console.log("failed to cleanly shutdown grpc server", e);
}
// NOTE: if the shutdown above finishes cleanly the socket will already be removed
let socketPath = bindSocket;
socketPath = socketPath.replace(/^unix:\/\//g, "");
if (socketPath && fs.existsSync(socketPath)) {
let fsStat = fs.statSync(socketPath);
if (fsStat.isSocket()) {
fs.unlinkSync(socketPath);
console.log(`removed grpc socket ${socketPath}`);
}
}
console.log("server fully shutdown, exiting");
process.exit(code);
});
}
@ -378,6 +400,15 @@ if (process.env.MANUAL_GC == "1") {
}, process.env.MANUAL_GC_INTERVAL || 60000);
}
if (process.env.LOG_GRPC_SESSIONS == "1") {
setInterval(() => {
console.log("dumping sessions");
try {
console.log(csiServer.sessions);
} catch (e) {}
}, 5000);
}
if (require.main === module) {
(async function () {
try {
@ -420,6 +451,8 @@ if (require.main === module) {
}
);
});
fs.chmodSync(socketPath, 0o666);
}
csiServer.start();
} catch (e) {

36
package-lock.json generated
View File

@ -70,9 +70,9 @@
}
},
"node_modules/@grpc/grpc-js": {
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.6.1.tgz",
"integrity": "sha512-ix3rQS64rKL1s6CfIaRgnts+RNYZZ2NaYyTK7iimai6an/0GGDbukzy990hJ5vtKHjhaqJxJMB6Qq7BMZ0zZSQ==",
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.6.2.tgz",
"integrity": "sha512-9+89Ne1K8F9u86T+l1yIV2DS+dWHYVK61SsDZN4MFTFehOOaJ4rHxa1cW8Lwdn2/6tOx7N3+SY/vfcjztOHopA==",
"dependencies": {
"@grpc/proto-loader": "^0.6.4",
"@types/node": ">=12.12.47"
@ -1689,9 +1689,9 @@
}
},
"node_modules/keyv": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.2.0.tgz",
"integrity": "sha512-mcRm0yqY2Z2FdE3Tkb7hNEUN7J7VdedNZ8F6vS5jX04gNo2pyOWqfyW+chW9amiS3gbULPucyRzVq0gjPUmhTA==",
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.2.1.tgz",
"integrity": "sha512-cAJq5cTfxQdq1DHZEVNpnk4mEvhP+8UP8UQftLtTtJ98beKkRHf+62M0mIDM2u/IWXyP8bmGB375/6uGdSX2MA==",
"dependencies": {
"compress-brotli": "^1.3.6",
"json-buffer": "3.0.1"
@ -2843,9 +2843,9 @@
}
},
"node_modules/winston": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/winston/-/winston-3.6.0.tgz",
"integrity": "sha512-9j8T75p+bcN6D00sF/zjFVmPp+t8KMPB1MzbbzYjeN9VWxdsYnTB40TkbNUEXAmILEfChMvAMgidlX64OG3p6w==",
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/winston/-/winston-3.7.2.tgz",
"integrity": "sha512-QziIqtojHBoyzUOdQvQiar1DH0Xp9nF1A1y7NVy2DGEsz82SBDtOalS0ulTRGVT14xPX3WRWkCsdcJKqNflKng==",
"dependencies": {
"@dabh/diagnostics": "^2.0.2",
"async": "^3.2.3",
@ -3008,9 +3008,9 @@
}
},
"@grpc/grpc-js": {
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.6.1.tgz",
"integrity": "sha512-ix3rQS64rKL1s6CfIaRgnts+RNYZZ2NaYyTK7iimai6an/0GGDbukzy990hJ5vtKHjhaqJxJMB6Qq7BMZ0zZSQ==",
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.6.2.tgz",
"integrity": "sha512-9+89Ne1K8F9u86T+l1yIV2DS+dWHYVK61SsDZN4MFTFehOOaJ4rHxa1cW8Lwdn2/6tOx7N3+SY/vfcjztOHopA==",
"requires": {
"@grpc/proto-loader": "^0.6.4",
"@types/node": ">=12.12.47"
@ -4278,9 +4278,9 @@
}
},
"keyv": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.2.0.tgz",
"integrity": "sha512-mcRm0yqY2Z2FdE3Tkb7hNEUN7J7VdedNZ8F6vS5jX04gNo2pyOWqfyW+chW9amiS3gbULPucyRzVq0gjPUmhTA==",
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.2.1.tgz",
"integrity": "sha512-cAJq5cTfxQdq1DHZEVNpnk4mEvhP+8UP8UQftLtTtJ98beKkRHf+62M0mIDM2u/IWXyP8bmGB375/6uGdSX2MA==",
"requires": {
"compress-brotli": "^1.3.6",
"json-buffer": "3.0.1"
@ -5147,9 +5147,9 @@
}
},
"winston": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/winston/-/winston-3.6.0.tgz",
"integrity": "sha512-9j8T75p+bcN6D00sF/zjFVmPp+t8KMPB1MzbbzYjeN9VWxdsYnTB40TkbNUEXAmILEfChMvAMgidlX64OG3p6w==",
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/winston/-/winston-3.7.2.tgz",
"integrity": "sha512-QziIqtojHBoyzUOdQvQiar1DH0Xp9nF1A1y7NVy2DGEsz82SBDtOalS0ulTRGVT14xPX3WRWkCsdcJKqNflKng==",
"requires": {
"@dabh/diagnostics": "^2.0.2",
"async": "^3.2.3",

View File

@ -1,6 +1,6 @@
{
"name": "democratic-csi",
"version": "1.6.1",
"version": "1.6.2",
"description": "kubernetes csi driver framework",
"main": "bin/democratic-csi",
"scripts": {

View File

@ -3,8 +3,10 @@ const http = require("http");
const https = require("https");
const { axios_request, stringify } = require("../../../utils/general");
const Mutex = require("async-mutex").Mutex;
const registry = require("../../../utils/registry");
const USER_AGENT = "democratic-csi";
const __REGISTRY_NS__ = "SynologyHttpClient";
class SynologyHttpClient {
constructor(options = {}) {
@ -22,27 +24,23 @@ class SynologyHttpClient {
}
getHttpAgent() {
if (!this.httpAgent) {
this.httpAgent = new http.Agent({
return registry.get(`${__REGISTRY_NS__}:http_agent`, () => {
return new http.Agent({
keepAlive: true,
maxSockets: Infinity,
rejectUnauthorized: !!!this.options.allowInsecure,
});
}
return this.httpAgent;
});
}
getHttpsAgent() {
if (!this.httpsAgent) {
this.httpsAgent = new https.Agent({
return registry.get(`${__REGISTRY_NS__}:https_agent`, () => {
return new https.Agent({
keepAlive: true,
maxSockets: Infinity,
rejectUnauthorized: !!!this.options.allowInsecure,
});
}
return this.httpsAgent;
});
}
log_response(error, response, body, options) {

View File

@ -1,9 +1,12 @@
const { CsiBaseDriver } = require("../index");
const { GrpcError, grpc } = require("../../utils/grpc");
const registry = require("../../utils/registry");
const SynologyHttpClient = require("./http").SynologyHttpClient;
const semver = require("semver");
const sleep = require("../../utils/general").sleep;
const __REGISTRY_NS__ = "ControllerSynologyDriver";
/**
*
* Driver to provision storage on a synology device
@ -109,10 +112,9 @@ class ControllerSynologyDriver extends CsiBaseDriver {
}
async getHttpClient() {
if (!this.httpClient) {
this.httpClient = new SynologyHttpClient(this.options.httpConnection);
}
return this.httpClient;
return registry.get(`${__REGISTRY_NS__}:http_client`, () => {
return new SynologyHttpClient(this.options.httpConnection);
});
}
getDriverResourceType() {

View File

@ -1,6 +1,7 @@
const _ = require("lodash");
const { ControllerZfsBaseDriver } = require("../controller-zfs");
const { GrpcError, grpc } = require("../../utils/grpc");
const registry = require("../../utils/registry");
const SshClient = require("../../utils/ssh").SshClient;
const sleep = require("../../utils/general").sleep;
const { Zetabyte, ZfsSshProcessManager } = require("../../utils/zfs");
@ -8,36 +9,40 @@ const { Zetabyte, ZfsSshProcessManager } = require("../../utils/zfs");
const Handlebars = require("handlebars");
const ISCSI_ASSETS_NAME_PROPERTY_NAME = "democratic-csi:iscsi_assets_name";
const __REGISTRY_NS__ = "ControllerZfsGenericDriver";
class ControllerZfsGenericDriver extends ControllerZfsBaseDriver {
getExecClient() {
return new SshClient({
logger: this.ctx.logger,
connection: this.options.sshConnection,
return registry.get(`${__REGISTRY_NS__}:exec_client`, () => {
return new SshClient({
logger: this.ctx.logger,
connection: this.options.sshConnection,
});
});
}
async getZetabyte() {
const execClient = this.getExecClient();
const options = {};
options.executor = new ZfsSshProcessManager(execClient);
options.idempotent = true;
if (
this.options.zfs.hasOwnProperty("cli") &&
this.options.zfs.cli &&
this.options.zfs.cli.hasOwnProperty("paths")
) {
options.paths = this.options.zfs.cli.paths;
}
options.sudo = _.get(this.options, "zfs.cli.sudoEnabled", false);
if (typeof this.setZetabyteCustomOptions === "function") {
await this.setZetabyteCustomOptions(options);
}
return new Zetabyte(options);
return registry.get(`${__REGISTRY_NS__}:zb`, () => {
const execClient = this.getExecClient();
const options = {};
options.executor = new ZfsSshProcessManager(execClient);
options.idempotent = true;
if (
this.options.zfs.hasOwnProperty("cli") &&
this.options.zfs.cli &&
this.options.zfs.cli.hasOwnProperty("paths")
) {
options.paths = this.options.zfs.cli.paths;
}
options.sudo = _.get(this.options, "zfs.cli.sudoEnabled", false);
if (typeof this.setZetabyteCustomOptions === "function") {
await this.setZetabyteCustomOptions(options);
}
return new Zetabyte(options);
});
}
/**
@ -362,7 +367,9 @@ delete ${iscsiName}
taregetCliCommand.push("|");
taregetCliCommand.push("targetcli");
if (_.get(this.options, "iscsi.shareStrategyTargetCli.sudoEnabled", false)) {
if (
_.get(this.options, "iscsi.shareStrategyTargetCli.sudoEnabled", false)
) {
command = "sudo";
args.unshift("sh");
}

View File

@ -2,12 +2,14 @@ const _ = require("lodash");
const { ControllerZfsBaseDriver } = require("../controller-zfs");
const { GrpcError, grpc } = require("../../utils/grpc");
const LocalCliExecClient = require("./exec").LocalCliClient;
const os = require("os");
const registry = require("../../utils/registry");
const { Zetabyte } = require("../../utils/zfs");
const ZFS_ASSET_NAME_PROPERTY_NAME = "zfs_asset_name";
const NODE_TOPOLOGY_KEY_NAME = "org.democratic-csi.topology/node";
const __REGISTRY_NS__ = "ControllerZfsLocalDriver";
class ControllerZfsLocalDriver extends ControllerZfsBaseDriver {
constructor(ctx, options) {
const i_caps = _.get(
@ -29,43 +31,47 @@ class ControllerZfsLocalDriver extends ControllerZfsBaseDriver {
}
getExecClient() {
return new LocalCliExecClient({
logger: this.ctx.logger,
});
return registry.get(`${__REGISTRY_NS__}:exec_client`, () => {
return new LocalCliExecClient({
logger: this.ctx.logger,
});
})
}
async getZetabyte() {
const execClient = this.getExecClient();
return registry.get(`${__REGISTRY_NS__}:zb`, () => {
const execClient = this.getExecClient();
const options = {};
options.executor = execClient;
options.idempotent = true;
/*
if (
this.options.zfs.hasOwnProperty("cli") &&
this.options.zfs.cli &&
this.options.zfs.cli.hasOwnProperty("paths")
) {
options.paths = this.options.zfs.cli.paths;
}
*/
// use env based paths to allow for custom wrapper scripts to chroot to the host
options.paths = {
zfs: "zfs",
zpool: "zpool",
sudo: "sudo",
chroot: "chroot",
};
options.sudo = _.get(this.options, "zfs.cli.sudoEnabled", false);
if (typeof this.setZetabyteCustomOptions === "function") {
await this.setZetabyteCustomOptions(options);
}
return new Zetabyte(options);
const options = {};
options.executor = execClient;
options.idempotent = true;
/*
if (
this.options.zfs.hasOwnProperty("cli") &&
this.options.zfs.cli &&
this.options.zfs.cli.hasOwnProperty("paths")
) {
options.paths = this.options.zfs.cli.paths;
}
*/
// use env based paths to allow for custom wrapper scripts to chroot to the host
options.paths = {
zfs: "zfs",
zpool: "zpool",
sudo: "sudo",
chroot: "chroot",
};
options.sudo = _.get(this.options, "zfs.cli.sudoEnabled", false);
if (typeof this.setZetabyteCustomOptions === "function") {
await this.setZetabyteCustomOptions(options);
}
return new Zetabyte(options);
});
}
/**

View File

@ -5,6 +5,7 @@ const HttpClient = require("./http").Client;
const TrueNASApiClient = require("./http/api").Api;
const { Zetabyte } = require("../../utils/zfs");
const getLargestNumber = require("../../utils/general").getLargestNumber;
const registry = require("../../utils/registry");
const sleep = require("../../utils/general").sleep;
const Handlebars = require("handlebars");
@ -44,6 +45,8 @@ const VOLUME_CONTEXT_PROVISIONER_DRIVER_PROPERTY_NAME =
const VOLUME_CONTEXT_PROVISIONER_INSTANCE_ID_PROPERTY_NAME =
"democratic-csi:volume_context_provisioner_instance_id";
const __REGISTRY_NS__ = "FreeNASApiDriver";
class FreeNASApiDriver extends CsiBaseDriver {
constructor(ctx, options) {
super(...arguments);
@ -154,14 +157,16 @@ class FreeNASApiDriver extends CsiBaseDriver {
* @returns
*/
async getZetabyte() {
return new Zetabyte({
executor: {
spawn: function () {
throw new Error(
"cannot use the zb implementation to execute zfs commands, must use the http api"
);
return registry.get(`${__REGISTRY_NS__}:zb`, () => {
return new Zetabyte({
executor: {
spawn: function () {
throw new Error(
"cannot use the zb implementation to execute zfs commands, must use the http api"
);
},
},
},
});
});
}
@ -1887,11 +1892,12 @@ class FreeNASApiDriver extends CsiBaseDriver {
}
async getHttpClient() {
const client = new HttpClient(this.options.httpConnection);
client.logger = this.ctx.logger;
client.setApiVersion(2); // requires version 2
return client;
return registry.get(`${__REGISTRY_NS__}:http_client`, () => {
const client = new HttpClient(this.options.httpConnection);
client.logger = this.ctx.logger;
client.setApiVersion(2); // requires version 2
return client;
});
}
async getMinimumVolumeSize() {
@ -1903,11 +1909,10 @@ class FreeNASApiDriver extends CsiBaseDriver {
}
async getTrueNASHttpApiClient() {
const driver = this;
const httpClient = await this.getHttpClient();
const apiClient = new TrueNASApiClient(httpClient, driver.ctx.cache);
return apiClient;
return registry.get(`${__REGISTRY_NS__}:api_client`, () => {
const httpClient = await this.getHttpClient();
return new TrueNASApiClient(httpClient, this.ctx.cache);
});
}
assertCapabilities(capabilities) {

View File

@ -1,8 +1,10 @@
const registry = require("../../../utils/registry");
const { sleep, stringify } = require("../../../utils/general");
const { Zetabyte } = require("../../../utils/zfs");
// used for in-memory cache of the version info
const FREENAS_SYSTEM_VERSION_CACHE_KEY = "freenas:system_version";
const __REGISTRY_NS__ = "FreeNASHttpApi";
class Api {
constructor(client, cache, options = {}) {
@ -20,14 +22,16 @@ class Api {
* @returns
*/
async getZetabyte() {
return new Zetabyte({
executor: {
spawn: function () {
throw new Error(
"cannot use the zb implementation to execute zfs commands, must use the http api"
);
return registry.get(`${__REGISTRY_NS__}:zb`, () => {
return new Zetabyte({
executor: {
spawn: function () {
throw new Error(
"cannot use the zb implementation to execute zfs commands, must use the http api"
);
},
},
},
});
});
}

View File

@ -1,6 +1,7 @@
const _ = require("lodash");
const { ControllerZfsBaseDriver } = require("../controller-zfs");
const { GrpcError, grpc } = require("../../utils/grpc");
const registry = require("../../utils/registry");
const SshClient = require("../../utils/ssh").SshClient;
const HttpClient = require("./http").Client;
const { Zetabyte, ZfsSshProcessManager } = require("../../utils/zfs");
@ -22,35 +23,41 @@ const FREENAS_ISCSI_ASSETS_NAME_PROPERTY_NAME =
// used for in-memory cache of the version info
const FREENAS_SYSTEM_VERSION_CACHE_KEY = "freenas:system_version";
const __REGISTRY_NS__ = "FreeNASSshDriver";
class FreeNASSshDriver extends ControllerZfsBaseDriver {
getExecClient() {
return new SshClient({
logger: this.ctx.logger,
connection: this.options.sshConnection,
return registry.get(`${__REGISTRY_NS__}:exec_client`, () => {
return new SshClient({
logger: this.ctx.logger,
connection: this.options.sshConnection,
});
});
}
async getZetabyte() {
const sshClient = this.getExecClient();
const options = {};
options.executor = new ZfsSshProcessManager(sshClient);
options.idempotent = true;
if (
this.options.zfs.hasOwnProperty("cli") &&
this.options.zfs.cli &&
this.options.zfs.cli.hasOwnProperty("paths")
) {
options.paths = this.options.zfs.cli.paths;
}
options.sudo = _.get(this.options, "zfs.cli.sudoEnabled", false);
if (typeof this.setZetabyteCustomOptions === "function") {
await this.setZetabyteCustomOptions(options);
}
return new Zetabyte(options);
return registry.get(`${__REGISTRY_NS__}:zb`, () => {
const sshClient = this.getExecClient();
const options = {};
options.executor = new ZfsSshProcessManager(sshClient);
options.idempotent = true;
if (
this.options.zfs.hasOwnProperty("cli") &&
this.options.zfs.cli &&
this.options.zfs.cli.hasOwnProperty("paths")
) {
options.paths = this.options.zfs.cli.paths;
}
options.sudo = _.get(this.options, "zfs.cli.sudoEnabled", false);
if (typeof this.setZetabyteCustomOptions === "function") {
await this.setZetabyteCustomOptions(options);
}
return new Zetabyte(options);
});
}
/**
@ -88,15 +95,19 @@ class FreeNASSshDriver extends ControllerZfsBaseDriver {
}
async getHttpClient(autoDetectVersion = true) {
const client = new HttpClient(this.options.httpConnection);
client.logger = this.ctx.logger;
if (autoDetectVersion && !!!this.options.httpConnection.apiVersion) {
const apiVersion = await this.getApiVersion();
client.setApiVersion(apiVersion);
}
return client;
const autodetectkey = autoDetectVersion === true ? 1 : 0
return registry.get(`${__REGISTRY_NS__}:http_client:autoDetectVersion_${autodetectkey}`, () => {
const client = new HttpClient(this.options.httpConnection);
client.logger = this.ctx.logger;
if (autoDetectVersion && !!!this.options.httpConnection.apiVersion) {
const apiVersion = await this.getApiVersion();
client.setApiVersion(apiVersion);
}
return client;
});
}
getDriverShareType() {

View File

@ -7,10 +7,13 @@ const { Mount } = require("../utils/mount");
const { OneClient } = require("../utils/oneclient");
const { Filesystem } = require("../utils/filesystem");
const { ISCSI } = require("../utils/iscsi");
const registry = require("../utils/registry");
const semver = require("semver");
const sleep = require("../utils/general").sleep;
const { Zetabyte } = require("../utils/zfs");
const __REGISTRY_NS__ = "CsiBaseDriver";
/**
* common code shared between all drivers
* this is **NOT** meant to work as a proxy
@ -93,6 +96,71 @@ class CsiBaseDriver {
return normalized;
}
/**
* Get an instance of the Filesystem class
*
* @returns Filesystem
*/
getDefaultFilesystemInstance() {
return registry.get(
`${__REGISTRY_NS__}:default_filesystem_instance`,
() => {
return new Filesystem();
}
);
}
/**
* Get an instance of the Mount class
*
* @returns Mount
*/
getDefaultMountInstance() {
return registry.get(`${__REGISTRY_NS__}:default_mount_instance`, () => {
const filesystem = this.getDefaultFilesystemInstance();
return new Mount({ filesystem });
});
}
/**
* Get an instance of the ISCSI class
*
* @returns ISCSI
*/
getDefaultISCSIInstance() {
return registry.get(`${__REGISTRY_NS__}:default_iscsi_instance`, () => {
return new ISCSI();
});
}
getDefaultZetabyteInstance() {
return registry.get(`${__REGISTRY_NS__}:default_zb_instance`, () => {
return new Zetabyte({
idempotent: true,
paths: {
zfs: "zfs",
zpool: "zpool",
sudo: "sudo",
chroot: "chroot",
},
//logger: driver.ctx.logger,
executor: {
spawn: function () {
const command = `${arguments[0]} ${arguments[1].join(" ")}`;
return cp.exec(command);
},
},
log_commands: true,
});
});
}
getDefaultOneClientInstance() {
return registry.get(`${__REGISTRY_NS__}:default_oneclient_instance`, () => {
return new OneClient();
});
}
async GetPluginInfo(call) {
return {
name: this.ctx.args.csiName,
@ -276,9 +344,9 @@ class CsiBaseDriver {
*/
async NodeStageVolume(call) {
const driver = this;
const mount = new Mount();
const filesystem = new Filesystem();
const iscsi = new ISCSI();
const mount = driver.getDefaultMountInstance();
const filesystem = driver.getDefaultFilesystemInstance();
const iscsi = driver.getDefaultISCSIInstance();
let result;
let device;
@ -590,7 +658,7 @@ class CsiBaseDriver {
break;
case "oneclient":
let oneclient = new OneClient();
let oneclient = driver.getDefaultOneClientInstance();
device = "oneclient";
result = await mount.deviceIsMountedAtPath(device, staging_target_path);
if (result) {
@ -634,23 +702,7 @@ class CsiBaseDriver {
break;
case "zfs-local":
// TODO: make this a geneic zb instance (to ensure works with node-manual driver)
const zb = new Zetabyte({
idempotent: true,
paths: {
zfs: "zfs",
zpool: "zpool",
sudo: "sudo",
chroot: "chroot",
},
//logger: driver.ctx.logger,
executor: {
spawn: function () {
const command = `${arguments[0]} ${arguments[1].join(" ")}`;
return cp.exec(command);
},
},
log_commands: true,
});
const zb = driver.getDefaultZetabyteInstance();
result = await zb.zfs.get(`${volume_context.zfs_asset_name}`, [
"type",
"mountpoint",
@ -861,9 +913,9 @@ class CsiBaseDriver {
*/
async NodeUnstageVolume(call) {
const driver = this;
const mount = new Mount();
const filesystem = new Filesystem();
const iscsi = new ISCSI();
const mount = driver.getDefaultMountInstance();
const filesystem = driver.getDefaultFilesystemInstance();
const iscsi = driver.getDefaultISCSIInstance();
let result;
let is_block = false;
let is_device_mapper = false;
@ -1081,8 +1133,8 @@ class CsiBaseDriver {
async NodePublishVolume(call) {
const driver = this;
const mount = new Mount();
const filesystem = new Filesystem();
const mount = driver.getDefaultMountInstance();
const filesystem = driver.getDefaultFilesystemInstance();
let result;
const volume_id = call.request.volume_id;
@ -1236,8 +1288,8 @@ class CsiBaseDriver {
async NodeUnpublishVolume(call) {
const driver = this;
const mount = new Mount();
const filesystem = new Filesystem();
const mount = driver.getDefaultMountInstance();
const filesystem = driver.getDefaultFilesystemInstance();
let result;
const volume_id = call.request.volume_id;
@ -1316,8 +1368,8 @@ class CsiBaseDriver {
async NodeGetVolumeStats(call) {
const driver = this;
const mount = new Mount();
const filesystem = new Filesystem();
const mount = driver.getDefaultMountInstance();
const filesystem = driver.getDefaultFilesystemInstance();
let result;
let device_path;
let access_type;
@ -1414,8 +1466,9 @@ class CsiBaseDriver {
* @param {*} call
*/
async NodeExpandVolume(call) {
const mount = new Mount();
const filesystem = new Filesystem();
const driver = this;
const mount = driver.getDefaultMountInstance();
const filesystem = driver.getDefaultFilesystemInstance();
let device;
let fs_info;
let device_path;

View File

@ -2,6 +2,7 @@ const fs = require("fs");
const { CsiBaseDriver } = require("../index");
const { GrpcError, grpc } = require("../../utils/grpc");
const { Filesystem } = require("../../utils/filesystem");
const registry = require("../../utils/registry");
const semver = require("semver");
const SshClient = require("../../utils/ssh").SshClient;
const { Zetabyte, ZfsSshProcessManager } = require("../../utils/zfs");
@ -14,6 +15,7 @@ const VOLUME_CONTEXT_PROVISIONER_DRIVER_PROPERTY_NAME =
"democratic-csi:volume_context_provisioner_driver";
const VOLUME_CONTEXT_PROVISIONER_INSTANCE_ID_PROPERTY_NAME =
"democratic-csi:volume_context_provisioner_instance_id";
const __REGISTRY_NS__ = "ZfsLocalEphemeralInlineDriver";
/**
* https://github.com/kubernetes/enhancements/blob/master/keps/sig-storage/20190122-csi-inline-volumes.md
@ -123,27 +125,31 @@ class ZfsLocalEphemeralInlineDriver extends CsiBaseDriver {
}
getSshClient() {
return new SshClient({
logger: this.ctx.logger,
connection: this.options.sshConnection,
return registry.get(`${__REGISTRY_NS__}:ssh_client`, () => {
return new SshClient({
logger: this.ctx.logger,
connection: this.options.sshConnection,
});
});
}
getZetabyte() {
let sshClient;
let executor;
if (this.options.sshConnection) {
sshClient = this.getSshClient();
executor = new ZfsSshProcessManager(sshClient);
}
return new Zetabyte({
executor,
idempotent: true,
chroot: this.options.zfs.chroot,
paths: {
zpool: "/usr/sbin/zpool",
zfs: "/usr/sbin/zfs",
},
return registry.get(`${__REGISTRY_NS__}:zb`, () => {
let sshClient;
let executor;
if (this.options.sshConnection) {
sshClient = this.getSshClient();
executor = new ZfsSshProcessManager(sshClient);
}
return new Zetabyte({
executor,
idempotent: true,
chroot: this.options.zfs.chroot,
paths: {
zpool: "/usr/sbin/zpool",
zfs: "/usr/sbin/zfs",
},
});
});
}

View File

@ -610,17 +610,17 @@ class Filesystem {
const filesystem = this;
args = args || [];
let stdout = "";
let stderr = "";
if (filesystem.options.sudo) {
args.unshift(command);
command = filesystem.options.paths.sudo;
}
console.log("executing filesystem command: %s %s", command, args.join(" "));
const child = filesystem.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = filesystem.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});

View File

@ -523,17 +523,18 @@ class ISCSI {
const iscsi = this;
args = args || [];
let stdout = "";
let stderr = "";
if (iscsi.options.sudo) {
args.unshift(command);
command = iscsi.options.paths.sudo;
}
console.log("executing iscsi command: %s %s", command, args.join(" "));
const child = iscsi.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = iscsi.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});

View File

@ -1,3 +1,5 @@
const os = require("os");
/**
* Levels
*
@ -27,7 +29,7 @@ let formatters;
let defaultMeta;
if (env == "production") {
formatters = [winston.format.json()];
defaultMeta = { service: "democratic-csi" };
defaultMeta = { service: "democratic-csi", host: os.hostname() };
} else {
formatters = [winston.format.colorize(), winston.format.simple()];
defaultMeta = {};
@ -36,6 +38,7 @@ if (env == "production") {
const logger = winston.createLogger({
level: level,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.splat(),
...formatters
@ -43,9 +46,9 @@ const logger = winston.createLogger({
defaultMeta: defaultMeta,
transports: [
new winston.transports.Console({
handleExceptions: true
})
]
handleExceptions: true,
}),
],
});
/**
@ -102,9 +105,9 @@ var shim = bunyan.createLogger({
{
type: "raw",
level: "trace",
stream: new Bunyan2Winston(logger)
}
]
stream: new Bunyan2Winston(logger),
},
],
});
logger.bunyan = shim;
@ -112,5 +115,5 @@ logger.bunyan = shim;
//global.console = logger;
module.exports = {
logger: logger
logger: logger,
};

View File

@ -2,7 +2,7 @@ const cp = require("child_process");
const { Filesystem } = require("../utils/filesystem");
// avoid using avail,size,used as it causes hangs when the fs is stale
FINDMNT_COMMON_OPTIONS = [
const FINDMNT_COMMON_OPTIONS = [
"--output",
"source,target,fstype,label,options",
"-b",
@ -43,6 +43,14 @@ class Mount {
spawn: cp.spawn,
};
}
if (!options.filesystem) {
options.filesystem = new Filesystem();
}
}
async getFilesystemInstance() {
return this.options.filesystem;
}
/**
@ -51,12 +59,12 @@ class Mount {
* @param {*} device
*/
async deviceIsMounted(device) {
const filesystem = new Filesystem();
const mount = this;
const filesystem = await mount.getFilesystemInstance();
if (device.startsWith("/")) {
device = await filesystem.realpath(device);
}
const mount = this;
let args = [];
args = args.concat(["--source", device]);
args = args.concat(FINDMNT_COMMON_OPTIONS);
@ -114,12 +122,12 @@ class Mount {
* @param {*} device
*/
async deviceIsMountedAtPath(device, path) {
const filesystem = new Filesystem();
const mount = this;
const filesystem = await mount.getFilesystemInstance();
if (device.startsWith("/") && !device.startsWith("//")) {
device = await filesystem.realpath(device);
}
const mount = this;
let args = [];
args = args.concat(["--source", device]);
args = args.concat(["--mountpoint", path]);
@ -282,8 +290,8 @@ class Mount {
* @param {*} path
*/
async isBindMountedBlockDevice(path) {
const filesystem = new Filesystem();
const mount = this;
const filesystem = await mount.getFilesystemInstance();
const is_mounted = await mount.pathIsMounted(path);
if (!is_mounted) {
@ -385,9 +393,6 @@ class Mount {
const mount = this;
args = args || [];
let stdout = "";
let stderr = "";
if (mount.options.sudo) {
args.unshift(command);
command = mount.options.paths.sudo;
@ -402,9 +407,13 @@ class Mount {
);
console.log("executing mount command: %s", cleansedLog);
const child = mount.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = mount.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});

View File

@ -77,9 +77,6 @@ class OneClient {
const oneclient = this;
args = args || [];
let stdout = "";
let stderr = "";
if (oneclient.options.sudo) {
args.unshift(command);
command = oneclient.options.paths.sudo;
@ -93,9 +90,13 @@ class OneClient {
);
console.log("executing oneclient command: %s", cleansedLog);
const child = oneclient.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = oneclient.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});

37
src/utils/registry.js Normal file
View File

@ -0,0 +1,37 @@
class Registry {
constructor() {
this.data = {};
}
put(key, value) {
if (!value) {
delete this.data[key];
return;
}
this.data[key] = value;
}
get(key, initialValue = null) {
const val = this.data[key];
if (val) {
return val;
}
if (typeof initialValue == "function") {
initialValue = initialValue();
}
if (initialValue) {
this.put(key, initialValue);
return this.data[key];
}
}
delete(key) {
delete this.data[key];
}
}
const registry = new Registry();
module.exports = registry;