• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

elastic / cloudbeat / 8723654472

17 Apr 2024 02:23PM UTC coverage: 75.676% (-0.3%) from 75.999%
8723654472

Pull #2126

github

kubasobon
add add_host_metadata processor for KSPM
Pull Request #2126: Add correct host info for cloud provider virtual machines

10 of 55 new or added lines in 3 files covered. (18.18%)

2 existing lines in 1 file now uncovered.

7333 of 9690 relevant lines covered (75.68%)

18.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/internal/flavors/posture.go
1
// Licensed to Elasticsearch B.V. under one or more contributor
2
// license agreements. See the NOTICE file distributed with
3
// this work for additional information regarding copyright
4
// ownership. Elasticsearch B.V. licenses this file to you under
5
// the Apache License, Version 2.0 (the "License"); you may
6
// not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17

18
package flavors
19

20
import (
21
        "context"
22
        "fmt"
23

24
        "github.com/elastic/beats/v7/libbeat/beat"
25
        agentconfig "github.com/elastic/elastic-agent-libs/config"
26
        "github.com/elastic/elastic-agent-libs/logp"
27

28
        "github.com/elastic/cloudbeat/internal/config"
29
        "github.com/elastic/cloudbeat/internal/flavors/benchmark"
30
        "github.com/elastic/cloudbeat/internal/flavors/benchmark/builder"
31
        _ "github.com/elastic/cloudbeat/internal/processor" // Add cloudbeat default processors.
32
)
33

34
type posture struct {
35
        flavorBase
36
        benchmark builder.Benchmark
37
}
38

39
// NewPosture creates an instance of posture.
40
func NewPosture(b *beat.Beat, agentConfig *agentconfig.C) (beat.Beater, error) {
×
41
        cfg, err := config.New(agentConfig)
×
42
        if err != nil {
×
43
                return nil, fmt.Errorf("error reading config file: %w", err)
×
44
        }
×
45
        return newPostureFromCfg(b, cfg)
×
46
}
47

48
// NewPosture creates an instance of posture.
49
func newPostureFromCfg(b *beat.Beat, cfg *config.Config) (*posture, error) {
×
50
        log := logp.NewLogger("posture")
×
51
        log.Info("Config initiated with cycle period of ", cfg.Period)
×
52
        ctx, cancel := context.WithCancel(context.Background())
×
53

×
54
        strategy, err := benchmark.GetStrategy(cfg, log)
×
55
        if err != nil {
×
56
                cancel()
×
57
                return nil, err
×
58
        }
×
59

60
        log.Infof("Creating benchmark %T", strategy)
×
61
        bench, err := strategy.NewBenchmark(ctx, log, cfg)
×
62
        if err != nil {
×
63
                cancel()
×
64
                return nil, err
×
65
        }
×
66

NEW
67
        err = ensureHostProcessor(log, cfg)
×
NEW
68
        if err != nil {
×
NEW
69
                cancel()
×
NEW
70
                return nil, err
×
NEW
71
        }
×
72

73
        client, err := NewClient(b.Publisher, cfg.Processors)
×
74
        if err != nil {
×
75
                cancel()
×
76
                return nil, fmt.Errorf("failed to init client: %w", err)
×
77
        }
×
78
        log.Infof("posture configured %d processors", len(cfg.Processors))
×
79

×
80
        publisher := NewPublisher(log, flushInterval, eventsThreshold, client)
×
81

×
82
        return &posture{
×
83
                flavorBase: flavorBase{
×
84
                        ctx:       ctx,
×
85
                        cancel:    cancel,
×
86
                        publisher: publisher,
×
87
                        config:    cfg,
×
88
                        log:       log,
×
89
                },
×
90
                benchmark: bench,
×
91
        }, nil
×
92
}
93

94
// Run starts posture.
95
func (bt *posture) Run(*beat.Beat) error {
×
96
        bt.log.Info("posture is running! Hit CTRL-C to stop it")
×
97
        eventsCh, err := bt.benchmark.Run(bt.ctx)
×
98
        if err != nil {
×
99
                return err
×
100
        }
×
101

102
        bt.publisher.HandleEvents(bt.ctx, eventsCh)
×
103
        bt.log.Warn("Posture has finished running")
×
104
        return nil
×
105
}
106

107
// Stop stops posture.
108
func (bt *posture) Stop() {
×
109
        bt.benchmark.Stop()
×
110

×
111
        if err := bt.client.Close(); err != nil {
×
112
                bt.log.Fatal("Cannot close client", err)
×
113
        }
×
114

115
        bt.cancel()
×
116
}
117

118
// ensureAdditionalProcessors modifies cfg.Processors list to ensure 'host'
119
// processor is present for K8s and EKS benchmarks.
NEW
120
func ensureHostProcessor(log *logp.Logger, cfg *config.Config) error {
×
NEW
121
        if cfg.Benchmark != config.CIS_EKS && cfg.Benchmark != config.CIS_K8S {
×
NEW
122
                return nil
×
NEW
123
        }
×
NEW
124
        log.Info("Adding host processor config")
×
NEW
125
        hostProcessor, err := agentconfig.NewConfigFrom("add_host_metadata: ~")
×
NEW
126
        if err != nil {
×
NEW
127
                return err
×
NEW
128
        }
×
NEW
129
        cfg.Processors = append(cfg.Processors, hostProcessor)
×
NEW
130
        return nil
×
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc