Skip to content

Commit fc34205

Browse files
committed
fix tests
1 parent 840a289 commit fc34205

File tree

6 files changed

+66
-50
lines changed

6 files changed

+66
-50
lines changed

api/v1alpha1/configuration.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ func BuildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
9393
rawYamlConfiguration = cr.Spec.Configuration
9494
}
9595

96-
dynConfig, err := ParseDynconfig(rawYamlConfiguration)
96+
dynconfig, err := ParseDynconfig(rawYamlConfiguration)
9797
if err == nil {
98-
if dynConfig.Config["hosts"] == nil {
98+
if dynconfig.Config["hosts"] == nil {
9999
hosts := generateHosts(cr)
100-
dynConfig.Config["hosts"] = hosts
100+
dynconfig.Config["hosts"] = hosts
101101
}
102102

103-
return yaml.Marshal(dynConfig)
103+
return yaml.Marshal(dynconfig)
104104
}
105105

106106
err = yaml.Unmarshal([]byte(rawYamlConfiguration), &config)
@@ -122,6 +122,14 @@ func BuildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
122122
return yaml.Marshal(config)
123123
}
124124

125+
func ParseConfig(rawYamlConfiguration string) (schema.Configuration, error) {
126+
config := schema.Configuration{}
127+
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
128+
dec.KnownFields(false)
129+
err := dec.Decode(&config)
130+
return config, err
131+
}
132+
125133
func ParseDynconfig(rawYamlConfiguration string) (schema.Dynconfig, error) {
126134
dynconfig := schema.Dynconfig{}
127135
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))

api/v1alpha1/storage_webhook.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,28 @@ var _ webhook.Validator = &Storage{}
175175
func (r *Storage) ValidateCreate() error {
176176
storagelog.Info("validate create", "name", r.Name)
177177

178-
configuration := make(map[string]interface{})
179-
err := yaml.Unmarshal([]byte(r.Spec.Configuration), &configuration)
180-
if err != nil {
181-
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
182-
}
178+
var configuration schema.Configuration
183179

184-
dynConfig, err := ParseDynconfig(r.Spec.Configuration)
180+
rawYamlConfiguration := r.Spec.Configuration
181+
dynconfig, err := ParseDynconfig(r.Spec.Configuration)
185182
if err == nil {
186-
configuration = dynConfig.Config
183+
config, err := yaml.Marshal(dynconfig.Config)
184+
if err != nil {
185+
return fmt.Errorf("failed to parse .config from dynconfig, error: %w", err)
186+
}
187+
rawYamlConfiguration = string(config)
187188
}
188189

189-
hostsConfig, ok := configuration["hosts"].([]schema.Host)
190-
if !ok {
191-
return fmt.Errorf("failed to parse YAML to determine `hosts`")
190+
configuration, err = ParseConfig(rawYamlConfiguration)
191+
if err != nil {
192+
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
192193
}
193194

194195
var nodesNumber int32
195-
if len(hostsConfig) == 0 {
196+
if len(configuration.Hosts) == 0 {
196197
nodesNumber = r.Spec.Nodes
197198
} else {
198-
nodesNumber = int32(len(hostsConfig))
199+
nodesNumber = int32(len(configuration.Hosts))
199200
}
200201

201202
minNodesPerErasure := map[ErasureType]int32{
@@ -207,13 +208,8 @@ func (r *Storage) ValidateCreate() error {
207208
return fmt.Errorf("erasure type %v requires at least %v storage nodes", r.Spec.Erasure, minNodesPerErasure[r.Spec.Erasure])
208209
}
209210

210-
domainsConfig, ok := configuration["domains_config"].(schema.DomainsConfig)
211-
if !ok {
212-
return fmt.Errorf("failed to parse YAML to determine `domains_config`")
213-
}
214-
215211
var authEnabled bool
216-
if domainsConfig.SecurityConfig.EnforceUserTokenRequirement {
212+
if configuration.DomainsConfig.SecurityConfig.EnforceUserTokenRequirement {
217213
authEnabled = true
218214
}
219215

@@ -276,27 +272,28 @@ func hasUpdatesBesidesFrozen(oldStorage, newStorage *Storage) (bool, string) {
276272
func (r *Storage) ValidateUpdate(old runtime.Object) error {
277273
storagelog.Info("validate update", "name", r.Name)
278274

279-
configuration := make(map[string]interface{})
280-
err := yaml.Unmarshal([]byte(r.Spec.Configuration), &configuration)
281-
if err != nil {
282-
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
283-
}
275+
var configuration schema.Configuration
284276

285-
dynConfig, err := ParseDynconfig(r.Spec.Configuration)
277+
rawYamlConfiguration := r.Spec.Configuration
278+
dynconfig, err := ParseDynconfig(r.Spec.Configuration)
286279
if err == nil {
287-
configuration = dynConfig.Config
280+
config, err := yaml.Marshal(dynconfig.Config)
281+
if err != nil {
282+
return fmt.Errorf("failed to parse .config from dynconfig, error: %w", err)
283+
}
284+
rawYamlConfiguration = string(config)
288285
}
289286

290-
hostsConfig, ok := configuration["hosts"].([]schema.Host)
291-
if !ok {
292-
return fmt.Errorf("failed to parse YAML to determine `hosts`")
287+
configuration, err = ParseConfig(rawYamlConfiguration)
288+
if err != nil {
289+
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
293290
}
294291

295292
var nodesNumber int32
296-
if len(hostsConfig) == 0 {
293+
if len(configuration.Hosts) == 0 {
297294
nodesNumber = r.Spec.Nodes
298295
} else {
299-
nodesNumber = int32(len(hostsConfig))
296+
nodesNumber = int32(len(configuration.Hosts))
300297
}
301298

302299
minNodesPerErasure := map[ErasureType]int32{
@@ -308,13 +305,8 @@ func (r *Storage) ValidateUpdate(old runtime.Object) error {
308305
return fmt.Errorf("erasure type %v requires at least %v storage nodes", r.Spec.Erasure, minNodesPerErasure[r.Spec.Erasure])
309306
}
310307

311-
domainsConfig, ok := configuration["domains_config"].(schema.DomainsConfig)
312-
if !ok {
313-
return fmt.Errorf("failed to parse YAML to determine `domains_config`")
314-
}
315-
316308
var authEnabled bool
317-
if domainsConfig.SecurityConfig.EnforceUserTokenRequirement {
309+
if configuration.DomainsConfig.SecurityConfig.EnforceUserTokenRequirement {
318310
authEnabled = true
319311
}
320312

internal/configuration/schema/configuration.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ type Dynconfig struct {
77
SelectorConfig []SelectorConfig `yaml:"selector_config"`
88
}
99
type Configuration struct {
10-
Hosts []Host `yaml:"hosts"`
11-
KeyConfig *KeyConfig `yaml:"key_config,omitempty"`
10+
DomainsConfig *DomainsConfig `yaml:"domains_config"`
11+
Hosts []Host `yaml:"hosts,omitempty"`
12+
KeyConfig *KeyConfig `yaml:"key_config,omitempty"`
1213
}
1314

1415
type Metadata struct {

internal/configuration/schema/domains.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package schema
22

33
type DomainsConfig struct {
4-
Domain map[string]interface{} `yaml:"domain"`
5-
StateStorage map[string]interface{} `yaml:"state_storage"`
6-
SecurityConfig *SecurityConfig `yaml:"security_config"`
4+
SecurityConfig *SecurityConfig `yaml:"security_config"`
75
}
86

97
type SecurityConfig struct {

internal/configuration/schema/schema_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,32 @@ import (
66

77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9-
"gopkg.in/yaml.v2"
109

1110
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
1211
"github.com/ydb-platform/ydb-kubernetes-operator/internal/configuration/schema"
1312
)
1413

1514
var configurationExample = `
1615
---
16+
yaml_config_enabled: true
17+
domains_config:
18+
domain:
19+
- name: Root
20+
storage_pool_types:
21+
- kind: ssd
22+
pool_config:
23+
box_id: 1
24+
erasure_species: block-4-2
25+
kind: ssd
26+
pdisk_filter:
27+
- property:
28+
- type: SSD
29+
vdisk_kind: Default
30+
state_storage:
31+
- ring:
32+
node: [1, 2, 3, 4, 5, 6, 7, 8]
33+
nto_select: 5
34+
ssid: 1
1735
hosts:
1836
- host: storage-0
1937
walle_location: {body: 0, data_center: 'dcExample', rack: '0'}
@@ -104,8 +122,7 @@ var _ = Describe("Testing schema", func() {
104122
})
105123

106124
It("Parse static config", func() {
107-
yamlConfig := schema.Configuration{}
108-
err := yaml.Unmarshal([]byte(configurationExample), &yamlConfig)
125+
yamlConfig, err := v1alpha1.ParseConfig(configurationExample)
109126
Expect(err).ShouldNot(HaveOccurred())
110127
hosts := []schema.Host{}
111128
for i := 0; i < 8; i++ {

internal/resources/storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R
4141

4242
var optionalBuilders []ResourceBuilder
4343

44-
dynConfig, err := api.ParseDynconfig(b.Spec.Configuration)
44+
dynconfig, err := api.ParseDynconfig(b.Spec.Configuration)
4545
if err != nil {
4646
// YDBOPS-9722 backward compatibility
4747
cfg, _ := api.BuildConfiguration(b.Unwrap(), nil)
@@ -57,7 +57,7 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R
5757
},
5858
)
5959
} else {
60-
cfg, _ := yaml.Marshal(dynConfig.Config)
60+
cfg, _ := yaml.Marshal(dynconfig.Config)
6161
optionalBuilders = append(
6262
optionalBuilders,
6363
&ConfigMapBuilder{

0 commit comments

Comments
 (0)