From 9230b3329a0c1781c692c7368496b8921e288c69 Mon Sep 17 00:00:00 2001 From: Anuj Chaudhari Date: Thu, 12 Sep 2024 20:28:34 -0700 Subject: [PATCH] Read default database refresh threshold from Central Config --- pkg/centralconfig/central_config.go | 3 + pkg/centralconfig/central_config_defaults.go | 10 +++ pkg/centralconfig/central_config_keys.go | 1 + pkg/centralconfig/central_config_reader.go | 18 +++++ .../central_config_reader_test.go | 59 ++++++++++++++++ .../fakes/central_config_fake.go | 70 +++++++++++++++++++ .../central_config_cache_init.go | 2 +- .../central_config_cache_init_test.go | 2 +- 8 files changed, 163 insertions(+), 2 deletions(-) rename pkg/{centralconfig => centralconfiginit}/central_config_cache_init.go (99%) rename pkg/{centralconfig => centralconfiginit}/central_config_cache_init_test.go (99%) diff --git a/pkg/centralconfig/central_config.go b/pkg/centralconfig/central_config.go index 2b873e0d9..c70993439 100644 --- a/pkg/centralconfig/central_config.go +++ b/pkg/centralconfig/central_config.go @@ -25,6 +25,9 @@ type CentralConfig interface { // GetDefaultTanzuEndpoint returns default endpoint for the tanzu platform from the default // central configuration file GetDefaultTanzuEndpoint() (string, error) + // GetPluginDBCacheRefreshThresholdSeconds returns default value for central db cache refresh + // from the default central configuration file + GetPluginDBCacheRefreshThresholdSeconds() (int, error) // GetTanzuPlatformEndpointToServiceEndpointMap returns Map of tanzu platform endpoint to service endpoints // from the default central configuration file GetTanzuPlatformEndpointToServiceEndpointMap() (TanzuPlatformEndpointToServiceEndpointMap, error) diff --git a/pkg/centralconfig/central_config_defaults.go b/pkg/centralconfig/central_config_defaults.go index 56c9501ce..0caef1360 100644 --- a/pkg/centralconfig/central_config_defaults.go +++ b/pkg/centralconfig/central_config_defaults.go @@ -8,6 +8,11 @@ var ( // It serves as a fallback default only if reading the central configuration fails. DefaultTanzuPlatformEndpoint = "https://api.tanzu.cloud.vmware.com" + // DefaultPluginDBCacheRefreshThresholdSeconds is the default value for db cache refresh + // For testing, it can be overridden using the environment variable TANZU_CLI_PLUGIN_DB_CACHE_REFRESH_THRESHOLD_SECONDS. + // It serves as a fallback default only if reading the central configuration fails. + DefaultPluginDBCacheRefreshThresholdSeconds = 24 * 60 * 60 // 24 hours + defaultSaaSEndpoints = []string{ "https://(www.)?platform(.)*.tanzu.broadcom.com", "https://api.tanzu(.)*.cloud.vmware.com", @@ -24,4 +29,9 @@ func init() { if endpoint != "" { DefaultTanzuPlatformEndpoint = endpoint } + // initialize the value of `DefaultPluginDBCacheRefreshThresholdSeconds` from default central configuration if specified there + seconds, err := DefaultCentralConfigReader.GetPluginDBCacheRefreshThresholdSeconds() + if err == nil && seconds > 0 { + DefaultPluginDBCacheRefreshThresholdSeconds = seconds + } } diff --git a/pkg/centralconfig/central_config_keys.go b/pkg/centralconfig/central_config_keys.go index 46de1f2e1..3e2115725 100644 --- a/pkg/centralconfig/central_config_keys.go +++ b/pkg/centralconfig/central_config_keys.go @@ -6,6 +6,7 @@ package centralconfig const ( KeyDefaultTanzuEndpoint = "cli.core.tanzu_default_endpoint" + KeyDefaultPluginDBCacheRefreshThresholdSeconds = "cli.core.tanzu_cli_default_plugin_db_cache_refresh_threshold_seconds" KeyTanzuEndpointMap = "cli.core.tanzu_endpoint_map" KeyTanzuPlatformSaaSEndpointsAsRegularExpression = "cli.core.tanzu_cli_platform_saas_endpoints_as_regular_expression" KeyTanzuConfigEndpointUpdateVersion = "cli.core.tanzu_cli_config_endpoint_update_version" diff --git a/pkg/centralconfig/central_config_reader.go b/pkg/centralconfig/central_config_reader.go index dd05d6fb8..523ad6b49 100644 --- a/pkg/centralconfig/central_config_reader.go +++ b/pkg/centralconfig/central_config_reader.go @@ -3,6 +3,8 @@ package centralconfig +import "strconv" + // GetDefaultTanzuEndpoint returns default endpoint for the tanzu platform from the default // central configuration file func (c *centralConfigYamlReader) GetDefaultTanzuEndpoint() (string, error) { @@ -11,6 +13,22 @@ func (c *centralConfigYamlReader) GetDefaultTanzuEndpoint() (string, error) { return endpoint, err } +// GetPluginDBCacheRefreshThresholdSeconds returns default value for central db cache refresh +// from the default central configuration file +func (c *centralConfigYamlReader) GetPluginDBCacheRefreshThresholdSeconds() (int, error) { + secondsStr := "" + err := c.GetCentralConfigEntry(KeyDefaultPluginDBCacheRefreshThresholdSeconds, &secondsStr) + if err != nil { + return 0, err + } + + seconds, err := strconv.ParseInt(secondsStr, 10, 64) + if err != nil { + return 0, err + } + return int(seconds), nil +} + // GetTanzuPlatformEndpointToServiceEndpointMap returns Map of tanzu platform endpoint to service endpoints // from the default central configuration file func (c *centralConfigYamlReader) GetTanzuPlatformEndpointToServiceEndpointMap() (TanzuPlatformEndpointToServiceEndpointMap, error) { diff --git a/pkg/centralconfig/central_config_reader_test.go b/pkg/centralconfig/central_config_reader_test.go index 9f5c89291..82389067e 100644 --- a/pkg/centralconfig/central_config_reader_test.go +++ b/pkg/centralconfig/central_config_reader_test.go @@ -72,6 +72,65 @@ cli.core.tanzu_default_endpoint: "" } } +func TestGetPluginDBCacheRefreshThresholdSeconds(t *testing.T) { + tcs := []struct { + name string + cfgContent string + expectedSeconds int + expectError bool + }{ + { + name: "when the key does not exist", + cfgContent: "testKey: testValue", + expectedSeconds: 0, + expectError: true, + }, + { + name: "when the key exists", + cfgContent: ` +testKey: testValue +cli.core.tanzu_cli_default_plugin_db_cache_refresh_threshold_seconds: 200 +`, + expectedSeconds: 200, + }, + { + name: "when the key exists but value is set to 0", + cfgContent: ` +testKey: testValue +cli.core.tanzu_cli_default_plugin_db_cache_refresh_threshold_seconds: 0 +`, + expectedSeconds: 0, + }, + } + + dir, err := os.MkdirTemp("", "test-central-config") + assert.Nil(t, err) + defer os.RemoveAll(dir) + common.DefaultCacheDir = dir + reader := newCentralConfigReader("my_discovery") + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + path := reader.(*centralConfigYamlReader).configFile + // Write the central config test content to the file + err = os.MkdirAll(filepath.Dir(path), 0755) + assert.Nil(t, err) + + err = os.WriteFile(path, []byte(tc.cfgContent), 0644) + assert.Nil(t, err) + + seconds, err := reader.GetPluginDBCacheRefreshThresholdSeconds() + + if tc.expectError { + assert.NotNil(t, err) + } else { + assert.Nil(t, err) + } + assert.Equal(t, tc.expectedSeconds, seconds) + }) + } +} + func TestGetTanzuPlatformEndpointToServiceEndpointMap(t *testing.T) { tcs := []struct { name string diff --git a/pkg/centralconfig/fakes/central_config_fake.go b/pkg/centralconfig/fakes/central_config_fake.go index 4919b2ad8..2d28bb86c 100644 --- a/pkg/centralconfig/fakes/central_config_fake.go +++ b/pkg/centralconfig/fakes/central_config_fake.go @@ -32,6 +32,18 @@ type CentralConfig struct { result1 string result2 error } + GetPluginDBCacheRefreshThresholdSecondsStub func() (int, error) + getPluginDBCacheRefreshThresholdSecondsMutex sync.RWMutex + getPluginDBCacheRefreshThresholdSecondsArgsForCall []struct { + } + getPluginDBCacheRefreshThresholdSecondsReturns struct { + result1 int + result2 error + } + getPluginDBCacheRefreshThresholdSecondsReturnsOnCall map[int]struct { + result1 int + result2 error + } GetTanzuConfigEndpointUpdateMappingStub func() (map[string]string, error) getTanzuConfigEndpointUpdateMappingMutex sync.RWMutex getTanzuConfigEndpointUpdateMappingArgsForCall []struct { @@ -200,6 +212,62 @@ func (fake *CentralConfig) GetDefaultTanzuEndpointReturnsOnCall(i int, result1 s }{result1, result2} } +func (fake *CentralConfig) GetPluginDBCacheRefreshThresholdSeconds() (int, error) { + fake.getPluginDBCacheRefreshThresholdSecondsMutex.Lock() + ret, specificReturn := fake.getPluginDBCacheRefreshThresholdSecondsReturnsOnCall[len(fake.getPluginDBCacheRefreshThresholdSecondsArgsForCall)] + fake.getPluginDBCacheRefreshThresholdSecondsArgsForCall = append(fake.getPluginDBCacheRefreshThresholdSecondsArgsForCall, struct { + }{}) + stub := fake.GetPluginDBCacheRefreshThresholdSecondsStub + fakeReturns := fake.getPluginDBCacheRefreshThresholdSecondsReturns + fake.recordInvocation("GetPluginDBCacheRefreshThresholdSeconds", []interface{}{}) + fake.getPluginDBCacheRefreshThresholdSecondsMutex.Unlock() + if stub != nil { + return stub() + } + if specificReturn { + return ret.result1, ret.result2 + } + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *CentralConfig) GetPluginDBCacheRefreshThresholdSecondsCallCount() int { + fake.getPluginDBCacheRefreshThresholdSecondsMutex.RLock() + defer fake.getPluginDBCacheRefreshThresholdSecondsMutex.RUnlock() + return len(fake.getPluginDBCacheRefreshThresholdSecondsArgsForCall) +} + +func (fake *CentralConfig) GetPluginDBCacheRefreshThresholdSecondsCalls(stub func() (int, error)) { + fake.getPluginDBCacheRefreshThresholdSecondsMutex.Lock() + defer fake.getPluginDBCacheRefreshThresholdSecondsMutex.Unlock() + fake.GetPluginDBCacheRefreshThresholdSecondsStub = stub +} + +func (fake *CentralConfig) GetPluginDBCacheRefreshThresholdSecondsReturns(result1 int, result2 error) { + fake.getPluginDBCacheRefreshThresholdSecondsMutex.Lock() + defer fake.getPluginDBCacheRefreshThresholdSecondsMutex.Unlock() + fake.GetPluginDBCacheRefreshThresholdSecondsStub = nil + fake.getPluginDBCacheRefreshThresholdSecondsReturns = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *CentralConfig) GetPluginDBCacheRefreshThresholdSecondsReturnsOnCall(i int, result1 int, result2 error) { + fake.getPluginDBCacheRefreshThresholdSecondsMutex.Lock() + defer fake.getPluginDBCacheRefreshThresholdSecondsMutex.Unlock() + fake.GetPluginDBCacheRefreshThresholdSecondsStub = nil + if fake.getPluginDBCacheRefreshThresholdSecondsReturnsOnCall == nil { + fake.getPluginDBCacheRefreshThresholdSecondsReturnsOnCall = make(map[int]struct { + result1 int + result2 error + }) + } + fake.getPluginDBCacheRefreshThresholdSecondsReturnsOnCall[i] = struct { + result1 int + result2 error + }{result1, result2} +} + func (fake *CentralConfig) GetTanzuConfigEndpointUpdateMapping() (map[string]string, error) { fake.getTanzuConfigEndpointUpdateMappingMutex.Lock() ret, specificReturn := fake.getTanzuConfigEndpointUpdateMappingReturnsOnCall[len(fake.getTanzuConfigEndpointUpdateMappingArgsForCall)] @@ -428,6 +496,8 @@ func (fake *CentralConfig) Invocations() map[string][][]interface{} { defer fake.getCentralConfigEntryMutex.RUnlock() fake.getDefaultTanzuEndpointMutex.RLock() defer fake.getDefaultTanzuEndpointMutex.RUnlock() + fake.getPluginDBCacheRefreshThresholdSecondsMutex.RLock() + defer fake.getPluginDBCacheRefreshThresholdSecondsMutex.RUnlock() fake.getTanzuConfigEndpointUpdateMappingMutex.RLock() defer fake.getTanzuConfigEndpointUpdateMappingMutex.RUnlock() fake.getTanzuConfigEndpointUpdateVersionMutex.RLock() diff --git a/pkg/centralconfig/central_config_cache_init.go b/pkg/centralconfiginit/central_config_cache_init.go similarity index 99% rename from pkg/centralconfig/central_config_cache_init.go rename to pkg/centralconfiginit/central_config_cache_init.go index d7da4f6e4..8d56afabc 100644 --- a/pkg/centralconfig/central_config_cache_init.go +++ b/pkg/centralconfiginit/central_config_cache_init.go @@ -1,7 +1,7 @@ // Copyright 2024 VMware, Inc. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -package centralconfig +package centralconfiginit import ( "io" diff --git a/pkg/centralconfig/central_config_cache_init_test.go b/pkg/centralconfiginit/central_config_cache_init_test.go similarity index 99% rename from pkg/centralconfig/central_config_cache_init_test.go rename to pkg/centralconfiginit/central_config_cache_init_test.go index f41053d2f..3a12df0ec 100644 --- a/pkg/centralconfig/central_config_cache_init_test.go +++ b/pkg/centralconfiginit/central_config_cache_init_test.go @@ -1,7 +1,7 @@ // Copyright 2024 VMware, Inc. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -package centralconfig +package centralconfiginit import ( "fmt"