2018-10-01 15:27:59 +00:00
|
|
|
package services
|
2018-09-28 06:52:17 +00:00
|
|
|
|
|
|
|
import (
|
2018-10-01 15:05:57 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2020-12-21 18:25:32 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
2018-10-01 15:27:59 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2018-10-01 15:05:57 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-09-28 06:52:17 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
2018-10-01 15:27:59 +00:00
|
|
|
// ServicesFromFiles returns the list of agent service registration structs
|
|
|
|
// from a set of file arguments.
|
2020-05-02 00:17:27 +00:00
|
|
|
func ServicesFromFiles(ui cli.Ui, files []string) ([]*api.AgentServiceRegistration, error) {
|
2018-10-01 15:27:59 +00:00
|
|
|
// We set devMode to true so we can get the basic valid default
|
|
|
|
// configuration. devMode doesn't set any services by default so this
|
|
|
|
// is okay since we only look at services.
|
|
|
|
devMode := true
|
2020-12-21 18:46:41 +00:00
|
|
|
r, err := config.Load(config.LoadOpts{
|
2018-10-01 15:27:59 +00:00
|
|
|
ConfigFiles: files,
|
|
|
|
DevMode: &devMode,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-21 18:46:41 +00:00
|
|
|
for _, w := range r.Warnings {
|
2020-05-02 00:17:27 +00:00
|
|
|
ui.Warn(w)
|
|
|
|
}
|
2018-10-01 15:27:59 +00:00
|
|
|
|
|
|
|
// The services are now in "structs.ServiceDefinition" form and we need
|
|
|
|
// them in "api.AgentServiceRegistration" form so do the conversion.
|
2020-12-21 18:46:41 +00:00
|
|
|
services := r.RuntimeConfig.Services
|
|
|
|
result := make([]*api.AgentServiceRegistration, 0, len(services))
|
|
|
|
for _, svc := range services {
|
2018-10-01 15:27:59 +00:00
|
|
|
apiSvc, err := serviceToAgentService(svc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, apiSvc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:05:57 +00:00
|
|
|
// serviceToAgentService converts a ServiceDefinition struct to an
|
2018-09-28 06:52:17 +00:00
|
|
|
// AgentServiceRegistration API struct.
|
2018-10-01 15:05:57 +00:00
|
|
|
func serviceToAgentService(svc *structs.ServiceDefinition) (*api.AgentServiceRegistration, error) {
|
2018-10-01 02:17:45 +00:00
|
|
|
// mapstructure can do this for us, but we encapsulate it in this
|
|
|
|
// helper function in case we need to change the logic in the future.
|
2018-09-28 06:52:17 +00:00
|
|
|
var result api.AgentServiceRegistration
|
2018-10-01 15:05:57 +00:00
|
|
|
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
Result: &result,
|
|
|
|
DecodeHook: timeDurationToStringHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := d.Decode(svc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The structs version has non-pointer checks and the destination
|
|
|
|
// has pointers, so we need to set the destination to nil if there
|
2019-11-15 15:06:33 +00:00
|
|
|
// is a zero-value Check field.
|
|
|
|
if result.Check != nil && reflect.DeepEqual(*result.Check, api.AgentServiceCheck{}) {
|
2018-10-01 15:05:57 +00:00
|
|
|
result.Check = nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 20:18:15 +00:00
|
|
|
// The structs version has non-pointer Proxy.TransparentProxy and Proxy.AccessLogs
|
|
|
|
// The destination has pointers, so we need to set the destination to nil if there
|
|
|
|
// is a zero-value field.
|
|
|
|
if result.Proxy != nil && result.Proxy.TransparentProxy != nil && reflect.DeepEqual(*result.Proxy.TransparentProxy, api.TransparentProxyConfig{}) {
|
|
|
|
result.Proxy.TransparentProxy = nil
|
|
|
|
}
|
|
|
|
if result.Proxy != nil && result.Proxy.AccessLogs != nil && reflect.DeepEqual(*result.Proxy.AccessLogs, api.AccessLogsConfig{}) {
|
|
|
|
result.Proxy.AccessLogs = nil
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:05:57 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// timeDurationToStringHookFunc returns a DecodeHookFunc that converts
|
|
|
|
// time.Duration to string.
|
|
|
|
func timeDurationToStringHookFunc() mapstructure.DecodeHookFunc {
|
|
|
|
return func(
|
|
|
|
f reflect.Type,
|
|
|
|
t reflect.Type,
|
|
|
|
data interface{}) (interface{}, error) {
|
|
|
|
dur, ok := data.(time.Duration)
|
|
|
|
if !ok {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
if t.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
if dur == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert it by parsing
|
|
|
|
return data.(time.Duration).String(), nil
|
|
|
|
}
|
2018-09-28 06:52:17 +00:00
|
|
|
}
|