diff --git a/pkg/api/handler.go b/pkg/api/handler.go index 0165da9e8..3ce67acb9 100644 --- a/pkg/api/handler.go +++ b/pkg/api/handler.go @@ -50,6 +50,38 @@ type RunTimeRepresentation struct { UDPServices map[string]*runtime.UDPServiceInfo `json:"udpServices,omitempty"` } +// TODO: think about this for longer +func GetRunTimeRepresentation(runtimeConfiguration *runtime.Configuration) *RunTimeRepresentation { + siRepr := make(map[string]*serviceInfoRepresentation, len(runtimeConfiguration.Services)) + for k, v := range runtimeConfiguration.Services { + siRepr[k] = &serviceInfoRepresentation{ + ServiceInfo: v, + ServerStatus: v.GetAllStatus(), + } + } + + tcpSIRepr := make(map[string]*tcpServiceInfoRepresentation, len(runtimeConfiguration.Services)) + for k, v := range runtimeConfiguration.TCPServices { + tcpSIRepr[k] = &tcpServiceInfoRepresentation{ + TCPServiceInfo: v, + ServerStatus: v.GetAllStatus(), + } + } + + result := RunTimeRepresentation{ + Routers: runtimeConfiguration.Routers, + Middlewares: runtimeConfiguration.Middlewares, + Services: siRepr, + TCPRouters: runtimeConfiguration.TCPRouters, + TCPMiddlewares: runtimeConfiguration.TCPMiddlewares, + TCPServices: tcpSIRepr, + UDPRouters: runtimeConfiguration.UDPRouters, + UDPServices: runtimeConfiguration.UDPServices, + } + + return &result +} + // Handler serves the configuration and status of Traefik on API endpoints. type Handler struct { staticConfig static.Configuration @@ -124,32 +156,7 @@ func (h Handler) createRouter() *mux.Router { } func (h Handler) getRuntimeConfiguration(rw http.ResponseWriter, request *http.Request) { - siRepr := make(map[string]*serviceInfoRepresentation, len(h.runtimeConfiguration.Services)) - for k, v := range h.runtimeConfiguration.Services { - siRepr[k] = &serviceInfoRepresentation{ - ServiceInfo: v, - ServerStatus: v.GetAllStatus(), - } - } - - tcpSIRepr := make(map[string]*tcpServiceInfoRepresentation, len(h.runtimeConfiguration.Services)) - for k, v := range h.runtimeConfiguration.TCPServices { - tcpSIRepr[k] = &tcpServiceInfoRepresentation{ - TCPServiceInfo: v, - ServerStatus: v.GetAllStatus(), - } - } - - result := RunTimeRepresentation{ - Routers: h.runtimeConfiguration.Routers, - Middlewares: h.runtimeConfiguration.Middlewares, - Services: siRepr, - TCPRouters: h.runtimeConfiguration.TCPRouters, - TCPMiddlewares: h.runtimeConfiguration.TCPMiddlewares, - TCPServices: tcpSIRepr, - UDPRouters: h.runtimeConfiguration.UDPRouters, - UDPServices: h.runtimeConfiguration.UDPServices, - } + result := GetRunTimeRepresentation(h.runtimeConfiguration) rw.Header().Set("Content-Type", "application/json") diff --git a/pkg/updater/provider.go b/pkg/updater/provider.go index 26d1fc469..d703bd1a2 100644 --- a/pkg/updater/provider.go +++ b/pkg/updater/provider.go @@ -6,7 +6,9 @@ import ( "net/http" "github.com/rs/zerolog/log" + "github.com/traefik/traefik/v3/pkg/api" "github.com/traefik/traefik/v3/pkg/config/dynamic" + "github.com/traefik/traefik/v3/pkg/config/runtime" "github.com/traefik/traefik/v3/pkg/config/static" "github.com/traefik/traefik/v3/pkg/safe" ) @@ -24,23 +26,23 @@ func New(config *static.Configuration) *Updater { } func (u *Updater) HandleConfigUpdate(cfg dynamic.Configuration) { - body, err := json.Marshal(cfg) + runtimeConfig := runtime.NewConfig(cfg) - if err != nil { - // should never happen? + body := bytes.NewBuffer([]byte{}) + result := api.GetRunTimeRepresentation(runtimeConfig) + + if err := json.NewEncoder(body).Encode(result); err != nil { log.Error().Err(err).Msg("Error while marshalling dynamic configuration data to json") return } - requestBody := bytes.NewBuffer(body) - for _, url := range u.callbackUrls { safe.Go(func() { - resp, err := http.Post(url, "application/json", requestBody) + resp, err := http.Post(url, "application/json", body) if err != nil { log.Error().Err(err).Str("url", url).Msg("Error while sending configuration data to callback") - } else { + } else { log.Debug().Str("url", url).Msg("Configuration data sent") resp.Body.Close() }