Skip to content

Commit

Permalink
Clean up version: null from "k0sctl init" output (#641)
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke authored May 6, 2024
1 parent 1da82c9 commit e846cf7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion phase/default_k0s_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (p *DefaultK0sVersion) Title() string {
}

func (p *DefaultK0sVersion) Run() error {
isStable := p.Config.Spec.K0s.VersionChannel == "stable"
isStable := p.Config.Spec.K0s.VersionChannel == "" || p.Config.Spec.K0s.VersionChannel == "stable"

var msg string
if isStable {
Expand Down
30 changes: 25 additions & 5 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ var (

// K0s holds configuration for bootstraping a k0s cluster
type K0s struct {
Version *version.Version `yaml:"version"`
VersionChannel string `yaml:"versionChannel" default:"stable"`
DynamicConfig bool `yaml:"dynamicConfig"`
Config dig.Mapping `yaml:"config"`
Version *version.Version `yaml:"version,omitempty"`
VersionChannel string `yaml:"versionChannel,omitempty"`
DynamicConfig bool `yaml:"dynamicConfig,omitempty" default:"false"`
Config dig.Mapping `yaml:"config,omitempty"`
Metadata K0sMetadata `yaml:"-"`
}

Expand All @@ -56,6 +56,26 @@ func (k *K0s) UnmarshalYAML(unmarshal func(interface{}) error) error {
return defaults.Set(k)
}

// MarshalYAML implements yaml.Marshaler interface
func (k *K0s) MarshalYAML() (interface{}, error) {
if k == nil {
return nil, nil
}
type k0s K0s
yk := (*k0s)(k)

yml, err := yaml.Marshal(yk)
if err != nil {
return nil, fmt.Errorf("marshal k0s: %w", err)
}

if string(yml) == "{}\n" {
return nil, nil
}

return yk, nil
}

// SetDefaults sets default values
func (k *K0s) SetDefaults() {
if k.Version == nil {
Expand Down Expand Up @@ -88,7 +108,7 @@ func (k *K0s) Validate() error {
return validation.ValidateStruct(k,
validation.Field(&k.Version, validation.By(validateVersion)),
validation.Field(&k.DynamicConfig, validation.By(k.validateMinDynamic())),
validation.Field(&k.VersionChannel, validation.In("stable", "latest")),
validation.Field(&k.VersionChannel, validation.In("stable", "latest"), validation.When(k.VersionChannel != "")),
)
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

// Spec defines cluster config spec section
type Spec struct {
Hosts Hosts `yaml:"hosts"`
K0s *K0s `yaml:"k0s"`
Hosts Hosts `yaml:"hosts,omitempty"`
K0s *K0s `yaml:"k0s,omitempty"`

k0sLeader *Host
}
Expand All @@ -28,6 +28,19 @@ func (s *Spec) UnmarshalYAML(unmarshal func(interface{}) error) error {
return defaults.Set(s)
}

// MarshalYAML implements yaml.Marshaler interface
func (s *Spec) MarshalYAML() (interface{}, error) {
k0s, err := s.K0s.MarshalYAML()
if err != nil {
return nil, err
}
if k0s == nil {
return Spec{Hosts: s.Hosts}, nil
}

return s, nil
}

// SetDefaults sets defaults
func (s *Spec) SetDefaults() {
if s.K0s == nil {
Expand Down

0 comments on commit e846cf7

Please sign in to comment.