Skip to content

Commit 9c30564

Browse files
authored
Merge pull request #5514 from twz123/remove-storage-param
Remove storage parameter from DefaultClusterConfig
2 parents 7e5a5d1 + 775388f commit 9c30564

File tree

12 files changed

+133
-142
lines changed

12 files changed

+133
-142
lines changed

cmd/config/validate.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,26 @@ func NewValidateCmd() *cobra.Command {
3535
Long: `Example:
3636
k0s config validate --config path_to_config.yaml`,
3737
Args: cobra.NoArgs,
38-
RunE: func(cmd *cobra.Command, _ []string) error {
39-
var reader io.Reader
38+
RunE: func(cmd *cobra.Command, _ []string) (err error) {
39+
var bytes []byte
4040

4141
// config.CfgFile is the global value holder for --config flag, set by cobra/pflag
4242
switch config.CfgFile {
4343
case "-":
44-
reader = cmd.InOrStdin()
44+
if bytes, err = io.ReadAll(cmd.InOrStdin()); err != nil {
45+
return fmt.Errorf("failed to read configuration from standard input: %w", err)
46+
}
4547
case "":
4648
return errors.New("--config can't be empty")
4749
default:
48-
f, err := os.Open(config.CfgFile)
49-
if err != nil {
50-
return err
50+
if bytes, err = os.ReadFile(config.CfgFile); err != nil {
51+
return fmt.Errorf("failed to read configuration file: %w", err)
5152
}
52-
defer f.Close()
53-
reader = f
5453
}
5554

56-
cfg, err := v1beta1.ConfigFromReader(reader)
55+
cfg, err := v1beta1.ConfigFromBytes(bytes)
5756
if err != nil {
58-
return fmt.Errorf("failed to read config: %w", err)
57+
return fmt.Errorf("failed to parse configuration: %w", err)
5958
}
6059

6160
return errors.Join(cfg.Validate()...)

pkg/apis/k0s/v1beta1/clusterconfig_types.go

+21-29
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23-
"io"
2423
"reflect"
2524

2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -198,30 +197,24 @@ func (s *SchedulerSpec) IsZero() bool {
198197
return len(s.ExtraArgs) == 0
199198
}
200199

201-
func ConfigFromString(yml string, defaultStorage ...*StorageSpec) (*ClusterConfig, error) {
202-
config := DefaultClusterConfig(defaultStorage...)
203-
err := strictyaml.YamlUnmarshalStrictIgnoringFields([]byte(yml), config, "interval", "podSecurityPolicy")
204-
if err != nil {
205-
return config, err
206-
}
207-
if config.Spec == nil {
208-
config.Spec = DefaultClusterSpec(defaultStorage...)
209-
}
210-
return config, nil
200+
func ConfigFromBytes(bytes []byte) (*ClusterConfig, error) {
201+
return DefaultClusterConfig().MergedWithYAML(bytes)
211202
}
212203

213-
// ConfigFromReader reads the configuration from any reader (can be stdin, file reader, etc)
214-
func ConfigFromReader(r io.Reader, defaultStorage ...*StorageSpec) (*ClusterConfig, error) {
215-
input, err := io.ReadAll(r)
204+
func (c *ClusterConfig) MergedWithYAML(bytes []byte) (*ClusterConfig, error) {
205+
merged := c.DeepCopy()
206+
err := strictyaml.YamlUnmarshalStrictIgnoringFields(bytes, merged, "interval", "podSecurityPolicy")
216207
if err != nil {
217208
return nil, err
218209
}
219-
return ConfigFromString(string(input), defaultStorage...)
210+
if merged.Spec == nil {
211+
merged.Spec = c.Spec
212+
}
213+
return merged, nil
220214
}
221215

222216
// DefaultClusterConfig sets the default ClusterConfig values, when none are given
223-
func DefaultClusterConfig(defaultStorage ...*StorageSpec) *ClusterConfig {
224-
clusterSpec := DefaultClusterSpec(defaultStorage...)
217+
func DefaultClusterConfig() *ClusterConfig {
225218
return &ClusterConfig{
226219
TypeMeta: metav1.TypeMeta{
227220
APIVersion: GroupVersion.String(),
@@ -231,7 +224,7 @@ func DefaultClusterConfig(defaultStorage ...*StorageSpec) *ClusterConfig {
231224
Name: constant.ClusterConfigObjectName,
232225
Namespace: constant.ClusterConfigNamespace,
233226
},
234-
Spec: clusterSpec,
227+
Spec: DefaultClusterSpec(),
235228
}
236229
}
237230

@@ -247,7 +240,10 @@ func (c *ClusterConfig) UnmarshalJSON(data []byte) error {
247240
if c.Spec != nil && c.Spec.Storage != nil {
248241
storage = c.Spec.Storage
249242
}
250-
c.Spec = DefaultClusterSpec(storage)
243+
c.Spec = DefaultClusterSpec()
244+
if storage != nil {
245+
c.Spec.Storage = storage
246+
}
251247

252248
type config ClusterConfig
253249
jc := (*config)(c)
@@ -261,7 +257,10 @@ func (c *ClusterConfig) UnmarshalJSON(data []byte) error {
261257
}
262258

263259
if jc.Spec == nil {
264-
jc.Spec = DefaultClusterSpec(storage)
260+
jc.Spec = DefaultClusterSpec()
261+
if storage != nil {
262+
jc.Spec.Storage = storage
263+
}
265264
return nil
266265
}
267266
if jc.Spec.Storage == nil {
@@ -301,17 +300,10 @@ func (c *ClusterConfig) UnmarshalJSON(data []byte) error {
301300
}
302301

303302
// DefaultClusterSpec default settings
304-
func DefaultClusterSpec(defaultStorage ...*StorageSpec) *ClusterSpec {
305-
var storage *StorageSpec
306-
if defaultStorage == nil || defaultStorage[0] == nil {
307-
storage = DefaultStorageSpec()
308-
} else {
309-
storage = defaultStorage[0]
310-
}
311-
303+
func DefaultClusterSpec() *ClusterSpec {
312304
spec := &ClusterSpec{
313305
Extensions: DefaultExtensions(),
314-
Storage: storage,
306+
Storage: DefaultStorageSpec(),
315307
Network: DefaultNetwork(),
316308
API: DefaultAPISpec(),
317309
ControllerManager: DefaultControllerManagerSpec(),

pkg/apis/k0s/v1beta1/clusterconfig_types_test.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,29 @@ import (
3030
)
3131

3232
func TestClusterDefaults(t *testing.T) {
33-
c, err := ConfigFromString("apiVersion: k0s.k0sproject.io/v1beta1")
33+
c, err := ConfigFromBytes([]byte("apiVersion: k0s.k0sproject.io/v1beta1"))
3434
assert.NoError(t, err)
3535
assert.Equal(t, DefaultStorageSpec(), c.Spec.Storage)
3636
}
3737

3838
func TestUnknownFieldValidation(t *testing.T) {
39-
_, err := ConfigFromString(`
39+
_, err := ConfigFromBytes([]byte(`
4040
apiVersion: k0s.k0sproject.io/v1beta1
4141
kind: ClusterConfig
42-
unknown: 1`)
42+
unknown: 1`))
4343

4444
assert.Error(t, err)
4545
}
4646

4747
func TestStorageDefaults(t *testing.T) {
48-
yamlData := `
48+
yamlData := []byte(`
4949
apiVersion: k0s.k0sproject.io/v1beta1
5050
kind: ClusterConfig
5151
metadata:
5252
name: foobar
53-
`
53+
`)
5454

55-
c, err := ConfigFromString(yamlData)
55+
c, err := ConfigFromBytes(yamlData)
5656
assert.NoError(t, err)
5757
assert.Equal(t, EtcdStorageType, c.Spec.Storage.Type)
5858
addr, err := iface.FirstPublicAddress()
@@ -122,17 +122,17 @@ func TestClusterSpecCustomImages(t *testing.T) {
122122
}
123123

124124
func TestEtcdDefaults(t *testing.T) {
125-
yamlData := `
125+
yamlData := []byte(`
126126
apiVersion: k0s.k0sproject.io/v1beta1
127127
kind: ClusterConfig
128128
metadata:
129129
name: foobar
130130
spec:
131131
storage:
132132
type: etcd
133-
`
133+
`)
134134

135-
c, err := ConfigFromString(yamlData)
135+
c, err := ConfigFromBytes(yamlData)
136136
assert.NoError(t, err)
137137
assert.Equal(t, EtcdStorageType, c.Spec.Storage.Type)
138138
addr, err := iface.FirstPublicAddress()
@@ -141,7 +141,7 @@ spec:
141141
}
142142

143143
func TestNetworkValidation_Custom(t *testing.T) {
144-
yamlData := `
144+
yamlData := []byte(`
145145
apiVersion: k0s.k0sproject.io/v1beta1
146146
kind: ClusterConfig
147147
metadata:
@@ -151,16 +151,16 @@ spec:
151151
provider: custom
152152
storage:
153153
type: etcd
154-
`
154+
`)
155155

156-
c, err := ConfigFromString(yamlData)
156+
c, err := ConfigFromBytes(yamlData)
157157
assert.NoError(t, err)
158158
errors := c.Validate()
159159
assert.Zero(t, errors)
160160
}
161161

162162
func TestNetworkValidation_Calico(t *testing.T) {
163-
yamlData := `
163+
yamlData := []byte(`
164164
apiVersion: k0s.k0sproject.io/v1beta1
165165
kind: ClusterConfig
166166
metadata:
@@ -170,16 +170,16 @@ spec:
170170
provider: calico
171171
storage:
172172
type: etcd
173-
`
173+
`)
174174

175-
c, err := ConfigFromString(yamlData)
175+
c, err := ConfigFromBytes(yamlData)
176176
assert.NoError(t, err)
177177
errors := c.Validate()
178178
assert.Zero(t, errors)
179179
}
180180

181181
func TestNetworkValidation_Invalid(t *testing.T) {
182-
yamlData := `
182+
yamlData := []byte(`
183183
apiVersion: k0s.k0sproject.io/v1beta1
184184
kind: ClusterConfig
185185
metadata:
@@ -189,9 +189,9 @@ spec:
189189
provider: invalidProvider
190190
storage:
191191
type: etcd
192-
`
192+
`)
193193

194-
c, err := ConfigFromString(yamlData)
194+
c, err := ConfigFromBytes(yamlData)
195195
assert.NoError(t, err)
196196
errors := c.Validate()
197197
if assert.Len(t, errors, 1) {
@@ -200,7 +200,7 @@ spec:
200200
}
201201

202202
func TestApiExternalAddress(t *testing.T) {
203-
yamlData := `
203+
yamlData := []byte(`
204204
apiVersion: k0s.k0sproject.io/v1beta1
205205
kind: ClusterConfig
206206
metadata:
@@ -209,33 +209,33 @@ spec:
209209
api:
210210
externalAddress: foo.bar.com
211211
address: 1.2.3.4
212-
`
212+
`)
213213

214-
c, err := ConfigFromString(yamlData)
214+
c, err := ConfigFromBytes(yamlData)
215215
assert.NoError(t, err)
216216
assert.Equal(t, "https://foo.bar.com:6443", c.Spec.API.APIAddressURL())
217217
assert.Equal(t, "https://foo.bar.com:9443", c.Spec.API.K0sControlPlaneAPIAddress())
218218
}
219219

220220
func TestApiNoExternalAddress(t *testing.T) {
221-
yamlData := `
221+
yamlData := []byte(`
222222
apiVersion: k0s.k0sproject.io/v1beta1
223223
kind: ClusterConfig
224224
metadata:
225225
name: foobar
226226
spec:
227227
api:
228228
address: 1.2.3.4
229-
`
229+
`)
230230

231-
c, err := ConfigFromString(yamlData)
231+
c, err := ConfigFromBytes(yamlData)
232232
assert.NoError(t, err)
233233
assert.Equal(t, "https://1.2.3.4:6443", c.Spec.API.APIAddressURL())
234234
assert.Equal(t, "https://1.2.3.4:9443", c.Spec.API.K0sControlPlaneAPIAddress())
235235
}
236236

237237
func TestNullValues(t *testing.T) {
238-
yamlData := `
238+
yamlData := []byte(`
239239
apiVersion: k0s.k0sproject.io/v1beta1
240240
kind: ClusterConfig
241241
metadata:
@@ -251,18 +251,18 @@ spec:
251251
installConfig: null
252252
telemetry: null
253253
konnectivity: null
254-
`
255-
extensionsYamlData := `
254+
`)
255+
extensionsYamlData := []byte(`
256256
apiVersion: k0s.k0sproject.io/v1beta1
257257
kind: ClusterConfig
258258
metadata:
259259
name: foobar
260260
spec:
261261
extensions:
262262
storage: null
263-
`
263+
`)
264264

265-
c, err := ConfigFromString(yamlData)
265+
c, err := ConfigFromBytes(yamlData)
266266
assert.NoError(t, err)
267267
assert.Equal(t, DefaultClusterImages(), c.Spec.Images)
268268
assert.Equal(t, DefaultStorageSpec(), c.Spec.Storage)
@@ -276,13 +276,13 @@ spec:
276276
assert.Equal(t, DefaultClusterTelemetry(), c.Spec.Telemetry)
277277
assert.Equal(t, DefaultKonnectivitySpec(), c.Spec.Konnectivity)
278278

279-
e, err := ConfigFromString(extensionsYamlData)
279+
e, err := ConfigFromBytes(extensionsYamlData)
280280
assert.NoError(t, err)
281281
assert.Equal(t, DefaultExtensions(), e.Spec.Extensions)
282282
}
283283

284284
func TestWorkerProfileConfig(t *testing.T) {
285-
yamlData := `
285+
yamlData := []byte(`
286286
apiVersion: k0s.k0sproject.io/v1beta1
287287
kind: ClusterConfig
288288
metadata:
@@ -303,8 +303,8 @@ spec:
303303
authentication:
304304
anonymous:
305305
enabled: false
306-
`
307-
c, err := ConfigFromString(yamlData)
306+
`)
307+
c, err := ConfigFromBytes(yamlData)
308308
assert.NoError(t, err)
309309
require.Len(t, c.Spec.WorkerProfiles, 2)
310310
assert.Equal(t, "profile_XXX", c.Spec.WorkerProfiles[0].Name)
@@ -340,7 +340,7 @@ func TestDefaultClusterConfigYaml(t *testing.T) {
340340
}
341341

342342
func TestFeatureGates(t *testing.T) {
343-
yamlData := `
343+
yamlData := []byte(`
344344
apiVersion: k0s.k0sproject.io/v1beta1
345345
kind: ClusterConfig
346346
metadata:
@@ -355,8 +355,8 @@ func TestFeatureGates(t *testing.T) {
355355
-
356356
name: feature_ZZZ
357357
enabled: false
358-
`
359-
c, err := ConfigFromString(yamlData)
358+
`)
359+
c, err := ConfigFromBytes(yamlData)
360360
assert.NoError(t, err)
361361
require.Len(t, c.Spec.FeatureGates, 3)
362362
assert.Equal(t, "feature_XXX", c.Spec.FeatureGates[0].Name)

0 commit comments

Comments
 (0)