Skip to content

Commit

Permalink
Fix bootstrap config conversion (#122)
Browse files Browse the repository at this point in the history
* fix bootstrap config conversion

* set defaults for testing
  • Loading branch information
louiseschmidtgen authored Feb 14, 2024
1 parent 0799b2f commit 6616fa0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/k8s/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

type BootstrapConfig struct {
// Components are the components that should be enabled on bootstrap.
Components []string
Components []string `yaml:"components"`
// ClusterCIDR is the CIDR of the cluster.
ClusterCIDR string
ClusterCIDR string `yaml:"cluster-cidr"`
}

// SetDefaults sets the fields to default values.
Expand All @@ -23,16 +23,13 @@ func (b *BootstrapConfig) SetDefaults() {

// ToMap marshals the BootstrapConfig into yaml and map it to "bootstrapConfig".
func (b *BootstrapConfig) ToMap() (map[string]string, error) {
config := map[string]any{
"components": b.Components,
"cluster-cidr": b.ClusterCIDR,
}
c, err := yaml.Marshal(config)
config, err := yaml.Marshal(b)
if err != nil {
return nil, fmt.Errorf("failed to marshal config map: %w", err)
}

return map[string]string{
"bootstrapConfig": string(c),
"bootstrapConfig": string(config),
}, nil
}

Expand Down
29 changes: 29 additions & 0 deletions src/k8s/api/v1/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v1

import (
"testing"

. "github.com/onsi/gomega"
)

func TestBootstrapConfigFromMap(t *testing.T) {
g := NewWithT(t)
// Create a new BootstrapConfig with default values
bc := &BootstrapConfig{
Components: []string{"dns", "network", "storage"},
ClusterCIDR: "10.1.0.0/16",
}

// Convert the BootstrapConfig to a map
m, err := bc.ToMap()
g.Expect(err).To(BeNil())

// Unmarshal the YAML string from the map into a new BootstrapConfig instance
bcyaml, err := BootstrapConfigFromMap(m)

// Check for errors
g.Expect(err).To(BeNil())
// Compare the unmarshaled BootstrapConfig with the original one
g.Expect(bcyaml).To(Equal(bc)) // Note the *bc here to compare values, not pointers

}

0 comments on commit 6616fa0

Please sign in to comment.