Skip to content

Commit

Permalink
refactor: switch refresherConfig from map to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
duffney committed Sep 6, 2024
1 parent faeea08 commit da11a4a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func (r *KeyManagementProviderReconciler) ReconcileWithType(ctx context.Context,
return ctrl.Result{}, err
}

config := map[string]interface{}{
"refresherType": refresherType,
"provider": provider,
"providerType": keyManagementProvider.Spec.Type,
"providerRefreshInterval": keyManagementProvider.Spec.RefreshInterval,
"resource": resource,
refresherConfig := refresh.RefresherConfig{
RefresherType: refresherType,
Provider: provider,
ProviderType: keyManagementProvider.Spec.Type,
ProviderRefreshInterval: keyManagementProvider.Spec.RefreshInterval,
Resource: resource,
}

refresher, err := refresh.CreateRefresherFromConfig(config)
refresher, err := refresh.CreateRefresherFromConfig(refresherConfig)
if err != nil {
writeKMProviderStatus(ctx, r, &keyManagementProvider, logger, false, err.Error(), lastFetchedTime, nil)
return ctrl.Result{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ func (mr *MockRefresher) GetStatus() interface{} {
return mr.Status
}

func (mr *MockRefresher) Create(config map[string]interface{}) (refresh.Refresher, error) {
if resource, ok := config["resource"].(string); ok && resource == "refreshError" {
func (mr *MockRefresher) Create(config refresh.RefresherConfig) (refresh.Refresher, error) {
if config.Resource == "refreshError" {
return &MockRefresher{
RefreshError: true,
}, nil
}
if resource, ok := config["resource"].(string); ok && resource == "resultError" {
if config.Resource == "resultError" {
return &MockRefresher{
ResultError: true,
}, nil
}
if resource, ok := config["resource"].(string); ok && resource == "statusError" {
if config.Resource == "statusError" {
return &MockRefresher{
StatusError: true,
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func (r *KeyManagementProviderReconciler) ReconcileWithType(ctx context.Context,
return ctrl.Result{}, err
}

config := map[string]interface{}{
"refresherType": refresherType,
"provider": provider,
"providerType": keyManagementProvider.Spec.Type,
"providerRefreshInterval": keyManagementProvider.Spec.RefreshInterval,
"resource": resource,
refresherConfig := refresh.RefresherConfig{
RefresherType: refresherType,
Provider: provider,
ProviderType: keyManagementProvider.Spec.Type,
ProviderRefreshInterval: keyManagementProvider.Spec.RefreshInterval,
Resource: resource,
}

refresher, err := refresh.CreateRefresherFromConfig(config)
refresher, err := refresh.CreateRefresherFromConfig(refresherConfig)
if err != nil {
writeKMProviderStatus(ctx, r, &keyManagementProvider, logger, isFetchSuccessful, err.Error(), lastFetchedTime, nil)
return ctrl.Result{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,24 @@ func (mr *MockRefresher) GetStatus() interface{} {
return mr.Status
}

func (mr *MockRefresher) Create(config map[string]interface{}) (refresh.Refresher, error) {
if resource, ok := config["resource"].(string); ok && resource == "refreshError/test" {
func (mr *MockRefresher) Create(config refresh.RefresherConfig) (refresh.Refresher, error) {
if config.Resource == "refreshError/test" {
return &MockRefresher{
RefreshError: true,
}, nil
}
if resource, ok := config["resource"].(string); ok && resource == "resultError/test" {
if config.Resource == "resultError/test" {
return &MockRefresher{
ResultError: true,
}, nil
}
if resource, ok := config["resource"].(string); ok && resource == "statusError/test" {
if config.Resource == "statusError/test" {
return &MockRefresher{
StatusError: true,
}, nil
}
return &MockRefresher{}, nil
}

func TestKeyManagementProviderReconciler_ReconcileWithType(t *testing.T) {
tests := []struct {
name string
Expand Down
20 changes: 5 additions & 15 deletions pkg/keymanagementprovider/refresh/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var refresherFactories = make(map[string]RefresherFactory)

type RefresherFactory interface {
// Create creates a new instance of the refresher using the provided configuration
Create(config map[string]interface{}) (Refresher, error)
// Create(config map[string]interface{}) (Refresher, error)
Create(config RefresherConfig) (Refresher, error)
}

// Refresher is an interface that defines methods to be implemented by a each refresher
Expand All @@ -37,21 +38,10 @@ func Register(name string, factory RefresherFactory) {
}

// CreateRefresherFromConfig creates a new instance of the refresher using the provided configuration
func CreateRefresherFromConfig(refresherConfig map[string]interface{}) (Refresher, error) {
refresherTypeValue, exists := refresherConfig["refresherType"]
if !exists {
return nil, fmt.Errorf("refresherType not found in config")
}
refresherType, ok := refresherTypeValue.(string)
if !ok {
return nil, fmt.Errorf("refresherType is not a string")
}
if !ok || refresherType == "" {
return nil, fmt.Errorf("refresherType cannot be empty")
}
factory, ok := refresherFactories[refresherType]
func CreateRefresherFromConfig(refresherConfig RefresherConfig) (Refresher, error) {
factory, ok := refresherFactories[refresherConfig.RefresherType]
if !ok {
return nil, fmt.Errorf("refresher factory with name %s not found", refresherType)
return nil, fmt.Errorf("refresher factory with name %s not found", refresherConfig.RefresherType)
}
return factory.Create(refresherConfig)
}
10 changes: 5 additions & 5 deletions pkg/keymanagementprovider/refresh/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type MockRefresher struct{}

func (f *MockRefresher) Create(_ map[string]interface{}) (Refresher, error) {
func (f *MockRefresher) Create(_ RefresherConfig) (Refresher, error) {
return &MockRefresher{}, nil
}

Expand All @@ -40,8 +40,8 @@ func (f *MockRefresher) GetStatus() interface{} {

func TestRefreshFactory_Create(t *testing.T) {
Register("mockRefresher", &MockRefresher{})
refresherConfig := map[string]interface{}{
"refresherType": "mockRefresher",
refresherConfig := RefresherConfig{
RefresherType: "mockRefresher",
}
factory := refresherFactories["mockRefresher"]
refresher, err := factory.Create(refresherConfig)
Expand Down Expand Up @@ -108,8 +108,8 @@ func TestCreateRefresherFromConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
refresherConfig := map[string]interface{}{
"refresherType": tt.refresherType,
refresherConfig := RefresherConfig{
RefresherType: tt.refresherType,
}
_, err := CreateRefresherFromConfig(refresherConfig)
if tt.expectedError && err == nil {
Expand Down
49 changes: 5 additions & 44 deletions pkg/keymanagementprovider/refresh/kubeRefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,50 +99,11 @@ func (kr *KubeRefresher) GetStatus() interface{} {
}

// Create creates a new KubeRefresher instance
func (kr *KubeRefresher) Create(config map[string]interface{}) (Refresher, error) {
providerValue, exists := config["provider"]
if !exists {
return nil, fmt.Errorf("provider is required in config")
}
provider, ok := providerValue.(kmp.KeyManagementProvider)
if !ok {
return nil, fmt.Errorf("provider is not of type KeyManagementProvider")
}

provierTypeValue, exists := config["providerType"]
if !exists {
return nil, fmt.Errorf("providerType is required in config")
}

providerType, ok := provierTypeValue.(string)
if !ok {
return nil, fmt.Errorf("providerType is not of type string")
}

providerRefreshIntervaleValue, exists := config["providerRefreshInterval"]
if !exists {
return nil, fmt.Errorf("providerRefreshInterval is required in config")
}

providerRefreshInterval, ok := providerRefreshIntervaleValue.(string)
if !ok {
return nil, fmt.Errorf("providerRefreshInterval is not of type string")
}

resourceValue, exists := config["resource"]
if !exists {
return nil, fmt.Errorf("resource is required in config")
}

resource, ok := resourceValue.(string)
if !ok {
return nil, fmt.Errorf("resource is not of type string")
}

func (kr *KubeRefresher) Create(config RefresherConfig) (Refresher, error) {
return &KubeRefresher{
Provider: provider,
ProviderType: providerType,
ProviderRefreshInterval: providerRefreshInterval,
Resource: resource,
Provider: config.Provider,
ProviderType: config.ProviderType,
ProviderRefreshInterval: config.ProviderRefreshInterval,
Resource: config.Resource,
}, nil
}
Loading

0 comments on commit da11a4a

Please sign in to comment.