Skip to content

Commit

Permalink
Adapt code to k3s v1.29.4+k3s1 (openmediavault#1765)
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.

To avoid incompatibilities with older installations, these will be upgraded to K3S v1.29.4+k3s1. New installations will use this version as default. This version is now kept as the default as long as no upgrade to an existing version is necessary.

Users can adjust this as required and under their own responsibility via the environment variable `OMV_K8S_K3S_VERSION`.

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

* Upgrade K3S to v1.29.4+k3s1.
* 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
3 changes: 2 additions & 1 deletion deb/openmediavault-k8s/srv/salt/omv/deploy/k3s/default.sls
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# https://kubernetes.io/docs/concepts/storage/volumes/#hostpath-volume-types
# https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistent-volumes

{% set k3s_version = salt['pillar.get']('default:OMV_K8S_K3S_VERSION', 'v1.29.4+k3s1') %}
{% set k8s_config = salt['omv_conf.get']('conf.service.k8s') %}
{% set dns_config = salt['omv_conf.get']('conf.system.network.dns') %}
# {% set email_config = salt['omv_conf.get']('conf.system.notification.email') %}
Expand Down Expand Up @@ -331,7 +332,7 @@ create_k3s_local_storage_manifest:

install_k3s:
cmd.run:
- name: set -o pipefail; wget -O - https://get.k3s.io | INSTALL_K3S_SKIP_ENABLE=true sh -
- name: set -o pipefail; wget -O - https://get.k3s.io | INSTALL_K3S_SKIP_ENABLE=true INSTALL_K3S_VERSION='{{ k3s_version }}' sh -
- shell: /usr/bin/bash
- onlyif: "! which k3s || test -e /var/lib/openmediavault/upgrade_k3s"
- failhard: True
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 f872fd2

Please sign in to comment.