Skip to content

Commit ce07339

Browse files
author
Igor Komlew
authored
Configure Coordinator from ENV in init(), prepare for explicit init (#5577)
* skip k8s provider auto-updates in containerized environments
1 parent 54ebec5 commit ce07339

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

providers/coordinator.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ var BuiltinCoreID = coreconf.Config.ID
4646

4747
var Coordinator ProvidersCoordinator
4848

49-
func newCoordinator() *coordinator {
49+
func newCoordinator(globalAutoUpdateCfg UpdateProvidersConfig) *coordinator {
50+
log.Debug().
51+
Bool("global auto-update is enabled", globalAutoUpdateCfg.Enabled).
52+
Msg("providers.newCoordinator() is called")
53+
5054
c := &coordinator{
51-
runningByID: map[string]*RunningProvider{},
52-
runtimes: map[string]*Runtime{},
53-
schema: newExtensibleSchema(),
55+
runningByID: map[string]*RunningProvider{},
56+
runtimes: map[string]*Runtime{},
57+
schema: newExtensibleSchema(),
58+
autoUpdateConfig: globalAutoUpdateCfg,
5459
}
5560
c.schema.coordinator = c
5661
return c
@@ -68,6 +73,7 @@ type coordinator struct {
6873
runtimeCnt int
6974
mutex sync.Mutex
7075
schema extensibleSchema
76+
autoUpdateConfig UpdateProvidersConfig
7177
}
7278

7379
type builtinProvider struct {
@@ -108,9 +114,7 @@ func (c *coordinator) newRuntime() *Runtime {
108114
providers: map[string]*ConnectedProvider{},
109115
recording: recording.Null{},
110116
shutdownTimeout: defaultShutdownTimeout,
111-
AutoUpdate: UpdateProvidersConfig{
112-
Enabled: true,
113-
},
117+
AutoUpdate: c.autoUpdateConfig,
114118
}
115119

116120
c.mutex.Lock()
@@ -245,6 +249,11 @@ func (c *coordinator) RemoveRuntime(runtime *Runtime) {
245249
}
246250

247251
func (c *coordinator) GetRunningProvider(id string, update UpdateProvidersConfig) (*RunningProvider, error) {
252+
log.Debug().
253+
Str("id", id).
254+
Bool("update is enabled", update.Enabled).
255+
Msg("coordinator.GetRunningProvider() is called")
256+
248257
c.mutex.Lock()
249258
defer c.mutex.Unlock()
250259
running := c.runningByID[id]

providers/extensible_schema_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313

1414
func TestExtensibleSchema(t *testing.T) {
1515
s := newExtensibleSchema()
16-
s.coordinator = newCoordinator()
16+
s.coordinator = newCoordinator(UpdateProvidersConfig{
17+
Enabled: true,
18+
})
1719

1820
s.Add("first", &resources.Schema{
1921
Resources: map[string]*resources.ResourceInfo{

providers/providers.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/hashicorp/go-retryablehttp"
2323
"github.com/rs/zerolog/log"
2424
"github.com/spf13/afero"
25+
"github.com/spf13/viper"
2526
"github.com/ulikunitz/xz"
2627
"go.mondoo.com/cnquery/v11/cli/config"
2728
"go.mondoo.com/cnquery/v11/logger/zerologadapter"
@@ -61,9 +62,24 @@ func init() {
6162

6263
LastProviderInstall = time.Now().Unix()
6364

64-
// Initialize the global coordinator instance
65-
coordinator := newCoordinator()
66-
Coordinator = coordinator
65+
viper.SetEnvPrefix("mondoo")
66+
viper.AutomaticEnv()
67+
68+
autoUpdateEnabled := true
69+
config.InitViperConfig()
70+
if viper.IsSet("auto_update") {
71+
autoUpdateEnabled = viper.GetBool("auto_update")
72+
}
73+
74+
log.Info().
75+
Bool("autoUpdateEnabled", autoUpdateEnabled).
76+
Msg("cnquery.providers.init(): initializing global Coordinator from ENV.")
77+
78+
coordinatorCfg := UpdateProvidersConfig{
79+
Enabled: autoUpdateEnabled,
80+
RefreshInterval: 60 * 60,
81+
}
82+
Coordinator = newCoordinator(coordinatorCfg)
6783
}
6884

6985
type ProviderLookup struct {
@@ -936,7 +952,9 @@ func TryProviderUpdate(provider *Provider, update UpdateProvidersConfig) (*Provi
936952
log.Info().
937953
Str("installed", provider.Version).
938954
Str("latest", latest).
955+
Bool("update is enabled", update.Enabled).
939956
Msg("found a new version for '" + provider.Name + "' provider")
957+
940958
provider, err = installVersion(provider.Name, latest)
941959
if err != nil {
942960
return nil, err

providers/runtime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ func (r *Runtime) setProviderConnection(c *plugin.ConnectRes, err error) {
141141
}
142142

143143
func (r *Runtime) addProvider(id string) (*ConnectedProvider, error) {
144+
log.Debug().
145+
Str("id", id).
146+
Bool("update is enabled", r.AutoUpdate.Enabled).
147+
Msg("Runtime.addProvider() is called")
148+
144149
// TODO: we need to detect only the shared running providers
145150
running, err := r.coordinator.GetRunningProvider(id, r.AutoUpdate)
146151
if err != nil {

0 commit comments

Comments
 (0)