Skip to content

Commit

Permalink
Include host in hosts (#1802) (#1804)
Browse files Browse the repository at this point in the history
Include host in hosts (#1802)

(cherry picked from commit 5b7a0ff)

Co-authored-by: Michal Pristas <[email protected]>
  • Loading branch information
mergify[bot] and michalpristas authored Nov 28, 2022
1 parent 25f383c commit c4510ee
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/pkg/agent/application/fleet_server_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func FleetServerComponentModifier(serverCfg *configuration.FleetServerConfig) co
// that need to be able to connect to fleet server.
func InjectFleetConfigComponentModifier(fleetCfg *configuration.FleetAgentConfig) coordinator.ComponentsModifier {
return func(comps []component.Component, cfg map[string]interface{}) ([]component.Component, error) {
hostsStr := fleetCfg.Client.GetHosts()
fleetHosts := make([]interface{}, 0, len(hostsStr))
for _, host := range hostsStr {
fleetHosts = append(fleetHosts, host)
}

for i, comp := range comps {
if comp.InputSpec != nil && (comp.InputSpec.InputType == endpoint || comp.InputSpec.InputType == apmServer) {
for j, unit := range comp.Units {
Expand All @@ -104,6 +110,7 @@ func InjectFleetConfigComponentModifier(fleetCfg *configuration.FleetAgentConfig
if v, ok := unitCfgMap["fleet"]; ok {
if m, ok := v.(map[string]interface{}); ok {
m["host"] = cfg["host"]
m["hosts"] = fleetHosts
}
}
unitCfg, err := component.ExpectedConfig(unitCfgMap)
Expand Down
73 changes: 73 additions & 0 deletions internal/pkg/agent/application/fleet_server_bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,84 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/structpb"

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
"github.com/elastic/elastic-agent/internal/pkg/remote"
"github.com/elastic/elastic-agent/internal/pkg/testutils"
"github.com/elastic/elastic-agent/pkg/component"
)

func TestInjectFleetConfigComponentModifier(t *testing.T) {
fleetConfig := &configuration.FleetAgentConfig{
Enabled: true,
Client: remote.Config{
Host: "sample.host",
},
}

cfg := map[string]interface{}{
"host": map[string]interface{}{
"id": "agent-id",
},
}

modifier := InjectFleetConfigComponentModifier(fleetConfig)
apmSource, err := structpb.NewStruct(map[string]interface{}{
"sample": "config",
})
require.NoError(t, err)

apmComponent := component.Component{
InputSpec: &component.InputRuntimeSpec{
InputType: "apm",
},
Units: []component.Unit{
{
Type: client.UnitTypeInput,
Config: &proto.UnitExpectedConfig{
Type: "apm",
Source: apmSource,
},
},
},
}
comps := []component.Component{apmComponent}
resComps, err := modifier(comps, cfg)
require.NoError(t, err)

require.Equal(t, 1, len(resComps))
require.Equal(t, 1, len(resComps[0].Units))
resConfig := resComps[0].Units[0].Config.Source.AsMap()
fleet, ok := resConfig["fleet"]
require.True(t, ok)

fleetMap, ok := fleet.(map[string]interface{})
require.True(t, ok)

hostRaw, found := fleetMap["host"]
require.True(t, found)

hostsRaw, found := fleetMap["hosts"]
require.True(t, found)

hostMap, ok := hostRaw.(map[string]interface{})
require.True(t, ok)

idRaw, found := hostMap["id"]
require.True(t, found)
require.Equal(t, "agent-id", idRaw.(string))

hostsSlice, ok := hostsRaw.([]interface{})
require.True(t, ok)
require.Equal(t, 1, len(hostsSlice))
require.Equal(t, "sample.host", hostsSlice[0].(string))

}

func TestFleetServerBootstrapManager(t *testing.T) {
l := testutils.NewErrorLogger(t)
mgr := newFleetServerBootstrapManager(l)
Expand Down

0 comments on commit c4510ee

Please sign in to comment.