Skip to content

Commit

Permalink
Read default database refresh threshold from Central Config
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Sep 13, 2024
1 parent 0d8192a commit 9230b33
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/centralconfig/central_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions pkg/centralconfig/central_config_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
}
1 change: 1 addition & 0 deletions pkg/centralconfig/central_config_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions pkg/centralconfig/central_config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
59 changes: 59 additions & 0 deletions pkg/centralconfig/central_config_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions pkg/centralconfig/fakes/central_config_fake.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package centralconfig
package centralconfiginit

Check failure on line 4 in pkg/centralconfiginit/central_config_cache_init.go

View workflow job for this annotation

GitHub Actions / Build

ST1000: at least one file in a package should have a package comment (stylecheck)

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package centralconfig
package centralconfiginit

import (
"fmt"
Expand Down

0 comments on commit 9230b33

Please sign in to comment.