From fbdc592fbae30820fa3a9d01562dce194e3f378c Mon Sep 17 00:00:00 2001 From: Travis Glenn Hansen Date: Wed, 13 Jan 2021 21:15:10 -0700 Subject: [PATCH] fix targetCli when using nameTemplate --- src/driver/controller-zfs-generic/index.js | 71 ++++++++++++++++++---- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/src/driver/controller-zfs-generic/index.js b/src/driver/controller-zfs-generic/index.js index 0f20336..71173cf 100644 --- a/src/driver/controller-zfs-generic/index.js +++ b/src/driver/controller-zfs-generic/index.js @@ -3,6 +3,8 @@ const { GrpcError, grpc } = require("../../utils/grpc"); const Handlebars = require("handlebars"); +const ISCSI_ASSETS_NAME_PROPERTY_NAME = "democratic-csi:iscsi_assets_name"; + class ControllerZfsGenericDriver extends ControllerZfsSshBaseDriver { /** * cannot make this a storage class parameter as storage class/etc context is *not* sent @@ -159,6 +161,11 @@ create /backstores/block/${iscsiName} let iqn = basename + ":" + iscsiName; this.ctx.logger.info("iqn: " + iqn); + // store this off to make delete process more bullet proof + await zb.zfs.set(datasetName, { + [ISCSI_ASSETS_NAME_PROPERTY_NAME]: iscsiName, + }); + volume_context = { node_attach_driver: "iscsi", portal: this.options.iscsi.targetPortal, @@ -182,6 +189,7 @@ create /backstores/block/${iscsiName} const sshClient = this.getSshClient(); let response; + let properties; switch (this.options.driver) { case "zfs-generic-nfs": @@ -200,21 +208,35 @@ create /backstores/block/${iscsiName} let basename; let iscsiName; - if (this.options.iscsi.nameTemplate) { - iscsiName = Handlebars.compile(this.options.iscsi.nameTemplate)({ - name: call.request.name, - parameters: call.request.parameters, - }); + // Delete iscsi assets + try { + properties = await zb.zfs.get(datasetName, [ + ISCSI_ASSETS_NAME_PROPERTY_NAME, + ]); + } catch (err) { + if (err.toString().includes("dataset does not exist")) { + return; + } + throw err; + } + + properties = properties[datasetName]; + this.ctx.logger.debug("zfs props data: %j", properties); + + iscsiName = properties[ISCSI_ASSETS_NAME_PROPERTY_NAME].value; + + if (zb.helpers.isPropertyValueSet(iscsiName)) { + //do nothing } else { iscsiName = zb.helpers.extractLeafName(datasetName); - } - if (this.options.iscsi.namePrefix) { - iscsiName = this.options.iscsi.namePrefix + iscsiName; - } + if (this.options.iscsi.namePrefix) { + iscsiName = this.options.iscsi.namePrefix + iscsiName; + } - if (this.options.iscsi.nameSuffix) { - iscsiName += this.options.iscsi.nameSuffix; + if (this.options.iscsi.nameSuffix) { + iscsiName += this.options.iscsi.nameSuffix; + } } iscsiName = iscsiName.toLowerCase(); @@ -223,9 +245,11 @@ create /backstores/block/${iscsiName} basename = this.options.iscsi.shareStrategyTargetCli.basename; response = await this.targetCliCommand( ` +# delete target cd /iscsi delete ${basename}:${iscsiName} +# delete extent cd /backstores/block delete ${iscsiName} ` @@ -268,6 +292,8 @@ delete ${iscsiName} async targetCliCommand(data) { const sshClient = this.getSshClient(); + const driver = this; + data = data.trim(); let command = "sh"; @@ -284,7 +310,28 @@ delete ${iscsiName} args.push("'" + taregetCliCommand.join(" ") + "'"); - return sshClient.exec(sshClient.buildCommand(command, args)); + let logCommandTmp = command + " " + args.join(" "); + let logCommand = ""; + + logCommandTmp.split("\n").forEach((line) => { + if (line.startsWith("set auth password=")) { + logCommand += "set auth password="; + } else if (line.startsWith("set auth mutual_password=")) { + logCommand += "set auth mutual_password="; + } else { + logCommand += line; + } + + logCommand += "\n"; + }); + + driver.ctx.logger.verbose("TargetCLI command: " + logCommand); + + let response = await sshClient.exec(sshClient.buildCommand(command, args)); + driver.ctx.logger.verbose( + "TargetCLI response: " + JSON.stringify(response) + ); + return response; } }