Skip to content

Commit

Permalink
Merge pull request #4941 from agm650/dev/strip-network-agent-entity-s…
Browse files Browse the repository at this point in the history
…tate

Strip network agent entity state
  • Loading branch information
echlebek authored Jan 24, 2023
2 parents a864a74 + 7afc62f commit adf024a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG-6.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- Adding a flag at agent level to avoid collecting system.networks property in the agent entity state

## [6.9.1] - 2022-12-01

### Changed
Expand Down
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (a *Agent) RefreshSystemInfo(ctx context.Context) error {
var info corev2.System
var err error

info, err = system.Info()
info, err = system.Info(!a.config.StripNetworks)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions agent/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const (
flagRetryMax = "retry-max"
flagRetryMultiplier = "retry-multiplier"
flagMaxSessionLength = "max-session-length"
flagStripNetworks = "strip-networks"

// TLS flags
flagTrustedCAFile = "trusted-ca-file"
Expand Down Expand Up @@ -148,6 +149,7 @@ func NewAgentConfig(cmd *cobra.Command) (*agent.Config, error) {
cfg.RetryMax = viper.GetDuration(flagRetryMax)
cfg.RetryMultiplier = viper.GetFloat64(flagRetryMultiplier)
cfg.MaxSessionLength = viper.GetDuration(flagMaxSessionLength)
cfg.StripNetworks = viper.GetBool(flagStripNetworks)

// Set the labels & annotations using values defined configuration files
// and/or environment variables for now
Expand Down Expand Up @@ -331,6 +333,7 @@ func handleConfig(cmd *cobra.Command, arguments []string) error {
viper.SetDefault(flagRetryMax, 120*time.Second)
viper.SetDefault(flagRetryMultiplier, 2.0)
viper.SetDefault(flagMaxSessionLength, 0*time.Second)
viper.SetDefault(flagStripNetworks, false)

// Merge in flag set so that it appears in command usage
flags := flagSet()
Expand Down Expand Up @@ -456,6 +459,7 @@ func flagSet() *pflag.FlagSet {
flagSet.Duration(flagRetryMax, viper.GetDuration(flagRetryMax), "maximum amount of time to wait before retrying an agent connection to the backend")
flagSet.Float64(flagRetryMultiplier, viper.GetFloat64(flagRetryMultiplier), "value multiplied with the current retry delay to produce a longer retry delay (bounded by --retry-max)")
flagSet.Duration(flagMaxSessionLength, viper.GetDuration(flagMaxSessionLength), "maximum amount of time after which the agent will reconnect to one of the configured backends (no maximum by default)")
flagSet.Bool(flagStripNetworks, viper.GetBool(flagStripNetworks), "do not include Network info in agent entity state")

flagSet.SetOutput(ioutil.Discard)

Expand Down
4 changes: 4 additions & 0 deletions agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ type Config struct {
// MaxSessionLength is the maximum duration after which the agent will
// reconnect to one of the backends.
MaxSessionLength time.Duration

// StripNetworks is a boolean to specify if we need to strip network
// information from the agent entity state
StripNetworks bool
}

// StatsdServerConfig contains the statsd server configuration
Expand Down
2 changes: 1 addition & 1 deletion backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ func getDefaultBackendID() string {

// getSystemInfo returns the system info of the backend
func getSystemInfo() corev2.System {
info, err := system.Info()
info, err := system.Info(true)
if err != nil {
logger.WithError(err).Error("error getting system info")
}
Expand Down
2 changes: 1 addition & 1 deletion backend/resource/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (br *BackendResource) GenerateBackendEvent(component string, status uint32,
}

func getEntity() (*corev2.Entity, error) {
systemInfo, err := system.Info()
systemInfo, err := system.Info(true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmdmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *CommandPlugin) SetObjectMeta(meta corev2.ObjectMeta) {

func getEntity() (*corev2.Entity, error) {
// create an entity for using with command asset filtering
systemInfo, err := system.Info()
systemInfo, err := system.Info(true)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type azureMetadata struct {

// Info describes the local system, hostname, OS, platform, platform
// family, platform version, and network interfaces.
func Info() (types.System, error) {
func Info(includeNetworks bool) (types.System, error) {
info, err := host.Info()
if err != nil {
return types.System{}, err
Expand All @@ -52,10 +52,11 @@ func Info() (types.System, error) {
if system.Hostname == "" {
system.Hostname = defaultHostname
}

network, err := NetworkInfo()
if err == nil {
system.Network = network
if includeNetworks {
network, err := NetworkInfo()
if err == nil {
system.Network = network
}
}

vmSystem, vmRole, err := host.Virtualization()
Expand Down
29 changes: 26 additions & 3 deletions system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

func TestInfo(t *testing.T) {
info, err := Info()
// Test first with network information included
info, err := Info(true)
assert.NoError(t, err)
assert.NotEmpty(t, info.Arch)
assert.NotEmpty(t, info.Hostname)
Expand All @@ -19,9 +20,31 @@ func TestInfo(t *testing.T) {
assert.NotEmpty(t, info.PlatformFamily)
}
assert.NotEmpty(t, info.PlatformVersion)
assert.NoError(t, err)
assert.NotEmpty(t, info.Network.Interfaces)
nInterface := info.Network.Interfaces[0]
assert.NotEmpty(t, nInterface.Name)
//assert.NotEmpty(t, nInterface.MAC) // can be empty
assert.NotEmpty(t, nInterface.Addresses)

// Then we have to test if with network data stripped
infoWithoutNet, err := Info(false)
assert.NoError(t, err)
assert.NotEmpty(t, infoWithoutNet.Arch)
assert.NotEmpty(t, infoWithoutNet.Hostname)
assert.NotEmpty(t, infoWithoutNet.OS)
assert.NotEmpty(t, infoWithoutNet.Platform)
if info.Platform == "linux" {
assert.NotEmpty(t, infoWithoutNet.PlatformFamily)
}
assert.NotEmpty(t, infoWithoutNet.PlatformVersion)
assert.NoError(t, err)
assert.Empty(t, infoWithoutNet.Network.Interfaces)
}

func TestNetworkInfo(t *testing.T) {
network, err := NetworkInfo()
assert.NoError(t, err)
assert.NotEmpty(t, network.Interfaces)
nInterface := network.Interfaces[0]
assert.NotEmpty(t, nInterface.Name)
// assert.NotEmpty(t, nInterface.MAC) // can be empty
}

0 comments on commit adf024a

Please sign in to comment.