config.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package orchestration
  2. import (
  3. "encoding/json"
  4. "github.com/cloudflare/cloudflared/config"
  5. "github.com/cloudflare/cloudflared/ingress"
  6. )
  7. type newRemoteConfig struct {
  8. ingress.RemoteConfig
  9. // Add more fields when we support other settings in tunnel orchestration
  10. }
  11. type newLocalConfig struct {
  12. RemoteConfig ingress.RemoteConfig
  13. ConfigurationFlags map[string]string `json:"__configuration_flags,omitempty"`
  14. }
  15. // Config is the original config as read and parsed by cloudflared.
  16. type Config struct {
  17. Ingress *ingress.Ingress
  18. WarpRouting ingress.WarpRoutingConfig
  19. OriginDialerService *ingress.OriginDialerService
  20. // Extra settings used to configure this instance but that are not eligible for remotely management
  21. // ie. (--protocol, --loglevel, ...)
  22. ConfigurationFlags map[string]string
  23. }
  24. func (rc *newLocalConfig) MarshalJSON() ([]byte, error) {
  25. var r = struct {
  26. ConfigurationFlags map[string]string `json:"__configuration_flags,omitempty"`
  27. ingress.RemoteConfigJSON
  28. }{
  29. ConfigurationFlags: rc.ConfigurationFlags,
  30. RemoteConfigJSON: ingress.RemoteConfigJSON{
  31. // UI doesn't support top level configs, so we reconcile to individual ingress configs.
  32. GlobalOriginRequest: nil,
  33. IngressRules: convertToUnvalidatedIngressRules(rc.RemoteConfig.Ingress),
  34. WarpRouting: rc.RemoteConfig.WarpRouting.RawConfig(),
  35. },
  36. }
  37. return json.Marshal(r)
  38. }
  39. func convertToUnvalidatedIngressRules(i ingress.Ingress) []config.UnvalidatedIngressRule {
  40. result := make([]config.UnvalidatedIngressRule, 0)
  41. for _, rule := range i.Rules {
  42. var path string
  43. if rule.Path != nil {
  44. path = rule.Path.String()
  45. }
  46. newRule := config.UnvalidatedIngressRule{
  47. Hostname: rule.Hostname,
  48. Path: path,
  49. Service: rule.Service.String(),
  50. OriginRequest: ingress.ConvertToRawOriginConfig(rule.Config),
  51. }
  52. result = append(result, newRule)
  53. }
  54. return result
  55. }