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

Commit

Permalink
Merge pull request #385 from Mia-Cross/scw_ipam
Browse files Browse the repository at this point in the history
scaleway: get IPs from IPAM instead of instance API
  • Loading branch information
k8s-ci-robot authored Oct 11, 2023
2 parents a1e4c70 + 4aea405 commit 499904f
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 19 deletions.
1 change: 1 addition & 0 deletions etcd-manager/pkg/volumes/scaleway/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//pkg/privateapi/discovery",
"//pkg/volumes",
"//vendor/github.com/scaleway/scaleway-sdk-go/api/instance/v1:instance",
"//vendor/github.com/scaleway/scaleway-sdk-go/api/ipam/v1alpha1",
"//vendor/github.com/scaleway/scaleway-sdk-go/scw",
"//vendor/k8s.io/klog/v2:klog",
],
Expand Down
20 changes: 5 additions & 15 deletions etcd-manager/pkg/volumes/scaleway/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package scaleway
import (
"fmt"

"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/privateapi/discovery"
)
Expand All @@ -44,25 +43,16 @@ func (a *Volumes) Poll() (map[string]discovery.Node, error) {
}
serverID := volume.Server.ID

server, err := a.instanceAPI.GetServer(&instance.GetServerRequest{
ServerID: serverID,
Zone: a.zone,
})
if err != nil || server == nil {
return nil, fmt.Errorf("failed to get the running server: %w", err)
ip, err := a.getServerIP(serverID)
if err != nil {
return nil, fmt.Errorf("getting IP for server %s: %w", serverID, err)
}
klog.V(2).Infof("Found the running server: %q", server.Server.Name)

if server.Server.PrivateIP == nil || *server.Server.PrivateIP == "" {
return nil, fmt.Errorf("failed to find private IP of server %s: ", serverID)
}
serverPrivateIP := server.Server.PrivateIP

klog.V(2).Infof("Discovered volume %s(%s) of type %s attached to server %s(%s)", volume.Name, volume.ID, volume.VolumeType, server.Server.Name, serverID)
klog.V(2).Infof("Discovered volume %s(%s) of type %s attached to server %s", volume.Name, volume.ID, volume.VolumeType, serverID)
// We use the etcd node ID as the persistent identifier, because the data determines who we are
node := discovery.Node{
ID: "vol-" + volume.ID,
Endpoints: []discovery.NodeEndpoint{{IP: *serverPrivateIP}},
Endpoints: []discovery.NodeEndpoint{{IP: ip}},
}
peers[node.ID] = node
}
Expand Down
32 changes: 28 additions & 4 deletions etcd-manager/pkg/volumes/scaleway/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
ipam "github.com/scaleway/scaleway-sdk-go/api/ipam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"k8s.io/klog/v2"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/volumes"
Expand Down Expand Up @@ -208,11 +209,12 @@ func (a *Volumes) AttachVolume(volume *volumes.Volume) error {

// MyIP returns the first private IP of the running server if successful.
func (a *Volumes) MyIP() (string, error) {
if a.server.PrivateIP == nil || *a.server.PrivateIP == "" {
return "", fmt.Errorf("failed to find private IP of server %s", a.server.ID)
ip, err := a.getServerIP(a.server.ID)
if err != nil {
return "", fmt.Errorf("getting IP for server %s: %w", a.server.ID, err)
}
klog.V(2).Infof("Found first private IP of the running server: %s", *a.server.PrivateIP)
return *a.server.PrivateIP, nil
klog.V(2).Infof("Found first private IP of the running server: %s", ip)
return ip, nil
}

// getMatchingVolumes returns all the volumes matching matchTags if successful.
Expand All @@ -227,3 +229,25 @@ func getMatchingVolumes(instanceAPI *instance.API, zone scw.Zone, matchTags []st
klog.V(6).Infof("Got %d matching volumes", matchingVolumes.TotalCount)
return matchingVolumes.Volumes, nil
}

func (a *Volumes) getServerIP(serverID string) (string, error) {
region, err := a.zone.Region()
if err != nil {
return "", fmt.Errorf("unable to parse Scaleway region: %w", err)
}

ips, err := ipam.NewAPI(a.scwClient).ListIPs(&ipam.ListIPsRequest{
Region: region,
ResourceID: scw.StringPtr(serverID),
IsIPv6: scw.BoolPtr(false),
Zonal: scw.StringPtr(a.server.Zone.String()),
}, scw.WithAllPages())
if err != nil {
return "", fmt.Errorf("listing server's IPs: %w", err)
}

if ips.TotalCount < 1 {
return "", fmt.Errorf("expected at least 1 IP attached to the server")
}
return ips.IPs[0].Address.IP.String(), nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 499904f

Please sign in to comment.