config.go 1.9 KB

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