Skip to content

Commit 770c7bc

Browse files
authored
Use FLEET_TOKEN_POLICY_NAME to select policy (#697)
* Use FLEET_TOKEN_POLICY_NAME * Use separate env file for Elastic Agent * Fix: missing envs * Fix * Fix: Kubernetes agent token * Fix: comment
1 parent 5e97962 commit 770c7bc

File tree

10 files changed

+62
-8
lines changed

10 files changed

+62
-8
lines changed

internal/profile/_static/docker-compose-stack.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ services:
103103
retries: 180
104104
interval: 1s
105105
hostname: docker-fleet-agent
106-
environment:
107-
- "FLEET_ENROLL=1"
108-
- "FLEET_INSECURE=1"
109-
- "FLEET_URL=http://fleet-server:8220"
106+
env_file: "./elastic-agent.${STACK_VERSION_VARIANT}.env"
110107
volumes:
111108
- type: bind
112109
source: ../../../tmp/service_logs/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FLEET_ENROLL=1
2+
FLEET_INSECURE=1
3+
FLEET_URL=http://fleet-server:8220
4+
FLEET_TOKEN_POLICY_NAME=Elastic-Agent (elastic-package)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FLEET_ENROLL=1
2+
FLEET_INSECURE=1
3+
FLEET_URL=http://fleet-server:8220

internal/profile/profile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type configFile string
3636
// managedProfileFiles is the list of all files managed in a profile
3737
// If you create a new file that's managed by a profile, it needs to go in this list
3838
var managedProfileFiles = map[configFile]NewConfig{
39+
ElasticAgentDefaultEnvFile: newElasticAgentDefaultEnv,
40+
ElasticAgent8xEnvFile: newElasticAgent8xEnv,
3941
ElasticsearchConfigDefaultFile: newElasticsearchConfigDefault,
4042
ElasticsearchConfig8xFile: newElasticsearchConfig8x,
4143
KibanaConfigDefaultFile: newKibanaConfigDefault,

internal/profile/static.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,32 @@ func newPackageRegistryDockerfile(_ string, profilePath string) (*simpleFile, er
115115
path: filepath.Join(profilePath, profileStackPath, string(PackageRegistryDockerfileFile)),
116116
body: packageRegistryDockerfile,
117117
}, nil
118+
}
119+
120+
// ElasticAgent8xEnvFile is the .env for the 8x stack.
121+
const ElasticAgent8xEnvFile configFile = "elastic-agent.8x.env"
122+
123+
//go:embed _static/elastic-agent_8x.env
124+
var elasticAgent8xEnv string
118125

126+
func newElasticAgent8xEnv(_ string, profilePath string) (*simpleFile, error) {
127+
return &simpleFile{
128+
name: string(ElasticAgent8xEnvFile),
129+
path: filepath.Join(profilePath, profileStackPath, string(ElasticAgent8xEnvFile)),
130+
body: elasticAgent8xEnv,
131+
}, nil
132+
}
133+
134+
// ElasticAgentDefaultEnvFile is the default .env file.
135+
const ElasticAgentDefaultEnvFile configFile = "elastic-agent.default.env"
136+
137+
//go:embed _static/elastic-agent_default.env
138+
var elasticAgentDefaultEnv string
139+
140+
func newElasticAgentDefaultEnv(_ string, profilePath string) (*simpleFile, error) {
141+
return &simpleFile{
142+
name: string(ElasticAgentDefaultEnvFile),
143+
path: filepath.Join(profilePath, profileStackPath, string(ElasticAgentDefaultEnvFile)),
144+
body: elasticAgentDefaultEnv,
145+
}, nil
119146
}

internal/stack/logs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/elastic/elastic-package/internal/compose"
1313
"github.com/elastic/elastic-package/internal/docker"
14+
"github.com/elastic/elastic-package/internal/install"
1415
)
1516

1617
func dockerComposeLogs(serviceName string, snapshotFile string) ([]byte, error) {
@@ -20,6 +21,9 @@ func dockerComposeLogs(serviceName string, snapshotFile string) ([]byte, error)
2021
}
2122

2223
opts := compose.CommandOptions{
24+
Env: newEnvBuilder().
25+
withEnv(stackVariantAsEnv(install.DefaultStackVersion)).
26+
build(),
2327
Services: []string{serviceName},
2428
}
2529

internal/stack/shellinit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ func ShellInit(elasticStackProfile *profile.Profile) (string, error) {
6363
}
6464

6565
serviceComposeConfig, err := p.Config(compose.CommandOptions{
66-
Env: append(appConfig.StackImageRefs(install.DefaultStackVersion).AsEnv(), elasticStackProfile.ComposeEnvVars()...),
66+
Env: newEnvBuilder().
67+
withEnvs(appConfig.StackImageRefs(install.DefaultStackVersion).AsEnv()).
68+
withEnvs(elasticStackProfile.ComposeEnvVars()).
69+
withEnv(stackVariantAsEnv(install.DefaultStackVersion)).
70+
build(),
6771
})
6872
if err != nil {
6973
return "", errors.Wrap(err, "could not get Docker Compose configuration for service")

internal/testrunner/runners/system/servicedeployer/elastic-agent-managed.yaml.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ spec:
3535
# If left empty KIBANA_HOST, KIBANA_FLEET_USERNAME, KIBANA_FLEET_PASSWORD are needed
3636
- name: FLEET_ENROLLMENT_TOKEN
3737
value: ""
38+
- name: FLEET_TOKEN_POLICY_NAME
39+
value: "{{ .elasticAgentTokenPolicyName }}"
3840
- name: KIBANA_HOST
3941
value: "http://kibana:5601"
4042
- name: KIBANA_FLEET_USERNAME

internal/testrunner/runners/system/servicedeployer/kubernetes.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
_ "embed"
1010
"path/filepath"
11+
"strings"
1112
"text/template"
1213

1314
"github.com/pkg/errors"
@@ -179,12 +180,22 @@ func getElasticAgentYAML(stackVersion string) ([]byte, error) {
179180

180181
var elasticAgentYaml bytes.Buffer
181182
err = tmpl.Execute(&elasticAgentYaml, map[string]string{
182-
"fleetURL": "http://fleet-server:8220",
183-
"elasticAgentImage": appConfig.StackImageRefs(stackVersion).ElasticAgent,
183+
"fleetURL": "http://fleet-server:8220",
184+
"elasticAgentImage": appConfig.StackImageRefs(stackVersion).ElasticAgent,
185+
"elasticAgentTokenPolicyName": getTokenPolicyName(stackVersion),
184186
})
185187
if err != nil {
186188
return nil, errors.Wrap(err, "can't generate elastic agent manifest")
187189
}
188190

189191
return elasticAgentYaml.Bytes(), nil
190192
}
193+
194+
// getTokenPolicyName function returns the policy name for the 8.x Elastic stack. The agent's policy
195+
// is predefined in the Kibana configuration file. The logic is not present in older stacks.
196+
func getTokenPolicyName(stackVersion string) string {
197+
if strings.HasPrefix(stackVersion, "8.") {
198+
return "Elastic-Agent (elastic-package)"
199+
}
200+
return ""
201+
}

test/packages/with-kind/kubernetes/manifest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories:
1010
- kubernetes
1111
release: ga
1212
conditions:
13-
kibana.version: "^7.16.0 || ^8.0.0"
13+
kibana.version: "^8.0.0"
1414
screenshots:
1515
- src: /img/metricbeat_kubernetes_overview.png
1616
title: Metricbeat Kubernetes Overview

0 commit comments

Comments
 (0)