Skip to content

Commit 1528a14

Browse files
committed
Revert "fix panic on GetHostFromConfigEndpoint"
This reverts commit 90969e2.
1 parent 90969e2 commit 1528a14

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

api/v1alpha1/configuration.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,12 @@ func BuildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
122122
return yaml.Marshal(config)
123123
}
124124

125-
func ParseConfiguration(rawYamlConfiguration string) (schema.Configuration, error) {
126-
configuration := schema.Configuration{}
127-
128-
dynconfig, err := ParseDynconfig(rawYamlConfiguration)
129-
if err == nil {
130-
config, err := yaml.Marshal(dynconfig.Config)
131-
if err != nil {
132-
return configuration, err
133-
}
134-
rawYamlConfiguration = string(config)
135-
}
136-
125+
func ParseConfig(rawYamlConfiguration string) (schema.Configuration, error) {
126+
config := schema.Configuration{}
137127
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
138128
dec.KnownFields(false)
139-
err = dec.Decode(&configuration)
140-
141-
return configuration, err
129+
err := dec.Decode(&config)
130+
return config, err
142131
}
143132

144133
func ParseDynconfig(rawYamlConfiguration string) (schema.Dynconfig, error) {

api/v1alpha1/storage_webhook.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/google/go-cmp/cmp"
99
"github.com/google/go-cmp/cmp/cmpopts"
10+
"gopkg.in/yaml.v3"
1011
corev1 "k8s.io/api/core/v1"
1112
"k8s.io/apimachinery/pkg/runtime"
1213
"k8s.io/utils/strings/slices"
@@ -61,12 +62,15 @@ func (r *Storage) GetGRPCServiceEndpoint() string {
6162
}
6263

6364
func (r *Storage) GetHostFromConfigEndpoint() string {
64-
var configuration schema.Configuration
65+
configuration := make(map[string]interface{})
6566

6667
// skip handle error because we already checked in webhook
67-
configuration, _ = ParseConfiguration(r.Spec.Configuration)
68-
randNum := rand.Intn(len(configuration.Hosts)) // #nosec G404
69-
return fmt.Sprintf("%s:%d", configuration.Hosts[randNum].Host, GRPCPort)
68+
_ = yaml.Unmarshal([]byte(r.Spec.Configuration), &configuration)
69+
hostsConfig := configuration["hosts"].([]schema.Host)
70+
71+
randNum := rand.Int31n(r.Spec.Nodes) // #nosec G404
72+
host := hostsConfig[randNum].Host
73+
return fmt.Sprintf("%s:%d", host, GRPCPort)
7074
}
7175

7276
func (r *Storage) IsStorageEndpointSecure() bool {
@@ -177,9 +181,19 @@ func (r *Storage) ValidateCreate() error {
177181

178182
var configuration schema.Configuration
179183

180-
configuration, err := ParseConfiguration(r.Spec.Configuration)
184+
rawYamlConfiguration := r.Spec.Configuration
185+
dynconfig, err := ParseDynconfig(r.Spec.Configuration)
186+
if err == nil {
187+
config, err := yaml.Marshal(dynconfig.Config)
188+
if err != nil {
189+
return fmt.Errorf("failed to parse .config from dynconfig, error: %w", err)
190+
}
191+
rawYamlConfiguration = string(config)
192+
}
193+
194+
configuration, err = ParseConfig(rawYamlConfiguration)
181195
if err != nil {
182-
return fmt.Errorf("failed to parse configuration, error: %w", err)
196+
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
183197
}
184198

185199
var nodesNumber int32
@@ -264,9 +278,21 @@ func hasUpdatesBesidesFrozen(oldStorage, newStorage *Storage) (bool, string) {
264278
func (r *Storage) ValidateUpdate(old runtime.Object) error {
265279
storagelog.Info("validate update", "name", r.Name)
266280

267-
configuration, err := ParseConfiguration(r.Spec.Configuration)
281+
var configuration schema.Configuration
282+
283+
rawYamlConfiguration := r.Spec.Configuration
284+
dynconfig, err := ParseDynconfig(r.Spec.Configuration)
285+
if err == nil {
286+
config, err := yaml.Marshal(dynconfig.Config)
287+
if err != nil {
288+
return fmt.Errorf("failed to parse .config from dynconfig, error: %w", err)
289+
}
290+
rawYamlConfiguration = string(config)
291+
}
292+
293+
configuration, err = ParseConfig(rawYamlConfiguration)
268294
if err != nil {
269-
return fmt.Errorf("failed to parse configuration, error: %w", err)
295+
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
270296
}
271297

272298
var nodesNumber int32

deploy/ydb-operator/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.5.17
18+
version: 0.5.16
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "0.5.17"
24+
appVersion: "0.5.16"

internal/configuration/schema/schema_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ var _ = Describe("Testing schema", func() {
122122
Expect(err).Should(HaveOccurred())
123123
})
124124

125-
It("Parse configuration with static config", func() {
126-
yamlConfig, err := v1alpha1.ParseConfiguration(configurationExample)
125+
It("Parse static config", func() {
126+
yamlConfig, err := v1alpha1.ParseConfig(configurationExample)
127127
Expect(err).ShouldNot(HaveOccurred())
128128
hosts := []schema.Host{}
129129
for i := 0; i < 8; i++ {
@@ -149,9 +149,4 @@ var _ = Describe("Testing schema", func() {
149149
},
150150
}))
151151
})
152-
153-
It("Parse configuration with dynamic config", func() {
154-
_, err := v1alpha1.ParseConfiguration(dynconfigExample)
155-
Expect(err).ShouldNot(HaveOccurred())
156-
})
157152
})

0 commit comments

Comments
 (0)