diff --git a/agent/agent.go b/agent/agent.go index c66b01e..2ec7b49 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -20,9 +20,7 @@ import ( "github.com/netboxlabs/orb-agent/agent/version" ) -var ( - ErrMqttConnection = errors.New("failed to connect to a broker") -) +var ErrMqttConnection = errors.New("failed to connect to a broker") type Agent interface { Start(ctx context.Context, cancelFunc context.CancelFunc) error @@ -69,10 +67,12 @@ type orbAgent struct { configManager config.ConfigManager } -const retryRequestDuration = time.Second -const retryRequestFixedTime = 15 -const retryDurationIncrPerAttempts = 10 -const retryMaxAttempts = 4 +const ( + retryRequestDuration = time.Second + retryRequestFixedTime = 15 + retryDurationIncrPerAttempts = 10 + retryMaxAttempts = 4 +) type GroupInfo struct { Name string @@ -97,7 +97,6 @@ func New(logger *zap.Logger, c config.Config) (Agent, error) { } func (a *orbAgent) managePolicies() error { - if a.config.OrbAgent.Policies == nil { return errors.New("no policies specified") } @@ -222,6 +221,7 @@ func (a *orbAgent) logoffWithHeartbeat(ctx context.Context) { } } } + func (a *orbAgent) Stop(ctx context.Context) { a.logger.Info("routine call for stop agent", zap.Any("routine", ctx.Value("routine"))) if a.rpcFromCancelFunc != nil { diff --git a/agent/agent_prof_test.go b/agent/agent_prof_test.go index 64e1a3a..dc280b4 100644 --- a/agent/agent_prof_test.go +++ b/agent/agent_prof_test.go @@ -6,7 +6,6 @@ import ( ) func Test_orbAgent_startBackends(t *testing.T) { - type args struct { agentCtx context.Context } diff --git a/agent/backend/devicediscovery/device_discovery.go b/agent/backend/devicediscovery/device_discovery.go index 51f68ea..1b8b4b7 100644 --- a/agent/backend/devicediscovery/device_discovery.go +++ b/agent/backend/devicediscovery/device_discovery.go @@ -93,8 +93,8 @@ func (d *deviceDiscoveryBackend) SetCommsClient(agentID string, client *mqtt.Cli d.mqttClient = client otelBaseTopic := strings.Replace(baseTopic, "?", "otlp", 1) d.otlpMetricsTopic = fmt.Sprintf("%s/m/%c", otelBaseTopic, agentID[0]) - } + func (d *deviceDiscoveryBackend) Version() (string, error) { var info Info err := d.request("status", &info, http.MethodGet, http.NoBody, "application/json", VersionTimeout) @@ -103,6 +103,7 @@ func (d *deviceDiscoveryBackend) Version() (string, error) { } return info.Version, nil } + func (d *deviceDiscoveryBackend) Start(ctx context.Context, cancelFunc context.CancelFunc) error { d.startTime = time.Now() d.cancelFunc = cancelFunc @@ -192,7 +193,6 @@ func (d *deviceDiscoveryBackend) Start(ctx context.Context, cancelFunc context.C } return nil - } func (d *deviceDiscoveryBackend) Stop(ctx context.Context) error { diff --git a/agent/backend/networkdiscovery/network_discovery.go b/agent/backend/networkdiscovery/network_discovery.go index 21b3235..2542d70 100644 --- a/agent/backend/networkdiscovery/network_discovery.go +++ b/agent/backend/networkdiscovery/network_discovery.go @@ -93,8 +93,8 @@ func (d *networkDiscoveryBackend) SetCommsClient(agentID string, client *mqtt.Cl d.mqttClient = client otelBaseTopic := strings.Replace(baseTopic, "?", "otlp", 1) d.otlpMetricsTopic = fmt.Sprintf("%s/m/%c", otelBaseTopic, agentID[0]) - } + func (d *networkDiscoveryBackend) Version() (string, error) { var info Info err := d.request("status", &info, http.MethodGet, http.NoBody, "application/json", VersionTimeout) @@ -103,6 +103,7 @@ func (d *networkDiscoveryBackend) Version() (string, error) { } return info.Version, nil } + func (d *networkDiscoveryBackend) Start(ctx context.Context, cancelFunc context.CancelFunc) error { d.startTime = time.Now() d.cancelFunc = cancelFunc @@ -192,7 +193,6 @@ func (d *networkDiscoveryBackend) Start(ctx context.Context, cancelFunc context. } return nil - } func (d *networkDiscoveryBackend) Stop(ctx context.Context) error { diff --git a/agent/backend/otel/exporter_builder_test.go b/agent/backend/otel/exporter_builder_test.go index 92dd010..835e21e 100644 --- a/agent/backend/otel/exporter_builder_test.go +++ b/agent/backend/otel/exporter_builder_test.go @@ -63,7 +63,6 @@ service: if _, ok := expectedStruct.Processors["transform/policy_data"]; !ok { t.Error("missing required attributes/policy_data processor", err) } - }) } } diff --git a/agent/backend/otel/otel.go b/agent/backend/otel/otel.go index 05b9ea1..d996aca 100644 --- a/agent/backend/otel/otel.go +++ b/agent/backend/otel/otel.go @@ -22,15 +22,17 @@ import ( var _ backend.Backend = (*openTelemetryBackend)(nil) -const DefaultPath = "otelcol-contrib" -const DefaultHost = "localhost" -const DefaultPort = 4317 +const ( + DefaultPath = "otelcol-contrib" + DefaultHost = "localhost" + DefaultPort = 4317 +) type openTelemetryBackend struct { logger *zap.Logger startTime time.Time - //policies + // policies policyRepo policies.PolicyRepo policyConfigDirectory string agentTags map[string]string @@ -64,7 +66,8 @@ type openTelemetryBackend struct { // Configure initializes the backend with the given configuration func (o *openTelemetryBackend) Configure(logger *zap.Logger, repo policies.PolicyRepo, - config map[string]interface{}, common config.BackendCommons) error { + config map[string]interface{}, common config.BackendCommons, +) error { o.logger = logger o.logger.Info("configuring OpenTelemetry backend") o.policyRepo = repo @@ -135,7 +138,6 @@ func (o *openTelemetryBackend) Version() (string, error) { o.logger.Info("running opentelemetry-contrib version", zap.String("version", versionOutput)) return versionOutput, nil - } func (o *openTelemetryBackend) Start(ctx context.Context, cancelFunc context.CancelFunc) (err error) { diff --git a/agent/backend/otel/policy.go b/agent/backend/otel/policy.go index d537910..c177e3c 100644 --- a/agent/backend/otel/policy.go +++ b/agent/backend/otel/policy.go @@ -7,10 +7,11 @@ import ( "os" "github.com/go-cmd/cmd" - "github.com/netboxlabs/orb-agent/agent/policies" "go.uber.org/zap" "golang.org/x/exp/slices" "gopkg.in/yaml.v3" + + "github.com/netboxlabs/orb-agent/agent/policies" ) const tempFileNamePattern = "otel-%s-config.yml" diff --git a/agent/backend/pktvisor/pktvisor.go b/agent/backend/pktvisor/pktvisor.go index f15f71d..e671161 100644 --- a/agent/backend/pktvisor/pktvisor.go +++ b/agent/backend/pktvisor/pktvisor.go @@ -134,7 +134,6 @@ func (p *pktvisorBackend) Version() (string, error) { } func (p *pktvisorBackend) Start(ctx context.Context, cancelFunc context.CancelFunc) error { - // this should record the start time whether it's successful or not // because it is used by the automatic restart system for last attempt p.startTime = time.Now() @@ -319,7 +318,6 @@ func (p *pktvisorBackend) GetCapabilities() (map[string]interface{}, error) { } func (p *pktvisorBackend) FullReset(ctx context.Context) error { - // force a stop, which stops scrape as well. if proc is dead, it no ops. if state, _, _ := p.getProcRunningStatus(); state == backend.Running { if err := p.Stop(ctx); err != nil { diff --git a/agent/backend/pktvisor/policy.go b/agent/backend/pktvisor/policy.go index 7bec4a9..c670b7c 100644 --- a/agent/backend/pktvisor/policy.go +++ b/agent/backend/pktvisor/policy.go @@ -12,7 +12,6 @@ import ( ) func (p *pktvisorBackend) ApplyPolicy(data policies.PolicyData, updatePolicy bool) error { - if updatePolicy { // To update a policy it's necessary first remove it and then apply a new version if err := p.RemovePolicy(data); err != nil { diff --git a/agent/heartbeats.go b/agent/heartbeats.go index c12513f..d76fea1 100644 --- a/agent/heartbeats.go +++ b/agent/heartbeats.go @@ -20,7 +20,6 @@ const HeartbeatFreq = 50 * time.Second const RestartTimeMin = 5 * time.Minute func (a *orbAgent) sendSingleHeartbeat(ctx context.Context, t time.Time, agentsState fleet.State) { - if a.heartbeatsTopic == "" { a.logger.Debug("heartbeat topic not yet set, skipping") return diff --git a/agent/logging.go b/agent/logging.go index 37bd5ed..7cebe2c 100644 --- a/agent/logging.go +++ b/agent/logging.go @@ -18,32 +18,41 @@ type agentLoggerError struct { a *orbAgent } -var _ mqtt.Logger = (*agentLoggerDebug)(nil) -var _ mqtt.Logger = (*agentLoggerWarn)(nil) -var _ mqtt.Logger = (*agentLoggerCritical)(nil) -var _ mqtt.Logger = (*agentLoggerError)(nil) +var ( + _ mqtt.Logger = (*agentLoggerDebug)(nil) + _ mqtt.Logger = (*agentLoggerWarn)(nil) + _ mqtt.Logger = (*agentLoggerCritical)(nil) + _ mqtt.Logger = (*agentLoggerError)(nil) +) func (a *agentLoggerWarn) Println(v ...interface{}) { a.a.logger.Warn("WARN mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerWarn) Printf(format string, v ...interface{}) { a.a.logger.Warn("WARN mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerDebug) Println(v ...interface{}) { a.a.logger.Debug("DEBUG mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerDebug) Printf(format string, v ...interface{}) { a.a.logger.Debug("DEBUG mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerCritical) Println(v ...interface{}) { a.a.logger.Error("CRITICAL mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerCritical) Printf(format string, v ...interface{}) { a.a.logger.Error("CRITICAL mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerError) Println(v ...interface{}) { a.a.logger.Error("ERROR mqtt log", zap.Any("payload", v)) } + func (a *agentLoggerError) Printf(format string, v ...interface{}) { a.a.logger.Error("ERROR mqtt log", zap.Any("payload", v)) } diff --git a/cmd/e2e_agent_test.go b/cmd/e2e_agent_test.go index 6e6507d..71c032c 100644 --- a/cmd/e2e_agent_test.go +++ b/cmd/e2e_agent_test.go @@ -111,7 +111,7 @@ func Test_main(t *testing.T) { go func() { sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) select { case <-sigs: logger.Warn("stop signal received stopping agent") diff --git a/cmd/main.go b/cmd/main.go index 6684deb..35eca6e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -44,7 +44,6 @@ func Version(_ *cobra.Command, _ []string) { } func Run(_ *cobra.Command, _ []string) { - initConfig() // configuration @@ -120,7 +119,6 @@ func Run(_ *cobra.Command, _ []string) { } func mergeOrError(path string) { - v := viper.New() if len(path) > 0 { v.SetConfigFile(path) @@ -186,7 +184,6 @@ func initConfig() { } func main() { - rootCmd := &cobra.Command{ Use: "orb-agent", }