Skip to content

Commit

Permalink
Adding snapshotter.type flag install command
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <[email protected]>
  • Loading branch information
davidcassany committed Mar 4, 2024
1 parent ca4f47c commit bdb073a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 33 deletions.
20 changes: 16 additions & 4 deletions cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,30 @@ var _ = Describe("Config", Label("config"), func() {
Expect(debug).To(BeTrue())
Expect(cfg.Logger.GetLevel()).To(Equal(logrus.DebugLevel))
})
It("reads the snaphotter configuration", func() {
// Default value
It("reads the snaphotter configuration and environment variables", func() {
err := os.Setenv("ELEMENTAL_REBOOT", "true")
Expect(err).ShouldNot(HaveOccurred())

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

Expect(err).To(BeNil())
Expect(cfg.Reboot).To(BeTrue())
Expect(cfg.Snapshotter.Type).To(Equal(constants.LoopDeviceSnapshotterType))
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(7))
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(constants.LoopDeviceMaxSnaps))
snapshooterCfg, ok := cfg.Snapshotter.Config.(*v1.LoopDeviceConfig)
Expect(ok).To(BeTrue())
Expect(snapshooterCfg.FS).To(Equal("xfs"))
Expect(snapshooterCfg.Size).To(Equal(uint(1024)))

// Reads snapshotter type from env vars
err = os.Setenv("ELEMENTAL_SNAPSHOTTER_TYPE", "btrfs")
Expect(err).ShouldNot(HaveOccurred())

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(constants.BtrfsMaxSnaps))
})
})
Describe("Read runtime specs", Label("spec"), func() {
Expand Down
1 change: 0 additions & 1 deletion cmd/config/fixtures/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ upgrade:

snapshotter:
type: loopdevice
max-snaps: 7
config:
fs: xfs
size: 1024
5 changes: 5 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func NewInstallCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command {
}
firmType := newEnumFlag([]string{v1.EFI}, v1.EFI)
pTableType := newEnumFlag([]string{v1.GPT}, v1.GPT)
snapshotterType := newEnumFlag(
[]string{constants.LoopDeviceSnapshotterType, constants.BtrfsSnapshotterType},
constants.LoopDeviceSnapshotterType,
)

root.AddCommand(c)
c.Flags().StringSliceP("cloud-init", "c", []string{}, "Cloud-init config files")
Expand All @@ -110,6 +114,7 @@ func NewInstallCmd(root *cobra.Command, addCheckRoot bool) *cobra.Command {
c.Flags().Bool("force", false, "Force install")
c.Flags().Bool("eject-cd", false, "Try to eject the cd on reboot, only valid if booting from iso")
c.Flags().Bool("disable-boot-entry", false, "Dont create an EFI entry for the system install.")
c.Flags().Var(snapshotterType, "snapshotter.type", "Sets the snapshotter type to install")
addSharedInstallUpgradeFlags(c)
addLocalImageFlag(c)
addPlatformFlags(c)
Expand Down
16 changes: 4 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ func NewConfig(opts ...GenericOptions) *v1.Config {
func NewRunConfig(opts ...GenericOptions) *v1.RunConfig {
config := NewConfig(opts...)

snapshotter := v1.SnapshotterConfig{
Type: constants.LoopDeviceSnapshotterType,
MaxSnaps: constants.MaxSnaps,
Config: v1.NewLoopDeviceConfig(),
}
snapshotter := v1.NewLoopDevice()

// Load snapshotter setup from state.yaml for reset and upgrade
installState, _ := config.LoadInstallState()
Expand Down Expand Up @@ -550,13 +546,9 @@ func NewISO() *v1.LiveISO {

func NewBuildConfig(opts ...GenericOptions) *v1.BuildConfig {
b := &v1.BuildConfig{
Config: *NewConfig(opts...),
Name: constants.BuildImgName,
Snapshotter: v1.SnapshotterConfig{
Type: constants.LoopDeviceSnapshotterType,
MaxSnaps: constants.MaxSnaps,
Config: v1.NewLoopDeviceConfig(),
},
Config: *NewConfig(opts...),
Name: constants.BuildImgName,
Snapshotter: v1.NewLoopDevice(),
}
return b
}
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Types", Label("types", "config"), func() {
Expect(cfg.Mounter).To(Equal(mounter))
Expect(cfg.Runner).NotTo(BeNil())
It("sets the default snapshot", func() {
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(constants.MaxSnaps))
Expect(cfg.Snapshotter.MaxSnaps).To(Equal(constants.LoopDeviceMaxSnaps))
Expect(cfg.Snapshotter.Type).To(Equal(constants.LoopDeviceSnapshotterType))
snapshotterCfg, ok := cfg.Snapshotter.Config.(*v1.LoopDeviceConfig)
Expect(ok).To(BeTrue())
Expand Down
12 changes: 7 additions & 5 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ const (
Rsync = "rsync"

// Snapshotters
MaxSnaps = 2
LoopDeviceMaxSnaps = 2
BtrfsMaxSnaps = 4
LoopDeviceSnapshotterType = "loopdevice"
BtrfsSnapshotterType = "btrfs"
ActiveSnapshot = "active"
Expand Down Expand Up @@ -279,10 +280,11 @@ 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",
"poweroff": "POWEROFF",
"reboot": "REBOOT",
"strict": "STRICT",
"eject-cd": "EJECT_CD",
"snapshotter.type": "SNAPSHOTTER_TYPE",
}
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/snapshotter/loopdevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ var _ = Describe("LoopDevice", Label("snapshotter", "loopdevice"), func() {
conf.WithMounter(mounter),
conf.WithPlatform("linux/amd64"),
)
snapCfg = v1.SnapshotterConfig{
Type: constants.LoopDeviceSnapshotterType,
MaxSnaps: constants.MaxSnaps,
Config: v1.NewLoopDeviceConfig(),
}
snapCfg = v1.NewLoopDevice()

Expect(utils.MkdirAll(fs, rootDir, constants.DirPerm)).To(Succeed())
})
Expand Down
6 changes: 1 addition & 5 deletions pkg/types/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ func (c Config) WriteInstallState(i *InstallState, statePath, recoveryPath strin
// LoadInstallState loads the state.yaml file and unmarshals it to an InstallState object
func (c Config) LoadInstallState() (*InstallState, error) {
installState := &InstallState{
Snapshotter: SnapshotterConfig{
Type: constants.LoopDeviceSnapshotterType,
MaxSnaps: constants.MaxSnaps,
Config: NewLoopDeviceConfig(),
},
Snapshotter: NewLoopDevice(),
}
stateFile := filepath.Join(constants.RunningStateDir, constants.InstallStateFile)
data, err := c.Fs.ReadFile(stateFile)
Expand Down
20 changes: 20 additions & 0 deletions pkg/types/v1/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ func NewBtrfsConfig() *BtrfsConfig {
return &BtrfsConfig{}
}

func NewLoopDevice() SnapshotterConfig {
return SnapshotterConfig{
Type: constants.LoopDeviceSnapshotterType,
MaxSnaps: constants.LoopDeviceMaxSnaps,
Config: NewLoopDeviceConfig(),
}
}

func NewBtrfs() SnapshotterConfig {
return SnapshotterConfig{
Type: constants.BtrfsSnapshotterType,
MaxSnaps: constants.BtrfsMaxSnaps,
Config: NewBtrfsConfig(),
}
}

type snapshotterConfFactory func(defConfig interface{}, data interface{}) (interface{}, error)

var snapshotterConfFactories = map[string]snapshotterConfFactory{}
Expand Down Expand Up @@ -129,6 +145,10 @@ func (c *SnapshotterConfig) CustomUnmarshal(data interface{}) (bool, error) {
return false, fmt.Errorf("'max-snap' must be of integer type")
}
c.MaxSnaps = maxSnaps
} else if c.Type == constants.BtrfsSnapshotterType {
// constants.LoopDeviceMaxSnaps is already the default if nothing is provided
// switch to btrfs default in case btrfs is requested and no max-snaps is provided
c.MaxSnaps = constants.BtrfsMaxSnaps
}

factory := snapshotterConfFactories[c.Type]
Expand Down

0 comments on commit bdb073a

Please sign in to comment.