bug: Fix agent not able to get subkeys (#1299)

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka
2023-04-15 13:45:13 +02:00
parent b8b3e83ef1
commit ef0c14006b
2 changed files with 21 additions and 6 deletions

View File

@@ -3,6 +3,7 @@
package collector package collector
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -17,7 +18,7 @@ import (
"github.com/avast/retry-go" "github.com/avast/retry-go"
"github.com/imdario/mergo" "github.com/imdario/mergo"
"github.com/itchyny/gojq" "github.com/itchyny/gojq"
"gopkg.in/yaml.v1" "gopkg.in/yaml.v3"
) )
const DefaultHeader = "#cloud-config" const DefaultHeader = "#cloud-config"
@@ -290,12 +291,23 @@ func (c Config) Query(s string) (res string, err error) {
s = fmt.Sprintf(".%s", s) s = fmt.Sprintf(".%s", s)
var dat map[string]interface{} var dat map[string]interface{}
var dat1 map[string]interface{}
yamlStr, err := c.String() yamlStr, err := c.String()
if err != nil { if err != nil {
panic(err) 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) panic(err)
} }

View File

@@ -297,12 +297,12 @@ options:
k = (*c)["remote_key_4"].(string) k = (*c)["remote_key_4"].(string)
Expect(k).To(Equal("remote_value_4")) 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["foo"]).To(Equal("bar"))
Expect(options["remote_option_1"]).To(Equal("remote_option_value_1")) Expect(options["remote_option_1"]).To(Equal("remote_option_value_1"))
Expect(options["remote_option_2"]).To(Equal("remote_option_value_2")) 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["name"]).NotTo(Equal("Toad"))
Expect(player["surname"]).To(Equal("Bros")) Expect(player["surname"]).To(Equal("Bros"))
}) })
@@ -436,8 +436,11 @@ some:
Expect(v).To(Equal("local_value_1\n")) Expect(v).To(Equal("local_value_1\n"))
v, err = c.Query("some") v, err = c.Query("some")
Expect(err).ToNot(HaveOccurred()) 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")) Expect(c.Query("options")).To(Equal("foo: bar\n"))
}) })
}) })