Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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

22 changes: 22 additions & 0 deletions internal/profile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package profile

import (
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -48,3 +49,24 @@ func (c *config) get(name string) (string, bool) {
return fmt.Sprintf("%v", v), true
}
}

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

data, err := json.Marshal(v)
if err != nil {
return err
}

if err := json.Unmarshal(data, 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) Unmarshal(name string, dst any) error {
return profile.config.Unmarshal(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
3 changes: 3 additions & 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,9 @@ services:
interval: 5s
hostname: docker-fleet-agent
env_file: "./elastic-agent.env"
{{- if eq (fact "agent_has_publish_ports") "true" }}
ports: {{ fact "agent_publish_ports" }}
{{- end }}
volumes:
- "../certs/ca-cert.pem:/etc/ssl/certs/elastic-package.pem"
- type: bind
Expand Down
22 changes: 19 additions & 3 deletions internal/stack/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func applyResources(profile *profile.Profile, stackVersion string) error {
stackDir := filepath.Join(profile.ProfilePath, ProfileStackPath)

resourceManager := resource.NewManager()
resourceManager.AddFacter(resource.StaticFacter{
facter := resource.StaticFacter{
"registry_base_image": PackageRegistryBaseImage,
"elasticsearch_version": stackVersion,
"kibana_version": stackVersion,
Expand All @@ -152,9 +152,25 @@ 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"),
})
}

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

os.MkdirAll(stackDir, 0755)
if len(agentPorts) > 0 {
facter["agent_has_publish_ports"] = "true"
facter["agent_publish_ports"] = fmt.Sprintf("[%s]", strings.Join(agentPorts, ","))
} else {
facter["agent_has_publish_ports"] = "false"
}

resourceManager.AddFacter(facter)

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