Skip to content

Commit

Permalink
[Ingest Manager] Refactor: remove deadcode (elastic#24715)
Browse files Browse the repository at this point in the history
[Ingest Manager] Refactor: remove deadcode (elastic#24715)
  • Loading branch information
michalpristas authored Mar 24, 2021
1 parent a4a3ff0 commit 01eb297
Show file tree
Hide file tree
Showing 54 changed files with 105 additions and 287 deletions.
9 changes: 0 additions & 9 deletions x-pack/elastic-agent/pkg/agent/application/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
package application

import (
"io/ioutil"
"testing"
"time"

"gopkg.in/yaml.v2"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -77,9 +74,3 @@ func mustWithConfigMode(standalone bool) *config.Config {
},
)
}

func dumpToYAML(t *testing.T, out string, in interface{}) {
b, err := yaml.Marshal(in)
require.NoError(t, err)
ioutil.WriteFile(out, b, 0600)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
)

const semiLongString = ""

func TestStreamCheck(t *testing.T) {
type testCase struct {
name string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func wrapStrToResp(code int, body string) *http.Response {
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Header: make(http.Header, 0),
Header: make(http.Header),
}
}

Expand Down
14 changes: 0 additions & 14 deletions x-pack/elastic-agent/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ package application
import (
"context"
"fmt"
"io"
"net/http"
"net/url"

"github.com/elastic/go-sysinfo"

Expand Down Expand Up @@ -48,16 +45,6 @@ import (
logreporter "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/reporter/log"
)

type apiClient interface {
Send(
method string,
path string,
params url.Values,
headers http.Header,
body io.Reader,
) (*http.Response, error)
}

type stateStore interface {
Add(fleetapi.Action)
AckToken() string
Expand All @@ -73,7 +60,6 @@ type Managed struct {
cancelCtxFn context.CancelFunc
log *logger.Logger
Config configuration.FleetAgentConfig
api apiClient
agentInfo *info.AgentInfo
gateway gateway.FleetGateway
router pipeline.Router
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestActionDispatcher(t *testing.T) {
action := &mockActionUnknown{}
err = d.Dispatch(ack, action)

require.NoError(t, err)
require.True(t, def.called)
require.Equal(t, action, def.received)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func InjectMonitoring(agentInfo *info.AgentInfo, outputGroup string, rootAst *tr
},
}

config := make(map[string]interface{})
// if monitoring is not specified use default one where everything is enabled
if _, found := transpiler.Lookup(rootAst, monitoringKey); !found {
monitoringNode := transpiler.NewDict([]transpiler.Node{
Expand Down Expand Up @@ -72,7 +71,7 @@ func InjectMonitoring(agentInfo *info.AgentInfo, outputGroup string, rootAst *tr
return programsToRun, err
}

config, err = ast.Map()
config, err := ast.Map()
if err != nil {
return programsToRun, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/pipeline"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/monitoring"
)

type operatorStream struct {
configHandler pipeline.ConfigHandler
log *logger.Logger
monitor monitoring.Monitor
}

func (b *operatorStream) Close() error {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
}

// prompt only when it is not forced and is already enrolled
if !force && (cfg.Fleet != nil && cfg.Fleet.Enabled == true) {
if !force && (cfg.Fleet != nil && cfg.Fleet.Enabled) {
confirm, err := c.Confirm("This will replace your current settings. Do you want to continue?", true)
if err != nil {
return errors.New(err, "problem reading prompt response")
Expand Down
9 changes: 4 additions & 5 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ type saver interface {
Save(io.Reader) error
}

type storeLoad interface {
saver
Load() (io.ReadCloser, error)
}

// enrollCmd is an enroll subcommand that interacts between the Kibana API and the Agent.
type enrollCmd struct {
log *logger.Logger
Expand Down Expand Up @@ -218,6 +213,10 @@ func (c *enrollCmd) fleetServerBootstrap(ctx context.Context) error {
c.options.FleetServer.ConnStr, c.options.FleetServer.PolicyID,
c.options.FleetServer.Host, c.options.FleetServer.Port,
c.options.FleetServer.Cert, c.options.FleetServer.CertKey, c.options.FleetServer.ElasticsearchCA)
if err != nil {
return err
}

configToStore := map[string]interface{}{
"fleet": fleetConfig,
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func exposeMetricsEndpoint(log *logger.Logger, config *common.Config, ns func(st
} else {
content = string(bytes)
}
fmt.Fprintf(w, content)
fmt.Fprint(w, content)
}
}

Expand Down
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/agent/cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func watchCmd(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, ar

isWithinGrace, tilGrace := gracePeriod(marker)
if !isWithinGrace {
log.Debugf("not within grace [updatedOn %v] %v", marker.UpdatedOn.String(), time.Now().Sub(marker.UpdatedOn).String())
log.Debugf("not within grace [updatedOn %v] %v", marker.UpdatedOn.String(), time.Since(marker.UpdatedOn).String())
// if it is started outside of upgrade loop
// if we're not within grace and marker is still there it might mean
// that cleanup was not performed ok, cleanup everything except current version
Expand Down Expand Up @@ -172,7 +172,7 @@ WATCHLOOP:
// gracePeriod returns true if it is within grace period and time until grace period ends.
// otherwise it returns false and 0
func gracePeriod(marker *upgrade.UpdateMarker) (bool, time.Duration) {
sinceUpdate := time.Now().Sub(marker.UpdatedOn)
sinceUpdate := time.Since(marker.UpdatedOn)

if 0 < sinceUpdate && sinceUpdate < gracePeriodDuration {
return true, gracePeriodDuration - sinceUpdate
Expand Down
10 changes: 4 additions & 6 deletions x-pack/elastic-agent/pkg/agent/control/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ type Client interface {

// client manages the state and communication to the Elastic Agent.
type client struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
client proto.ElasticAgentControlClient
cfgLock sync.RWMutex
obsLock sync.RWMutex
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
client proto.ElasticAgentControlClient
}

// New creates a client connection to Elastic Agent.
Expand Down
13 changes: 12 additions & 1 deletion x-pack/elastic-agent/pkg/agent/install/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ func programsFromConfig(cfg *config.Config) ([]program.Program, error) {
}

ppMap, err := program.Programs(agentInfo, ast)
if err != nil {
return nil, errors.New("failed to get programs from config", err)
}

var pp []program.Program
check := make(map[string]bool)
Expand All @@ -196,6 +199,10 @@ func programsFromConfig(cfg *config.Config) ([]program.Program, error) {

func applyDynamics(ctx context.Context, log *logger.Logger, cfg *config.Config) (*config.Config, error) {
cfgMap, err := cfg.ToMapStr()
if err != nil {
return nil, err
}

ast, err := transpiler.NewAST(cfgMap)
if err != nil {
return nil, err
Expand All @@ -204,7 +211,7 @@ func applyDynamics(ctx context.Context, log *logger.Logger, cfg *config.Config)
// apply dynamic inputs
inputs, ok := transpiler.Lookup(ast, "inputs")
if ok {
varsArray := make([]*transpiler.Vars, 0, 0)
varsArray := make([]*transpiler.Vars, 0)
var wg sync.WaitGroup
wg.Add(1)
varsCallback := func(vv []*transpiler.Vars) {
Expand Down Expand Up @@ -246,5 +253,9 @@ func applyDynamics(ctx context.Context, log *logger.Logger, cfg *config.Config)
}

finalConfig, err := newAst.Map()
if err != nil {
return nil, err
}

return config.NewConfigFrom(finalConfig)
}
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/operation/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func waitFor(t *testing.T, check func() error) {
if err == nil {
return
}
if time.Now().Sub(started) >= 15*time.Second {
if time.Since(started) >= 15*time.Second {
t.Fatalf("check timed out after 15 second: %s", err)
}
time.Sleep(10 * time.Millisecond)
Expand Down
42 changes: 0 additions & 42 deletions x-pack/elastic-agent/pkg/agent/operation/operation_remove.go

This file was deleted.

7 changes: 1 addition & 6 deletions x-pack/elastic-agent/pkg/agent/operation/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (o *Operator) HandleConfig(cfg configrequest.Request) error {
o.statusController.UpdateStateID(stateID)

for _, step := range steps {
if strings.ToLower(step.ProgramSpec.Cmd) != strings.ToLower(monitoringName) {
if !strings.EqualFold(step.ProgramSpec.Cmd, monitoringName) {
if _, isSupported := program.SupportedMap[strings.ToLower(step.ProgramSpec.Cmd)]; !isSupported {
// mark failed, new config cannot be run
msg := fmt.Sprintf("program '%s' is not supported", step.ProgramSpec.Cmd)
Expand Down Expand Up @@ -344,8 +344,3 @@ func (o *Operator) deleteApp(p Descriptor) {
o.logger.Debugf("operator is removing %s from app collection: %v", p.ID(), o.apps)
delete(o.apps, id)
}

func isMonitorable(descriptor Descriptor) bool {
isSidecar := app.IsSidecar(descriptor)
return !isSidecar // everything is monitorable except sidecar
}
2 changes: 0 additions & 2 deletions x-pack/elastic-agent/pkg/agent/stateresolver/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ const (
unchangedState // UNCHANGED
)

type id string

// state represent the SHOULD state of the system, contains a reference to the actual bundle of
// configuration received by the upstream call and keep track of the last change executed on a program.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestStateResolverAcking(t *testing.T) {
ack()

// Current sate is not empty lets try to resolve the same configuration.
_, _, steps, ack, err = r.Resolve(submit)
_, _, steps, _, err = r.Resolve(submit)
require.NoError(t, err)
require.Equal(t, 0, len(steps))
})
Expand Down
4 changes: 0 additions & 4 deletions x-pack/elastic-agent/pkg/agent/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ type Store interface {
Save(io.Reader) error
}

type load interface {
Load() (io.ReadCloser, error)
}

// NullStore this is only use to split the work into multiples PRs.
type NullStore struct{}

Expand Down
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/agent/transpiler/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func (s *BoolVal) Find(key string) (Node, bool) {
}

func (s *BoolVal) String() string {
if s.value == true {
if s.value {
return "true"
}
return "false"
Expand Down Expand Up @@ -1077,7 +1077,7 @@ func Insert(a *AST, node Node, to Selector) error {
// to create 2 different sub AST and want to merge them together again.
func Combine(a, b *AST) (*AST, error) {
newAST := &AST{}
if reflect.TypeOf(b.root) != reflect.TypeOf(b.root) {
if reflect.TypeOf(a.root) != reflect.TypeOf(b.root) {
return nil, fmt.Errorf("incompatible node type to combine, received %T and %T", a, b)
}

Expand Down
6 changes: 3 additions & 3 deletions x-pack/elastic-agent/pkg/agent/transpiler/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ func (r *TranslateWithRegexpRule) Apply(_ AgentInfo, ast *AST) error {
return fmt.Errorf("cannot rename, invalid type expected 'Key' received '%T'", node)
}

candidate, ok := n.value.(Node).Value().(string)
candidate, ok := n.value.Value().(string)
if !ok {
return fmt.Errorf("cannot filter on value expected 'string' and received %T", candidate)
}
Expand Down Expand Up @@ -1170,7 +1170,7 @@ func (r *FilterValuesRule) Apply(_ AgentInfo, ast *AST) error {
}

for _, v := range r.Values {
if v == n.value.(Node).Value() {
if v == n.value.Value() {
newNodes = append(newNodes, item)
break
}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ func (r *FilterValuesWithRegexpRule) Apply(_ AgentInfo, ast *AST) error {
continue
}

candidate, ok := n.value.(Node).Value().(string)
candidate, ok := n.value.Value().(string)
if !ok {
return fmt.Errorf("cannot filter on value expected 'string' and received %T", candidate)
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/transpiler/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (v *Vars) Lookup(name string) (interface{}, bool) {
func nodeToValue(node Node) Node {
switch n := node.(type) {
case *Key:
return n.value.(Node)
return n.value
}
return node
}
Expand Down
Loading

0 comments on commit 01eb297

Please sign in to comment.