Skip to content

Commit

Permalink
Read default database refresh threshold from Central Config (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 authored Sep 16, 2024
1 parent fd1bf73 commit e18fceb
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pkg/centralconfig/central_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ 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 in seconds
// from the default central configuration file
GetPluginDBCacheRefreshThresholdSeconds() (int, error)
// GetInventoryRefreshTTLSeconds returns default value for central db refresh TTL in seconds
// from the default central configuration file
GetInventoryRefreshTTLSeconds() (int, error)
// GetTanzuPlatformEndpointToServiceEndpointMap returns Map of tanzu platform endpoint to service endpoints
// from the default central configuration file
GetTanzuPlatformEndpointToServiceEndpointMap() (TanzuPlatformEndpointToServiceEndpointMap, error)
Expand Down
19 changes: 19 additions & 0 deletions pkg/centralconfig/central_config_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ 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

// DefaultInventoryRefreshTTLSeconds is the interval in seconds between two checks of the inventory digest.
// For testing, it can be overridden using the environment variable TANZU_CLI_PLUGIN_DB_CACHE_TTL_SECONDS.
DefaultInventoryRefreshTTLSeconds = 30 * 60 // 30 minutes

defaultSaaSEndpoints = []string{
"https://(www.)?platform(.)*.tanzu.broadcom.com",
"https://api.tanzu(.)*.cloud.vmware.com",
Expand All @@ -24,4 +33,14 @@ func init() {
if endpoint != "" {
DefaultTanzuPlatformEndpoint = endpoint
}
// initialize the value of `DefaultPluginDBCacheRefreshThresholdSeconds` from default central configuration if specified there
secondsThreshold, err := DefaultCentralConfigReader.GetPluginDBCacheRefreshThresholdSeconds()
if err == nil && secondsThreshold > 0 {
DefaultPluginDBCacheRefreshThresholdSeconds = secondsThreshold
}
// initialize the value of `DefaultInventoryRefreshTTLSeconds` from default central configuration if specified there
secondsTTL, err := DefaultCentralConfigReader.GetInventoryRefreshTTLSeconds()
if err == nil && secondsTTL > 0 {
DefaultInventoryRefreshTTLSeconds = secondsTTL
}
}
2 changes: 2 additions & 0 deletions pkg/centralconfig/central_config_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package centralconfig

const (
KeyDefaultTanzuEndpoint = "cli.core.tanzu_default_endpoint"
KeyDefaultPluginDBCacheRefreshThresholdSeconds = "cli.core.tanzu_cli_default_plugin_db_cache_refresh_threshold_seconds"
KeyDefaultInventoryRefreshTTLSeconds = "cli.core.tanzu_cli_default_inventory_refresh_ttl_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
34 changes: 34 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,38 @@ func (c *centralConfigYamlReader) GetDefaultTanzuEndpoint() (string, error) {
return endpoint, err
}

// GetPluginDBCacheRefreshThresholdSeconds returns default value for central db cache refresh in seconds
// 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
}

// GetInventoryRefreshTTLSeconds returns default value for central db refresh TTL in seconds
// from the default central configuration file
func (c *centralConfigYamlReader) GetInventoryRefreshTTLSeconds() (int, error) {
secondsStr := ""
err := c.GetCentralConfigEntry(KeyDefaultInventoryRefreshTTLSeconds, &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
118 changes: 118 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,124 @@ 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 TestGetInventoryRefreshTTLSeconds(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_inventory_refresh_ttl_seconds: 200
`,
expectedSeconds: 200,
},
{
name: "when the key exists but value is set to 0",
cfgContent: `
testKey: testValue
cli.core.tanzu_cli_default_inventory_refresh_ttl_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.GetInventoryRefreshTTLSeconds()

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
140 changes: 140 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,8 @@
// Copyright 2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package centralconfig
// Package centralconfiginit is used to add inventory updater to the globalinitializer
package centralconfiginit

import (
"io"
Expand Down
Loading

0 comments on commit e18fceb

Please sign in to comment.