Skip to content

Commit 0ea19e1

Browse files
authored
Merge branch 'master' into bugfix-asset-test-runner-es-tmpl-name
2 parents 6880079 + ec24773 commit 0ea19e1

6 files changed

Lines changed: 164 additions & 18 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package install
6+
7+
import (
8+
"fmt"
9+
"io/ioutil"
10+
"path/filepath"
11+
12+
"github.com/pkg/errors"
13+
"gopkg.in/yaml.v3"
14+
)
15+
16+
// ApplicationConfiguration represents the configuration of the elastic-package.
17+
type ApplicationConfiguration struct {
18+
c configFile
19+
}
20+
21+
type configFile struct {
22+
Stack stack `yaml:"stack"`
23+
}
24+
25+
type stack struct {
26+
ImageRefOverrides map[string]ImageRefs `yaml:"image_ref_overrides"`
27+
}
28+
29+
func (s stack) ImageRefOverridesForVersion(version string) ImageRefs {
30+
refs, ok := s.ImageRefOverrides[version]
31+
if !ok {
32+
return ImageRefs{}
33+
}
34+
return refs
35+
}
36+
37+
// ImageRefs stores Docker image references used to create the Elastic stack containers.
38+
type ImageRefs struct {
39+
ElasticAgent string `yaml:"elastic-agent"`
40+
Elasticsearch string `yaml:"elasticsearch"`
41+
Kibana string `yaml:"kibana"`
42+
}
43+
44+
// AsEnv method returns key=value representation of image refs.
45+
func (ir ImageRefs) AsEnv() []string {
46+
var vars []string
47+
vars = append(vars, "ELASTIC_AGENT_IMAGE_REF="+ir.ElasticAgent)
48+
vars = append(vars, "ELASTICSEARCH_IMAGE_REF="+ir.Elasticsearch)
49+
vars = append(vars, "KIBANA_IMAGE_REF="+ir.Kibana)
50+
return vars
51+
}
52+
53+
// DefaultStackImageRefs function selects the appropriate set of Docker image references for the default stack version.
54+
func (ac *ApplicationConfiguration) DefaultStackImageRefs() ImageRefs {
55+
return ac.StackImageRefs(DefaultStackVersion)
56+
}
57+
58+
// StackImageRefs function selects the appropriate set of Docker image references for the given stack version.
59+
func (ac *ApplicationConfiguration) StackImageRefs(version string) ImageRefs {
60+
refs := ac.c.Stack.ImageRefOverridesForVersion(version)
61+
refs.ElasticAgent = stringOrDefault(refs.ElasticAgent, fmt.Sprintf("%s:%s", elasticAgentImageName, version))
62+
refs.Elasticsearch = stringOrDefault(refs.Elasticsearch, fmt.Sprintf("%s:%s", elasticsearchImageName, version))
63+
refs.Kibana = stringOrDefault(refs.Kibana, fmt.Sprintf("%s:%s", kibanaImageName, version))
64+
return refs
65+
}
66+
67+
// Configuration function returns the elastic-package configuration.
68+
func Configuration() (*ApplicationConfiguration, error) {
69+
configPath, err := configurationDir()
70+
if err != nil {
71+
return nil, errors.Wrap(err, "can't read configuration directory")
72+
}
73+
74+
cfg, err := ioutil.ReadFile(filepath.Join(configPath, applicationConfigurationYmlFile))
75+
if err != nil {
76+
return nil, errors.Wrap(err, "can't read configuration file")
77+
}
78+
79+
var c configFile
80+
err = yaml.Unmarshal(cfg, &c)
81+
if err != nil {
82+
return nil, errors.Wrap(err, "can't unmarshal configuration file")
83+
}
84+
85+
return &ApplicationConfiguration{
86+
c: c,
87+
}, nil
88+
}
89+
90+
func stringOrDefault(value string, defaultValue string) string {
91+
if value == "" {
92+
return defaultValue
93+
}
94+
return value
95+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package install
6+
7+
const (
8+
elasticAgentImageName = "docker.elastic.co/beats/elastic-agent"
9+
elasticsearchImageName = "docker.elastic.co/elasticsearch/elasticsearch"
10+
kibanaImageName = "docker.elastic.co/kibana/kibana"
11+
)
12+
13+
const applicationConfigurationYmlFile = "config.yml"
14+
15+
/*
16+
17+
Uncomment and use the commented definition of "stack" in case of emergency to define Docker image overrides
18+
(stack.image_ref_overrides). The following sample defines overrides for the Elastic stack ver. 7.13.0-SNAPSHOT.
19+
It's advised to use latest stable snapshots for the stack snapshot.
20+
21+
const applicationConfigurationYml = `stack:
22+
image_ref_overrides:
23+
7.13.0-SNAPSHOT:
24+
# Use stable image versions for Agent and Kibana
25+
elastic-agent: ` + elasticAgentImageName + `@sha256:76c294cf55654bc28dde72ce936032f34ad5f40c345f3df964924778b249e581
26+
kibana: ` + kibanaImageName + `@sha256:78ae3b1ca09efee242d2c77597dfab18670e984adb96c2407ec03fe07ceca4f6`
27+
*/
28+
29+
const applicationConfigurationYml = `stack:
30+
image_ref_overrides:
31+
`

internal/install/install.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func EnsureInstalled() error {
5050
return errors.Wrap(err, "creating elastic package directory failed")
5151
}
5252

53+
err = writeConfigFile(elasticPackagePath)
54+
if err != nil {
55+
return errors.Wrap(err, "writing configuration file failed")
56+
}
57+
5358
err = writeVersionFile(elasticPackagePath)
5459
if err != nil {
5560
return errors.Wrap(err, "writing version file failed")
@@ -181,8 +186,14 @@ func writeKubernetesDeployerResources(elasticPackagePath string) error {
181186
return errors.Wrapf(err, "creating directory failed (path: %s)", kubernetesDeployer)
182187
}
183188

189+
appConfig, err := Configuration()
190+
if err != nil {
191+
return errors.Wrap(err, "can't read application configuration")
192+
}
193+
184194
err = writeStaticResource(err, filepath.Join(kubernetesDeployer, kubernetesDeployerElasticAgentYmlFile),
185-
strings.ReplaceAll(kubernetesDeployerElasticAgentYml, "{{ STACK_VERSION }}", DefaultStackVersion))
195+
strings.ReplaceAll(kubernetesDeployerElasticAgentYml, "{{ ELASTIC_AGENT_IMAGE_REF }}",
196+
appConfig.DefaultStackImageRefs().ElasticAgent))
186197
if err != nil {
187198
return errors.Wrap(err, "writing static resource failed")
188199
}
@@ -205,6 +216,15 @@ func writeTerraformDeployerResources(elasticPackagePath string) error {
205216
return nil
206217
}
207218

219+
func writeConfigFile(elasticPackagePath string) error {
220+
var err error
221+
err = writeStaticResource(err, filepath.Join(elasticPackagePath, applicationConfigurationYmlFile), applicationConfigurationYml)
222+
if err != nil {
223+
return errors.Wrap(err, "writing static resource failed")
224+
}
225+
return nil
226+
}
227+
208228
func writeStaticResource(err error, path, content string) error {
209229
if err != nil {
210230
return err

internal/install/static_kubernetes_elastic_agent_yml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ spec:
2626
serviceAccountName: kind-fleet-agent
2727
containers:
2828
- name: kind-fleet-agent-clusterscope
29-
image: docker.elastic.co/beats/elastic-agent:{{ STACK_VERSION }}
29+
image: {{ ELASTIC_AGENT_IMAGE_REF }}
3030
env:
3131
- name: FLEET_ENROLL
3232
value: "1"

internal/install/static_snapshot_yml.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package install
77
const snapshotYml = `version: '2.3'
88
services:
99
elasticsearch:
10-
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
10+
image: ${ELASTICSEARCH_IMAGE_REF}
1111
healthcheck:
1212
test: ["CMD", "curl", "-f", "-u", "elastic:changeme", "http://127.0.0.1:9200/"]
1313
retries: 300
@@ -36,7 +36,7 @@ services:
3636
condition: service_healthy
3737
3838
kibana:
39-
image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
39+
image: ${KIBANA_IMAGE_REF}
4040
depends_on:
4141
elasticsearch:
4242
condition: service_healthy
@@ -75,7 +75,7 @@ services:
7575
condition: service_healthy
7676
7777
elastic-agent:
78-
image: docker.elastic.co/beats/elastic-agent:${STACK_VERSION}
78+
image: ${ELASTIC_AGENT_IMAGE_REF}
7979
depends_on:
8080
elasticsearch:
8181
condition: service_healthy

internal/stack/compose.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func dockerComposeBuild(options Options) error {
2828
}
2929

3030
opts := compose.CommandOptions{
31-
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
3231
Services: withIsReadyServices(withDependentServices(options.Services)),
3332
}
3433

@@ -49,8 +48,13 @@ func dockerComposePull(options Options) error {
4948
return errors.Wrap(err, "could not create docker compose project")
5049
}
5150

51+
appConfig, err := install.Configuration()
52+
if err != nil {
53+
return errors.Wrap(err, "can't read application configuration")
54+
}
55+
5256
opts := compose.CommandOptions{
53-
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
57+
Env: appConfig.StackImageRefs(options.StackVersion).AsEnv(),
5458
Services: withIsReadyServices(withDependentServices(options.Services)),
5559
}
5660

@@ -76,8 +80,13 @@ func dockerComposeUp(options Options) error {
7680
args = append(args, "-d")
7781
}
7882

83+
appConfig, err := install.Configuration()
84+
if err != nil {
85+
return errors.Wrap(err, "can't read application configuration")
86+
}
87+
7988
opts := compose.CommandOptions{
80-
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
89+
Env: appConfig.StackImageRefs(options.StackVersion).AsEnv(),
8190
ExtraArgs: args,
8291
Services: withIsReadyServices(withDependentServices(options.Services)),
8392
}
@@ -99,13 +108,7 @@ func dockerComposeDown() error {
99108
return errors.Wrap(err, "could not create docker compose project")
100109
}
101110

102-
opts := compose.CommandOptions{
103-
// We set the STACK_VERSION env var here to avoid showing a warning to the user about
104-
// it not being set.
105-
Env: []string{fmt.Sprintf("STACK_VERSION=%s", install.DefaultStackVersion)},
106-
}
107-
108-
if err := c.Down(opts); err != nil {
111+
if err := c.Down(compose.CommandOptions{}); err != nil {
109112
return errors.Wrap(err, "running command failed")
110113
}
111114
return nil
@@ -123,9 +126,6 @@ func dockerComposeLogs(serviceName string) ([]byte, error) {
123126
}
124127

125128
opts := compose.CommandOptions{
126-
// We set the STACK_VERSION env var here to avoid showing a warning to the user about
127-
// it not being set.
128-
Env: []string{fmt.Sprintf("STACK_VERSION=%s", install.DefaultStackVersion)},
129129
Services: []string{serviceName},
130130
}
131131

0 commit comments

Comments
 (0)