Skip to content

Commit e846cf7

Browse files
authored
Clean up version: null from "k0sctl init" output (#641)
Signed-off-by: Kimmo Lehto <[email protected]>
1 parent 1da82c9 commit e846cf7

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

phase/default_k0s_version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (p *DefaultK0sVersion) Title() string {
2121
}
2222

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

2626
var msg string
2727
if isStable {

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/k0s.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ var (
3131

3232
// K0s holds configuration for bootstraping a k0s cluster
3333
type K0s struct {
34-
Version *version.Version `yaml:"version"`
35-
VersionChannel string `yaml:"versionChannel" default:"stable"`
36-
DynamicConfig bool `yaml:"dynamicConfig"`
37-
Config dig.Mapping `yaml:"config"`
34+
Version *version.Version `yaml:"version,omitempty"`
35+
VersionChannel string `yaml:"versionChannel,omitempty"`
36+
DynamicConfig bool `yaml:"dynamicConfig,omitempty" default:"false"`
37+
Config dig.Mapping `yaml:"config,omitempty"`
3838
Metadata K0sMetadata `yaml:"-"`
3939
}
4040

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

59+
// MarshalYAML implements yaml.Marshaler interface
60+
func (k *K0s) MarshalYAML() (interface{}, error) {
61+
if k == nil {
62+
return nil, nil
63+
}
64+
type k0s K0s
65+
yk := (*k0s)(k)
66+
67+
yml, err := yaml.Marshal(yk)
68+
if err != nil {
69+
return nil, fmt.Errorf("marshal k0s: %w", err)
70+
}
71+
72+
if string(yml) == "{}\n" {
73+
return nil, nil
74+
}
75+
76+
return yk, nil
77+
}
78+
5979
// SetDefaults sets default values
6080
func (k *K0s) SetDefaults() {
6181
if k.Version == nil {
@@ -88,7 +108,7 @@ func (k *K0s) Validate() error {
88108
return validation.ValidateStruct(k,
89109
validation.Field(&k.Version, validation.By(validateVersion)),
90110
validation.Field(&k.DynamicConfig, validation.By(k.validateMinDynamic())),
91-
validation.Field(&k.VersionChannel, validation.In("stable", "latest")),
111+
validation.Field(&k.VersionChannel, validation.In("stable", "latest"), validation.When(k.VersionChannel != "")),
92112
)
93113
}
94114

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
// Spec defines cluster config spec section
1111
type Spec struct {
12-
Hosts Hosts `yaml:"hosts"`
13-
K0s *K0s `yaml:"k0s"`
12+
Hosts Hosts `yaml:"hosts,omitempty"`
13+
K0s *K0s `yaml:"k0s,omitempty"`
1414

1515
k0sLeader *Host
1616
}
@@ -28,6 +28,19 @@ func (s *Spec) UnmarshalYAML(unmarshal func(interface{}) error) error {
2828
return defaults.Set(s)
2929
}
3030

31+
// MarshalYAML implements yaml.Marshaler interface
32+
func (s *Spec) MarshalYAML() (interface{}, error) {
33+
k0s, err := s.K0s.MarshalYAML()
34+
if err != nil {
35+
return nil, err
36+
}
37+
if k0s == nil {
38+
return Spec{Hosts: s.Hosts}, nil
39+
}
40+
41+
return s, nil
42+
}
43+
3144
// SetDefaults sets defaults
3245
func (s *Spec) SetDefaults() {
3346
if s.K0s == nil {

0 commit comments

Comments
 (0)