core: implement operator settings as env vars

The operator settings loaded from the configmap have proven
inefficient for load time and frequently checking the configmap.
To avoid this ineffenciency, the configmap is only loaded once
each time it is created or updated. The values in the configmap
are applied as environment variables, which then are very efficient
to query throughout the various controllers, without needing
to be concerned about loading the configmap again.

Co-authored-by: Dmitry Mishin <dmitry.mishin@gmail.com>
Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
This commit is contained in:
Travis NielsenandDmitry Mishin committed 2025-02-24 12:12:45 -07:00
1 parent 4eed2a644f
commit d109dc9029
25 files changed
+372 -468

No files matched your search

@@ -18,6 +18,7 @@ package controller
import (
ctx "context"
"os"
"testing"
"time"
@@ -80,83 +81,94 @@ func TestCanIgnoreHealthErrStatusInReconcile(t *testing.T) {
}
func TestSetCephCommandsTimeout(t *testing.T) {
SetCephCommandsTimeout(map[string]string{})
defer os.Unsetenv("ROOK_CEPH_COMMANDS_TIMEOUT_SECONDS")
SetCephCommandsTimeout()
assert.Equal(t, 15*time.Second, exec.CephCommandsTimeout)
exec.CephCommandsTimeout = 0
SetCephCommandsTimeout(map[string]string{"ROOK_CEPH_COMMANDS_TIMEOUT_SECONDS": "0"})
os.Setenv("ROOK_CEPH_COMMANDS_TIMEOUT_SECONDS", "0")
SetCephCommandsTimeout()
assert.Equal(t, 15*time.Second, exec.CephCommandsTimeout)
exec.CephCommandsTimeout = 0
SetCephCommandsTimeout(map[string]string{"ROOK_CEPH_COMMANDS_TIMEOUT_SECONDS": "1"})
os.Setenv("ROOK_CEPH_COMMANDS_TIMEOUT_SECONDS", "1")
SetCephCommandsTimeout()
assert.Equal(t, 1*time.Second, exec.CephCommandsTimeout)
}
func TestSetAllowLoopDevices(t *testing.T) {
SetAllowLoopDevices(map[string]string{})
defer os.Unsetenv("ROOK_CEPH_ALLOW_LOOP_DEVICES")
SetAllowLoopDevices()
assert.False(t, LoopDevicesAllowed())
SetAllowLoopDevices(map[string]string{"ROOK_CEPH_ALLOW_LOOP_DEVICES": "foo"})
os.Setenv("ROOK_CEPH_ALLOW_LOOP_DEVICES", "foo")
SetAllowLoopDevices()
assert.False(t, LoopDevicesAllowed())
SetAllowLoopDevices(map[string]string{"ROOK_CEPH_ALLOW_LOOP_DEVICES": "false"})
os.Setenv("ROOK_CEPH_ALLOW_LOOP_DEVICES", "false")
SetAllowLoopDevices()
assert.False(t, LoopDevicesAllowed())
SetAllowLoopDevices(map[string]string{"ROOK_CEPH_ALLOW_LOOP_DEVICES": "true"})
os.Setenv("ROOK_CEPH_ALLOW_LOOP_DEVICES", "true")
SetAllowLoopDevices()
assert.True(t, LoopDevicesAllowed())
}
func TestSetEnforceHostNetwork(t *testing.T) {
defer os.Unsetenv(enforceHostNetworkSettingName)
logger.Infof("testing default value for %v", enforceHostNetworkSettingName)
opConfig := map[string]string{}
SetEnforceHostNetwork(opConfig)
SetEnforceHostNetwork()
assert.False(t, EnforceHostNetwork())
// test invalid setting
var value string = "foo"
logger.Infof("testing invalid value'%v' for %v", value, enforceHostNetworkSettingName)
opConfig[enforceHostNetworkSettingName] = value
SetEnforceHostNetwork(opConfig)
os.Setenv(enforceHostNetworkSettingName, value)
SetEnforceHostNetwork()
assert.False(t, EnforceHostNetwork())
// test valid settings
value = "true"
logger.Infof("testing valid value'%v' for %v", value, enforceHostNetworkSettingName)
opConfig[enforceHostNetworkSettingName] = value
SetEnforceHostNetwork(opConfig)
os.Setenv(enforceHostNetworkSettingName, value)
SetEnforceHostNetwork()
assert.True(t, EnforceHostNetwork())
value = "false"
logger.Infof("testing valid value'%v' for %v", value, enforceHostNetworkSettingName)
opConfig[enforceHostNetworkSettingName] = value
SetEnforceHostNetwork(opConfig)
os.Setenv(enforceHostNetworkSettingName, value)
SetEnforceHostNetwork()
assert.False(t, EnforceHostNetwork())
}
func TestSetRevisionHistoryLimit(t *testing.T) {
opConfig := map[string]string{}
defer os.Unsetenv(revisionHistoryLimitSettingName)
t.Run("ROOK_REVISION_HISTORY_LIMIT: test default value", func(t *testing.T) {
SetRevisionHistoryLimit(opConfig)
SetRevisionHistoryLimit()
assert.Nil(t, RevisionHistoryLimit())
})
var value string = "foo"
t.Run("ROOK_REVISION_HISTORY_LIMIT: test invalid value 'foo'", func(t *testing.T) {
opConfig[revisionHistoryLimitSettingName] = value
SetRevisionHistoryLimit(opConfig)
os.Setenv(revisionHistoryLimitSettingName, value)
SetRevisionHistoryLimit()
assert.Nil(t, RevisionHistoryLimit())
})
t.Run("ROOK_REVISION_HISTORY_LIMIT: test empty string value", func(t *testing.T) {
value = ""
opConfig[revisionHistoryLimitSettingName] = value
SetRevisionHistoryLimit(opConfig)
os.Setenv(revisionHistoryLimitSettingName, value)
SetRevisionHistoryLimit()
assert.Nil(t, RevisionHistoryLimit())
})
t.Run("ROOK_REVISION_HISTORY_LIMIT: test valig value '10'", func(t *testing.T) {
value = "10"
opConfig[revisionHistoryLimitSettingName] = value
SetRevisionHistoryLimit(opConfig)
os.Setenv(revisionHistoryLimitSettingName, value)
SetRevisionHistoryLimit()
assert.Equal(t, int32(10), *RevisionHistoryLimit())
})
}
@@ -249,36 +261,39 @@ func TestIsReadyToReconcile(t *testing.T) {
func TestObcAllowAdditionalConfigFields(t *testing.T) {
tests := []struct {
name string
data map[string]string
shouldAllow []string
shouldDisallow []string
name string
additionalConfig string
shouldAllow []string
shouldDisallow []string
}{
{"not set", map[string]string{},
{"not set", "<notset>",
[]string{"maxObjects", "maxSize"}, // default allowlist is unlikely to need changing EVER
[]string{"bucketMaxObjects", "bucketMaxSize", "bucketPolicy", "bucketLifecycle", "bucketOwner", "random"}},
{"set to empty", map[string]string{"ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS": ""},
{"set to empty", "",
[]string{}, // admin can allow no quota options if desired
[]string{"maxObjects", "maxSize", "bucketMaxObjects", "bucketMaxSize", "bucketPolicy", "bucketLifecycle", "bucketOwner", "random"}},
{"set to default", map[string]string{"ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS": "maxObjects,maxSize"},
{"set to default", "maxObjects,maxSize",
[]string{"maxObjects", "maxSize"},
[]string{"bucketMaxObjects", "bucketMaxSize", "bucketPolicy", "bucketLifecycle", "bucketOwner", "random"}},
{"all quota fields", map[string]string{"ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS": "maxObjects,maxSize,bucketMaxObjects,bucketMaxSize"},
{"all quota fields", "maxObjects,maxSize,bucketMaxObjects,bucketMaxSize",
[]string{"maxObjects", "maxSize", "bucketMaxObjects", "bucketMaxSize"},
[]string{"bucketPolicy", "bucketLifecycle", "bucketOwner", "random"}},
{"all fields", map[string]string{"ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS": "maxObjects,maxSize,bucketMaxObjects,bucketMaxSize,bucketPolicy,bucketLifecycle,bucketOwner"},
{"all fields", "maxObjects,maxSize,bucketMaxObjects,bucketMaxSize,bucketPolicy,bucketLifecycle,bucketOwner",
[]string{"maxObjects", "maxSize", "bucketMaxObjects", "bucketMaxSize", "bucketPolicy", "bucketLifecycle", "bucketOwner"},
[]string{"random"}},
// this mechanism doesn't do any field checking - that isn't it's job - it merely handles
// allow-listing essentially arbitrary config keys
{"all fields including unknown", map[string]string{"ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS": "maxObjects,maxSize,bucketMaxObjects,bucketMaxSize,bucketPolicy,bucketLifecycle,bucketOwner,random"},
{"all fields including unknown", "maxObjects,maxSize,bucketMaxObjects,bucketMaxSize,bucketPolicy,bucketLifecycle,bucketOwner,random",
[]string{"maxObjects", "maxSize", "bucketMaxObjects", "bucketMaxSize", "bucketPolicy", "bucketLifecycle", "bucketOwner", "random"},
[]string{"otherRandom"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetObcAllowAdditionalConfigFields(tt.data)
if tt.additionalConfig != "<notset>" {
os.Setenv("ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS", tt.additionalConfig)
defer os.Unsetenv("ROOK_OBC_ALLOW_ADDITIONAL_CONFIG_FIELDS")
}
SetObcAllowAdditionalConfigFields()
for _, toAllow := range tt.shouldAllow {
assert.True(t, ObcAdditionalConfigKeyIsAllowed(toAllow))
}