Skip to content

Commit 90969e2

Browse files
committed
fix panic on GetHostFromConfigEndpoint
1 parent 36ee6cf commit 90969e2

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
lines changed

api/v1alpha1/configuration.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,23 @@ 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{}
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+
127137
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
128138
dec.KnownFields(false)
129-
err := dec.Decode(&config)
130-
return config, err
139+
err = dec.Decode(&configuration)
140+
141+
return configuration, err
131142
}
132143

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

api/v1alpha1/storage_webhook.go

+8-34
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

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

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

6766
// skip handle error because we already checked in webhook
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)
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)
7470
}
7571

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

182178
var configuration schema.Configuration
183179

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)
180+
configuration, err := ParseConfiguration(r.Spec.Configuration)
195181
if err != nil {
196-
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
182+
return fmt.Errorf("failed to parse configuration, error: %w", err)
197183
}
198184

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

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)
267+
configuration, err := ParseConfiguration(r.Spec.Configuration)
294268
if err != nil {
295-
return fmt.Errorf("failed to parse .spec.configuration, error: %w", err)
269+
return fmt.Errorf("failed to parse configuration, error: %w", err)
296270
}
297271

298272
var nodesNumber int32

deploy/ydb-operator/Chart.yaml

+2-2
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.16
18+
version: 0.5.17
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.16"
24+
appVersion: "0.5.17"

internal/configuration/schema/schema_test.go

+7-2
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 static config", func() {
126-
yamlConfig, err := v1alpha1.ParseConfig(configurationExample)
125+
It("Parse configuration with static config", func() {
126+
yamlConfig, err := v1alpha1.ParseConfiguration(configurationExample)
127127
Expect(err).ShouldNot(HaveOccurred())
128128
hosts := []schema.Host{}
129129
for i := 0; i < 8; i++ {
@@ -149,4 +149,9 @@ 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+
})
152157
})

0 commit comments

Comments
 (0)