Skip to content

Commit 558a01f

Browse files
anmazzottidavidcassany
authored andcommitted
Expose max snapshots environment variable (rancher#2193)
* Expose SNAPSHOTTER_MAX_SNAPS run variable Signed-off-by: Andrea Mazzotti <[email protected]> (cherry picked from commit 98ade87)
1 parent b4765b9 commit 558a01f

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

cmd/config/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ var _ = Describe("Config", Label("config"), func() {
272272

273273
Expect(cfg.Snapshotter.Type).To(Equal(constants.BtrfsSnapshotterType))
274274
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(constants.BtrfsMaxSnaps))
275+
276+
// Test MAX_SNAPS string conversion from env
277+
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_TYPE", "btrfs")).Should(Succeed())
278+
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_MAX_SNAPS", "42")).Should(Succeed())
279+
280+
cfg, err = ReadConfigRun("fixtures/config/", nil, mounter)
281+
Expect(err).ShouldNot(HaveOccurred())
282+
283+
Expect(cfg.Snapshotter.Type).To(Equal(constants.BtrfsSnapshotterType))
284+
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(42))
285+
286+
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_TYPE", "loopdevice")).Should(Succeed())
287+
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_MAX_SNAPS", "42")).Should(Succeed())
288+
289+
cfg, err = ReadConfigRun("fixtures/config/", nil, mounter)
290+
Expect(err).ShouldNot(HaveOccurred())
291+
292+
Expect(cfg.Snapshotter.Type).To(Equal(constants.LoopDeviceSnapshotterType))
293+
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(42))
275294
})
276295
})
277296
Describe("Read runtime specs", Label("spec"), func() {

pkg/constants/constants.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,13 @@ func GetDefaultSquashfsCompressionOptions() []string {
289289
// GetRunKeyEnvMap returns environment variable bindings to RunConfig data
290290
func GetRunKeyEnvMap() map[string]string {
291291
return map[string]string{
292-
"poweroff": "POWEROFF",
293-
"reboot": "REBOOT",
294-
"strict": "STRICT",
295-
"eject-cd": "EJECT_CD",
296-
"snapshotter.type": "SNAPSHOTTER_TYPE",
297-
"cloud-init-paths": "CLOUD_INIT_PATHS",
292+
"poweroff": "POWEROFF",
293+
"reboot": "REBOOT",
294+
"strict": "STRICT",
295+
"eject-cd": "EJECT_CD",
296+
"snapshotter.type": "SNAPSHOTTER_TYPE",
297+
"snapshotter.max-snaps": "SNAPSHOTTER_MAX_SNAPS",
298+
"cloud-init-paths": "CLOUD_INIT_PATHS",
298299
}
299300
}
300301

pkg/types/snapshotter.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package types
1818

1919
import (
2020
"fmt"
21+
"strconv"
2122

2223
mapstructure "github.com/mitchellh/mapstructure"
2324
"github.com/rancher/elemental-toolkit/v2/pkg/constants"
@@ -142,7 +143,17 @@ func (c *SnapshotterConfig) CustomUnmarshal(data interface{}) (bool, error) {
142143
if mData["max-snaps"] != nil {
143144
maxSnaps, ok := mData["max-snaps"].(int)
144145
if !ok {
145-
return false, fmt.Errorf("'max-snap' must be of integer type")
146+
// If not integer value, try to convert from string.
147+
// This will be the case when passed as env variable ELEMENTAL_SNAPSHOTTER_MAX_SNAPS
148+
maxSnapsFromEnv, ok := mData["max-snaps"].(string)
149+
if !ok {
150+
return false, fmt.Errorf("'max-snap' must be of integer type")
151+
}
152+
maxSnapsConverted, err := strconv.Atoi(maxSnapsFromEnv)
153+
if err != nil {
154+
return false, fmt.Errorf("converting 'max-snap' from string input to integer type: %w", err)
155+
}
156+
maxSnaps = maxSnapsConverted
146157
}
147158
c.MaxSnaps = maxSnaps
148159
} else if c.Type == constants.BtrfsSnapshotterType {

0 commit comments

Comments
 (0)