Skip to content

Commit

Permalink
Adapt code to k3s v1.29.4+k3s1
Browse files Browse the repository at this point in the history
The output of `k3s etcd-snapshot ls` has been changed with k3s-io/k3s#9816.

Signed-off-by: Volker Theile <votdev@gmx.de>
  • Loading branch information
votdev committed May 11, 2024
1 parent e93c43b commit bd49301
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
7 changes: 7 additions & 0 deletions deb/openmediavault-k8s/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
openmediavault-k8s (7.1.1-1) unstable; urgency=low

* Adapt code to changes to the `k3s etcd-snapshot` sub-command
introduced in v1.29.4+k3s1.

-- Volker Theile <volker.theile@openmediavault.org> Sat, 11 May 2024 21:17:22 +0200

openmediavault-k8s (7.1.0-4) unstable; urgency=low

* Use a PHP template engine to implement a DSL that allows users
Expand Down
4 changes: 4 additions & 0 deletions deb/openmediavault-k8s/debian/openmediavault-k8s.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ case "$1" in
# Action to be done depending on the previous version.
########################################################################
echo "Updating configuration ..."
if dpkg --compare-versions "$2" lt-nl "7.1.1"; then
touch /var/lib/openmediavault/upgrade_k3s || :
omv_module_set_dirty k3s
fi
;;

abort-upgrade|abort-remove|abort-deconfigure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,42 @@ class OMVRpcServiceEtcd extends \OMV\Rpc\ServiceAbstract {
$this->registerMethod("restoreSnapshot");
}

function getSnapshots($params, $context) {
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
$this->validateMethodParams($params, "rpc.common.getlist");
private function getSnapshotDir(): string {
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.k8s");
$sfObject = $db->get("conf.system.sharedfolder",
$object->get("etcdsnapshots_sharedfolderref"));
$meObject = $db->get("conf.system.filesystem.mountpoint",
$sfObject->get("mntentref"));
$snapshotDir = build_path(DIRECTORY_SEPARATOR,
return build_path(DIRECTORY_SEPARATOR,
$meObject->get("dir"), $sfObject->get("reldirpath"));
}

function getSnapshots($params, $context) {
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
$this->validateMethodParams($params, "rpc.common.getlist");
$cmdArgs = [];
$cmdArgs[] = "etcd-snapshot";
$cmdArgs[] = "ls";
$cmdArgs[] = sprintf("--etcd-snapshot-dir %s", escapeshellarg(
$snapshotDir));
$this->getSnapshotDir()));
$cmdArgs[] = "--output json";
$cmd = new \OMV\System\Process("k3s", $cmdArgs);
$cmd->setRedirect2toFile("/dev/null");
$cmd->execute($output);
$output = json_decode_safe(implode("", $output), TRUE);
$output = array_value($output, "items", []);
$result = [];
foreach($output as $outputk => $outputv) {
$outputv['createdAtTs'] = strpdate($outputv['createdAt'],
"Y-m-d*H:i:sT");
$outputv['status']['compressed'] = str_ends_with(
$outputv['spec']['snapshotName'], ".zip");
if (is_null($outputv['metadata']['creationTimestamp'])) {
$outputv['metadata']['creationTimestamp'] = strpdate(
$outputv['status']['creationTime'],
"Y-m-d*H:i:sT");
}
$result[] = $outputv;
}
return $this->applyFilter($result, $params['start'],
Expand All @@ -72,65 +81,67 @@ class OMVRpcServiceEtcd extends \OMV\Rpc\ServiceAbstract {
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.k8s");
$sfObject = $db->get("conf.system.sharedfolder",
$object->get("etcdsnapshots_sharedfolderref"));
$meObject = $db->get("conf.system.filesystem.mountpoint",
$sfObject->get("mntentref"));
$snapshotDir = build_path(DIRECTORY_SEPARATOR,
$meObject->get("dir"), $sfObject->get("reldirpath"));
$cmdArgs = [];
$cmdArgs[] = "etcd-snapshot";
$cmdArgs[] = "save";
$cmdArgs[] = sprintf("--etcd-snapshot-dir %s", escapeshellarg(
$snapshotDir));
$this->getSnapshotDir()));
$cmdArgs[] = "--snapshot-compress";
$cmd = new \OMV\System\Process("k3s", $cmdArgs);
$cmd->setRedirect2to1();
$cmd->execute($output);
$cmd->execute();
}

function deleteSnapshot($params, $context) {
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
$this->validateMethodParams($params, "rpc.etcd.deleteSnapshot");
$snapshots = \OMV\Rpc\Rpc::call("Etcd", "getSnapshots",
$response = \OMV\Rpc\Rpc::call("Etcd", "getSnapshots",
[ "start" => 0, "limit" => -1 ], $context);
$snapshot = array_search_ex($snapshots['data'], "name",
$snapshots = array_map("array_flatten", $response['data']);
$snapshot = array_search_ex($snapshots, "spec.snapshotName",
$params['name']);
if (FALSE === $snapshot) {
throw new \OMV\Exception("The snapshot '%s' does not exist",
$params['name']);
}
unlink($snapshot['location']);
$cmdArgs = [];
$cmdArgs[] = "etcd-snapshot";
$cmdArgs[] = "delete";
$cmdArgs[] = sprintf("--etcd-snapshot-dir %s", escapeshellarg(
$this->getSnapshotDir()));
$cmdArgs[] = escapeshellarg($params['name']);
$cmd = new \OMV\System\Process("k3s", $cmdArgs);
$cmd->setRedirect2to1();
$cmd->execute();
}

function restoreSnapshot($params, $context) {
$this->validateMethodContext($context, [
"role" => OMV_ROLE_ADMINISTRATOR
]);
$this->validateMethodParams($params, "rpc.etcd.restoreSnapshot");
$snapshots = \OMV\Rpc\Rpc::call("Etcd", "getSnapshots",
$response = \OMV\Rpc\Rpc::call("Etcd", "getSnapshots",
[ "start" => 0, "limit" => -1 ], $context);
$snapshot = array_search_ex($snapshots['data'], "name",
$snapshots = array_map("array_flatten", $response['data']);
$snapshot = array_search_ex($snapshots, "spec.snapshotName",
$params['name']);
if (FALSE === $snapshot) {
throw new \OMV\Exception("The snapshot '%s' does not exist",
$params['name']);
}
$systemCtl = new \OMV\System\SystemCtl("k3s");
$systemCtl->stop();
$location = str_replace("file://", "", $snapshot['location']);
$location = str_replace("file://", "", $snapshot['spec']['location']);
$cmdArgs = [];
$cmdArgs[] = "server";
$cmdArgs[] = "--cluster-reset";
$cmdArgs[] = sprintf("--cluster-reset-restore-path=%s",
escapeshellarg($location));
$cmd = new \OMV\System\Process("k3s", $cmdArgs);
$cmd->setRedirect2to1();
$cmd->execute($output);
$cmd->execute();
$systemCtl->start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ data:
selectionType: single
columns:
- name: _("Name")
prop: name
prop: spec.snapshotName
flexGrow: 1
sortable: true
- name: _("Created")
prop: createdAtTs
prop: metadata.creationTimestamp
flexGrow: 1
sortable: true
cellTemplateName: localeDateTime
- name: _("Size")
prop: size
prop: status.size
flexGrow: 1
sortable: true
cellTemplateName: binaryUnit
- name: _("Compressed")
prop: compressed
prop: status.compressed
flexGrow: 1
sortable: true
cellTemplateName: checkIcon
hidden: true
- name: _("Ready")
prop: status.readyToUse
flexGrow: 1
sortable: true
cellTemplateName: checkIcon
Expand Down Expand Up @@ -61,13 +67,13 @@ data:
service: Etcd
method: restoreSnapshot
params:
name: "{{ name }}"
successNotification: _("The snapshot {{ name }} has been successfully restored.")
name: "{{ spec.snapshotName }}"
successNotification: _("The snapshot {{ spec.snapshotName }} has been successfully restored.")
- template: delete
execute:
type: request
request:
service: Etcd
method: deleteSnapshot
params:
name: "{{ name }}"
name: "{{ spec.snapshotName }}"

0 comments on commit bd49301

Please sign in to comment.