Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,6 @@ core,github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/la
core,github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports,Apache-2.0,"Copyright 2012-2013 Rackspace, Inc | Copyright Gophercloud authors"
core,github.com/gophercloud/gophercloud/v2/openstack/utils,Apache-2.0,"Copyright 2012-2013 Rackspace, Inc | Copyright Gophercloud authors"
core,github.com/gophercloud/gophercloud/v2/pagination,Apache-2.0,"Copyright 2012-2013 Rackspace, Inc | Copyright Gophercloud authors"
core,github.com/gorilla/handlers,BSD-3-Clause,Copyright (c) 2023 The Gorilla Authors. All rights reserved
core,github.com/gorilla/mux,BSD-3-Clause,Copyright (c) 2023 The Gorilla Authors. All rights reserved
core,github.com/gorilla/websocket,BSD-2-Clause,Copyright (c) 2013 The Gorilla WebSocket Authors. All rights reserved | Gary Burd <gary@beagledreams.com> | Google LLC (https://opensource.google.com/) | Joachim Bauch <mail@joachim-bauch.de>
core,github.com/gosnmp/gosnmp,BSD-2-Clause,Copyright 2012-2020 The GoSNMP Authors. All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"net/http"
"time"

"github.com/gorilla/mux"

v1 "github.com/DataDog/datadog-agent/cmd/agent/subcommands/run/internal/clcrunnerapi/v1"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery"
ipc "github.com/DataDog/datadog-agent/comp/core/ipc/def"
Expand All @@ -33,18 +31,20 @@ var clcListener net.Listener
// StartCLCRunnerServer creates the router and starts the HTTP server
func StartCLCRunnerServer(extraHandlers map[string]http.Handler, ac autodiscovery.Component, ipc ipc.Component) error {
// create the root HTTP router
r := mux.NewRouter()
mux := http.NewServeMux()

// IPC REST API server
v1.SetupHandlers(r.PathPrefix("/api/v1").Subrouter(), ac)
v1Mux := http.NewServeMux()
v1.SetupHandlers(v1Mux, ac)
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", v1Mux))

// Register extra hanlders
for path, handler := range extraHandlers {
r.Handle(path, handler)
mux.Handle(path, handler)
}

// Validate token for every request
r.Use(validateCLCRunnerToken)
r := validateCLCRunnerToken(mux)

// get the transport we're going to use under HTTP
var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"maps"
"net/http"

"github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/comp/core/autodiscovery"
"github.com/DataDog/datadog-agent/pkg/api/version"
checkid "github.com/DataDog/datadog-agent/pkg/collector/check/id"
Expand All @@ -32,12 +30,12 @@ import (
// The API is only meant to expose stats used by the Cluster Agent
// Check configs and any data that could contain sensitive information
// MUST NEVER be sent via this API
func SetupHandlers(r *mux.Router, ac autodiscovery.Component) {
r.HandleFunc("/clcrunner/version", version.Get).Methods("GET")
r.HandleFunc("/clcrunner/stats", func(w http.ResponseWriter, r *http.Request) {
func SetupHandlers(r *http.ServeMux, ac autodiscovery.Component) {
r.HandleFunc("GET /clcrunner/version", version.Get)
r.HandleFunc("GET /clcrunner/stats", func(w http.ResponseWriter, r *http.Request) {
getCLCRunnerStats(w, r, ac)
}).Methods("GET")
r.HandleFunc("/clcrunner/workers", getCLCRunnerWorkers).Methods("GET")
})
r.HandleFunc("GET /clcrunner/workers", getCLCRunnerWorkers)
}

// getCLCRunnerStats retrieves Cluster Level Check runners stats
Expand Down
4 changes: 2 additions & 2 deletions cmd/cluster-agent-cloudfoundry/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"regexp"
Expand All @@ -20,7 +21,6 @@ import (

"code.cloudfoundry.org/bbs"
"github.com/cloudfoundry-community/go-cfclient/v2"
"github.com/gorilla/mux"
"github.com/spf13/cobra"

"github.com/DataDog/datadog-agent/cmd/agent/common"
Expand Down Expand Up @@ -244,7 +244,7 @@ func run(
var clusterCheckHandler *clusterchecksHandler.Handler
clusterCheckHandler, err = setupClusterCheck(mainCtx, ac, taggerComp)
if err == nil {
api.ModifyAPIRouter(func(r *mux.Router) {
api.ModifyAPIRouter(func(r *http.ServeMux) {
dcav1.InstallChecksEndpoints(r, clusteragent.ServerContext{ClusterCheckHandler: clusterCheckHandler})
})

Expand Down
48 changes: 23 additions & 25 deletions cmd/cluster-agent/api/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"io"
"net/http"

"github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/cmd/agent/common/signals"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery"
diagnose "github.com/DataDog/datadog-agent/comp/core/diagnose/def"
Expand All @@ -40,32 +38,32 @@ import (
)

// SetupHandlers adds the specific handlers for cluster agent endpoints
func SetupHandlers(r *mux.Router, wmeta workloadmeta.Component, ac autodiscovery.Component, statusComponent status.Component, settings settings.Component, taggerComp tagger.Component, diagnoseComponent diagnose.Component, dcametadataComp dcametadata.Component, clusterChecksMetadataComp clusterchecksmetadata.Component, ipc ipc.Component) {
r.HandleFunc("/version", getVersion).Methods("GET")
r.HandleFunc("/hostname", getHostname).Methods("GET")
r.HandleFunc("/flare", func(w http.ResponseWriter, r *http.Request) {
func SetupHandlers(r *http.ServeMux, wmeta workloadmeta.Component, ac autodiscovery.Component, statusComponent status.Component, settings settings.Component, taggerComp tagger.Component, diagnoseComponent diagnose.Component, dcametadataComp dcametadata.Component, clusterChecksMetadataComp clusterchecksmetadata.Component, ipc ipc.Component) {
r.HandleFunc("GET /version", getVersion)
r.HandleFunc("GET /hostname", getHostname)
r.HandleFunc("POST /flare", func(w http.ResponseWriter, r *http.Request) {
makeFlare(w, r, statusComponent, diagnoseComponent, ipc)
}).Methods("POST")
r.HandleFunc("/stop", stopAgent).Methods("POST")
r.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { getStatus(w, r, statusComponent) }).Methods("GET")
r.HandleFunc("/status/health", getHealth).Methods("GET")
r.HandleFunc("/config-check", func(w http.ResponseWriter, r *http.Request) {
})
r.HandleFunc("POST /stop", stopAgent)
r.HandleFunc("GET /status", func(w http.ResponseWriter, r *http.Request) { getStatus(w, r, statusComponent) })
r.HandleFunc("GET /status/health", getHealth)
r.HandleFunc("GET /config-check", func(w http.ResponseWriter, r *http.Request) {
getConfigCheck(w, r, ac)
}).Methods("GET")
r.HandleFunc("/config", settings.GetFullConfig("")).Methods("GET")
r.HandleFunc("/config/without-defaults", settings.GetFullConfigWithoutDefaults("")).Methods("GET")
r.HandleFunc("/config/by-source", settings.GetFullConfigBySource()).Methods("GET")
r.HandleFunc("/config/list-runtime", settings.ListConfigurable).Methods("GET")
r.HandleFunc("/config/{setting}", settings.GetValue).Methods("GET")
r.HandleFunc("/config/{setting}", settings.SetValue).Methods("POST")
r.HandleFunc("/autoscaler-list", func(w http.ResponseWriter, r *http.Request) { getAutoscalerList(w, r) }).Methods("GET")
r.HandleFunc("/local-autoscaling-check", func(w http.ResponseWriter, r *http.Request) { getLocalAutoscalingWorkloadCheck(w, r) }).Methods("GET")
r.HandleFunc("/tagger-list", func(w http.ResponseWriter, r *http.Request) { getTaggerList(w, r, taggerComp) }).Methods("GET")
r.HandleFunc("/workload-list", func(w http.ResponseWriter, r *http.Request) {
})
r.HandleFunc("GET /config", settings.GetFullConfig(""))
r.HandleFunc("GET /config/without-defaults", settings.GetFullConfigWithoutDefaults(""))
r.HandleFunc("GET /config/by-source", settings.GetFullConfigBySource())
r.HandleFunc("GET /config/list-runtime", settings.ListConfigurable)
r.HandleFunc("GET /config/{setting}", settings.GetValue)
r.HandleFunc("POST /config/{setting}", settings.SetValue)
r.HandleFunc("GET /autoscaler-list", func(w http.ResponseWriter, r *http.Request) { getAutoscalerList(w, r) })
r.HandleFunc("GET /local-autoscaling-check", func(w http.ResponseWriter, r *http.Request) { getLocalAutoscalingWorkloadCheck(w, r) })
r.HandleFunc("GET /tagger-list", func(w http.ResponseWriter, r *http.Request) { getTaggerList(w, r, taggerComp) })
r.HandleFunc("GET /workload-list", func(w http.ResponseWriter, r *http.Request) {
getWorkloadList(w, r, wmeta)
}).Methods("GET")
r.HandleFunc("/metadata/cluster-agent", dcametadataComp.WritePayloadAsJSON).Methods("GET")
r.HandleFunc("/metadata/cluster-checks", clusterChecksMetadataComp.WritePayloadAsJSON).Methods("GET")
})
r.HandleFunc("GET /metadata/cluster-agent", dcametadataComp.WritePayloadAsJSON)
r.HandleFunc("GET /metadata/cluster-checks", clusterChecksMetadataComp.WritePayloadAsJSON)

// Special handler to compute running agent Code coverage
coverage.SetupCoverageHandler(r)
Expand Down
28 changes: 13 additions & 15 deletions cmd/cluster-agent/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"strings"
"time"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"google.golang.org/grpc"

Expand All @@ -44,6 +42,7 @@ import (
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
dcametadata "github.com/DataDog/datadog-agent/comp/metadata/clusteragent/def"
clusterchecksmetadata "github.com/DataDog/datadog-agent/comp/metadata/clusterchecks/def"
apiMiddleware "github.com/DataDog/datadog-agent/pkg/api/middleware"

"github.com/DataDog/datadog-agent/pkg/api/util"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
Expand All @@ -55,15 +54,16 @@ import (

var (
listener net.Listener
router *mux.Router
apiRouter *mux.Router
router *http.ServeMux
apiRouter *http.ServeMux
)

// StartServer creates the router and starts the HTTP server
func StartServer(ctx context.Context, w workloadmeta.Component, taggerComp tagger.Component, ac autodiscovery.Component, statusComponent status.Component, settings settings.Component, cfg config.Component, ipc ipc.Component, diagnoseComponent diagnose.Component, dcametadataComp dcametadata.Component, clusterChecksMetadataComp clusterchecksmetadata.Component, telemetry telemetry.Component) error {
// create the root HTTP router
router = mux.NewRouter()
apiRouter = router.PathPrefix("/api/v1").Subrouter()
router = http.NewServeMux()
apiRouter = http.NewServeMux()
router.Handle("/api/v1/", http.StripPrefix("/api/v1", apiRouter))
Comment thread
pgimalac marked this conversation as resolved.

// IPC REST API server
agent.SetupHandlers(router, w, ac, statusComponent, settings, taggerComp, diagnoseComponent, dcametadataComp, clusterChecksMetadataComp, ipc)
Expand All @@ -75,11 +75,12 @@ func StartServer(ctx context.Context, w workloadmeta.Component, taggerComp tagge
languagedetection.InstallLanguageDetectionEndpoints(ctx, apiRouter, w, cfg)

// API V2 Series APIs
v2ApiRouter := router.PathPrefix("/api/v2").Subrouter()
v2ApiRouter := http.NewServeMux()
router.Handle("/api/v2/", http.StripPrefix("/api/v2", v2ApiRouter))
series.InstallNodeMetricsEndpoints(ctx, v2ApiRouter, cfg)

// Validate token for every request
router.Use(validateToken(ipc))
httpHandler := validateToken(ipc)(router)

// get the transport we're going to use under HTTP
var err error
Expand Down Expand Up @@ -139,10 +140,7 @@ func StartServer(ctx context.Context, w workloadmeta.Component, taggerComp tagge
grpcSrv,
// Use a recovery handler to log panics if they happen.
// The client will receive a 500 error.
handlers.RecoveryHandler(
handlers.PrintRecoveryStack(true),
handlers.RecoveryLogger(errorLog),
)(router),
apiMiddleware.RecoveryHandler(errorLog)(httpHandler),
timeout,
)
srv.ErrorLog = errorLog
Expand All @@ -154,12 +152,12 @@ func StartServer(ctx context.Context, w workloadmeta.Component, taggerComp tagge
}

// ModifyAPIRouter allows to pass in a function to modify router used in server
func ModifyAPIRouter(f func(*mux.Router)) {
func ModifyAPIRouter(f func(*http.ServeMux)) {
f(apiRouter)
}

// ModifyRootRouter allows to pass in a function to modify the root router used in server
func ModifyRootRouter(f func(*mux.Router)) {
func ModifyRootRouter(f func(*http.ServeMux)) {
f(router)
}

Expand All @@ -173,7 +171,7 @@ func StopServer() {

// We only want to maintain 1 API and expose an external route to serve the cluster level metadata.
// As we have 2 different tokens for the validation, we need to validate accordingly.
func validateToken(ipc ipc.Component) mux.MiddlewareFunc {
func validateToken(ipc ipc.Component) func(http.Handler) http.Handler {
dcaTokenValidator := util.TokenValidator(util.GetDCAAuthToken)
localTokenGetter := util.TokenValidator(ipc.GetAuthToken)

Expand Down
22 changes: 9 additions & 13 deletions cmd/cluster-agent/api/v1/cloudfoundry_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"encoding/json"
"net/http"

"github.com/gorilla/mux"

workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/clusteragent/api"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
Expand All @@ -34,7 +32,7 @@ func NewCloudFoundryMetadataHandler(bbsCache cloudfoundry.BBSCacheI, ccCache clo
}
}

func installCloudFoundryMetadataEndpoints(r *mux.Router) {
func installCloudFoundryMetadataEndpoints(r *http.ServeMux) {
// Get the Cloud Foundry caches for the metadata handlers
bbsCache, err := cloudfoundry.GetGlobalBBSCache()
if err != nil {
Expand All @@ -47,23 +45,22 @@ func installCloudFoundryMetadataEndpoints(r *mux.Router) {

handler := NewCloudFoundryMetadataHandler(bbsCache, ccCache)

r.HandleFunc("/tags/cf/apps/{nodeName}", api.WithTelemetryWrapper("getCFAppsMetadataForNode", handler.getCFAppsMetadataForNode)).Methods("GET")
r.HandleFunc("GET /tags/cf/apps/{nodeName}", api.WithTelemetryWrapper("getCFAppsMetadataForNode", handler.getCFAppsMetadataForNode))

if pkgconfigsetup.Datadog().GetBool("cluster_agent.serve_nozzle_data") {
r.HandleFunc("/cf/apps/{guid}", api.WithTelemetryWrapper("getCFApplication", handler.getCFApplication)).Methods("GET")
r.HandleFunc("/cf/apps", api.WithTelemetryWrapper("getCFApplications", handler.getCFApplications)).Methods("GET")
r.HandleFunc("/cf/org_quotas", api.WithTelemetryWrapper("getCFOrgQuotas", handler.getCFOrgQuotas)).Methods("GET")
r.HandleFunc("/cf/orgs", api.WithTelemetryWrapper("getCFOrgs", handler.getCFOrgs)).Methods("GET")
r.HandleFunc("GET /cf/apps/{guid}", api.WithTelemetryWrapper("getCFApplication", handler.getCFApplication))
r.HandleFunc("GET /cf/apps", api.WithTelemetryWrapper("getCFApplications", handler.getCFApplications))
r.HandleFunc("GET /cf/org_quotas", api.WithTelemetryWrapper("getCFOrgQuotas", handler.getCFOrgQuotas))
r.HandleFunc("GET /cf/orgs", api.WithTelemetryWrapper("getCFOrgs", handler.getCFOrgs))
}
}

func installKubernetesMetadataEndpoints(r *mux.Router, w workloadmeta.Component) {}
func installKubernetesMetadataEndpoints(r *http.ServeMux, w workloadmeta.Component) {}

// getCFAppsMetadataForNode is only used when the node agent hits the DCA for the list of cloudfoundry applications tags
// It return a list of tags for each application that can be directly used in the tagger
func (h *CloudFoundryMetadataHandler) getCFAppsMetadataForNode(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
nodename := vars["nodeName"]
nodename := r.PathValue("nodeName")

if h.bbsCache == nil {
log.Errorf("BBS cache is not initialized")
Expand Down Expand Up @@ -123,8 +120,7 @@ func (h *CloudFoundryMetadataHandler) getCFApplications(w http.ResponseWriter, r
// getCFApplication is only used when the PCF firehose nozzle hits the DCA for a single cloudfoundry application
// It return a single CFApplication with the given guid
func (h *CloudFoundryMetadataHandler) getCFApplication(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
guid := vars["guid"]
guid := r.PathValue("guid")

if h.ccCache == nil {
log.Errorf("CC cache is not initialized")
Expand Down
7 changes: 4 additions & 3 deletions cmd/cluster-agent/api/v1/cloudfoundry_metadata_nocompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
package v1

import (
"net/http"

workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/gorilla/mux"
)

func installCloudFoundryMetadataEndpoints(_ *mux.Router) {}
func installCloudFoundryMetadataEndpoints(_ *http.ServeMux) {}

func installKubernetesMetadataEndpoints(_ *mux.Router, _ workloadmeta.Component) {}
func installKubernetesMetadataEndpoints(_ *http.ServeMux, _ workloadmeta.Component) {}
23 changes: 9 additions & 14 deletions cmd/cluster-agent/api/v1/clusterchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"net"
"net/http"

"github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/pkg/clusteragent"
"github.com/DataDog/datadog-agent/pkg/clusteragent/api"
cctypes "github.com/DataDog/datadog-agent/pkg/clusteragent/clusterchecks/types"
Expand All @@ -23,12 +21,12 @@ import (
)

// Install registers v1 API endpoints
func installClusterCheckEndpoints(r *mux.Router, sc clusteragent.ServerContext) {
r.HandleFunc("/clusterchecks/status/{identifier}", api.WithTelemetryWrapper("postCheckStatus", postCheckStatus(sc))).Methods("POST")
r.HandleFunc("/clusterchecks/configs/{identifier}", api.WithTelemetryWrapper("getCheckConfigs", getCheckConfigs(sc))).Methods("GET")
r.HandleFunc("/clusterchecks/rebalance", api.WithTelemetryWrapper("postRebalanceChecks", postRebalanceChecks(sc))).Methods("POST")
r.HandleFunc("/clusterchecks", api.WithTelemetryWrapper("getState", getState(sc))).Methods("GET")
r.HandleFunc("/clusterchecks/isolate/check/{identifier}", api.WithTelemetryWrapper("postIsolateCheck", postIsolateCheck(sc))).Methods("POST")
func installClusterCheckEndpoints(r *http.ServeMux, sc clusteragent.ServerContext) {
r.HandleFunc("POST /clusterchecks/status/{identifier}", api.WithTelemetryWrapper("postCheckStatus", postCheckStatus(sc)))
r.HandleFunc("GET /clusterchecks/configs/{identifier}", api.WithTelemetryWrapper("getCheckConfigs", getCheckConfigs(sc)))
r.HandleFunc("POST /clusterchecks/rebalance", api.WithTelemetryWrapper("postRebalanceChecks", postRebalanceChecks(sc)))
r.HandleFunc("GET /clusterchecks", api.WithTelemetryWrapper("getState", getState(sc)))
r.HandleFunc("POST /clusterchecks/isolate/check/{identifier}", api.WithTelemetryWrapper("postIsolateCheck", postIsolateCheck(sc)))
}

// RebalancePostPayload struct is for the JSON messages received from a client POST request
Expand All @@ -47,8 +45,7 @@ func postCheckStatus(sc clusteragent.ServerContext) func(w http.ResponseWriter,
return
}

vars := mux.Vars(r)
identifier := vars["identifier"]
identifier := r.PathValue("identifier")

decoder := json.NewDecoder(r.Body)
var status cctypes.NodeStatus
Expand Down Expand Up @@ -80,8 +77,7 @@ func getCheckConfigs(sc clusteragent.ServerContext) func(w http.ResponseWriter,
return
}

vars := mux.Vars(r)
identifier := vars["identifier"]
identifier := r.PathValue("identifier")
response, err := sc.ClusterCheckHandler.GetConfigs(identifier)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -132,8 +128,7 @@ func postIsolateCheck(sc clusteragent.ServerContext) func(w http.ResponseWriter,
return
}

vars := mux.Vars(r)
isolateCheckID := vars["identifier"]
isolateCheckID := r.PathValue("identifier")

response := sc.ClusterCheckHandler.IsolateCheck(isolateCheckID)

Expand Down
4 changes: 2 additions & 2 deletions cmd/cluster-agent/api/v1/clusterchecks_nocompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
package v1

import (
"github.com/gorilla/mux"
"net/http"

"github.com/DataDog/datadog-agent/pkg/clusteragent"
)

// installClusterCheckEndpoints not implemented
func installClusterCheckEndpoints(_ *mux.Router, _ clusteragent.ServerContext) {}
func installClusterCheckEndpoints(_ *http.ServeMux, _ clusteragent.ServerContext) {}
Loading
Loading