Skip to content

Commit 0d3c215

Browse files
authored
Add new setting in system configuration file to set Elastic Agent image type (#2044)
Added support to check the new setting in the system test configuration file to set the Elastic Agent base image to be used during tests.
1 parent 3131065 commit 0d3c215

File tree

13 files changed

+122
-53
lines changed

13 files changed

+122
-53
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,11 @@ There are available some environment variables that could be used to change some
694694
- `ELASTIC_PACKAGE_SERVERLESS_PIPELINE_TEST_DISABLE_COMPARE_RESULTS`: If set to `true`, the results from pipeline tests are not compared to avoid errors from GeoIP.
695695
- `ELASTIC_PACKAGE_DISABLE_ELASTIC_AGENT_WOLFI`: If set to `true`, the Elastic Agent image used for running agents will be using the Ubuntu docker images
696696
(e.g. `docker.elastic.co/elastic-agent/elastic-agent-complete`). If set to `false`, the Elastic Agent image used for the running agents will be based on the wolfi
697+
<<<<<<< HEAD
698+
images (e.g. `docker.elastic.co/elastic-agent/elastic-agent-wolfi`). Default: `true`.
699+
=======
697700
images (e.g. `docker.elastic.co/elastic-agent/elastic-agent-wolfi`). Default: `false`.
701+
>>>>>>> upstream/main
698702
699703
- To configure the Elastic stack to be used by `elastic-package`:
700704
- `ELASTIC_PACKAGE_ELASTICSEARCH_HOST`: Host of the elasticsearch (e.g. https://127.0.0.1:9200)

internal/agentdeployer/agent.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (d *DockerComposeAgentDeployer) SetUp(ctx context.Context, agentInfo AgentI
101101
logger.Debug("setting up agent using Docker Compose agent deployer")
102102
d.agentRunID = agentInfo.Test.RunID
103103

104-
appConfig, err := install.Configuration()
104+
appConfig, err := install.Configuration(install.OptionWithStackVersion(d.stackVersion))
105105
if err != nil {
106106
return nil, fmt.Errorf("can't read application configuration: %w", err)
107107
}
@@ -112,7 +112,7 @@ func (d *DockerComposeAgentDeployer) SetUp(ctx context.Context, agentInfo AgentI
112112
}
113113

114114
env := append(
115-
appConfig.StackImageRefs(d.stackVersion).AsEnv(),
115+
appConfig.StackImageRefs().AsEnv(),
116116
fmt.Sprintf("%s=%s", serviceLogsDirEnv, agentInfo.Logs.Folder.Local),
117117
fmt.Sprintf("%s=%s", localCACertEnv, caCertPath),
118118
fmt.Sprintf("%s=%s", fleetPolicyEnv, d.policyName),
@@ -264,14 +264,14 @@ func (d *DockerComposeAgentDeployer) installDockerCompose(agentInfo AgentInfo) (
264264
stackVersion = config.Parameters[stack.ParamServerlessLocalStackVersion]
265265
}
266266

267-
appConfig, err := install.Configuration()
267+
agentImage, err := selectElasticAgentImage(stackVersion, agentInfo.Agent.BaseImage)
268268
if err != nil {
269-
return "", fmt.Errorf("can't read application configuration: %w", err)
269+
return "", nil
270270
}
271271

272272
resourceManager := resource.NewManager()
273273
resourceManager.AddFacter(resource.StaticFacter{
274-
"agent_image": appConfig.StackImageRefs(stackVersion).ElasticAgent,
274+
"agent_image": agentImage,
275275
"user": agentInfo.Agent.User,
276276
"capabilities": strings.Join(agentInfo.Agent.LinuxCapabilities, ","),
277277
"runtime": agentInfo.Agent.Runtime,
@@ -303,6 +303,16 @@ func (d *DockerComposeAgentDeployer) installDockerCompose(agentInfo AgentInfo) (
303303
return customAgentDir, nil
304304
}
305305

306+
func selectElasticAgentImage(stackVersion, agentBaseImage string) (string, error) {
307+
appConfig, err := install.Configuration(install.OptionWithAgentBaseImage(agentBaseImage), install.OptionWithStackVersion(stackVersion))
308+
if err != nil {
309+
return "", fmt.Errorf("can't read application configuration: %w", err)
310+
}
311+
312+
agentImage := appConfig.StackImageRefs().ElasticAgent
313+
return agentImage, nil
314+
}
315+
306316
func (d *DockerComposeAgentDeployer) installDockerfileResources(agentSettings AgentSettings, folder string) error {
307317
agentResources := []resource.Resource{
308318
&resource.File{

internal/agentdeployer/info.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type AgentScript struct {
2626
type AgentSettings struct {
2727
// User user to run Elastic Agent process
2828
User string `config:"user"`
29+
// BaseImage elastic-agent base image to be used for testing
30+
BaseImage string `config:"base_image"`
2931
// PidMode selects the host PID mode
3032
// (From docker-compose docs) Turns on sharing between container and the host
3133
// operating system the PID address space

internal/agentdeployer/kubernetes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ var elasticAgentManagedYamlTmpl string
179179
func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName, agentName string) ([]byte, error) {
180180
logger.Debugf("Prepare YAML definition for Elastic Agent running in stack v%s", stackVersion)
181181

182-
appConfig, err := install.Configuration()
182+
appConfig, err := install.Configuration(install.OptionWithStackVersion(stackVersion))
183183
if err != nil {
184184
return nil, fmt.Errorf("can't read application configuration: %w", err)
185185
}
@@ -196,7 +196,7 @@ func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName, age
196196
"fleetURL": "https://fleet-server:8220",
197197
"kibanaURL": "https://kibana:5601",
198198
"caCertPem": caCert,
199-
"elasticAgentImage": appConfig.StackImageRefs(stackVersion).ElasticAgent,
199+
"elasticAgentImage": appConfig.StackImageRefs().ElasticAgent,
200200
"elasticAgentTokenPolicyName": getTokenPolicyName(stackVersion, policyName),
201201
"agentName": agentName,
202202
})

internal/install/application_configuration.go

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ func DefaultConfiguration() *ApplicationConfiguration {
7171

7272
// ApplicationConfiguration represents the configuration of the elastic-package.
7373
type ApplicationConfiguration struct {
74-
c configFile
74+
c configFile
75+
agentBaseImage string
76+
stackVersion string
7577
}
7678

7779
type configFile struct {
@@ -119,12 +121,12 @@ func (ir ImageRefs) AsEnv() []string {
119121
}
120122

121123
// StackImageRefs function selects the appropriate set of Docker image references for the given stack version.
122-
func (ac *ApplicationConfiguration) StackImageRefs(version string) ImageRefs {
123-
refs := ac.c.Stack.ImageRefOverridesForVersion(version)
124-
refs.ElasticAgent = stringOrDefault(refs.ElasticAgent, fmt.Sprintf("%s:%s", selectElasticAgentImageName(version), version))
125-
refs.Elasticsearch = stringOrDefault(refs.Elasticsearch, fmt.Sprintf("%s:%s", elasticsearchImageName, version))
126-
refs.Kibana = stringOrDefault(refs.Kibana, fmt.Sprintf("%s:%s", kibanaImageName, version))
127-
refs.Logstash = stringOrDefault(refs.Logstash, fmt.Sprintf("%s:%s", logstashImageName, version))
124+
func (ac *ApplicationConfiguration) StackImageRefs() ImageRefs {
125+
refs := ac.c.Stack.ImageRefOverridesForVersion(ac.stackVersion)
126+
refs.ElasticAgent = stringOrDefault(refs.ElasticAgent, fmt.Sprintf("%s:%s", selectElasticAgentImageName(ac.stackVersion, ac.agentBaseImage), ac.stackVersion))
127+
refs.Elasticsearch = stringOrDefault(refs.Elasticsearch, fmt.Sprintf("%s:%s", elasticsearchImageName, ac.stackVersion))
128+
refs.Kibana = stringOrDefault(refs.Kibana, fmt.Sprintf("%s:%s", kibanaImageName, ac.stackVersion))
129+
refs.Logstash = stringOrDefault(refs.Logstash, fmt.Sprintf("%s:%s", logstashImageName, ac.stackVersion))
128130
return refs
129131
}
130132

@@ -148,7 +150,7 @@ func (ac *ApplicationConfiguration) SetCurrentProfile(name string) {
148150

149151
// selectElasticAgentImageName function returns the appropriate image name for Elastic-Agent depending on the stack version.
150152
// This is mandatory as "elastic-agent-complete" is available since 7.15.0-SNAPSHOT.
151-
func selectElasticAgentImageName(version string) string {
153+
func selectElasticAgentImageName(version, agentBaseImage string) string {
152154
if version == "" { // as version is optional and can be empty
153155
return elasticAgentImageName
154156
}
@@ -164,20 +166,41 @@ func selectElasticAgentImageName(version string) string {
164166
if ok && strings.ToLower(valueEnv) != "false" {
165167
disableWolfiImages = true
166168
}
167-
if !disableWolfiImages && !v.LessThan(elasticAgentWolfiVersion) {
169+
switch {
170+
case !disableWolfiImages && !v.LessThan(elasticAgentWolfiVersion) && agentBaseImage != "complete":
168171
return elasticAgentWolfiImageName
169-
}
170-
if !v.LessThan(elasticAgentCompleteOwnNamespaceVersion) {
172+
case !v.LessThan(elasticAgentCompleteOwnNamespaceVersion):
171173
return elasticAgentCompleteImageName
172-
}
173-
if !v.LessThan(elasticAgentCompleteFirstSupportedVersion) {
174+
case !v.LessThan(elasticAgentCompleteFirstSupportedVersion):
174175
return elasticAgentCompleteLegacyImageName
176+
default:
177+
return elasticAgentImageName
178+
}
179+
}
180+
181+
type configurationOptions struct {
182+
agentBaseImage string
183+
stackVersion string
184+
}
185+
186+
type ConfigurationOption func(*configurationOptions)
187+
188+
// OptionWithAgentBaseImage sets the agent image type to be used.
189+
func OptionWithAgentBaseImage(agentBaseImage string) ConfigurationOption {
190+
return func(opts *configurationOptions) {
191+
opts.agentBaseImage = agentBaseImage
192+
}
193+
}
194+
195+
// OptionWithStackVersion sets the Elastic Stack version to be used.
196+
func OptionWithStackVersion(stackVersion string) ConfigurationOption {
197+
return func(opts *configurationOptions) {
198+
opts.stackVersion = stackVersion
175199
}
176-
return elasticAgentImageName
177200
}
178201

179202
// Configuration function returns the elastic-package configuration.
180-
func Configuration() (*ApplicationConfiguration, error) {
203+
func Configuration(options ...ConfigurationOption) (*ApplicationConfiguration, error) {
181204
configPath, err := locations.NewLocationManager()
182205
if err != nil {
183206
return nil, fmt.Errorf("can't read configuration directory: %w", err)
@@ -197,9 +220,18 @@ func Configuration() (*ApplicationConfiguration, error) {
197220
return nil, fmt.Errorf("can't unmarshal configuration file: %w", err)
198221
}
199222

200-
return &ApplicationConfiguration{
201-
c: c,
202-
}, nil
223+
configOptions := configurationOptions{}
224+
for _, option := range options {
225+
option(&configOptions)
226+
}
227+
228+
configuration := ApplicationConfiguration{
229+
c: c,
230+
agentBaseImage: configOptions.agentBaseImage,
231+
stackVersion: configOptions.stackVersion,
232+
}
233+
234+
return &configuration, nil
203235
}
204236

205237
func stringOrDefault(value string, defaultValue string) string {

internal/install/application_configuration_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,72 @@ import (
1212

1313
func TestSelectElasticAgentImageName_NoVersion(t *testing.T) {
1414
var version string
15-
selected := selectElasticAgentImageName(version)
15+
selected := selectElasticAgentImageName(version, "")
1616
assert.Equal(t, selected, elasticAgentImageName)
1717
}
1818

1919
func TestSelectElasticAgentImageName_OlderStack(t *testing.T) {
2020
version := "7.14.99-SNAPSHOT"
21-
selected := selectElasticAgentImageName(version)
21+
selected := selectElasticAgentImageName(version, "")
2222
assert.Equal(t, selected, elasticAgentImageName)
2323
}
2424

2525
func TestSelectElasticAgentImageName_FirstStackWithCompleteAgent(t *testing.T) {
2626
version := stackVersion715
27-
selected := selectElasticAgentImageName(version)
27+
selected := selectElasticAgentImageName(version, "")
2828
assert.Equal(t, selected, elasticAgentCompleteLegacyImageName)
2929
}
3030

3131
func TestSelectElasticAgentImageName_NextStackWithAgentComplete(t *testing.T) {
3232
version := "7.16.0-SNAPSHOT"
33-
selected := selectElasticAgentImageName(version)
33+
selected := selectElasticAgentImageName(version, "")
3434
assert.Equal(t, selected, elasticAgentCompleteLegacyImageName)
3535
}
3636

3737
func TestSelectElasticAgentImageName_OwnNamespace(t *testing.T) {
3838
version := "8.2.0-SNAPSHOT"
39-
selected := selectElasticAgentImageName(version)
39+
selected := selectElasticAgentImageName(version, "")
4040
assert.Equal(t, selected, elasticAgentCompleteImageName)
4141
}
4242

4343
func TestSelectElasticAgentImageName_OwnNamespace_Release(t *testing.T) {
4444
version := "8.2.0"
45-
selected := selectElasticAgentImageName(version)
45+
selected := selectElasticAgentImageName(version, "")
4646
assert.Equal(t, selected, elasticAgentCompleteImageName)
4747
}
4848

4949
func TestSelectElasticAgentImageName_NextStackInOwnNamespace(t *testing.T) {
5050
version := "8.4.0-SNAPSHOT"
51-
selected := selectElasticAgentImageName(version)
51+
selected := selectElasticAgentImageName(version, "")
5252
assert.Equal(t, selected, elasticAgentCompleteImageName)
5353
}
5454

5555
func TestSelectElasticAgentImageName_WolfiImage(t *testing.T) {
5656
version := "8.16.0-SNAPSHOT"
57-
selected := selectElasticAgentImageName(version)
57+
selected := selectElasticAgentImageName(version, "")
5858
assert.Equal(t, selected, elasticAgentWolfiImageName)
5959
}
6060

6161
func TestSelectElasticAgentImageName_DisableWolfiImageEnvVar(t *testing.T) {
6262
version := "8.16.0-SNAPSHOT"
6363
t.Setenv(disableElasticAgentWolfiEnvVar, "true")
64-
selected := selectElasticAgentImageName(version)
64+
selected := selectElasticAgentImageName(version, "")
6565
assert.Equal(t, selected, elasticAgentCompleteImageName)
6666
}
6767
func TestSelectElasticAgentImageName_EnableWolfiImageEnvVar(t *testing.T) {
6868
version := "8.16.0-SNAPSHOT"
6969
t.Setenv(disableElasticAgentWolfiEnvVar, "false")
70-
selected := selectElasticAgentImageName(version)
70+
selected := selectElasticAgentImageName(version, "")
71+
assert.Equal(t, selected, elasticAgentWolfiImageName)
72+
}
73+
74+
func TestSelectCompleteElasticAgentImageName_ForceCompleteImage(t *testing.T) {
75+
version := "8.16.0-SNAPSHOT"
76+
selected := selectElasticAgentImageName(version, "complete")
77+
assert.Equal(t, selected, elasticAgentCompleteImageName)
78+
}
79+
func TestSelectCompleteElasticAgentImageName_ForceDefaultImage(t *testing.T) {
80+
version := "8.16.0-SNAPSHOT"
81+
selected := selectElasticAgentImageName(version, "default")
7182
assert.Equal(t, selected, elasticAgentWolfiImageName)
7283
}

internal/servicedeployer/custom_agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewCustomAgentDeployer(options CustomAgentDeployerOptions) (*CustomAgentDep
7272
func (d *CustomAgentDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (DeployedService, error) {
7373
logger.Warn("DEPRECATED - setting up service using Docker Compose service deployer")
7474

75-
appConfig, err := install.Configuration()
75+
appConfig, err := install.Configuration(install.OptionWithStackVersion(d.stackVersion))
7676
if err != nil {
7777
return nil, fmt.Errorf("can't read application configuration: %w", err)
7878
}
@@ -90,7 +90,7 @@ func (d *CustomAgentDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (D
9090
svcInfo.Hostname = dockerCustomAgentName
9191

9292
env := append(
93-
appConfig.StackImageRefs(d.stackVersion).AsEnv(),
93+
appConfig.StackImageRefs().AsEnv(),
9494
fmt.Sprintf("%s=%s", serviceLogsDirEnv, svcInfo.Logs.Folder.Local),
9595
fmt.Sprintf("%s=%s", localCACertEnv, caCertPath),
9696
fmt.Sprintf("%s=%s", fleetPolicyEnv, d.policyName),

internal/servicedeployer/kubernetes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ var elasticAgentManagedYamlTmpl string
229229
func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName string) ([]byte, error) {
230230
logger.Debugf("Prepare YAML definition for Elastic Agent running in stack v%s", stackVersion)
231231

232-
appConfig, err := install.Configuration()
232+
appConfig, err := install.Configuration(install.OptionWithStackVersion(stackVersion))
233233
if err != nil {
234234
return nil, fmt.Errorf("can't read application configuration: %w", err)
235235
}
@@ -246,7 +246,7 @@ func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName stri
246246
"fleetURL": "https://fleet-server:8220",
247247
"kibanaURL": "https://kibana:5601",
248248
"caCertPem": caCert,
249-
"elasticAgentImage": appConfig.StackImageRefs(stackVersion).ElasticAgent,
249+
"elasticAgentImage": appConfig.StackImageRefs().ElasticAgent,
250250
"elasticAgentTokenPolicyName": getTokenPolicyName(stackVersion, policyName),
251251
})
252252
if err != nil {

internal/stack/compose.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ func dockerComposeBuild(ctx context.Context, options Options) error {
5656
return fmt.Errorf("could not create docker compose project: %w", err)
5757
}
5858

59-
appConfig, err := install.Configuration()
59+
appConfig, err := install.Configuration(install.OptionWithStackVersion(options.StackVersion))
6060
if err != nil {
6161
return fmt.Errorf("can't read application configuration: %w", err)
6262
}
6363

6464
opts := compose.CommandOptions{
6565
Env: newEnvBuilder().
66-
withEnvs(appConfig.StackImageRefs(options.StackVersion).AsEnv()).
66+
withEnvs(appConfig.StackImageRefs().AsEnv()).
6767
withEnv(stackVariantAsEnv(options.StackVersion)).
6868
withEnvs(options.Profile.ComposeEnvVars()).
6969
build(),
@@ -82,14 +82,14 @@ func dockerComposePull(ctx context.Context, options Options) error {
8282
return fmt.Errorf("could not create docker compose project: %w", err)
8383
}
8484

85-
appConfig, err := install.Configuration()
85+
appConfig, err := install.Configuration(install.OptionWithStackVersion(options.StackVersion))
8686
if err != nil {
8787
return fmt.Errorf("can't read application configuration: %w", err)
8888
}
8989

9090
opts := compose.CommandOptions{
9191
Env: newEnvBuilder().
92-
withEnvs(appConfig.StackImageRefs(options.StackVersion).AsEnv()).
92+
withEnvs(appConfig.StackImageRefs().AsEnv()).
9393
withEnv(stackVariantAsEnv(options.StackVersion)).
9494
withEnvs(options.Profile.ComposeEnvVars()).
9595
build(),
@@ -113,14 +113,14 @@ func dockerComposeUp(ctx context.Context, options Options) error {
113113
args = append(args, "-d")
114114
}
115115

116-
appConfig, err := install.Configuration()
116+
appConfig, err := install.Configuration(install.OptionWithStackVersion(options.StackVersion))
117117
if err != nil {
118118
return fmt.Errorf("can't read application configuration: %w", err)
119119
}
120120

121121
opts := compose.CommandOptions{
122122
Env: newEnvBuilder().
123-
withEnvs(appConfig.StackImageRefs(options.StackVersion).AsEnv()).
123+
withEnvs(appConfig.StackImageRefs().AsEnv()).
124124
withEnv(stackVariantAsEnv(options.StackVersion)).
125125
withEnvs(options.Profile.ComposeEnvVars()).
126126
build(),
@@ -140,14 +140,14 @@ func dockerComposeDown(ctx context.Context, options Options) error {
140140
return fmt.Errorf("could not create docker compose project: %w", err)
141141
}
142142

143-
appConfig, err := install.Configuration()
143+
appConfig, err := install.Configuration(install.OptionWithStackVersion(options.StackVersion))
144144
if err != nil {
145145
return fmt.Errorf("can't read application configuration: %w", err)
146146
}
147147

148148
downOptions := compose.CommandOptions{
149149
Env: newEnvBuilder().
150-
withEnvs(appConfig.StackImageRefs(options.StackVersion).AsEnv()).
150+
withEnvs(appConfig.StackImageRefs().AsEnv()).
151151
withEnv(stackVariantAsEnv(options.StackVersion)).
152152
withEnvs(options.Profile.ComposeEnvVars()).
153153
build(),

0 commit comments

Comments
 (0)