diff --git a/configs/example.yaml b/configs/example.yaml index 8934656..404a805 100644 --- a/configs/example.yaml +++ b/configs/example.yaml @@ -13,9 +13,22 @@ myAppID: dynamic: p6spy: false primary: master + json: > + [ + { + "hello": "hall" + }, + { + "hello": "hall" + } + ] myNamespace3: releaseKey: "20200309212653-7fec91b6d277b5ad" yml: > plain text xml: > - plain text \ No newline at end of file + plain text + yaml: > + plain text + json: > + plain text diff --git a/internal/routes/apollo/routes.go b/internal/routes/apollo/routes.go index 3b34305..e7f825f 100644 --- a/internal/routes/apollo/routes.go +++ b/internal/routes/apollo/routes.go @@ -116,7 +116,11 @@ func (a *Apollo) getNamespace(appID string, cluster string, namespace string) (w func (a *Apollo) getNamespaceConfig(extension string, namespace watcher.Namespace) (interface{}, error) { switch extension { case ".yml": + return map[string]string{"content": namespace.Yml}, nil + case ".yaml": return map[string]string{"content": namespace.Yaml}, nil + case ".json": + return map[string]string{"content": namespace.JSON}, nil case ".xml": return map[string]string{"content": namespace.XML}, nil case ".properties": diff --git a/internal/routes/apollo/routes_test.go b/internal/routes/apollo/routes_test.go index 65c7ba8..45c8103 100644 --- a/internal/routes/apollo/routes_test.go +++ b/internal/routes/apollo/routes_test.go @@ -30,16 +30,36 @@ var stubConfigs = []watcher.ConfigMap{ dynamic: p6spy: false primary: master +`, Yml: `spring: + datasource: + dynamic: + p6spy: false + primary: master `, XML: "", + JSON: `[ +{ + "abc":"lbs-test", +}, +{ + "def":"456", +} +] +`, }, "ns2": { ReleaseKey: "abc", Properties: map[string]string{}, - Yaml: `[raw] + Yml: `[raw] +key = value +`, + Yaml: `[raw] key = value `, XML: "plain text", + JSON: `[raw] +key = value +`, }, }, }, @@ -186,8 +206,8 @@ func TestGetNamespaceConfig(t *testing.T) { ) }) - t.Run("get yml", func(t *testing.T) { - cfg, err := a.getNamespaceConfig(".yml", stubConfigs[0]["app"]["cluster"]["ns"]) + t.Run("get yaml", func(t *testing.T) { + cfg, err := a.getNamespaceConfig(".yaml", stubConfigs[0]["app"]["cluster"]["ns"]) require.Nil(t, err) c, ok := cfg.(map[string]string) @@ -200,7 +220,6 @@ func TestGetNamespaceConfig(t *testing.T) { err = yaml.Unmarshal([]byte(content), y) require.Nil(t, err) b, err := yaml.Marshal(y) - t.Log(string(b)) require.Nil(t, err) require.Equal( @@ -227,8 +246,42 @@ func TestGetNamespaceConfig(t *testing.T) { ) }) - t.Run("get invalid yml", func(t *testing.T) { - cfg, err := a.getNamespaceConfig(".yml", stubConfigs[0]["app"]["cluster"]["ns2"]) + t.Run("get json", func(t *testing.T) { + cfg, err := a.getNamespaceConfig(".json", stubConfigs[0]["app"]["cluster"]["ns"]) + require.Nil(t, err) + + c, ok := cfg.(map[string]string) + require.True(t, ok) + + content, found := c["content"] + require.True(t, found) + + require.Equal( + t, + stubConfigs[0]["app"]["cluster"]["ns"].JSON, + content, + ) + }) + + t.Run("get invalid json", func(t *testing.T) { + cfg, err := a.getNamespaceConfig(".json", stubConfigs[0]["app"]["cluster"]["ns2"]) + require.Nil(t, err) + + c, ok := cfg.(map[string]string) + require.True(t, ok) + + content, found := c["content"] + require.True(t, found) + + require.Equal( + t, + stubConfigs[0]["app"]["cluster"]["ns2"].JSON, + content, + ) + }) + + t.Run("get invalid yaml", func(t *testing.T) { + cfg, err := a.getNamespaceConfig(".yaml", stubConfigs[0]["app"]["cluster"]["ns2"]) require.Nil(t, err) c, ok := cfg.(map[string]string) diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index cf349f4..2255b5e 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -2,6 +2,7 @@ package watcher import ( "context" + "encoding/json" "errors" "fmt" "os" @@ -19,7 +20,9 @@ import ( type Namespace struct { ReleaseKey string `yaml:"releaseKey" json:"releaseKey"` Properties map[string]string `yaml:"properties" json:"properties"` - Yaml string `yaml:"yml" json:"yml"` + Yml string `yaml:"yml" json:"yml"` + Yaml string `yaml:"yaml" json:"yaml"` + JSON string `yaml:"json" json:"json"` XML string `yaml:"xml" json:"xml"` } @@ -156,7 +159,7 @@ func (w *Watcher) readConfigMap(log nlogger.Provider) error { if nsKey == "" { return fmt.Errorf("invalid namespace name '%s' in %s/%s", nsKey, appKey, clusterKey) } - if ns.Properties == nil && ns.Yaml == "" && ns.XML == "" { + if ns.Properties == nil && ns.Yml == "" && ns.Yaml == "" && ns.XML == "" && ns.JSON == "" { return fmt.Errorf("invalid namespace '%s' in %s/%s", nsKey, appKey, clusterKey) } for configKey := range ns.Properties { @@ -164,6 +167,17 @@ func (w *Watcher) readConfigMap(log nlogger.Provider) error { return fmt.Errorf("invalid config key '%s' in %s/%s/%s", configKey, appKey, clusterKey, nsKey) } } + // validate Yml + if ns.Yml != "" { + cfg := make(map[interface{}]interface{}) + if err := yaml.Unmarshal([]byte(ns.Yml), &cfg); err != nil { + log.Get().Warn(fmt.Sprintf( + "failed to parse yml config for namespace '%s' in %s/%s: %s", + nsKey, appKey, clusterKey, err.Error(), + )) + } + } + // validate Yaml if ns.Yaml != "" { cfg := make(map[interface{}]interface{}) @@ -174,6 +188,17 @@ func (w *Watcher) readConfigMap(log nlogger.Provider) error { )) } } + + // validate JSON + if ns.JSON != "" { + var cfg interface{} + if err := json.Unmarshal([]byte(ns.JSON), &cfg); err != nil { + log.Get().Warn(fmt.Sprintf( + "failed to parse json config for namespace '%s' in %s/%s: %s", + nsKey, appKey, clusterKey, err.Error(), + )) + } + } } } } diff --git a/pkg/watcher/watcher_test.go b/pkg/watcher/watcher_test.go index 45c7809..fc1b231 100644 --- a/pkg/watcher/watcher_test.go +++ b/pkg/watcher/watcher_test.go @@ -23,8 +23,10 @@ var stubConfigs = []ConfigMap{ "mysql.uri": "mysql://root@localhost/mysql", "snowflake.uri": "http://192.168.0.1/snowflake", }, + Yml: "", Yaml: "", XML: "", + JSON: "", }, }, }, @@ -38,8 +40,10 @@ var stubConfigs = []ConfigMap{ "mysql.uri": "mysql://root@localhost/mysql2", "snowflake.uri": "http://192.168.0.1/snowflake", }, + Yml: "invalid yml; key = value", Yaml: "invalid yml; key = value", XML: "", + JSON: "invalid json; key = value", }, }, },