Skip to content

Commit 8e461fb

Browse files
rhr323naemonopebrc
authored
Consistently mount CONFIG_PATH and STATE_PATH to same directory in Fleet mode (#8856) (#8859)
* Consistently mount CONFIG_PATH and STATE_PATH to same directory in Fleet mode. * Add comment * Maybe add init container. * Fix to be deployment specific * remove unneeded call * Add some logging * Fix bug * Add check for existing init container * Add volume mount * Remove test code to add init container. * Adjust test expecations to new command --------- (cherry picked from commit 63bff02) Signed-off-by: Michael Montgomery <[email protected]> Co-authored-by: Michael Montgomery <[email protected]> Co-authored-by: Peter Brachwitz <[email protected]>
1 parent 7cef480 commit 8e461fb

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

pkg/controller/agent/pod.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ func buildPodTemplate(params Params, fleetCerts *certificates.CertificatesSecret
148148
if builder, err = amendBuilderForFleetMode(params, fleetCerts, fleetToken, builder, configHash); err != nil {
149149
return corev1.PodTemplateSpec{}, err
150150
}
151+
// Point agent to static config file mounted from a secret to /etc/agent/elastic-agent.yml
152+
builder = builder.WithArgs("-e", "-c", path.Join(ConfigMountPath, ConfigFileName))
151153
} else if spec.StandaloneModeEnabled() {
152154
// cleanup secret used in Fleet mode
153155
if err := cleanupEnvVarsSecret(params); err != nil {
@@ -156,6 +158,8 @@ func buildPodTemplate(params Params, fleetCerts *certificates.CertificatesSecret
156158

157159
builder = builder.
158160
WithResources(defaultResources).
161+
WithEnv(corev1.EnvVar{Name: "STATE_PATH", Value: DataMountPath}).
162+
// Point agent to static config file mounted from a secret to /etc/agent/elastic-agent.yml
159163
WithArgs("-e", "-c", path.Join(ConfigMountPath, ConfigFileName))
160164
}
161165

@@ -193,15 +197,6 @@ func buildPodTemplate(params Params, fleetCerts *certificates.CertificatesSecret
193197
return builder.PodTemplate, nil
194198
}
195199

196-
func fleetConfigPath(v version.Version) string {
197-
if v.LT(agentv1alpha1.FleetAdvancedConfigMinVersion) {
198-
// default to the in-container config directory for older versions of Elastic Agent
199-
// that still try to rewrite the config file in Fleet mode during enrollment.
200-
return "/usr/share/elastic-agent"
201-
}
202-
return ConfigMountPath
203-
}
204-
205200
func amendBuilderForFleetMode(params Params, fleetCerts *certificates.CertificatesSecret, fleetToken EnrollmentAPIKey, builder *defaults.PodTemplateBuilder, configHash hash.Hash) (*defaults.PodTemplateBuilder, error) {
206201
esAssociation, err := getRelatedEsAssoc(params)
207202
if err != nil {
@@ -241,7 +236,10 @@ func amendBuilderForFleetMode(params Params, fleetCerts *certificates.Certificat
241236

242237
builder = builder.
243238
WithResources(defaultFleetResources).
244-
WithEnv(corev1.EnvVar{Name: "CONFIG_PATH", Value: fleetConfigPath(params.AgentVersion)})
239+
WithEnv(
240+
corev1.EnvVar{Name: "STATE_PATH", Value: DataMountPath},
241+
corev1.EnvVar{Name: "CONFIG_PATH", Value: DataMountPath},
242+
)
245243

246244
return builder, nil
247245
}
@@ -452,7 +450,7 @@ if [[ -f %[1]s ]]; then
452450
%[5]s
453451
fi
454452
fi
455-
/usr/bin/tini -- /usr/local/bin/docker-entrypoint -e
453+
/usr/bin/tini -- /usr/local/bin/docker-entrypoint -e "$@"
456454
`, caPath, ubiSharedCAPath, ubiUpdateCmd, debianSharedCAPath, debianUpdateCmd)
457455
}
458456

pkg/controller/agent/pod_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ func Test_amendBuilderForFleetMode(t *testing.T) {
8181
fleetCerts: fleetCertsFixture,
8282
wantPodSpec: generatePodSpec(func(ps corev1.PodSpec) corev1.PodSpec {
8383
ps.Containers[0].Env = []corev1.EnvVar{
84+
{
85+
Name: "STATE_PATH",
86+
Value: "/usr/share/elastic-agent/state",
87+
},
8488
{
8589
Name: "CONFIG_PATH",
86-
Value: "/usr/share/elastic-agent",
90+
Value: "/usr/share/elastic-agent/state",
8791
},
8892
}
8993

@@ -179,9 +183,13 @@ func Test_amendBuilderForFleetMode(t *testing.T) {
179183
Name: "FLEET_URL",
180184
Value: "https://agent-agent-http.default.svc:8220",
181185
},
186+
{
187+
Name: "STATE_PATH",
188+
Value: "/usr/share/elastic-agent/state",
189+
},
182190
{
183191
Name: "CONFIG_PATH",
184-
Value: "/etc/agent",
192+
Value: "/usr/share/elastic-agent/state",
185193
},
186194
}
187195

@@ -274,9 +282,13 @@ func Test_amendBuilderForFleetMode(t *testing.T) {
274282
Name: "FLEET_URL",
275283
Value: "https://agent-agent-http.default.svc:8220",
276284
},
285+
{
286+
Name: "STATE_PATH",
287+
Value: "/usr/share/elastic-agent/state",
288+
},
277289
{
278290
Name: "CONFIG_PATH",
279-
Value: "/etc/agent",
291+
Value: "/usr/share/elastic-agent/state",
280292
},
281293
}
282294

@@ -311,9 +323,13 @@ func Test_amendBuilderForFleetMode(t *testing.T) {
311323
fleetCerts: fleetCertsFixture,
312324
wantPodSpec: generatePodSpec(func(ps corev1.PodSpec) corev1.PodSpec {
313325
ps.Containers[0].Env = []corev1.EnvVar{
326+
{
327+
Name: "STATE_PATH",
328+
Value: "/usr/share/elastic-agent/state",
329+
},
314330
{
315331
Name: "CONFIG_PATH",
316-
Value: "/etc/agent",
332+
Value: "/usr/share/elastic-agent/state",
317333
},
318334
}
319335

@@ -401,9 +417,13 @@ func Test_amendBuilderForFleetMode(t *testing.T) {
401417
Name: "FLEET_URL",
402418
Value: "http://agent-agent-http.default.svc:8220",
403419
},
420+
{
421+
Name: "STATE_PATH",
422+
Value: "/usr/share/elastic-agent/state",
423+
},
404424
{
405425
Name: "CONFIG_PATH",
406-
Value: "/etc/agent",
426+
Value: "/usr/share/elastic-agent/state",
407427
},
408428
}
409429

@@ -924,7 +944,7 @@ if [[ -f /mnt/elastic-internal/elasticsearch-association/%[1]s/elasticsearch/cer
924944
/usr/sbin/update-ca-certificates
925945
fi
926946
fi
927-
/usr/bin/tini -- /usr/local/bin/docker-entrypoint -e
947+
/usr/bin/tini -- /usr/local/bin/docker-entrypoint -e "$@"
928948
`, ns)}
929949
}
930950
for _, tt := range []struct {

pkg/controller/agent/reconcile.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func reconcilePodVehicle(params Params, podTemplate corev1.PodTemplateSpec) (*re
9898
agent: params.Agent,
9999
podTemplate: podTemplate,
100100
})
101-
102101
if err != nil {
103102
return results.WithError(err), params.Status
104103
}

0 commit comments

Comments
 (0)