Skip to content

Commit

Permalink
Add fleet-server to elastic-agent inspect output (#243)
Browse files Browse the repository at this point in the history
* Add fleet-server to elastic-agent inspect output

Add the fleet-server input from the full filled config that is parsed
for programs in elastic-agent inspect output. This will show the policy
for fleet-server instead of the contents of fleet.yml

* Add unit test

* fix linter
  • Loading branch information
michel-laterman committed Mar 24, 2022
1 parent 0696c10 commit 05d4ad7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
- Reduce Elastic Agent shut down time by stopping processes concurrently {pull}29650[29650]
- Move `context cancelled` error from fleet gateway into debug level. {pull}187[187]
- Update library containerd to 1.5.10. {pull}186[186]
- Add fleet-server to output of elastic-agent inspect output command (and diagnostic bundle). {pull}243[243]

==== New features

Expand Down
42 changes: 42 additions & 0 deletions internal/pkg/agent/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,51 @@ func getProgramsFromConfig(log *logger.Logger, agentInfo *info.AgentInfo, cfg *c
return nil, err
}
composableWaiter.Wait()

// add the fleet-server input to default programs list
// this does not correspond to the actual config that fleet-server uses as it's in fleet.yml and not part of the assembled config (cfg)
fleetCFG, err := cfg.ToMapStr()
if err != nil {
return nil, err
}
if fleetInput := getFleetInput(fleetCFG); fleetInput != nil {
ast, err := transpiler.NewAST(fleetInput)
if err != nil {
return nil, err
}
router.programs["default"] = append(router.programs["default"], program.Program{
Spec: program.Spec{
Name: "fleet-server",
Cmd: "fleet-server",
},
Config: ast,
})
}

return router.programs, nil
}

func getFleetInput(o map[string]interface{}) map[string]interface{} {
arr, ok := o["inputs"].([]interface{})
if !ok {
return nil
}
for _, iface := range arr {
input, ok := iface.(map[string]interface{})
if !ok {
continue
}
t, ok := input["type"]
if !ok {
continue
}
if t.(string) == "fleet-server" {
return input
}
}
return nil
}

type inmemRouter struct {
programs map[string][]program.Program
}
Expand Down
51 changes: 51 additions & 0 deletions internal/pkg/agent/cmd/inspect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package cmd

import (
"testing"
)

func TestGetFleetInput(t *testing.T) {
tests := []struct {
name string
input map[string]interface{}
expect map[string]interface{}
}{{
name: "fleet-server input found",
input: map[string]interface{}{
"inputs": []map[string]interface{}{
map[string]interface{}{
"type": "fleet-server",
}},
},
expect: map[string]interface{}{
"type": "fleet-server",
},
}, {
name: "no fleet-server input",
input: map[string]interface{}{
"inputs": []map[string]interface{}{
map[string]interface{}{
"type": "test-server",
}},
},
expect: nil,
}, {
name: "wrong input formant",
input: map[string]interface{}{
"inputs": "example",
},
expect: nil,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := getFleetInput(tt.input)
if tt.expect == nil && r != nil {
t.Error("expected nil")
}
})
}
}

0 comments on commit 05d4ad7

Please sign in to comment.