Repository navigation
feat(scaleway): add flexible ips, load balancers, serverless and kapsule #783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package scaleway | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/url" | ||
|
|
||
| "github.com/projectdiscovery/cloudlist/pkg/schema" | ||
| container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1" | ||
| function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1" | ||
| k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" | ||
| "github.com/scaleway/scaleway-sdk-go/scw" | ||
| ) | ||
|
|
||
| // containerProvider lists endpoints of Serverless Containers, Serverless | ||
| // Functions and Kapsule clusters. | ||
| type containerProvider struct { | ||
| id string | ||
| containerAPI *container.API | ||
| functionAPI *function.API | ||
| k8sAPI *k8s.API | ||
| } | ||
|
|
||
| // GetContainers returns Serverless Containers domain names. | ||
| // Listing goes through namespaces because this SDK version always sends | ||
| // namespace_id on ListContainers. | ||
| func (d *containerProvider) GetContainers(ctx context.Context) (*schema.Resources, error) { | ||
| list := schema.NewResources() | ||
| var listingErr error | ||
|
|
||
| err := forEachLocality(d.containerAPI.Regions(), func(region scw.Region) error { | ||
| namespaces, err := d.containerAPI.ListNamespaces(&container.ListNamespacesRequest{Region: region}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, namespace := range namespaces.Namespaces { | ||
| resp, err := d.containerAPI.ListContainers(&container.ListContainersRequest{Region: region, NamespaceID: namespace.ID}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| listingErr = err | ||
| continue | ||
| } | ||
| for _, c := range resp.Containers { | ||
| list.Append(d.dnsResource(c.DomainName, "container")) | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| if listingErr != nil { | ||
| err = listingErr | ||
| } | ||
| if err != nil && len(list.Items) == 0 { | ||
| return nil, err | ||
| } | ||
| return list, err | ||
| } | ||
|
|
||
| // GetFunctions returns Serverless Functions domain names. | ||
| func (d *containerProvider) GetFunctions(ctx context.Context) (*schema.Resources, error) { | ||
| list := schema.NewResources() | ||
| var listingErr error | ||
|
|
||
| err := forEachLocality(d.functionAPI.Regions(), func(region scw.Region) error { | ||
| namespaces, err := d.functionAPI.ListNamespaces(&function.ListNamespacesRequest{Region: region}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, namespace := range namespaces.Namespaces { | ||
| resp, err := d.functionAPI.ListFunctions(&function.ListFunctionsRequest{Region: region, NamespaceID: namespace.ID}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| listingErr = err | ||
| continue | ||
| } | ||
| for _, f := range resp.Functions { | ||
| list.Append(d.dnsResource(f.DomainName, "function")) | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| if listingErr != nil { | ||
| err = listingErr | ||
| } | ||
| if err != nil && len(list.Items) == 0 { | ||
| return nil, err | ||
| } | ||
| return list, err | ||
| } | ||
|
|
||
| // GetKapsuleClusters returns Kapsule cluster API server hostnames. | ||
| func (d *containerProvider) GetKapsuleClusters(ctx context.Context) (*schema.Resources, error) { | ||
| list := schema.NewResources() | ||
|
|
||
| err := forEachLocality(d.k8sAPI.Regions(), func(region scw.Region) error { | ||
| resp, err := d.k8sAPI.ListClusters(&k8s.ListClustersRequest{Region: region}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, cluster := range resp.Clusters { | ||
| // ClusterURL is a full URL such as https://<id>.api.k8s.fr-par.scw.cloud:6443 | ||
| if u, err := url.Parse(cluster.ClusterURL); err == nil { | ||
| list.Append(d.dnsResource(u.Hostname(), "kapsule")) | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil && len(list.Items) == 0 { | ||
| return nil, err | ||
| } | ||
| return list, err | ||
| } | ||
|
|
||
| func (d *containerProvider) dnsResource(hostname, service string) *schema.Resource { | ||
| return &schema.Resource{ | ||
| Provider: providerName, | ||
| ID: d.id, | ||
| DNSName: hostname, | ||
| Public: true, | ||
| Service: service, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package scaleway | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
|
|
||
| "github.com/projectdiscovery/cloudlist/pkg/schema" | ||
| "github.com/projectdiscovery/gologger" | ||
| "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1" | ||
| "github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
| "github.com/scaleway/scaleway-sdk-go/api/lb/v1" | ||
| "github.com/scaleway/scaleway-sdk-go/scw" | ||
| ) | ||
|
|
||
| // ipProvider lists reserved IPs, which are returned whether or not they are | ||
| // attached, so detached IPs that ListServers never sees are covered too. | ||
| type ipProvider struct { | ||
| id string | ||
| instanceAPI *instance.API | ||
| flexibleIPAPI *flexibleip.API | ||
| lbAPI *lb.ZonedAPI | ||
| } | ||
|
|
||
| // GetFlexibleIPs returns Instance flexible IPs and Elastic Metal flexible IPs. | ||
| func (d *ipProvider) GetFlexibleIPs(ctx context.Context) (*schema.Resources, error) { | ||
| list := schema.NewResources() | ||
|
|
||
| instanceErr := forEachLocality(d.instanceAPI.Zones(), func(zone scw.Zone) error { | ||
| resp, err := d.instanceAPI.ListIPs(&instance.ListIPsRequest{Zone: zone}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, ip := range resp.IPs { | ||
| list.Append(d.ipResource(ip.Address, "flexibleip")) | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| metalErr := forEachLocality(d.flexibleIPAPI.Zones(), func(zone scw.Zone) error { | ||
| resp, err := d.flexibleIPAPI.ListFlexibleIPs(&flexibleip.ListFlexibleIPsRequest{Zone: zone}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, ip := range resp.FlexibleIPs { | ||
| list.Append(d.ipResource(ip.IPAddress.IP, "flexibleip")) | ||
| } | ||
| return nil | ||
| }) | ||
| if len(list.Items) == 0 && instanceErr != nil && metalErr != nil { | ||
| return nil, fmt.Errorf("scaleway: flexible ip listing failed: %v; %v", instanceErr, metalErr) | ||
| } | ||
| for _, err := range []error{instanceErr, metalErr} { | ||
| if err != nil { | ||
| gologger.Warning().Msgf("scaleway: flexible ip listing failed: %v", err) | ||
| } | ||
| } | ||
| return list, nil | ||
| } | ||
|
|
||
| // GetLoadBalancerIPs returns load balancer IPs. | ||
| func (d *ipProvider) GetLoadBalancerIPs(ctx context.Context) (*schema.Resources, error) { | ||
| list := schema.NewResources() | ||
|
|
||
| err := forEachLocality(d.lbAPI.Zones(), func(zone scw.Zone) error { | ||
| resp, err := d.lbAPI.ListIPs(&lb.ZonedAPIListIPsRequest{Zone: zone}, scw.WithAllPages(), scw.WithContext(ctx)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, ip := range resp.IPs { | ||
| list.Append(d.ipResource(net.ParseIP(ip.IPAddress), "lb")) | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return list, nil | ||
| } | ||
|
|
||
| func (d *ipProvider) ipResource(ip net.IP, service string) *schema.Resource { | ||
| resource := &schema.Resource{ | ||
| Provider: providerName, | ||
| ID: d.id, | ||
| Public: true, | ||
| Service: service, | ||
| } | ||
| if ip.To4() != nil { | ||
| resource.PublicIPv4 = ip.String() | ||
| } else if ip != nil { | ||
| resource.PublicIPv6 = ip.String() | ||
| } | ||
| return resource | ||
| } | ||
|
|
||
| // forEachLocality calls fn for every zone or region, skipping ones that fail | ||
| // (e.g. a product not offered there), and only errors when all of them failed. | ||
| func forEachLocality[T scw.Zone | scw.Region](localities []T, fn func(T) error) error { | ||
| var errs []error | ||
| for _, locality := range localities { | ||
| if err := fn(locality); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
| if len(errs) > 0 && len(errs) == len(localities) { | ||
| return fmt.Errorf("all localities failed: %v", errs) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Enumerate custom domain bindings. Both getters collect workload
DomainNamevalues but omit separately bound domains. The SDK providesListDomainsand binding hostnames for both services. (raw.githubusercontent.com)pkg/providers/scaleway/containers.go#L39-L40: list each Container's domain bindings and append their hostnames.pkg/providers/scaleway/containers.go#L65-L66: list each Function's domain bindings and append their hostnames.📍 Affects 1 file
pkg/providers/scaleway/containers.go#L39-L40(this comment)pkg/providers/scaleway/containers.go#L65-L66🤖 Prompt for AI Agents