Skip to content

Commit

Permalink
chore: move GetVersion in its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
pgimalac committed Mar 6, 2025
1 parent 8af78c4 commit 0e413aa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
11 changes: 0 additions & 11 deletions cmd/agent/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
package common

import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"

"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/config/settings"
settingshttp "github.com/DataDog/datadog-agent/pkg/config/settings/http"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/util/defaultpaths"
"github.com/DataDog/datadog-agent/pkg/version"
)

// GetPythonPaths returns the paths (in order of precedence) from where the agent
Expand All @@ -33,14 +30,6 @@ func GetPythonPaths() []string {
}
}

// GetVersion returns the version of the agent in a http response json
func GetVersion(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
av, _ := version.Agent()
j, _ := json.Marshal(av)
w.Write(j)
}

// NewSettingsClient returns a configured runtime settings client.
func NewSettingsClient() (settings.Client, error) {
ipcAddress, err := pkgconfigsetup.GetIPCAddress(pkgconfigsetup.Datadog())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/cmd/agent/common"
"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"
"github.com/DataDog/datadog-agent/pkg/status"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand All @@ -32,7 +32,7 @@ import (
// 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", common.GetVersion).Methods("GET")
r.HandleFunc("/clcrunner/version", version.Get).Methods("GET")
r.HandleFunc("/clcrunner/stats", func(w http.ResponseWriter, r *http.Request) {
getCLCRunnerStats(w, r, ac)
}).Methods("GET")
Expand Down
4 changes: 2 additions & 2 deletions cmd/security-agent/api/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (

"github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/cmd/agent/common"
"github.com/DataDog/datadog-agent/cmd/agent/common/signals"
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/settings"
"github.com/DataDog/datadog-agent/comp/core/status"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
apiutil "github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/api/version"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/flare/securityagent"
"github.com/DataDog/datadog-agent/pkg/status/health"
Expand Down Expand Up @@ -48,7 +48,7 @@ func NewAgent(statusComponent status.Component, settings settings.Component, wme

// SetupHandlers adds the specific handlers for /agent endpoints
func (a *Agent) SetupHandlers(r *mux.Router) {
r.HandleFunc("/version", common.GetVersion).Methods("GET")
r.HandleFunc("/version", version.Get).Methods("GET")
r.HandleFunc("/flare", a.makeFlare).Methods("POST")
r.HandleFunc("/hostname", a.getHostname).Methods("GET")
r.HandleFunc("/stop", a.stopAgent).Methods("POST")
Expand Down
4 changes: 2 additions & 2 deletions comp/api/commonendpoints/impl/common_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"encoding/json"
"net/http"

"github.com/DataDog/datadog-agent/cmd/agent/common"
"github.com/DataDog/datadog-agent/cmd/agent/common/signals"
api "github.com/DataDog/datadog-agent/comp/api/api/def"
"github.com/DataDog/datadog-agent/pkg/api/version"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
Expand All @@ -27,7 +27,7 @@ type Provider struct {
// CommonEndpointProvider return a filled Provider struct
func CommonEndpointProvider() Provider {
return Provider{
VersionEndpoint: api.NewAgentEndpointProvider(common.GetVersion, "/version", "GET"),
VersionEndpoint: api.NewAgentEndpointProvider(version.Get, "/version", "GET"),
HostnameEndpoint: api.NewAgentEndpointProvider(getHostname, "/hostname", "GET"),
StopEndpoint: api.NewAgentEndpointProvider(stopAgent, "/stop", "POST"),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/DataDog/datadog-agent/pkg/util/filesystem v0.61.0
github.com/DataDog/datadog-agent/pkg/util/log v0.64.0-devel
github.com/DataDog/datadog-agent/pkg/util/system v0.61.0
github.com/DataDog/datadog-agent/pkg/version v0.62.3
github.com/gofrs/flock v0.12.1
github.com/stretchr/testify v1.10.0
)
Expand All @@ -35,7 +36,6 @@ require (
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.62.3 // indirect
github.com/DataDog/datadog-agent/pkg/util/system/socket v0.61.0 // indirect
github.com/DataDog/datadog-agent/pkg/util/winutil v0.61.0 // indirect
github.com/DataDog/datadog-agent/pkg/version v0.62.3 // indirect
github.com/DataDog/viper v1.14.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
Expand Down
22 changes: 22 additions & 0 deletions pkg/api/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

// Package version contains helpers to return the agent version in the API
package version

import (
"encoding/json"
"net/http"

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

// Get returns the version of the agent in a http response json
func Get(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
av, _ := version.Agent()
j, _ := json.Marshal(av)
w.Write(j)
}

0 comments on commit 0e413aa

Please sign in to comment.