Skip to content

feat(compose/agent): add stack.agent.ports option in config.yml to publish ports for agent #1793

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

Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/magefile/mage v1.15.0
github.com/mholt/archiver/v3 v3.5.1
github.com/mitchellh/mapstructure v1.5.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
github.com/shirou/gopsutil/v3 v3.24.3
Expand Down Expand Up @@ -119,7 +120,6 @@ require (
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.5.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions internal/profile/_static/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
# Flag to enable logstash in elastic-package stack profile config
# stack.logstash_enabled: true

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

17 changes: 17 additions & 0 deletions internal/profile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/elastic/go-ucfg/yaml"

"github.com/elastic/elastic-package/internal/common"

"github.com/mitchellh/mapstructure"
)

type config struct {
Expand Down Expand Up @@ -48,3 +50,18 @@ func (c *config) get(name string) (string, bool) {
return fmt.Sprintf("%v", v), true
}
}

func (c *config) Decode(name string, out any) error {
v, err := c.settings.GetValue(name)
if err != nil {
if errors.Is(err, common.ErrKeyNotFound) {
return nil
}
return err
}
if err := mapstructure.Decode(v, out); err != nil {
return err
}

return nil
}
10 changes: 7 additions & 3 deletions internal/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ type Profile struct {
}

// Path returns an absolute path to the given file
func (profile Profile) Path(names ...string) string {
func (profile *Profile) Path(names ...string) string {
Copy link
Member

Choose a reason for hiding this comment

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

Nit. Are these changed to pointer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so here it depends, it is considered an unusual pattern to have a struct definition that mixes both value and pointer receivers, and from a quick look at the code, profile is widely used as a pointer. Thus I homogenise the struct by making these pointer receivers but if there is a reason behind this divergence happy revert this change

Copy link
Member

Choose a reason for hiding this comment

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

Good point. I think I would rather use the profile by value, but it is true that it is mostly used as pointer now, so as you prefer.

elems := append([]string{profile.ProfilePath}, names...)
return filepath.Join(elems...)
}

// Config returns a configuration setting, or its default if setting not found
func (profile Profile) Config(name string, def string) string {
func (profile *Profile) Config(name string, def string) string {
v, found := profile.overrides[name]
if found {
return v
Expand All @@ -161,6 +161,10 @@ func (profile Profile) Config(name string, def string) string {
return def
}

func (profile *Profile) Decode(name string, dst any) error {
return profile.config.Decode(name, dst)
}

// RuntimeOverrides defines configuration overrides for the current session.
func (profile *Profile) RuntimeOverrides(overrides map[string]string) {
profile.overrides = overrides
Expand All @@ -171,7 +175,7 @@ var ErrNotAProfile = errors.New("not a profile")

// ComposeEnvVars returns a list of environment variables that can be passed
// to docker-compose for the sake of filling out paths and names in the snapshot.yml file.
func (profile Profile) ComposeEnvVars() []string {
func (profile *Profile) ComposeEnvVars() []string {
return []string{
fmt.Sprintf("PROFILE_NAME=%s", profile.ProfileName),
}
Expand Down
1 change: 1 addition & 0 deletions internal/stack/_static/docker-compose-stack.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ services:
interval: 5s
hostname: docker-fleet-agent
env_file: "./elastic-agent.env"
ports: [{{ fact "agent_publish_ports" }}]
volumes:
- "../certs/ca-cert.pem:/etc/ssl/certs/elastic-package.pem"
- type: bind
Expand Down
10 changes: 9 additions & 1 deletion internal/stack/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ var (
func applyResources(profile *profile.Profile, stackVersion string) error {
stackDir := filepath.Join(profile.ProfilePath, ProfileStackPath)

var agentPorts []string
if err := profile.Decode("stack.agent.ports", &agentPorts); err != nil {
return fmt.Errorf("failed to unmarshal stack.agent.ports: %w", err)
}

resourceManager := resource.NewManager()
resourceManager.AddFacter(resource.StaticFacter{
"registry_base_image": PackageRegistryBaseImage,
Expand All @@ -152,9 +157,12 @@ func applyResources(profile *profile.Profile, stackVersion string) error {
"geoip_dir": profile.Config(configGeoIPDir, "./ingest-geoip"),
"logstash_enabled": profile.Config(configLogstashEnabled, "false"),
"self_monitor_enabled": profile.Config(configSelfMonitorEnabled, "false"),
"agent_publish_ports": strings.Join(agentPorts, ","),
})

os.MkdirAll(stackDir, 0755)
if err := os.MkdirAll(stackDir, 0755); err != nil {
return fmt.Errorf("failed to create stack directory: %w", err)
}
resourceManager.RegisterProvider("file", &resource.FileProvider{
Prefix: stackDir,
})
Expand Down