Skip to content

Commit c003285

Browse files
[Elastic Agent] Pick up version from libbeat (#18350) (#18559)
[Elastic Agent] Pick up version from libbeat (#18350)
1 parent 75f78d0 commit c003285

File tree

5 files changed

+35
-118
lines changed

5 files changed

+35
-118
lines changed

x-pack/elastic-agent/CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@
5656
- Do not require unnecessary configuration {pull}18003[18003]
5757
- Use nested objects so fleet can handle metadata correctly {pull}18234[18234]
5858
- Enable debug log level for Metricbeat and Filebeat when run under the Elastic Agent. {pull}17935[17935]
59+
- Pick up version from libbeat {pull}18350[18350]

x-pack/elastic-agent/magefile.go

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (Build) GenerateConfig() error {
108108
// Do not use directly, use crossBuild instead.
109109
func GolangCrossBuildOSS() error {
110110
params := devtools.DefaultGolangCrossBuildArgs()
111-
params.LDFlags = flagsSet()
111+
injectBuildVars(params.Vars)
112112
return devtools.GolangCrossBuild(params)
113113
}
114114

@@ -117,7 +117,8 @@ func GolangCrossBuildOSS() error {
117117
func GolangCrossBuild() error {
118118
params := devtools.DefaultGolangCrossBuildArgs()
119119
params.OutputDir = "build/golang-crossbuild"
120-
params.LDFlags = flagsSet()
120+
injectBuildVars(params.Vars)
121+
121122
if err := devtools.GolangCrossBuild(params); err != nil {
122123
return err
123124
}
@@ -136,32 +137,23 @@ func BuildGoDaemon() error {
136137
// BinaryOSS build the fleet artifact.
137138
func (Build) BinaryOSS() error {
138139
mg.Deps(Prepare.Env)
139-
return RunGo(
140-
"build",
141-
"-o", filepath.Join(buildDir, "elastic-agent-oss"),
142-
"-ldflags", flags(),
143-
)
140+
buildArgs := devtools.DefaultBuildArgs()
141+
buildArgs.Name = "elastic-agent-oss"
142+
buildArgs.OutputDir = buildDir
143+
injectBuildVars(buildArgs.Vars)
144+
145+
return devtools.Build(buildArgs)
144146
}
145147

146148
// Binary build the fleet artifact.
147149
func (Build) Binary() error {
148150
mg.Deps(Prepare.Env)
149-
return RunGo(
150-
"build",
151-
"-o", filepath.Join(buildDir, "elastic-agent"),
152-
"-ldflags", flags(),
153-
)
154-
}
155151

156-
// Dev make a special build with the Dev tags.
157-
func (Build) Dev() error {
158-
mg.Deps(Prepare.Env)
159-
return RunGo(
160-
"build",
161-
"-tags", "dev",
162-
"-o", filepath.Join(buildDir, "elastic-agent"),
163-
"-ldflags", flags(),
164-
)
152+
buildArgs := devtools.DefaultBuildArgs()
153+
buildArgs.OutputDir = buildDir
154+
injectBuildVars(buildArgs.Vars)
155+
156+
return devtools.Build(buildArgs)
165157
}
166158

167159
// Clean up dev environment.
@@ -325,22 +317,6 @@ func commitID() string {
325317
return commitID
326318
}
327319

328-
func flags() string {
329-
return strings.Join(flagsSet(), " ")
330-
}
331-
332-
func flagsSet() []string {
333-
ts := time.Now().Format(time.RFC3339)
334-
commitID := commitID()
335-
isSnapshot, _ := os.LookupEnv(snapshotEnv)
336-
337-
return []string{
338-
fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.buildTime=%s"`, ts),
339-
fmt.Sprintf(`-X "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.commit=%s"`, commitID),
340-
fmt.Sprintf(` -X "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.snapshot=%s"`, isSnapshot),
341-
}
342-
}
343-
344320
// Update is an alias for executing fields, dashboards, config, includes.
345321
func Update() {
346322
mg.SerialDeps(Config, BuildSpec, BuildFleetCfg)
@@ -540,3 +516,18 @@ func dockerTag() string {
540516

541517
return tagBase
542518
}
519+
520+
func buildVars() map[string]string {
521+
vars := make(map[string]string)
522+
523+
isSnapshot, _ := os.LookupEnv(snapshotEnv)
524+
vars["github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release.snapshot"] = isSnapshot
525+
526+
return vars
527+
}
528+
529+
func injectBuildVars(m map[string]string) {
530+
for k, v := range buildVars() {
531+
m[k] = v
532+
}
533+
}

x-pack/elastic-agent/pkg/release/release_dev.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

x-pack/elastic-agent/pkg/release/version.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,26 @@ package release
77
import (
88
"strconv"
99
"time"
10-
)
11-
12-
// version is the current version of the elastic-agent.
13-
var version = "7.8.0"
14-
15-
// buildHash is the hash of the current build.
16-
var commit = "<unknown>"
1710

18-
// buildTime when the binary was build
19-
var buildTime = "<unknown>"
20-
21-
// qualifier returns the version qualifier like alpha1.
22-
var qualifier = ""
11+
libbeatVersion "github.com/elastic/beats/v7/libbeat/version"
12+
)
2313

2414
// snapshot is a flag marking build as a snapshot.
2515
var snapshot = ""
2616

2717
// Commit returns the current build hash or unknown if it was not injected in the build process.
2818
func Commit() string {
29-
return commit
19+
return libbeatVersion.Commit()
3020
}
3121

3222
// BuildTime returns the build time of the binaries.
3323
func BuildTime() time.Time {
34-
t, err := time.Parse(time.RFC3339, buildTime)
35-
if err != nil {
36-
return time.Time{}
37-
}
38-
return t
24+
return libbeatVersion.BuildTime()
3925
}
4026

4127
// Version returns the version of the application.
4228
func Version() string {
43-
if qualifier == "" {
44-
return version
45-
}
46-
return version + "-" + qualifier
29+
return libbeatVersion.GetDefaultVersion()
4730
}
4831

4932
// Snapshot returns true if binary was built as snapshot.

x-pack/elastic-agent/pkg/release/version_test.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)