Skip to content

Commit

Permalink
refactor: Sources' REST API client
Browse files Browse the repository at this point in the history
The REST API functions that were being used were almost a copy paste of
one another, which made following the code difficult.

The goal of the refactor was to simplify the code and make it easier to
follow and reason about. Also, it introduces a timeout for the requests
by default, which could be the cause for which the Superkey struggles so
much and ends up restarting non-stop.

Another goal of this refactor was to leave everything in an state where
it is easier to reason about how the Superkey worker should be
refactored further, like introducing proper dependency injection and
untangling the resource making from the "ForgeApplication" struct.
However, that's for another PR.

RHCLOUD-35843
  • Loading branch information
MikelAlejoBR committed Jan 15, 2025
1 parent 59597a7 commit 735e8c9
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 254 deletions.
76 changes: 46 additions & 30 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import (

// SuperKeyWorkerConfig is the struct for storing runtime configuration
type SuperKeyWorkerConfig struct {
Hostname string
KafkaBrokerConfig []clowder.BrokerConfig
KafkaTopics map[string]string
KafkaGroupID string
MetricsPort int
LogLevel string
LogGroup string
LogHandler string
AwsRegion string
AwsAccessKeyID string
AwsSecretAccessKey string
SourcesHost string
SourcesScheme string
SourcesPort int
SourcesPSK string
Hostname string
KafkaBrokerConfig []clowder.BrokerConfig
KafkaTopics map[string]string
KafkaGroupID string
MetricsPort int
LogLevel string
LogGroup string
LogHandler string
AwsRegion string
AwsAccessKeyID string
AwsSecretAccessKey string
SourcesHost string
SourcesScheme string
SourcesPort int
SourcesPSK string
SourcesRequestsMaxAttempts int
}

// Get - returns the config parsed from runtime vars
Expand Down Expand Up @@ -81,6 +82,20 @@ func Get() *SuperKeyWorkerConfig {
options.SetDefault("SourcesPort", os.Getenv("SOURCES_PORT"))
options.SetDefault("SourcesPSK", os.Getenv("SOURCES_PSK"))

// Get the number of maximum request attempts we want to make to the Sources' API.
sourcesRequestsMaxAttempts, err := strconv.Atoi(os.Getenv("SOURCES_MAX_ATTEMPTS"))
if err != nil {
log.Printf(`Warning: the provided max attempts value \"%s\" is not an integer. Setting default value of 1.`, os.Getenv("SOURCES_MAX_ATTEMPTS"))
sourcesRequestsMaxAttempts = 1
}

if sourcesRequestsMaxAttempts < 1 {
log.Printf(`Warning: the provided max attempts value \"%s\" is lower than 1, and we need to at least make one attempt when calling Sources. Setting default value of 1.`, os.Getenv("SOURCES_MAX_ATTEMPTS"))
sourcesRequestsMaxAttempts = 1
}

options.SetDefault("SourcesRequestsMaxAttempts", sourcesRequestsMaxAttempts)

hostname, _ := os.Hostname()
options.SetDefault("Hostname", hostname)

Expand All @@ -94,21 +109,22 @@ func Get() *SuperKeyWorkerConfig {
options.AutomaticEnv()

return &SuperKeyWorkerConfig{
Hostname: options.GetString("Hostname"),
KafkaBrokerConfig: brokerConfig,
KafkaTopics: options.GetStringMapString("KafkaTopics"),
KafkaGroupID: options.GetString("KafkaGroupID"),
MetricsPort: options.GetInt("MetricsPort"),
LogLevel: options.GetString("LogLevel"),
LogHandler: options.GetString("LogHandler"),
LogGroup: options.GetString("LogGroup"),
AwsRegion: options.GetString("AwsRegion"),
AwsAccessKeyID: options.GetString("AwsAccessKeyID"),
AwsSecretAccessKey: options.GetString("AwsSecretAccessKey"),
SourcesHost: options.GetString("SourcesHost"),
SourcesScheme: options.GetString("SourcesScheme"),
SourcesPort: options.GetInt("SourcesPort"),
SourcesPSK: options.GetString("SourcesPSK"),
Hostname: options.GetString("Hostname"),
KafkaBrokerConfig: brokerConfig,
KafkaTopics: options.GetStringMapString("KafkaTopics"),
KafkaGroupID: options.GetString("KafkaGroupID"),
MetricsPort: options.GetInt("MetricsPort"),
LogLevel: options.GetString("LogLevel"),
LogHandler: options.GetString("LogHandler"),
LogGroup: options.GetString("LogGroup"),
AwsRegion: options.GetString("AwsRegion"),
AwsAccessKeyID: options.GetString("AwsAccessKeyID"),
AwsSecretAccessKey: options.GetString("AwsSecretAccessKey"),
SourcesHost: options.GetString("SourcesHost"),
SourcesScheme: options.GetString("SourcesScheme"),
SourcesPort: options.GetInt("SourcesPort"),
SourcesPSK: options.GetString("SourcesPSK"),
SourcesRequestsMaxAttempts: options.GetInt("SourcesRequestsMaxAttempts"),
}
}

Expand Down
11 changes: 9 additions & 2 deletions provider/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/redhatinsights/sources-superkey-worker/amazon"
"github.com/redhatinsights/sources-superkey-worker/config"
"github.com/redhatinsights/sources-superkey-worker/sources"
"github.com/redhatinsights/sources-superkey-worker/superkey"
)
Expand Down Expand Up @@ -45,8 +46,14 @@ func TearDown(ctx context.Context, f *superkey.ForgedApplication) []error {

// getProvider returns a provider based on create request's provider + credentials
func getProvider(ctx context.Context, request *superkey.CreateRequest) (superkey.Provider, error) {
client := sources.SourcesClient{AccountNumber: request.TenantID, IdentityHeader: request.IdentityHeader, OrgId: request.OrgIdHeader}
auth, err := client.GetInternalAuthentication(ctx, request.SuperKey)
sourcesRestClient := sources.NewSourcesClient(config.Get())

authData := sources.AuthenticationData{
IdentityHeader: request.IdentityHeader,
OrgId: request.OrgIdHeader,
}

auth, err := sourcesRestClient.GetInternalAuthentication(ctx, &authData, request.SuperKey)
if err != nil {
return nil, fmt.Errorf(`error while fetching internal authentication "%s" from Sources: %w`, request.SuperKey, err)
}
Expand Down
Loading

0 comments on commit 735e8c9

Please sign in to comment.