Skip to content

Commit d92388d

Browse files
feat(compose/agent): add stack.agent.ports option in config.yml to publish ports for agent
1 parent 28c8e4b commit d92388d

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

internal/profile/_static/config.yml.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@
1818
# Flag to enable logstash in elastic-package stack profile config
1919
# stack.logstash_enabled: true
2020

21+
## Specify agent ports to publish
22+
## port definition schema https://docs.docker.com/compose/compose-file/compose-file-v2/#ports
23+
# stack.agent.ports:
24+
# - 127.0.0.1:1514:1514/udp
25+

internal/profile/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package profile
66

77
import (
8+
"encoding/json"
89
"errors"
910
"fmt"
1011
"os"
@@ -48,3 +49,24 @@ func (c *config) get(name string) (string, bool) {
4849
return fmt.Sprintf("%v", v), true
4950
}
5051
}
52+
53+
func (c *config) Unmarshal(name string, out any) error {
54+
v, err := c.settings.GetValue(name)
55+
if err != nil {
56+
if errors.Is(err, common.ErrKeyNotFound) {
57+
return nil
58+
}
59+
return err
60+
}
61+
62+
data, err := json.Marshal(v)
63+
if err != nil {
64+
return err
65+
}
66+
67+
if err := json.Unmarshal(data, out); err != nil {
68+
return err
69+
}
70+
71+
return nil
72+
}

internal/profile/profile.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ type Profile struct {
141141
}
142142

143143
// Path returns an absolute path to the given file
144-
func (profile Profile) Path(names ...string) string {
144+
func (profile *Profile) Path(names ...string) string {
145145
elems := append([]string{profile.ProfilePath}, names...)
146146
return filepath.Join(elems...)
147147
}
148148

149149
// Config returns a configuration setting, or its default if setting not found
150-
func (profile Profile) Config(name string, def string) string {
150+
func (profile *Profile) Config(name string, def string) string {
151151
v, found := profile.overrides[name]
152152
if found {
153153
return v
@@ -161,6 +161,10 @@ func (profile Profile) Config(name string, def string) string {
161161
return def
162162
}
163163

164+
func (profile *Profile) Unmarshal(name string, dst any) error {
165+
return profile.config.Unmarshal(name, dst)
166+
}
167+
164168
// RuntimeOverrides defines configuration overrides for the current session.
165169
func (profile *Profile) RuntimeOverrides(overrides map[string]string) {
166170
profile.overrides = overrides
@@ -171,7 +175,7 @@ var ErrNotAProfile = errors.New("not a profile")
171175

172176
// ComposeEnvVars returns a list of environment variables that can be passed
173177
// to docker-compose for the sake of filling out paths and names in the snapshot.yml file.
174-
func (profile Profile) ComposeEnvVars() []string {
178+
func (profile *Profile) ComposeEnvVars() []string {
175179
return []string{
176180
fmt.Sprintf("PROFILE_NAME=%s", profile.ProfileName),
177181
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ services:
139139
interval: 5s
140140
hostname: docker-fleet-agent
141141
env_file: "./elastic-agent.env"
142+
{{- if eq (fact "agent_has_publish_ports") "true" }}
143+
ports: {{ fact "agent_publish_ports" }}
144+
{{- end }}
142145
volumes:
143146
- "../certs/ca-cert.pem:/etc/ssl/certs/elastic-package.pem"
144147
- type: bind

internal/stack/resources.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func applyResources(profile *profile.Profile, stackVersion string) error {
135135
stackDir := filepath.Join(profile.ProfilePath, ProfileStackPath)
136136

137137
resourceManager := resource.NewManager()
138-
resourceManager.AddFacter(resource.StaticFacter{
138+
facter := resource.StaticFacter{
139139
"registry_base_image": PackageRegistryBaseImage,
140140
"elasticsearch_version": stackVersion,
141141
"kibana_version": stackVersion,
@@ -152,9 +152,25 @@ func applyResources(profile *profile.Profile, stackVersion string) error {
152152
"geoip_dir": profile.Config(configGeoIPDir, "./ingest-geoip"),
153153
"logstash_enabled": profile.Config(configLogstashEnabled, "false"),
154154
"self_monitor_enabled": profile.Config(configSelfMonitorEnabled, "false"),
155-
})
155+
}
156+
157+
var agentPorts []string
158+
if err := profile.Unmarshal("stack.agent.ports", &agentPorts); err != nil {
159+
return fmt.Errorf("failed to unmarshal stack.agent.ports: %w", err)
160+
}
156161

157-
os.MkdirAll(stackDir, 0755)
162+
if len(agentPorts) > 0 {
163+
facter["agent_has_publish_ports"] = "true"
164+
facter["agent_publish_ports"] = fmt.Sprintf("[%s]", strings.Join(agentPorts, ","))
165+
} else {
166+
facter["agent_has_publish_ports"] = "false"
167+
}
168+
169+
resourceManager.AddFacter(facter)
170+
171+
if err := os.MkdirAll(stackDir, 0755); err != nil {
172+
return fmt.Errorf("failed to create stack directory: %w", err)
173+
}
158174
resourceManager.RegisterProvider("file", &resource.FileProvider{
159175
Prefix: stackDir,
160176
})

0 commit comments

Comments
 (0)