Skip to content

Commit 8f650f2

Browse files
authored
Fix marshalling issue in config.Load() (#317)
* Add extra unmarshalling step in LoadConfig * Add extra test cases * Fix test cases * Refactor load ensembling config to separate block
1 parent 30c4b88 commit 8f650f2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

api/turing/config/config.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
// easily marshalled into JSON, which is currently the format required for experiment config.
2828
"github.com/ory/viper"
2929
"k8s.io/apimachinery/pkg/api/resource"
30+
sigyaml "sigs.k8s.io/yaml"
3031
)
3132

3233
// Quantity is an alias for resource.Quantity
@@ -495,7 +496,42 @@ func Load(filepaths ...string) (*Config, error) {
495496
if err != nil {
496497
return nil, fmt.Errorf("failed to unmarshal config values: %s", err)
497498
}
499+
config, err = loadEnsemblingSvcConfig(config, v.AllSettings())
500+
if err != nil {
501+
return nil, fmt.Errorf("Failed to load ensemblingservicek8sconfig, err %s", err)
502+
}
503+
504+
return config, nil
505+
}
506+
507+
func loadEnsemblingSvcConfig(config *Config, v map[string]interface{}) (*Config, error) {
508+
// NOTE: This section is added to parse any fields in EnsemblingServiceK8sConfig that does not
509+
// have yaml tags.
510+
// For example `certificate-authority-data` is not unmarshalled
511+
// by vipers unmarshal method.
498512

513+
clusterConfig, ok := v["clusterconfig"]
514+
if !ok {
515+
return config, nil
516+
}
517+
contents := clusterConfig.(map[string]interface{})
518+
ensemblingSvcK8sCfg, ok := contents["ensemblingservicek8sconfig"]
519+
if !ok {
520+
return config, nil
521+
}
522+
// convert back to byte string
523+
var byteForm []byte
524+
byteForm, err := yaml.Marshal(ensemblingSvcK8sCfg)
525+
if err != nil {
526+
return nil, err
527+
}
528+
// use sigyaml.Unmarshal to convert to json object then unmarshal
529+
530+
k8sConfig := mlpcluster.K8sConfig{}
531+
if err := sigyaml.Unmarshal(byteForm, &k8sConfig); err != nil {
532+
return nil, err
533+
}
534+
config.ClusterConfig.EnsemblingServiceK8sConfig = &k8sConfig
499535
return config, nil
500536
}
501537

api/turing/config/config_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,21 @@ func TestLoad(t *testing.T) {
243243
ClusterConfig: config.ClusterConfig{
244244
InClusterConfig: false,
245245
EnvironmentConfigPath: "path_to_env.yaml",
246+
EnsemblingServiceK8sConfig: &mlpcluster.K8sConfig{
247+
Name: "dev-server",
248+
Cluster: &clientcmdapiv1.Cluster{
249+
Server: "https://127.0.0.1",
250+
CertificateAuthorityData: []byte("some_string"),
251+
},
252+
AuthInfo: &clientcmdapiv1.AuthInfo{
253+
Exec: &clientcmdapiv1.ExecConfig{
254+
APIVersion: "some_api_version",
255+
Command: "some_command",
256+
InteractiveMode: clientcmdapiv1.IfAvailableExecInteractiveMode,
257+
ProvideClusterInfo: true,
258+
},
259+
},
260+
},
246261
},
247262
AlertConfig: &config.AlertConfig{
248263
GitLab: &config.GitlabConfig{
@@ -346,6 +361,21 @@ func TestLoad(t *testing.T) {
346361
ClusterConfig: config.ClusterConfig{
347362
InClusterConfig: false,
348363
EnvironmentConfigPath: "path_to_env.yaml",
364+
EnsemblingServiceK8sConfig: &mlpcluster.K8sConfig{
365+
Name: "dev-server",
366+
Cluster: &clientcmdapiv1.Cluster{
367+
Server: "https://127.0.0.1",
368+
CertificateAuthorityData: []byte("some_string"),
369+
},
370+
AuthInfo: &clientcmdapiv1.AuthInfo{
371+
Exec: &clientcmdapiv1.ExecConfig{
372+
APIVersion: "some_api_version",
373+
Command: "some_command",
374+
InteractiveMode: clientcmdapiv1.IfAvailableExecInteractiveMode,
375+
ProvideClusterInfo: true,
376+
},
377+
},
378+
},
349379
},
350380
AlertConfig: &config.AlertConfig{
351381
GitLab: &config.GitlabConfig{
@@ -469,6 +499,21 @@ func TestLoad(t *testing.T) {
469499
ClusterConfig: config.ClusterConfig{
470500
InClusterConfig: false,
471501
EnvironmentConfigPath: "env_var_path_to_env.yaml",
502+
EnsemblingServiceK8sConfig: &mlpcluster.K8sConfig{
503+
Name: "dev-server",
504+
Cluster: &clientcmdapiv1.Cluster{
505+
Server: "https://127.0.0.1",
506+
CertificateAuthorityData: []byte("some_string"),
507+
},
508+
AuthInfo: &clientcmdapiv1.AuthInfo{
509+
Exec: &clientcmdapiv1.ExecConfig{
510+
APIVersion: "some_api_version",
511+
Command: "some_command",
512+
InteractiveMode: clientcmdapiv1.IfAvailableExecInteractiveMode,
513+
ProvideClusterInfo: true,
514+
},
515+
},
516+
},
472517
},
473518
AlertConfig: &config.AlertConfig{
474519
GitLab: &config.GitlabConfig{

api/turing/config/testdata/config-1.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ Sentry:
3232
ClusterConfig:
3333
InClusterConfig: false
3434
EnvironmentConfigPath: "path_to_env.yaml"
35+
EnsemblingServiceK8sConfig:
36+
name: dev-server
37+
cluster:
38+
server: https://127.0.0.1
39+
certificate-authority-data: c29tZV9zdHJpbmc=
40+
41+
user:
42+
exec:
43+
apiVersion: some_api_version
44+
command: some_command
45+
interactiveMode: IfAvailable
46+
provideClusterInfo: true
47+
3548
Experiment:
3649
qux:
3750
quxkey1: quxval1

0 commit comments

Comments
 (0)