Skip to content
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

Include host in hosts #1802

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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
Copy link
Contributor

@aleksmaus aleksmaus Nov 28, 2022

Choose a reason for hiding this comment

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

this will always replace "hosts" content, even for the cases where it is defined, right? is this intentional?

Copy link
Contributor Author

@michalpristas michalpristas Nov 28, 2022

Choose a reason for hiding this comment

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

yeah i considered check but i would need to check for length as well, in case it has hosts entry but it's empty array. this is more readable and should not matter.
if you have perf concerns i can add check

Copy link
Contributor

Choose a reason for hiding this comment

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

you mentioned that there was an existing use case where the "hosts" property was already somehow specified (by the user?) do we want to overwrite this completely?

Copy link
Contributor Author

@michalpristas michalpristas Nov 28, 2022

Choose a reason for hiding this comment

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

i meant in original fleet configuration, the one we are injecting here.
the result of this change should not be data loss.
we either replace empty hosts with []{host} or list of hosts with the same list.
but to skip injection i would need to

  • replace it if m[hosts] is not found or
  • retype m[hosts] to []interface, check if it is really []interface and replace if len(m[hosts]0 == 0
    otherwise it's the same

so i rather did a simple replace to avoid retypes and 2 additional checks

}
}
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