Skip to content

Commit

Permalink
adding local config file
Browse files Browse the repository at this point in the history
  • Loading branch information
jt-dd committed Jul 30, 2024
1 parent e31957f commit 84594a6
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
const (
DefaultConfigType = "yaml"
DefaultClusterName = "unknown"
DefaultConfigName = "kubehound"

GlobalDebug = "debug"
)
Expand Down Expand Up @@ -88,50 +89,52 @@ func NewKubehoundConfig(configPath string, inLine bool) *KubehoundConfig {
}

// SetDefaultValues loads the default value from the different modules
func SetDefaultValues(c *viper.Viper) {
func SetDefaultValues(v *viper.Viper) {
// K8s Live collector module
c.SetDefault(CollectorLivePageSize, DefaultK8sAPIPageSize)
c.SetDefault(CollectorLivePageBufferSize, DefaultK8sAPIPageBufferSize)
c.SetDefault(CollectorLiveRate, DefaultK8sAPIRateLimitPerSecond)
c.SetDefault(CollectorNonInteractive, DefaultK8sAPINonInteractive)
v.SetDefault(CollectorLivePageSize, DefaultK8sAPIPageSize)
v.SetDefault(CollectorLivePageBufferSize, DefaultK8sAPIPageBufferSize)
v.SetDefault(CollectorLiveRate, DefaultK8sAPIRateLimitPerSecond)
v.SetDefault(CollectorNonInteractive, DefaultK8sAPINonInteractive)

// Default values for storage provider
c.SetDefault("storage.wipe", true)
c.SetDefault("storage.retry", DefaultRetry)
c.SetDefault("storage.retry_delay", DefaultRetryDelay)
v.SetDefault("storage.wipe", true)

Check failure on line 100 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / linter

undefined: c) (typecheck)

Check failure on line 100 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / linter

undefined: c) (typecheck)

Check failure on line 100 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / linter

undefined: c) (typecheck)

Check failure on line 100 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / linter

undefined: c (typecheck)

Check failure on line 100 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / system-test

undefined: c
v.SetDefault("storage.retry", DefaultRetry)
v.SetDefault("storage.retry_delay", DefaultRetryDelay)

// Disable Datadog telemetry by default
c.SetDefault(TelemetryEnabled, false)
v.SetDefault(TelemetryEnabled, false)

// Default value for MongoDB
c.SetDefault("mongodb.url", DefaultMongoUrl)
c.SetDefault("mongodb.connection_timeout", DefaultConnectionTimeout)
v.SetDefault("mongodb.url", DefaultMongoUrl)
v.SetDefault("mongodb.connection_timeout", DefaultConnectionTimeout)

// Defaults values for JanusGraph
c.SetDefault("janusgraph.url", DefaultJanusGraphUrl)
c.SetDefault("janusgraph.connection_timeout", DefaultConnectionTimeout)
v.SetDefault("janusgraph.url", DefaultJanusGraphUrl)
v.SetDefault("janusgraph.connection_timeout", DefaultConnectionTimeout)

// Profiler values
c.SetDefault(TelemetryProfilerPeriod, DefaultProfilerPeriod)
c.SetDefault(TelemetryProfilerCPUDuration, DefaultProfilerCPUDuration)
v.SetDefault(TelemetryProfilerPeriod, DefaultProfilerPeriod)
v.SetDefault(TelemetryProfilerCPUDuration, DefaultProfilerCPUDuration)

// Default values for graph builder
c.SetDefault("builder.vertex.batch_size", DefaultVertexBatchSize)
c.SetDefault("builder.vertex.batch_size_small", DefaultVertexBatchSizeSmall)
c.SetDefault("builder.edge.worker_pool_size", DefaultEdgeWorkerPoolSize)
c.SetDefault("builder.edge.worker_pool_capacity", DefaultEdgeWorkerPoolCapacity)
c.SetDefault("builder.edge.batch_size", DefaultEdgeBatchSize)
c.SetDefault("builder.edge.batch_size_small", DefaultEdgeBatchSizeSmall)
c.SetDefault("builder.edge.batch_size_cluster_impact", DefaultEdgeBatchSizeClusterImpact)
c.SetDefault("builder.stop_on_error", DefaultStopOnError)
c.SetDefault("builder.edge.large_cluster_optimizations", DefaultLargeClusterOptimizations)

c.SetDefault(IngestorAPIEndpoint, DefaultIngestorAPIEndpoint)
c.SetDefault(IngestorAPIInsecure, DefaultIngestorAPIInsecure)
c.SetDefault(IngestorBlobBucketName, DefaultBucketName)
c.SetDefault(IngestorTempDir, DefaultTempDir)
c.SetDefault(IngestorMaxArchiveSize, DefaultMaxArchiveSize)
c.SetDefault(IngestorArchiveName, DefaultArchiveName)
v.SetDefault("builder.vertex.batch_size", DefaultVertexBatchSize)
v.SetDefault("builder.vertex.batch_size_small", DefaultVertexBatchSizeSmall)
v.SetDefault("builder.edge.worker_pool_size", DefaultEdgeWorkerPoolSize)
v.SetDefault("builder.edge.worker_pool_capacity", DefaultEdgeWorkerPoolCapacity)
v.SetDefault("builder.edge.batch_size", DefaultEdgeBatchSize)
v.SetDefault("builder.edge.batch_size_small", DefaultEdgeBatchSizeSmall)
v.SetDefault("builder.edge.batch_size_cluster_impact", DefaultEdgeBatchSizeClusterImpact)
v.SetDefault("builder.stop_on_error", DefaultStopOnError)
v.SetDefault("builder.edge.large_cluster_optimizations", DefaultLargeClusterOptimizations)

v.SetDefault(IngestorAPIEndpoint, DefaultIngestorAPIEndpoint)
v.SetDefault(IngestorAPIInsecure, DefaultIngestorAPIInsecure)
v.SetDefault(IngestorBlobBucketName, DefaultBucketName)
v.SetDefault(IngestorTempDir, DefaultTempDir)
v.SetDefault(IngestorMaxArchiveSize, DefaultMaxArchiveSize)
v.SetDefault(IngestorArchiveName, DefaultArchiveName)

SetLocalConfig(v)
}

// SetEnvOverrides enables environment variable overrides for the config.
Expand Down Expand Up @@ -206,6 +209,22 @@ func NewInlineConfig(v *viper.Viper) (*KubehoundConfig, error) {
return kc, nil
}

// Load local config file if it exists, check for local file in current dir or in $HOME/.config/
// Not returning any error as it is not mandatory to have a local config file
func SetLocalConfig(v *viper.Viper) {
v.SetConfigName(DefaultConfigName) // name of config file (without extension)
v.SetConfigType(DefaultConfigType) // REQUIRED if the config file does not have the extension in the name
v.AddConfigPath("$HOME/.config/") // call multiple times to add many search paths
v.AddConfigPath(".") // optionally look for config in the working directory

err := v.ReadInConfig()
if err != nil {
log.I.Warnf("No local config file was found (%s.%s)", DefaultConfigName, DefaultConfigType)
log.I.Debugf("Error reading config: %v", err)
}
log.I.Infof("Using %s for default config\n", viper.ConfigFileUsed())
}

// NewEmbedConfig creates a new config instance from an embedded config file using viper.
func NewEmbedConfig(v *viper.Viper, configPath string) (*KubehoundConfig, error) {
v.SetConfigType(DefaultConfigType)
Expand Down

0 comments on commit 84594a6

Please sign in to comment.