Skip to content

Commit bc55f5d

Browse files
fix(compose/agent): refactor Unmarshal into Decode and remove agent_has_publish_ports
1 parent d92388d commit bc55f5d

File tree

4 files changed

+15
-30
lines changed

4 files changed

+15
-30
lines changed

internal/profile/config.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
package profile
66

77
import (
8-
"encoding/json"
98
"errors"
109
"fmt"
1110
"os"
1211

1312
"github.com/elastic/go-ucfg/yaml"
1413

1514
"github.com/elastic/elastic-package/internal/common"
15+
16+
"github.com/mitchellh/mapstructure"
1617
)
1718

1819
type config struct {
@@ -50,21 +51,15 @@ func (c *config) get(name string) (string, bool) {
5051
}
5152
}
5253

53-
func (c *config) Unmarshal(name string, out any) error {
54+
func (c *config) Decode(name string, out any) error {
5455
v, err := c.settings.GetValue(name)
5556
if err != nil {
5657
if errors.Is(err, common.ErrKeyNotFound) {
5758
return nil
5859
}
5960
return err
6061
}
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 {
62+
if err := mapstructure.Decode(v, out); err != nil {
6863
return err
6964
}
7065

internal/profile/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ 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)
164+
func (profile *Profile) Decode(name string, dst any) error {
165+
return profile.config.Decode(name, dst)
166166
}
167167

168168
// RuntimeOverrides defines configuration overrides for the current session.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ 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 }}
142+
ports: [{{ fact "agent_publish_ports" }}]
145143
volumes:
146144
- "../certs/ca-cert.pem:/etc/ssl/certs/elastic-package.pem"
147145
- type: bind

internal/stack/resources.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@ var (
134134
func applyResources(profile *profile.Profile, stackVersion string) error {
135135
stackDir := filepath.Join(profile.ProfilePath, ProfileStackPath)
136136

137+
var agentPorts []string
138+
if err := profile.Decode("stack.agent.ports", &agentPorts); err != nil {
139+
return fmt.Errorf("failed to unmarshal stack.agent.ports: %w", err)
140+
}
141+
137142
resourceManager := resource.NewManager()
138-
facter := resource.StaticFacter{
143+
resourceManager.AddFacter(resource.StaticFacter{
139144
"registry_base_image": PackageRegistryBaseImage,
140145
"elasticsearch_version": stackVersion,
141146
"kibana_version": stackVersion,
@@ -152,21 +157,8 @@ func applyResources(profile *profile.Profile, stackVersion string) error {
152157
"geoip_dir": profile.Config(configGeoIPDir, "./ingest-geoip"),
153158
"logstash_enabled": profile.Config(configLogstashEnabled, "false"),
154159
"self_monitor_enabled": profile.Config(configSelfMonitorEnabled, "false"),
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-
}
161-
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)
160+
"agent_publish_ports": strings.Join(agentPorts, ","),
161+
})
170162

171163
if err := os.MkdirAll(stackDir, 0755); err != nil {
172164
return fmt.Errorf("failed to create stack directory: %w", err)

0 commit comments

Comments
 (0)