Skip to content

Commit

Permalink
Prevent unwanted yaml fields to be marshalled (#323)
Browse files Browse the repository at this point in the history
Signed-off-by: Mauro Morales <[email protected]>
  • Loading branch information
mauromorales committed May 8, 2024
1 parent 2dc2972 commit 9528840
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
54 changes: 53 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package config_test

import (
"fmt"
"github.com/kairos-io/kairos-sdk/collector"
"path/filepath"
"reflect"
"strings"
Expand All @@ -28,8 +27,10 @@ import (
v1mocks "github.com/kairos-io/kairos-agent/v2/tests/mocks"
"github.com/twpayne/go-vfs/v4"
"github.com/twpayne/go-vfs/v4/vfst"
"gopkg.in/yaml.v3"

. "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-sdk/collector"
. "github.com/kairos-io/kairos-sdk/schema"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -114,6 +115,57 @@ var _ = Describe("Schema", func() {
structFieldsContainedInOtherStruct(Bundle{}, BundleSchema{})
})
})

Describe("Install unmarshall for payloads", func() {
It("produces a yaml without empty fields", func() {
wants := `install:
poweroff: true
bind_mounts:
- /var/lib/ceph
- /var/lib/osd
partitions:
oem:
size: 5120
fs: ext4
system:
size: 8192
recovery-system:
size: 10000
passive:
size: 8192
`
config := Config{
Install: &Install{
Poweroff: true,
BindMounts: []string{
"/var/lib/ceph",
"/var/lib/osd",
},
Active: v1.Image{
Size: 8192,
},
Passive: v1.Image{
Size: 8192,
},
Recovery: v1.Image{
Size: 10000,
},
Partitions: v1.ElementalPartitions{
OEM: &v1.Partition{
Size: 5120,
FS: "ext4",
},
},
},
}

got, err := yaml.Marshal(config)
Expect(Expect(err).NotTo(HaveOccurred()))

Expect(string(got)).To(Equal(wants))
})
})

Describe("Write and load installation state", func() {
var config *Config
var runner *v1mocks.FakeRunner
Expand Down
18 changes: 9 additions & 9 deletions pkg/types/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ func (r *EmptySpec) ShouldShutdown() bool { return false }

// Partition struct represents a partition with its commonly configurable values, size in MiB
type Partition struct {
Name string
Name string `yaml:"-"`
FilesystemLabel string `yaml:"label,omitempty" mapstructure:"label"`
Size uint `yaml:"size,omitempty" mapstructure:"size"`
FS string `yaml:"fs,omitempty" mapstrcuture:"fs"`
Flags []string `yaml:"flags,omitempty" mapstrcuture:"flags"`
MountPoint string
Path string
Disk string
MountPoint string `yaml:"-"`
Path string `yaml:"-"`
Disk string `yaml:"-"`
}

type PartitionList []*Partition
Expand Down Expand Up @@ -261,8 +261,8 @@ func (pl PartitionList) GetByLabel(label string) *Partition {
}

type ElementalPartitions struct {
BIOS *Partition
EFI *Partition
BIOS *Partition `yaml:"-"`
EFI *Partition `yaml:"-"`
OEM *Partition `yaml:"oem,omitempty" mapstructure:"oem"`
Recovery *Partition `yaml:"recovery,omitempty" mapstructure:"recovery"`
State *Partition `yaml:"state,omitempty" mapstructure:"state"`
Expand Down Expand Up @@ -434,13 +434,13 @@ func (ep ElementalPartitions) PartitionsByMountPoint(descending bool, excludes .

// Image struct represents a file system image with its commonly configurable values, size in MiB
type Image struct {
File string
File string `yaml:"-"`
Label string `yaml:"label,omitempty" mapstructure:"label"`
Size uint `yaml:"size,omitempty" mapstructure:"size"`
FS string `yaml:"fs,omitempty" mapstructure:"fs"`
Source *ImageSource `yaml:"uri,omitempty" mapstructure:"uri"`
MountPoint string
LoopDevice string
MountPoint string `yaml:"-"`
LoopDevice string `yaml:"-"`
}

// InstallState tracks the installation data of the whole system
Expand Down

0 comments on commit 9528840

Please sign in to comment.