Skip to content

Commit

Permalink
Merge pull request #787 from jinlinGuan/issue-4985
Browse files Browse the repository at this point in the history
feat!: Remove consul dependency
  • Loading branch information
cloudxxx8 authored Oct 30, 2024
2 parents cb863e3 + f96ab66 commit 2ade68f
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 339 deletions.
53 changes: 5 additions & 48 deletions bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,13 @@ func (cp *Processor) Process(
configProviderInfo.SetHost(remoteHosts[1])
}

getAccessToken, err := cp.getAccessTokenCallback(serviceKey, secretProvider, err, configProviderInfo)
if err != nil {
return err
}

if err := cp.loadCommonConfig(configStem, getAccessToken, configProviderInfo, serviceConfig, serviceType, CreateProviderClient); err != nil {
if err := cp.loadCommonConfig(configStem, configProviderInfo, serviceConfig, serviceType, CreateProviderClient); err != nil {
return err
}

cp.lc.Info("Common configuration loaded from the Configuration Provider. No overrides applied")

privateConfigClient, err = CreateProviderClient(cp.lc, serviceKey, configStem, getAccessToken, configProviderInfo.ServiceConfig())
privateConfigClient, err = CreateProviderClient(cp.lc, serviceKey, configStem, configProviderInfo.ServiceConfig())
if err != nil {
return fmt.Errorf("failed to create Configuration Provider client: %s", err.Error())
}
Expand Down Expand Up @@ -405,7 +400,6 @@ type createProviderCallback func(
logger.LoggingClient,
string,
string,
types.GetAccessTokenCallback,
types.ServiceConfig) (configuration.Client, error)

// loadCommonConfig will pull up to two separate common configs from the config provider
Expand All @@ -414,7 +408,6 @@ type createProviderCallback func(
// if there are separate configs, these will get merged into the serviceConfig
func (cp *Processor) loadCommonConfig(
configStem string,
getAccessToken types.GetAccessTokenCallback,
configProviderInfo *ProviderInfo,
serviceConfig interfaces.Configuration,
serviceType string,
Expand All @@ -424,7 +417,7 @@ func (cp *Processor) loadCommonConfig(
// check that common config is loaded into the provider
// this need a separate config provider client here because the config ready variable is stored at the common config level
// load the all services section of the common config
cp.commonConfigClient, err = createProvider(cp.lc, utils.BuildBaseKey(common.CoreCommonConfigServiceKey, allServicesKey), configStem, getAccessToken, configProviderInfo.ServiceConfig())
cp.commonConfigClient, err = createProvider(cp.lc, utils.BuildBaseKey(common.CoreCommonConfigServiceKey, allServicesKey), configStem, configProviderInfo.ServiceConfig())
if err != nil {
return fmt.Errorf("failed to create provider for %s: %s", allServicesKey, err.Error())
}
Expand All @@ -451,7 +444,7 @@ func (cp *Processor) loadCommonConfig(
if err != nil {
return fmt.Errorf("failed to copy the configuration structure for %s: %s", appServicesKey, err.Error())
}
cp.appConfigClient, err = createProvider(cp.lc, serviceTypeSectionKey, configStem, getAccessToken, configProviderInfo.ServiceConfig())
cp.appConfigClient, err = createProvider(cp.lc, serviceTypeSectionKey, configStem, configProviderInfo.ServiceConfig())
if err != nil {
return fmt.Errorf("failed to create provider for %s: %s", appServicesKey, err.Error())
}
Expand All @@ -471,7 +464,7 @@ func (cp *Processor) loadCommonConfig(
if err != nil {
return fmt.Errorf("failed to copy the configuration structure for %s: %s", deviceServicesKey, err.Error())
}
cp.deviceConfigClient, err = createProvider(cp.lc, serviceTypeSectionKey, configStem, getAccessToken, configProviderInfo.ServiceConfig())
cp.deviceConfigClient, err = createProvider(cp.lc, serviceTypeSectionKey, configStem, configProviderInfo.ServiceConfig())
if err != nil {
return fmt.Errorf("failed to create provider for %s: %s", deviceServicesKey, err.Error())
}
Expand Down Expand Up @@ -552,32 +545,6 @@ func (cp *Processor) loadCommonConfigFromFile(
return err
}

func (cp *Processor) getAccessTokenCallback(serviceKey string, secretProvider interfaces.SecretProviderExt, err error, configProviderInfo *ProviderInfo) (types.GetAccessTokenCallback, error) {
var accessToken string
var getAccessToken types.GetAccessTokenCallback

// secretProvider will be nil if not configured to be used. In that case, no access token required.
if secretProvider != nil {
// Define the callback function to retrieve the Access Token
getAccessToken = func() (string, error) {
accessToken, err = secretProvider.GetAccessToken(configProviderInfo.serviceConfig.Type, serviceKey)
if err != nil {
return "", fmt.Errorf(
"failed to get Configuration Provider (%s) access token: %s",
configProviderInfo.serviceConfig.Type,
err.Error())
}

cp.lc.Debugf("Using Configuration Provider access token of length %d", len(accessToken))
return accessToken, nil
}

} else {
cp.lc.Debug("Not configured to use Config Provider access token")
}
return getAccessToken, err
}

// LoadCustomConfigSection loads the specified custom configuration section from file or Configuration provider.
// Section will be seed if Configuration provider does yet have it. This is used for structures custom configuration
// in App and Device services
Expand Down Expand Up @@ -743,25 +710,15 @@ func CreateProviderClient(
lc logger.LoggingClient,
serviceKey string,
configStem string,
getAccessToken types.GetAccessTokenCallback,
providerConfig types.ServiceConfig) (configuration.Client, error) {

var err error

// The passed in configStem already contains the trailing '/' in most cases so must verify and add if missing.
if configStem[len(configStem)-1] != '/' {
configStem = configStem + "/"
}

// Note: Can't use filepath.Join as it uses `\` on Windows which Consul doesn't recognize as a path separator.
providerConfig.BasePath = fmt.Sprintf("%s%s", configStem, serviceKey)
if getAccessToken != nil {
providerConfig.AccessToken, err = getAccessToken()
if err != nil {
return nil, err
}
providerConfig.GetAccessToken = getAccessToken
}

lc.Info(fmt.Sprintf(
"Using Configuration provider (%s) from: %s with base path of %s",
Expand Down
10 changes: 3 additions & 7 deletions bootstrap/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,15 @@ func TestGetSecretNamesChanged(t *testing.T) {
}

func TestLoadCommonConfig(t *testing.T) {
getAccessToken := func() (string, error) {
return "", nil
}
// set up configs for use in tests
serviceConfig := ConfigurationMockStruct{
Writable: WritableInfo{
LogLevel: "INFO",
},
Registry: config.RegistryInfo{
Host: "localhost",
Port: 8500,
Type: "consul",
Port: 59890,
Type: "keeper",
},
}

Expand Down Expand Up @@ -208,7 +205,6 @@ func TestLoadCommonConfig(t *testing.T) {
providerClientCreator := func(logger.LoggingClient,
string,
string,
types.GetAccessTokenCallback,
types.ServiceConfig) (configuration.Client, error) {
return providerClientMock, tc.providerClientErr
}
Expand Down Expand Up @@ -241,7 +237,7 @@ func TestLoadCommonConfig(t *testing.T) {
providerClientMock.On("GetConfigurationKeys", mock.Anything).Return(configKeys, nil).Once()
}
// call load common config
err = proc.loadCommonConfig(common.ConfigStemAll, getAccessToken, &ProviderInfo{}, &serviceConfigMock, tc.serviceType, providerClientCreator)
err = proc.loadCommonConfig(common.ConfigStemAll, &ProviderInfo{}, &serviceConfigMock, tc.serviceType, providerClientCreator)
// make assertions
providerClientMock.AssertExpectations(t)
require.NotNil(t, cancel)
Expand Down
6 changes: 3 additions & 3 deletions bootstrap/config/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (

const (
envKeyConfigUrl = "EDGEX_CONFIG_PROVIDER"
goodUrlValue = "consul.http://localhost:8500"
goodUrlValue = "keeper.http://localhost:59890"
badUrlValue = "Not a url"

expectedTypeValue = "consul"
expectedTypeValue = "keeper"
expectedHostValue = "localhost"
expectedPortValue = 8500
expectedPortValue = 59890
expectedProtocolValue = "http"
)

Expand Down
6 changes: 2 additions & 4 deletions bootstrap/config/testdata/all-service-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ all-services:
# Common Security Service Metrics
SecuritySecretsRequested: false
SecuritySecretsStored: false
SecurityConsulTokensRequested: false
SecurityConsulTokenDuration: false
Tags: # Contains the service level tags to be attached to all the service's metrics
# Gateway: "my-iot-gateway" # Tag must be added here or via Consul Env Override can only change existing value, not added new ones.

Expand All @@ -40,8 +38,8 @@ all-services:

Registry:
Host: "localhost"
Port: 8500
Type: "consul"
Port: 59890
Type: "keeper"

Database:
Host: "localhost"
Expand Down
6 changes: 2 additions & 4 deletions bootstrap/config/testdata/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ all-services:
# Common Security Service Metrics
SecuritySecretsRequested: false
SecuritySecretsStored: false
SecurityConsulTokensRequested: false
SecurityConsulTokenDuration: false
# Tags: # Contains the service level tags to be attached to all the service's metrics
# Gateway: "my-iot-gateway" # Tag must be added here or via Consul Env Override can only change existing value, not added new ones.

Expand All @@ -40,8 +38,8 @@ all-services:

Registry:
Host: "localhost"
Port: 8500
Type: "consul"
Port: 59890
Type: "keeper"

Database:
Host: "localhost"
Expand Down
24 changes: 12 additions & 12 deletions bootstrap/environment/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import (
)

const (
goodUrlValue = "consul.http://localhost:8500"
goodUrlValue = "keeper.http://localhost:59890"
badUrlValue = "Not a url"

expectedTypeValue = "consul"
expectedTypeValue = "keeper"
expectedHostValue = "localhost"
expectedPortValue = 8500
expectedPortValue = 59890
expectedProtocolValue = "http"

defaultHostValue = "defaultHost"
Expand Down Expand Up @@ -364,8 +364,8 @@ func TestOverrideConfigurationExactCase(t *testing.T) {
}{
Registry: config.RegistryInfo{
Host: "localhost",
Port: 8500,
Type: "consul",
Port: 59890,
Type: "keeper",
},
List: []string{"val1"},
FloatVal: float32(11.11),
Expand All @@ -374,8 +374,8 @@ func TestOverrideConfigurationExactCase(t *testing.T) {
// only all upper case environment variable names now, so none of these overrides should have worked.
expectedOverrideCount := 0

expectedHost := "edgex-core-consul"
expectedPort := 98500
expectedHost := "edgex-core-keeper"
expectedPort := 59890
expectedFloatVal := float32(24.234)
expectedAuthType := "secure"

Expand All @@ -397,7 +397,7 @@ func TestOverrideConfigurationUppercase(t *testing.T) {
_, lc := initializeTest()

expectedOverrideCount := 4
expectedRegistryHost := "edgex-core-consul"
expectedRegistryHost := "edgex-core-keeper"
expectedCoreDataHost := "edgex-core-data"
expectedList := []string{"joe", "mary", "bob"}
expectedFloatVal := float32(24.234)
Expand All @@ -412,8 +412,8 @@ func TestOverrideConfigurationUppercase(t *testing.T) {
}{
Registry: config.RegistryInfo{
Host: "localhost",
Port: 8500,
Type: "consul",
Port: 59890,
Type: "keeper",
},
List: []string{"val1"},
FloatVal: float32(11.11),
Expand Down Expand Up @@ -458,8 +458,8 @@ func TestOverrideConfigurationWithBlankValue(t *testing.T) {
}{
Registry: config.RegistryInfo{
Host: "localhost",
Port: 8500,
Type: "consul",
Port: 59890,
Type: "keeper",
},
List: []string{"val1"},
FloatVal: float32(11.11),
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLoadFile(t *testing.T) {
ExpectedErr string
expectedSecretData map[string]string
}{
{"Valid - load from YAML file", path.Join("..", "config", "testdata", "configuration.yaml"), 4533, "", nil},
{"Valid - load from YAML file", path.Join("..", "config", "testdata", "configuration.yaml"), 4446, "", nil},
{"Valid - load from JSON file", path.Join(".", "testdata", "configuration.json"), 142, "", nil},
{"Valid - load from HTTP", "http://raw.githubusercontent.com/edgexfoundry/go-mod-bootstrap/main/bootstrap/config/testdata/configuration.yaml", 4533, "", nil},
{"Valid - load from HTTPS", "https://raw.githubusercontent.com/edgexfoundry/go-mod-bootstrap/main/bootstrap/config/testdata/configuration.yaml", 4533, "", nil},
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

const (
DefaultConfigProvider = "consul.http://localhost:8500"
DefaultConfigProvider = "keeper.http://localhost:59890"
DefaultConfigFile = "configuration.yaml"
)

Expand Down Expand Up @@ -175,7 +175,7 @@ func (d *Default) helpCallback() {
"Usage: %s [options]\n"+
"Server Options:\n"+
" -cp, --configProvider Indicates to use Configuration Provider service at specified URL.\n"+
" URL Format: {type}.{protocol}://{host}:{port} ex: consul.http://localhost:8500\n"+
" URL Format: {type}.{protocol}://{host}:{port} ex: keeper.http://localhost:59890\n"+
" -cc, --commonConfig Takes the location where the common configuration is loaded from when\n"+
" not using the Configuration Provider\n"+
" -o, --overwrite Overwrite configuration in provider with local configuration\n"+
Expand Down
8 changes: 4 additions & 4 deletions bootstrap/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestNewDefaultForCP(t *testing.T) {
}

func TestNewOverrideForCP(t *testing.T) {
expectedConfigProviderUrl := "consul.http://docker-core-consul:8500"
expectedConfigProviderUrl := "keeper.http://docker-core-keeper:59890"

actual := newSUT([]string{"-cp=" + expectedConfigProviderUrl})

Expand All @@ -85,7 +85,7 @@ func TestNewDefaultForConfigProvider(t *testing.T) {
}

func TestNewOverrideConfigProvider(t *testing.T) {
expectedConfigProviderUrl := "consul.http://docker-core-consul:8500"
expectedConfigProviderUrl := "keeper.http://docker-core-keeper:59890"

actual := newSUT([]string{"-configProvider=" + expectedConfigProviderUrl})

Expand All @@ -107,10 +107,10 @@ func TestConfigDirEquals(t *testing.T) {
}

func TestConfigCommonScenario(t *testing.T) {
expectedConfigProviderUrl := "consul.http://edgex-core-consul:8500"
expectedConfigProviderUrl := "keeper.http://docker-core-keeper:59890"
expectedConfigDirectory := "/res"

actual := newSUT([]string{"-cp=consul.http://edgex-core-consul:8500", "--registry", "--configDir=/res"})
actual := newSUT([]string{"-cp=keeper.http://docker-core-keeper:59890", "--registry", "--configDir=/res"})

assert.Equal(t, expectedConfigProviderUrl, actual.ConfigProviderUrl())
assert.True(t, actual.UseRegistry())
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/interfaces/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package interfaces
import "github.com/edgexfoundry/go-mod-bootstrap/v4/config"

// UpdatableConfig interface allows service to have their custom configuration populated from configuration stored
// in the Configuration Provider (aka Consul). A service using custom configuration must implement this interface
// in the Configuration Provider (aka keeper). A service using custom configuration must implement this interface
// on the custom configuration, even if not using Configuration Provider. If not using the Configuration Provider
// it can have dummy implementations of this interface.
type UpdatableConfig interface {
Expand Down
4 changes: 0 additions & 4 deletions bootstrap/interfaces/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ type SecretProviderExt interface {
// SecretsUpdated sets the secrets last updated time to current time.
SecretsUpdated()

// GetAccessToken return an access token for the specified token type and service key.
// Service key is use as the access token role which must have be previously setup.
GetAccessToken(tokenType string, serviceKey string) (string, error)

// SecretUpdatedAtSecretName performs updates and callbacks for an updated secret or secretName.
SecretUpdatedAtSecretName(secretName string)

Expand Down
Loading

0 comments on commit 2ade68f

Please sign in to comment.