Skip to content

Commit

Permalink
Expose max snapshots environment variable (#2193)
Browse files Browse the repository at this point in the history
* Expose SNAPSHOTTER_MAX_SNAPS run variable

Signed-off-by: Andrea Mazzotti <andrea.mazzotti@suse.com>
  • Loading branch information
anmazzotti authored Sep 25, 2024
1 parent b4765b9 commit 98ade87
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
19 changes: 19 additions & 0 deletions cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,25 @@ var _ = Describe("Config", Label("config"), func() {

Expect(cfg.Snapshotter.Type).To(Equal(constants.BtrfsSnapshotterType))
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(constants.BtrfsMaxSnaps))

// Test MAX_SNAPS string conversion from env
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_TYPE", "btrfs")).Should(Succeed())
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_MAX_SNAPS", "42")).Should(Succeed())

cfg, err = ReadConfigRun("fixtures/config/", nil, mounter)
Expect(err).ShouldNot(HaveOccurred())

Expect(cfg.Snapshotter.Type).To(Equal(constants.BtrfsSnapshotterType))
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(42))

Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_TYPE", "loopdevice")).Should(Succeed())
Expect(os.Setenv("ELEMENTAL_SNAPSHOTTER_MAX_SNAPS", "42")).Should(Succeed())

cfg, err = ReadConfigRun("fixtures/config/", nil, mounter)
Expect(err).ShouldNot(HaveOccurred())

Expect(cfg.Snapshotter.Type).To(Equal(constants.LoopDeviceSnapshotterType))
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(42))
})
})
Describe("Read runtime specs", Label("spec"), func() {
Expand Down
13 changes: 7 additions & 6 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,13 @@ func GetDefaultSquashfsCompressionOptions() []string {
// GetRunKeyEnvMap returns environment variable bindings to RunConfig data
func GetRunKeyEnvMap() map[string]string {
return map[string]string{
"poweroff": "POWEROFF",
"reboot": "REBOOT",
"strict": "STRICT",
"eject-cd": "EJECT_CD",
"snapshotter.type": "SNAPSHOTTER_TYPE",
"cloud-init-paths": "CLOUD_INIT_PATHS",
"poweroff": "POWEROFF",
"reboot": "REBOOT",
"strict": "STRICT",
"eject-cd": "EJECT_CD",
"snapshotter.type": "SNAPSHOTTER_TYPE",
"snapshotter.max-snaps": "SNAPSHOTTER_MAX_SNAPS",
"cloud-init-paths": "CLOUD_INIT_PATHS",
}
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/types/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types

import (
"fmt"
"strconv"

mapstructure "github.com/mitchellh/mapstructure"
"github.com/rancher/elemental-toolkit/v2/pkg/constants"
Expand Down Expand Up @@ -142,7 +143,17 @@ func (c *SnapshotterConfig) CustomUnmarshal(data interface{}) (bool, error) {
if mData["max-snaps"] != nil {
maxSnaps, ok := mData["max-snaps"].(int)
if !ok {
return false, fmt.Errorf("'max-snap' must be of integer type")
// If not integer value, try to convert from string.
// This will be the case when passed as env variable ELEMENTAL_SNAPSHOTTER_MAX_SNAPS
maxSnapsFromEnv, ok := mData["max-snaps"].(string)
if !ok {
return false, fmt.Errorf("'max-snap' must be of integer type")
}
maxSnapsConverted, err := strconv.Atoi(maxSnapsFromEnv)
if err != nil {
return false, fmt.Errorf("converting 'max-snap' from string input to integer type: %w", err)
}
maxSnaps = maxSnapsConverted
}
c.MaxSnaps = maxSnaps
} else if c.Type == constants.BtrfsSnapshotterType {
Expand Down

0 comments on commit 98ade87

Please sign in to comment.