diff --git a/pkg/config/collector/collector.go b/pkg/config/collector/collector.go index f8b2a5ef5..beb1f6d3d 100644 --- a/pkg/config/collector/collector.go +++ b/pkg/config/collector/collector.go @@ -3,6 +3,7 @@ package collector import ( + "encoding/json" "fmt" "io" "net/http" @@ -17,7 +18,7 @@ import ( "github.com/avast/retry-go" "github.com/imdario/mergo" "github.com/itchyny/gojq" - "gopkg.in/yaml.v1" + "gopkg.in/yaml.v3" ) const DefaultHeader = "#cloud-config" @@ -290,12 +291,23 @@ func (c Config) Query(s string) (res string, err error) { s = fmt.Sprintf(".%s", s) var dat map[string]interface{} + var dat1 map[string]interface{} yamlStr, err := c.String() if err != nil { panic(err) } - if err := yaml.Unmarshal([]byte(yamlStr), &dat); err != nil { + // Marshall it so it removes the first line which cannot be parsed + err = yaml.Unmarshal([]byte(yamlStr), &dat1) + if err != nil { + panic(err) + } + // Transform it to json so its parsed correctly by gojq + b, err := json.Marshal(dat1) + if err != nil { + panic(err) + } + if err := json.Unmarshal(b, &dat); err != nil { panic(err) } diff --git a/pkg/config/collector/collector_test.go b/pkg/config/collector/collector_test.go index 9a027888b..5ed6e322f 100644 --- a/pkg/config/collector/collector_test.go +++ b/pkg/config/collector/collector_test.go @@ -297,12 +297,12 @@ options: k = (*c)["remote_key_4"].(string) Expect(k).To(Equal("remote_value_4")) - options := (*c)["options"].(map[interface{}]interface{}) + options := (*c)["options"].(Config) Expect(options["foo"]).To(Equal("bar")) Expect(options["remote_option_1"]).To(Equal("remote_option_value_1")) Expect(options["remote_option_2"]).To(Equal("remote_option_value_2")) - player := (*c)["player"].(map[interface{}]interface{}) + player := (*c)["player"].(Config) Expect(player["name"]).NotTo(Equal("Toad")) Expect(player["surname"]).To(Equal("Bros")) }) @@ -436,8 +436,11 @@ some: Expect(v).To(Equal("local_value_1\n")) v, err = c.Query("some") Expect(err).ToNot(HaveOccurred()) - // TODO: there's a bug when trying to dig some.other.key, so making the test pass this way for now, since that was not tested before - Expect(v).To(Equal("other:\n key: 3\n")) + Expect(v).To(Equal("other:\n key: 3\n")) + v, err = c.Query("some.other") + Expect(v).To(Equal("key: 3\n")) + v, err = c.Query("some.other.key") + Expect(v).To(Equal("3\n")) Expect(c.Query("options")).To(Equal("foo: bar\n")) }) })