Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new setting in system configuration file to set Elastic Agent image type #2044

Merged
merged 17 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ There are available some environment variables that could be used to change some

- Related to tests:
- `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.
- `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
(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
images (e.g. `docker.elastic.co/elastic-agent/elastic-agent-wolfi`). Default: `true`.

- To configure the Elastic stack to be used by `elastic-package`:
- `ELASTIC_PACKAGE_ELASTICSEARCH_HOST`: Host of the elasticsearch (e.g. https://127.0.0.1:9200)
Expand Down
20 changes: 15 additions & 5 deletions internal/agentdeployer/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (d *DockerComposeAgentDeployer) SetUp(ctx context.Context, agentInfo AgentI
logger.Debug("setting up agent using Docker Compose agent deployer")
d.agentRunID = agentInfo.Test.RunID

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

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

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

resourceManager := resource.NewManager()
resourceManager.AddFacter(resource.StaticFacter{
"agent_image": appConfig.StackImageRefs(stackVersion).ElasticAgent,
"agent_image": agentImage,
"user": agentInfo.Agent.User,
"capabilities": strings.Join(agentInfo.Agent.LinuxCapabilities, ","),
"runtime": agentInfo.Agent.Runtime,
Expand Down Expand Up @@ -303,6 +303,16 @@ func (d *DockerComposeAgentDeployer) installDockerCompose(agentInfo AgentInfo) (
return customAgentDir, nil
}

func selectElasticAgentImage(stackVersion, agentBaseImage string) (string, error) {
appConfig, err := install.Configuration(install.OptionWithAgentBaseImage(agentBaseImage), install.OptionWithStackVersion(stackVersion))
if err != nil {
return "", fmt.Errorf("can't read application configuration: %w", err)
}

agentImage := appConfig.StackImageRefs().ElasticAgent
return agentImage, nil
}

func (d *DockerComposeAgentDeployer) installDockerfileResources(agentSettings AgentSettings, folder string) error {
agentResources := []resource.Resource{
&resource.File{
Expand Down
2 changes: 2 additions & 0 deletions internal/agentdeployer/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type AgentScript struct {
type AgentSettings struct {
// User user to run Elastic Agent process
User string `config:"user"`
// BaseImage elastic-agent base image to be used for testing
BaseImage string `config:"base_image"`
// PidMode selects the host PID mode
// (From docker-compose docs) Turns on sharing between container and the host
// operating system the PID address space
Expand Down
4 changes: 2 additions & 2 deletions internal/agentdeployer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ var elasticAgentManagedYamlTmpl string
func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName, agentName string) ([]byte, error) {
logger.Debugf("Prepare YAML definition for Elastic Agent running in stack v%s", stackVersion)

appConfig, err := install.Configuration()
appConfig, err := install.Configuration(install.OptionWithStackVersion(stackVersion))
if err != nil {
return nil, fmt.Errorf("can't read application configuration: %w", err)
}
Expand All @@ -196,7 +196,7 @@ func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName, age
"fleetURL": "https://fleet-server:8220",
"kibanaURL": "https://kibana:5601",
"caCertPem": caCert,
"elasticAgentImage": appConfig.StackImageRefs(stackVersion).ElasticAgent,
"elasticAgentImage": appConfig.StackImageRefs().ElasticAgent,
"elasticAgentTokenPolicyName": getTokenPolicyName(stackVersion, policyName),
"agentName": agentName,
})
Expand Down
82 changes: 66 additions & 16 deletions internal/install/application_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/Masterminds/semver/v3"

Expand All @@ -23,10 +24,14 @@ import (
const (
stackVersion715 = "7.15.0-SNAPSHOT"
stackVersion820 = "8.2.0-SNAPSHOT"
// Not setting here 8.16.0-SNAPSHOT to take also into account prerelease versions
// like 8.16.0-21bba6f5-SNAPSHOT
stackVersion8160 = "8.16.0-00000000-SNAPSHOT"

elasticAgentImageName = "docker.elastic.co/beats/elastic-agent"
elasticAgentCompleteLegacyImageName = "docker.elastic.co/beats/elastic-agent-complete"
elasticAgentCompleteImageName = "docker.elastic.co/elastic-agent/elastic-agent-complete"
elasticAgentWolfiImageName = "docker.elastic.co/elastic-agent/elastic-agent-wolfi"
elasticsearchImageName = "docker.elastic.co/elasticsearch/elasticsearch"
kibanaImageName = "docker.elastic.co/kibana/kibana"
logstashImageName = "docker.elastic.co/logstash/logstash"
Expand All @@ -37,9 +42,12 @@ const (
var (
elasticAgentCompleteFirstSupportedVersion = semver.MustParse(stackVersion715)
elasticAgentCompleteOwnNamespaceVersion = semver.MustParse(stackVersion820)
elasticAgentWolfiVersion = semver.MustParse(stackVersion8160)

// ProfileNameEnvVar is the name of the environment variable to set the default profile
ProfileNameEnvVar = environment.WithElasticPackagePrefix("PROFILE")

disableElasticAgentWolfiEnvVar = environment.WithElasticPackagePrefix("DISABLE_ELASTIC_AGENT_WOLFI")
)

func DefaultConfiguration() *ApplicationConfiguration {
Expand All @@ -63,7 +71,9 @@ func DefaultConfiguration() *ApplicationConfiguration {

// ApplicationConfiguration represents the configuration of the elastic-package.
type ApplicationConfiguration struct {
c configFile
c configFile
agentBaseImage string
stackVersion string
}

type configFile struct {
Expand Down Expand Up @@ -111,12 +121,12 @@ func (ir ImageRefs) AsEnv() []string {
}

// StackImageRefs function selects the appropriate set of Docker image references for the given stack version.
func (ac *ApplicationConfiguration) StackImageRefs(version string) ImageRefs {
refs := ac.c.Stack.ImageRefOverridesForVersion(version)
refs.ElasticAgent = stringOrDefault(refs.ElasticAgent, fmt.Sprintf("%s:%s", selectElasticAgentImageName(version), version))
refs.Elasticsearch = stringOrDefault(refs.Elasticsearch, fmt.Sprintf("%s:%s", elasticsearchImageName, version))
refs.Kibana = stringOrDefault(refs.Kibana, fmt.Sprintf("%s:%s", kibanaImageName, version))
refs.Logstash = stringOrDefault(refs.Logstash, fmt.Sprintf("%s:%s", logstashImageName, version))
func (ac *ApplicationConfiguration) StackImageRefs() ImageRefs {
refs := ac.c.Stack.ImageRefOverridesForVersion(ac.stackVersion)
refs.ElasticAgent = stringOrDefault(refs.ElasticAgent, fmt.Sprintf("%s:%s", selectElasticAgentImageName(ac.stackVersion, ac.agentBaseImage), ac.stackVersion))
refs.Elasticsearch = stringOrDefault(refs.Elasticsearch, fmt.Sprintf("%s:%s", elasticsearchImageName, ac.stackVersion))
refs.Kibana = stringOrDefault(refs.Kibana, fmt.Sprintf("%s:%s", kibanaImageName, ac.stackVersion))
refs.Logstash = stringOrDefault(refs.Logstash, fmt.Sprintf("%s:%s", logstashImageName, ac.stackVersion))
return refs
}

Expand All @@ -140,7 +150,7 @@ func (ac *ApplicationConfiguration) SetCurrentProfile(name string) {

// selectElasticAgentImageName function returns the appropriate image name for Elastic-Agent depending on the stack version.
// This is mandatory as "elastic-agent-complete" is available since 7.15.0-SNAPSHOT.
func selectElasticAgentImageName(version string) string {
func selectElasticAgentImageName(version, agentBaseImage string) string {
if version == "" { // as version is optional and can be empty
return elasticAgentImageName
}
Expand All @@ -150,17 +160,48 @@ func selectElasticAgentImageName(version string) string {
logger.Errorf("stack version not in semver format (value: %s): %v", v, err)
return elasticAgentImageName
}
if !v.LessThan(elasticAgentCompleteOwnNamespaceVersion) {
return elasticAgentCompleteImageName

// TODO: Set as default using wolfi images once they are available
disableWolfiImages := true
valueEnv, ok := os.LookupEnv(disableElasticAgentWolfiEnvVar)
if ok && strings.ToLower(valueEnv) != "true" {
disableWolfiImages = false
}
if !v.LessThan(elasticAgentCompleteFirstSupportedVersion) {
switch {
case !disableWolfiImages && !v.LessThan(elasticAgentWolfiVersion) && agentBaseImage != "complete":
return elasticAgentWolfiImageName
case !v.LessThan(elasticAgentCompleteOwnNamespaceVersion):
return elasticAgentCompleteImageName
case !v.LessThan(elasticAgentCompleteFirstSupportedVersion):
return elasticAgentCompleteLegacyImageName
default:
return elasticAgentImageName
}
}

type configurationOptions struct {
agentBaseImage string
stackVersion string
}

type ConfigurationOption func(*configurationOptions)

// OptionWithAgentBaseImage sets the agent image type to be used.
func OptionWithAgentBaseImage(agentBaseImage string) ConfigurationOption {
return func(opts *configurationOptions) {
opts.agentBaseImage = agentBaseImage
}
}

// OptionWithStackVersion sets the Elastic Stack version to be used.
func OptionWithStackVersion(stackVersion string) ConfigurationOption {
return func(opts *configurationOptions) {
opts.stackVersion = stackVersion
}
return elasticAgentImageName
}

// Configuration function returns the elastic-package configuration.
func Configuration() (*ApplicationConfiguration, error) {
func Configuration(options ...ConfigurationOption) (*ApplicationConfiguration, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work without a stack version? Should it be mandatory?

Suggested change
func Configuration(options ...ConfigurationOption) (*ApplicationConfiguration, error) {
func Configuration(stackVersion string, options ...ConfigurationOption) (*ApplicationConfiguration, error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While doing these changes, there was an usage where it was not used stack version:

config, err := install.Configuration()

In that command, it looks like that it just checked the config file in the ~/.elastic-package directory. It's not needed there the stack versions to retrieve the images of the services.

configPath, err := locations.NewLocationManager()
if err != nil {
return nil, fmt.Errorf("can't read configuration directory: %w", err)
Expand All @@ -180,9 +221,18 @@ func Configuration() (*ApplicationConfiguration, error) {
return nil, fmt.Errorf("can't unmarshal configuration file: %w", err)
}

return &ApplicationConfiguration{
c: c,
}, nil
configOptions := configurationOptions{}
for _, option := range options {
option(&configOptions)
}

configuration := ApplicationConfiguration{
c: c,
agentBaseImage: configOptions.agentBaseImage,
stackVersion: configOptions.stackVersion,
}

return &configuration, nil
}

func stringOrDefault(value string, defaultValue string) string {
Expand Down
40 changes: 33 additions & 7 deletions internal/install/application_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,68 @@ import (

func TestSelectElasticAgentImageName_NoVersion(t *testing.T) {
var version string
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentImageName)
}

func TestSelectElasticAgentImageName_OlderStack(t *testing.T) {
version := "7.14.99-SNAPSHOT"
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentImageName)
}

func TestSelectElasticAgentImageName_FirstStackWithCompleteAgent(t *testing.T) {
version := stackVersion715
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentCompleteLegacyImageName)
}

func TestSelectElasticAgentImageName_NextStackWithAgentComplete(t *testing.T) {
version := "7.16.0-SNAPSHOT"
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentCompleteLegacyImageName)
}

func TestSelectElasticAgentImageName_OwnNamespace(t *testing.T) {
version := "8.2.0-SNAPSHOT"
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentCompleteImageName)
}

func TestSelectElasticAgentImageName_OwnNamespace_Release(t *testing.T) {
version := "8.2.0"
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentCompleteImageName)
}

func TestSelectElasticAgentImageName_NextStackInOwnNamespace(t *testing.T) {
version := "8.4.0-SNAPSHOT"
selected := selectElasticAgentImageName(version)
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentCompleteImageName)
}

func TestSelectElasticAgentImageName_WolfiImage(t *testing.T) {
version := "8.16.0-SNAPSHOT"
selected := selectElasticAgentImageName(version, "")
// TODO: update once changed the default value
assert.Equal(t, selected, elasticAgentCompleteImageName)
}

func TestSelectElasticAgentImageName_DisableWolfiImageEnvVar(t *testing.T) {
version := "8.16.0-SNAPSHOT"
t.Setenv(disableElasticAgentWolfiEnvVar, "true")
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentCompleteImageName)
}
func TestSelectElasticAgentImageName_EnableWolfiImageEnvVar(t *testing.T) {
version := "8.16.0-SNAPSHOT"
t.Setenv(disableElasticAgentWolfiEnvVar, "false")
selected := selectElasticAgentImageName(version, "")
assert.Equal(t, selected, elasticAgentWolfiImageName)
}

func TestSelectCompleteElasticAgentImageName_ForceCompleteImage(t *testing.T) {
version := "8.16.0-SNAPSHOT"
selected := selectElasticAgentImageName(version, "complete")
assert.Equal(t, selected, elasticAgentCompleteImageName)
}
4 changes: 2 additions & 2 deletions internal/servicedeployer/custom_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewCustomAgentDeployer(options CustomAgentDeployerOptions) (*CustomAgentDep
func (d *CustomAgentDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (DeployedService, error) {
logger.Warn("DEPRECATED - setting up service using Docker Compose service deployer")

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

env := append(
appConfig.StackImageRefs(d.stackVersion).AsEnv(),
appConfig.StackImageRefs().AsEnv(),
fmt.Sprintf("%s=%s", serviceLogsDirEnv, svcInfo.Logs.Folder.Local),
fmt.Sprintf("%s=%s", localCACertEnv, caCertPath),
fmt.Sprintf("%s=%s", fleetPolicyEnv, d.policyName),
Expand Down
4 changes: 2 additions & 2 deletions internal/servicedeployer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ var elasticAgentManagedYamlTmpl string
func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName string) ([]byte, error) {
logger.Debugf("Prepare YAML definition for Elastic Agent running in stack v%s", stackVersion)

appConfig, err := install.Configuration()
appConfig, err := install.Configuration(install.OptionWithStackVersion(stackVersion))
if err != nil {
return nil, fmt.Errorf("can't read application configuration: %w", err)
}
Expand All @@ -246,7 +246,7 @@ func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName stri
"fleetURL": "https://fleet-server:8220",
"kibanaURL": "https://kibana:5601",
"caCertPem": caCert,
"elasticAgentImage": appConfig.StackImageRefs(stackVersion).ElasticAgent,
"elasticAgentImage": appConfig.StackImageRefs().ElasticAgent,
"elasticAgentTokenPolicyName": getTokenPolicyName(stackVersion, policyName),
})
if err != nil {
Expand Down
Loading