|
18 | 18 | package app |
19 | 19 |
|
20 | 20 | import ( |
21 | | - "github.com/pkg/errors" |
| 21 | + "github.com/duke-git/lancet/v2/slice" |
22 | 22 | "go.uber.org/multierr" |
23 | 23 |
|
| 24 | + "github.com/apache/dubbo-admin/pkg/common/bizerror" |
24 | 25 | "github.com/apache/dubbo-admin/pkg/config" |
25 | 26 | "github.com/apache/dubbo-admin/pkg/config/console" |
26 | 27 | "github.com/apache/dubbo-admin/pkg/config/diagnostics" |
27 | 28 | "github.com/apache/dubbo-admin/pkg/config/discovery" |
28 | 29 | "github.com/apache/dubbo-admin/pkg/config/engine" |
29 | | - "github.com/apache/dubbo-admin/pkg/config/mode" |
| 30 | + "github.com/apache/dubbo-admin/pkg/config/log" |
| 31 | + "github.com/apache/dubbo-admin/pkg/config/observability" |
30 | 32 | "github.com/apache/dubbo-admin/pkg/config/store" |
31 | 33 | ) |
32 | 34 |
|
33 | 35 | type AdminConfig struct { |
34 | 36 | config.BaseConfig |
35 | | - // Mode in which dubbo admin is running. Available values are: "test", "global", "zone" |
36 | | - Mode mode.Mode `json:"mode" envconfig:"DUBBO_MODE"` |
| 37 | + // Log configuration |
| 38 | + Log *log.Config `json:"log" yaml:"log"` |
37 | 39 | // Diagnostics configuration |
38 | | - Diagnostics *diagnostics.Config `json:"diagnostics,omitempty"` |
| 40 | + Diagnostics *diagnostics.Config `json:"diagnostics,omitempty" yaml:"diagnostics"` |
| 41 | + // Observability configuration |
| 42 | + Observability *observability.Config `json:"observability" yaml:"observability"` |
39 | 43 | // Console configuration |
40 | | - Console *console.Config `json:"admin"` |
| 44 | + Console *console.Config `json:"console" yaml:"console"` |
41 | 45 | // Store configuration |
42 | | - Store *store.Config `json:"store"` |
| 46 | + Store *store.Config `json:"store" yaml:"store"` |
43 | 47 | // Discovery configuration |
44 | | - Discovery *discovery.Config `json:"discovery"` |
| 48 | + Discovery []*discovery.Config `json:"discovery" yaml:"discovery"` |
45 | 49 | // Engine configuration |
46 | | - Engine *engine.Config `json:"engine"` |
| 50 | + Engine *engine.Config `json:"engine" yaml:"engine"` |
| 51 | + // MCP configuration |
| 52 | + MCP *MCPConfig `json:"mcp,omitempty" yaml:"mcp"` |
| 53 | +} |
| 54 | + |
| 55 | +// MCPConfig MCP配置 |
| 56 | +type MCPConfig struct { |
| 57 | + // Enabled 是否启用MCP端点 |
| 58 | + Enabled bool `json:"enabled" yaml:"enabled"` |
| 59 | + // Path MCP端点路径,默认 /api/mcp |
| 60 | + Path string `json:"path,omitempty" yaml:"path"` |
| 61 | + // APIKey MCP API密钥,用于认证。如果为空则不需要认证 |
| 62 | + APIKey string `json:"apiKey,omitempty" yaml:"apiKey"` |
47 | 63 | } |
48 | 64 |
|
49 | 65 | var _ = &AdminConfig{} |
50 | 66 |
|
51 | | -func (c *AdminConfig) Sanitize() { |
| 67 | +var DefaultAdminConfig = func() AdminConfig { |
| 68 | + return AdminConfig{ |
| 69 | + Log: log.DefaultLogConfig(), |
| 70 | + Store: store.DefaultStoreConfig(), |
| 71 | + Engine: engine.DefaultResourceEngineConfig(), |
| 72 | + Observability: observability.DefaultObservabilityConfig(), |
| 73 | + Diagnostics: diagnostics.DefaultDiagnosticsConfig(), |
| 74 | + Console: console.DefaultConsoleConfig(), |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (c AdminConfig) Sanitize() { |
52 | 79 | c.Engine.Sanitize() |
53 | | - c.Discovery.Sanitize() |
| 80 | + for _, d := range c.Discovery { |
| 81 | + d.Sanitize() |
| 82 | + } |
54 | 83 | c.Store.Sanitize() |
55 | 84 | c.Console.Sanitize() |
| 85 | + c.Observability.Sanitize() |
56 | 86 | c.Diagnostics.Sanitize() |
| 87 | + c.Log.Sanitize() |
57 | 88 | } |
58 | 89 |
|
59 | | -func (c *AdminConfig) PostProcess() error { |
| 90 | +func (c AdminConfig) PreProcess() error { |
| 91 | + discoveryPreProcess := func() error { |
| 92 | + for _, d := range c.Discovery { |
| 93 | + if err := d.PreProcess(); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + } |
| 97 | + return nil |
| 98 | + } |
| 99 | + return multierr.Combine( |
| 100 | + c.Engine.PreProcess(), |
| 101 | + discoveryPreProcess(), |
| 102 | + c.Store.PreProcess(), |
| 103 | + c.Console.PreProcess(), |
| 104 | + c.Observability.PreProcess(), |
| 105 | + c.Diagnostics.PreProcess(), |
| 106 | + c.Log.PreProcess(), |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +func (c AdminConfig) PostProcess() error { |
| 111 | + discoveryPostProcess := func() error { |
| 112 | + for _, d := range c.Discovery { |
| 113 | + if err := d.PostProcess(); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + } |
| 117 | + return nil |
| 118 | + } |
60 | 119 | return multierr.Combine( |
61 | 120 | c.Engine.PostProcess(), |
62 | | - c.Discovery.PostProcess(), |
| 121 | + discoveryPostProcess(), |
63 | 122 | c.Store.PostProcess(), |
64 | 123 | c.Console.PostProcess(), |
| 124 | + c.Observability.PostProcess(), |
65 | 125 | c.Diagnostics.PostProcess(), |
| 126 | + c.Log.PostProcess(), |
66 | 127 | ) |
67 | 128 | } |
68 | 129 |
|
69 | | -var DefaultAdminConfig = func() AdminConfig { |
70 | | - return AdminConfig{ |
71 | | - Mode: mode.Zone, |
72 | | - Store: store.DefaultStoreConfig(), |
73 | | - Engine: engine.DefaultResourceEngineConfig(), |
74 | | - Diagnostics: diagnostics.DefaultDiagnosticsConfig(), |
75 | | - Console: console.DefaultConsoleConfig(), |
76 | | - } |
77 | | -} |
78 | | - |
79 | | -func (c *AdminConfig) Validate() error { |
80 | | - if err := mode.ValidateMode(c.Mode); err != nil { |
81 | | - return errors.Wrap(err, "Mode Config validation failed") |
| 130 | +func (c AdminConfig) Validate() error { |
| 131 | + if c.Log == nil { |
| 132 | + c.Log = log.DefaultLogConfig() |
| 133 | + } else if err := c.Log.Validate(); err != nil { |
| 134 | + return bizerror.Wrap(err, bizerror.ConfigError, "log config validation failed") |
82 | 135 | } |
83 | 136 | if c.Store == nil { |
84 | 137 | c.Store = store.DefaultStoreConfig() |
85 | 138 | } else if err := c.Store.Validate(); err != nil { |
86 | | - return errors.Wrap(err, "Store Config validation failed") |
| 139 | + return bizerror.Wrap(err, bizerror.ConfigError, "store config validation failed") |
87 | 140 | } |
88 | 141 | if c.Diagnostics == nil { |
89 | 142 | c.Diagnostics = diagnostics.DefaultDiagnosticsConfig() |
90 | 143 | } else if err := c.Diagnostics.Validate(); err != nil { |
91 | | - return errors.Wrap(err, "Diagnostics Config validation failed") |
| 144 | + return bizerror.Wrap(err, bizerror.ConfigError, "diagnostics config validation failed") |
92 | 145 | } |
93 | 146 | if c.Console == nil { |
94 | 147 | c.Console = console.DefaultConsoleConfig() |
95 | 148 | } else if err := c.Console.Validate(); err != nil { |
96 | | - return errors.Wrap(err, "Admin validation failed") |
| 149 | + return bizerror.Wrap(err, bizerror.ConfigError, "console config validation failed") |
| 150 | + } |
| 151 | + if c.Observability == nil { |
| 152 | + c.Observability = observability.DefaultObservabilityConfig() |
| 153 | + } else if err := c.Observability.Validate(); err != nil { |
| 154 | + return bizerror.Wrap(err, bizerror.ConfigError, "observability config validation failed") |
| 155 | + } |
| 156 | + if c.Discovery == nil || len(c.Discovery) == 0 { |
| 157 | + return bizerror.New(bizerror.ConfigError, "discover config is needed") |
| 158 | + } |
| 159 | + for _, d := range c.Discovery { |
| 160 | + if err := d.Validate(); err != nil { |
| 161 | + return bizerror.Wrap(err, bizerror.ConfigError, "discovery config validation failed") |
| 162 | + } |
| 163 | + } |
| 164 | + discoveryIDList := slice.Map(c.Discovery, func(index int, item *discovery.Config) string { |
| 165 | + return item.ID |
| 166 | + }) |
| 167 | + if len(discoveryIDList) != len(slice.Unique(discoveryIDList)) { |
| 168 | + return bizerror.New(bizerror.ConfigError, "discovery id must be unique") |
| 169 | + } |
| 170 | + if c.Engine == nil { |
| 171 | + c.Engine = engine.DefaultResourceEngineConfig() |
| 172 | + } else if err := c.Engine.Validate(); err != nil { |
| 173 | + return bizerror.Wrap(err, bizerror.ConfigError, "engine config validation failed") |
97 | 174 | } |
98 | 175 | return nil |
99 | 176 | } |
| 177 | + |
| 178 | +// FindDiscovery finds the DiscoveryConfig by id, returns nil if not found |
| 179 | +func (c AdminConfig) FindDiscovery(id string) *discovery.Config { |
| 180 | + for _, d := range c.Discovery { |
| 181 | + if d.ID == id { |
| 182 | + return d |
| 183 | + } |
| 184 | + } |
| 185 | + return nil |
| 186 | +} |
| 187 | + |
| 188 | +// Meshes return the mesh id list of discoveries |
| 189 | +func (c AdminConfig) Meshes() []string { |
| 190 | + return slice.Map(c.Discovery, func(index int, item *discovery.Config) string { |
| 191 | + return item.ID |
| 192 | + }) |
| 193 | +} |
0 commit comments