[lxc-devel] [lxd/master] Allow `user.*` keys everywhere we store configuration

grant-he on Github lxc-bot at linuxcontainers.org
Tue Dec 1 00:31:32 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 465 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201130/34695ad3/attachment.bin>
-------------- next part --------------
From dae4f4083e8cdd97a8d3b264ed507058367d1467 Mon Sep 17 00:00:00 2001
From: JLRDRAGON92000 <jlrdragon at gmail.com>
Date: Mon, 30 Nov 2020 18:18:30 -0600
Subject: [PATCH] lxd/config: allowed user.* keys in server/cluster config

Signed-off-by: Jared Rankin <jared.rankin at utexas.edu>
---
 lxd/config/map.go | 51 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 11 deletions(-)

diff --git a/lxd/config/map.go b/lxd/config/map.go
index a4e949269b..dce73165e0 100644
--- a/lxd/config/map.go
+++ b/lxd/config/map.go
@@ -5,6 +5,7 @@ import (
 	"reflect"
 	"sort"
 	"strconv"
+	"strings"
 
 	"github.com/lxc/lxd/shared"
 )
@@ -95,14 +96,21 @@ func (m *Map) Change(changes map[string]interface{}) (map[string]string, error)
 func (m *Map) Dump() map[string]interface{} {
 	values := map[string]interface{}{}
 
-	for name, key := range m.schema {
-		value := m.GetRaw(name)
-		if value != key.Default {
-			if key.Hidden {
-				values[name] = true
-			} else {
-				values[name] = value
+	for name, value := range m.values {
+		key, ok := m.schema[name]
+		if ok {
+			// Schema key
+			value := m.GetRaw(name)
+			if value != key.Default {
+				if key.Hidden {
+					values[name] = true
+				} else {
+					values[name] = value
+				}
 			}
+		} else if strings.HasPrefix(name, "user.") {
+			// User key, just include it as is
+			values[name] = value
 		}
 	}
 
@@ -111,17 +119,21 @@ func (m *Map) Dump() map[string]interface{} {
 
 // GetRaw returns the value of the given key, which must be of type String.
 func (m *Map) GetRaw(name string) string {
-	key := m.schema.mustGetKey(name)
 	value, ok := m.values[name]
-	if !ok {
-		value = key.Default
+	if !strings.HasPrefix(name, "user.") {
+		key := m.schema.mustGetKey(name)
+		if !ok {
+			value = key.Default
+		}
 	}
 	return value
 }
 
 // GetString returns the value of the given key, which must be of type String.
 func (m *Map) GetString(name string) string {
-	m.schema.assertKeyType(name, String)
+	if !strings.HasPrefix(name, "user.") {
+		m.schema.assertKeyType(name, String)
+	}
 	return m.GetRaw(name)
 }
 
@@ -182,6 +194,23 @@ func (m *Map) update(values map[string]string) ([]string, error) {
 // effectively revert it to the default. Return a boolean indicating whether
 // the value has changed, and error if something went wrong.
 func (m *Map) set(name string, value string, initial bool) (bool, error) {
+	// Bypass schema for user.* keys
+	if strings.HasPrefix(name, "user.") {
+		current, ok := m.values[name]
+		if ok && value == current {
+			// Value is unchanged
+			return false, nil
+		}
+
+		if value == "" {
+			delete(m.values, name)
+		} else {
+			m.values[name] = value
+		}
+
+		return true, nil
+	}
+
 	key, ok := m.schema[name]
 	if !ok {
 		return false, fmt.Errorf("unknown key")


More information about the lxc-devel mailing list