Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
fix(discovery): allow the use of custom localities (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclimse authored Nov 6, 2023
1 parent e3c1031 commit 8474f2c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/scwtui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (cmd *TuiCmd) Run(cmdCtx *CmdContext) error {

discoverer = scaleway.NewResourceDiscoverer(logger, client, projects, &scaleway.ResourceDiscovererConfig{
NumWorkers: 10,
MaxRetries: 3,
})
}

Expand Down
55 changes: 47 additions & 8 deletions internal/discovery/scaleway/scaleway.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"golang.org/x/sync/errgroup"
)

const (
// MaxRetries is the maximum number of retries for a request.
MaxRetries = 3
)

// ErrShouldRetry is returned when the request should be retried.
var ErrShouldRetry = errors.New("should retry")

Expand All @@ -24,8 +19,10 @@ func NewResourceDiscoverer(logger *slog.Logger, client *scw.Client, projects []r
logger: logger,
client: client,
config: config,
requested: make(chan requestResources, 1000),
projects: projects,
requested: make(chan requestResources, 100),
regions: discoveryRegions(logger, client),
zones: discoveryZones(logger, client),
}
}

Expand All @@ -37,10 +34,14 @@ type ResourceDiscover struct {

projects []resource.Resource
requested chan requestResources

regions []scw.Region
zones []scw.Zone
}

type ResourceDiscovererConfig struct {
NumWorkers int
MaxRetries int
}

func (d *ResourceDiscover) Discover(ctx context.Context, ch chan resource.Resource) error {
Expand All @@ -61,7 +62,7 @@ func (d *ResourceDiscover) Discover(ctx context.Context, ch chan resource.Resour
d.requested <- requestResources{
Get: d.discoverIAMApplications,
}
for _, region := range scw.AllRegions {
for _, region := range d.regions {
region := region // !important
d.discoverInRegion(region, d.discoverRegistryNamespacesInRegion)
d.discoverInRegion(region, d.discoverContainersInRegion)
Expand Down Expand Up @@ -96,7 +97,7 @@ func (d *ResourceDiscover) runWorker(ctx context.Context, ch chan resource.Resou
if errors.Is(err, ErrShouldRetry) {
// Retry later
req.CurrentRetry++
if req.CurrentRetry < MaxRetries {
if req.CurrentRetry < d.config.MaxRetries {
d.requested <- req
continue
}
Expand Down Expand Up @@ -137,3 +138,41 @@ func handleRequestError(err error) error {

return err
}

// discoveryRegions returns the list of regions to discover resources in.
func discoveryRegions(logger *slog.Logger, client *scw.Client) []scw.Region {
region, ok := client.GetDefaultRegion()
if !ok {
return scw.AllRegions
}

// check if region is in the list of available regions
for _, r := range scw.AllRegions {
if r == region {
return scw.AllRegions
}
}

logger.Warn("discover: configured default region is unknown", slog.String("region", string(region)))

return []scw.Region{region}
}

// discoveryZones returns the list of zones to discover resources in.
func discoveryZones(logger *slog.Logger, client *scw.Client) []scw.Zone {
zone, ok := client.GetDefaultZone()
if !ok {
return scw.AllZones
}

// check if zone is in the list of available zones
for _, z := range scw.AllZones {
if z == zone {
return scw.AllZones
}
}

logger.Warn("discover: configured default zone is unknown", slog.String("zone", string(zone)))

return []scw.Zone{zone}
}

0 comments on commit 8474f2c

Please sign in to comment.