[lxc-devel] [lxd/master] Allow configuring snapshot expiry on create

stgraber on Github lxc-bot at linuxcontainers.org
Mon Feb 18 21:02:50 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190218/24a98813/attachment.bin>
-------------- next part --------------
From aadeda4ce3bfbf2252d79505d088fa071867c5dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 18 Feb 2019 16:00:08 -0500
Subject: [PATCH 1/6] shared/api: Add snapshot expiry configuration on create

---
 shared/api/container_snapshot.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/shared/api/container_snapshot.go b/shared/api/container_snapshot.go
index 057935bcf8..e68a0fb8cc 100644
--- a/shared/api/container_snapshot.go
+++ b/shared/api/container_snapshot.go
@@ -8,6 +8,9 @@ import (
 type ContainerSnapshotsPost struct {
 	Name     string `json:"name" yaml:"name"`
 	Stateful bool   `json:"stateful" yaml:"stateful"`
+
+	// API extension: snapshot_expiry_creation
+	ExpiresAt *time.Time `json:"expires_at" yaml:"expires_at"`
 }
 
 // ContainerSnapshotPost represents the fields required to rename/move a LXD container snapshot

From 594ed2cd9f4ccbf7f2af3b24df7246fa40fd6d51 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 18 Feb 2019 16:00:34 -0500
Subject: [PATCH 2/6] api: Add snapshot_expiry_creation API extension

---
 doc/api-extensions.md | 8 ++++++--
 shared/version/api.go | 1 +
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/doc/api-extensions.md b/doc/api-extensions.md
index 7ba0f853a8..613dbf287f 100644
--- a/doc/api-extensions.md
+++ b/doc/api-extensions.md
@@ -680,12 +680,16 @@ option `snapshots.expiry` takes an expression in the form of `1M 2H 3d 4w 5m
 parts have to be used.
 
 Snapshots which are then created will be given an expiry date based on the
-expression. This expiry date, defined by `expires_at`, can be manually edited
+expression. This expiry date, defined by `expires\_at`, can be manually edited
 using the API or `lxc config edit`. Snapshots with a valid expiry date will be
-removed when the task in run. Expiry can be disabled by setting `expires_at` to
+removed when the task in run. Expiry can be disabled by setting `expires\_at` to
 an empty string or `0001-01-01T00:00:00Z` (zero time). This is the default if
 `snapshots.expiry` is not set.
 
 This adds the following new endpoint (see [RESTful API](rest-api.md) for details):
 
 * `PUT /1.0/containers/<name>/snapshots/<name>`
+
+## snapshot\_expiry\_creation
+Adds `expires\_at` to container creation, allowing for override of a
+snapshot's expiry at creation time.
diff --git a/shared/version/api.go b/shared/version/api.go
index 2ce6924546..f01dc66b22 100644
--- a/shared/version/api.go
+++ b/shared/version/api.go
@@ -138,6 +138,7 @@ var APIExtensions = []string{
 	"container_protection_shift",
 	"snapshot_expiry",
 	"container_backup_override_pool",
+	"snapshot_expiry_creation",
 }
 
 // APIExtensionsCount returns the number of available API extensions.

From 2c1d5d790c9f087840e6fc1c639cc92790e66aef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 18 Feb 2019 16:00:47 -0500
Subject: [PATCH 3/6] client: Add snapshot expiry configuration on create

---
 client/lxd_containers.go | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/client/lxd_containers.go b/client/lxd_containers.go
index 0e3e1cd7a8..9b6efed094 100644
--- a/client/lxd_containers.go
+++ b/client/lxd_containers.go
@@ -1033,6 +1033,11 @@ func (r *ProtocolLXD) GetContainerSnapshot(containerName string, name string) (*
 
 // CreateContainerSnapshot requests that LXD creates a new snapshot for the container
 func (r *ProtocolLXD) CreateContainerSnapshot(containerName string, snapshot api.ContainerSnapshotsPost) (Operation, error) {
+	// Validate the request
+	if snapshot.ExpiresAt != nil && !r.HasExtension("snapshot_expiry_creation") {
+		return nil, fmt.Errorf("The server is missing the required \"snapshot_expiry_creation\" API extension")
+	}
+
 	// Send the request
 	op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/snapshots", url.QueryEscape(containerName)), snapshot, "")
 	if err != nil {

From 004416ab1c612a9908f8e84ee9e8d5c2dbaed8c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 18 Feb 2019 16:00:55 -0500
Subject: [PATCH 4/6] lxd: Add snapshot expiry configuration on create

---
 lxd/container_snapshot.go | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lxd/container_snapshot.go b/lxd/container_snapshot.go
index e1b73b3193..0d3fde4f76 100644
--- a/lxd/container_snapshot.go
+++ b/lxd/container_snapshot.go
@@ -127,9 +127,14 @@ func containerSnapshotsPost(d *Daemon, r *http.Request) Response {
 		shared.SnapshotDelimiter +
 		req.Name
 
-	expiry, err := shared.GetSnapshotExpiry(time.Now(), c.LocalConfig()["snapshots.expiry"])
-	if err != nil {
-		return BadRequest(err)
+	var expiry time.Time
+	if req.ExpiresAt != nil {
+		expiry = *req.ExpiresAt
+	} else {
+		expiry, err = shared.GetSnapshotExpiry(time.Now(), c.LocalConfig()["snapshots.expiry"])
+		if err != nil {
+			return BadRequest(err)
+		}
 	}
 
 	snapshot := func(op *operation) error {

From 7a9ef90affba7e32821a434fae5d63fa1749786f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 18 Feb 2019 16:01:13 -0500
Subject: [PATCH 5/6] lxc: Add no-expiry snapshot flag
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #5510

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc/snapshot.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/lxc/snapshot.go b/lxc/snapshot.go
index ca9e1bc378..c98db99634 100644
--- a/lxc/snapshot.go
+++ b/lxc/snapshot.go
@@ -6,12 +6,15 @@ import (
 	"github.com/lxc/lxd/shared/api"
 	cli "github.com/lxc/lxd/shared/cmd"
 	"github.com/lxc/lxd/shared/i18n"
+
+	"time"
 )
 
 type cmdSnapshot struct {
 	global *cmdGlobal
 
 	flagStateful bool
+	flagNoExpiry bool
 }
 
 func (c *cmdSnapshot) Command() *cobra.Command {
@@ -29,6 +32,7 @@ running state, including process memory state, TCP connections, ...`))
 
 	cmd.RunE = c.Run
 	cmd.Flags().BoolVar(&c.flagStateful, "stateful", false, i18n.G("Whether or not to snapshot the container's running state"))
+	cmd.Flags().BoolVar(&c.flagNoExpiry, "no-expiry", false, i18n.G("Ignore any configured auto-expiry for the container"))
 
 	return cmd
 }
@@ -64,6 +68,10 @@ func (c *cmdSnapshot) Run(cmd *cobra.Command, args []string) error {
 		Stateful: c.flagStateful,
 	}
 
+	if c.flagNoExpiry {
+		req.ExpiresAt = &time.Time{}
+	}
+
 	op, err := d.CreateContainerSnapshot(name, req)
 	if err != nil {
 		return err

From e84405b67f767f75d8839ab9fa8939fe6c709209 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 18 Feb 2019 16:02:20 -0500
Subject: [PATCH 6/6] tests: Add snapshot expiry configuration on create

---
 test/suites/snapshots.sh | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/test/suites/snapshots.sh b/test/suites/snapshots.sh
index 81a900f493..15d6670ae9 100644
--- a/test/suites/snapshots.sh
+++ b/test/suites/snapshots.sh
@@ -235,5 +235,8 @@ test_snap_expiry() {
   lxc snapshot c1
   ! lxc config show c1/snap1 | grep -q 'expires_at: 0001-01-01T00:00:00Z' || false
 
+  lxc snapshot c1 --no-expiry
+  lxc config show c1/snap2 | grep -q 'expires_at: 0001-01-01T00:00:00Z' || false
+
   lxc rm -f c1
 }


More information about the lxc-devel mailing list