Skip to content

Commit

Permalink
protokube: remove unused internal IP discovery, add metal support
Browse files Browse the repository at this point in the history
Metal support is basically a stub, but should be sufficient as we are
now only using the cloud provider in gossip mode (and metal does not
use gossip).
  • Loading branch information
justinsb committed Sep 14, 2024
1 parent 6509c42 commit 518b7b8
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 164 deletions.
13 changes: 6 additions & 7 deletions protokube/cmd/protokube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,13 @@ func run() error {
}
cloudProvider = scwCloudProvider

} else if cloud == "metal" {
cloudProvider = nil
} else {
klog.Errorf("Unknown cloud %q", cloud)
os.Exit(1)
}

internalIP := cloudProvider.InstanceInternalIP()
if internalIP == nil {
klog.Errorf("Cannot determine internal IP")
os.Exit(1)
}

if dnsInternalSuffix == "" {
if clusterID == "" {
return fmt.Errorf("cluster-id is required when dns-internal-suffix is not set")
Expand All @@ -182,6 +178,10 @@ func run() error {
protokube.RootFS = rootfs

if gossip {
if cloudProvider == nil {
return fmt.Errorf("gossip not supported with cloudprovider %q", cloud)
}

dnsTarget := &gossipdns.HostsFile{
Path: path.Join(rootfs, "etc/hosts"),
}
Expand Down Expand Up @@ -244,7 +244,6 @@ func run() error {
NodeName: nodeName,
Channels: channels,
InternalDNSSuffix: dnsInternalSuffix,
InternalIP: internalIP,
Kubernetes: protokube.NewKubernetesContext(),
Master: master,
}
Expand Down
17 changes: 0 additions & 17 deletions protokube/pkg/gossip/azure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"

Expand Down Expand Up @@ -130,22 +129,6 @@ func (c *Client) GetTags() (map[string]string, error) {
return c.metadata.Compute.GetTags()
}

// GetInternalIP returns the internal IP of the VM queried from Instance Metadata Service.
// This function returns nil if no internal IP is found.
func (c *Client) GetInternalIP() net.IP {
for _, iface := range c.metadata.Network.Interfaces {
if iface.IPv4 == nil {
continue
}
for _, ipAddr := range iface.IPv4.IPAddresses {
if a := ipAddr.PrivateIPAddress; a != "" {
return net.ParseIP(a)
}
}
}
return nil
}

// ListVMScaleSets returns VM ScaleSets in the resource group.
func (c *Client) ListVMScaleSets(ctx context.Context) ([]*compute.VirtualMachineScaleSet, error) {
var l []*compute.VirtualMachineScaleSet
Expand Down
19 changes: 0 additions & 19 deletions protokube/pkg/gossip/azure/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package azure

import (
"net"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -66,21 +65,3 @@ func TestUnmarshalMetadata(t *testing.T) {
t.Errorf("expected public IP address %s, but got %s", e, a)
}
}

func TestGetInternalIP(t *testing.T) {
data, err := os.ReadFile("testdata/metadata.json")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
metadata, err := unmarshalInstanceMetadata(data)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
c := Client{
metadata: metadata,
}

if a, e := c.GetInternalIP(), net.ParseIP("172.16.32.8"); !a.Equal(e) {
t.Errorf("expected internal address %s, but got %s", e, a)
}
}
14 changes: 0 additions & 14 deletions protokube/pkg/protokube/aws_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"net"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -42,7 +41,6 @@ type AWSCloudProvider struct {
deviceMap map[string]string
ec2 ec2.DescribeInstancesAPIClient
instanceId string
internalIP net.IP
imdsClient *imds.Client
zone string
}
Expand Down Expand Up @@ -98,10 +96,6 @@ func NewAWSCloudProvider() (*AWSCloudProvider, error) {
return a, nil
}

func (a *AWSCloudProvider) InstanceInternalIP() net.IP {
return a.internalIP
}

func (a *AWSCloudProvider) discoverTags(ctx context.Context) error {
instance, err := a.describeInstance(ctx)
if err != nil {
Expand All @@ -120,14 +114,6 @@ func (a *AWSCloudProvider) discoverTags(ctx context.Context) error {

a.clusterTag = clusterID

a.internalIP = net.ParseIP(aws.ToString(instance.Ipv6Address))
if a.internalIP == nil {
a.internalIP = net.ParseIP(aws.ToString(instance.PrivateIpAddress))
}
if a.internalIP == nil {
return fmt.Errorf("Internal IP not found on this instance (%q)", a.instanceId)
}

return nil
}

Expand Down
13 changes: 0 additions & 13 deletions protokube/pkg/protokube/azure_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package protokube
import (
"context"
"fmt"
"net"

compute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
network "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
Expand All @@ -33,7 +32,6 @@ type client interface {
ListVMSSNetworkInterfaces(ctx context.Context, vmScaleSetName string) ([]*network.Interface, error)
GetName() string
GetTags() (map[string]string, error)
GetInternalIP() net.IP
}

var _ client = &gossipazure.Client{}
Expand All @@ -44,7 +42,6 @@ type AzureCloudProvider struct {

clusterTag string
instanceID string
internalIP net.IP
}

var _ CloudProvider = &AzureCloudProvider{}
Expand All @@ -68,15 +65,10 @@ func NewAzureCloudProvider() (*AzureCloudProvider, error) {
if instanceID == "" {
return nil, fmt.Errorf("empty name")
}
internalIP := client.GetInternalIP()
if internalIP == nil {
return nil, fmt.Errorf("error querying internal IP")
}
return &AzureCloudProvider{
client: client,
clusterTag: clusterTag,
instanceID: instanceID,
internalIP: internalIP,
}, nil
}

Expand All @@ -85,11 +77,6 @@ func (a *AzureCloudProvider) InstanceID() string {
return a.instanceID
}

// InstanceInternalIP implements CloudProvider InstanceInternalIP.
func (a *AzureCloudProvider) InstanceInternalIP() net.IP {
return a.internalIP
}

// GossipSeeds implements CloudProvider GossipSeeds.
func (a *AzureCloudProvider) GossipSeeds() (gossip.SeedProvider, error) {
tags := map[string]string{
Expand Down
13 changes: 0 additions & 13 deletions protokube/pkg/protokube/do_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -55,7 +54,6 @@ type DOCloudProvider struct {
region string
dropletName string
dropletID int
dropletIP net.IP
dropletTags []string
}

Expand Down Expand Up @@ -102,12 +100,6 @@ func NewDOCloudProvider() (*DOCloudProvider, error) {
return nil, fmt.Errorf("failed to convert droplet ID to int: %s", err)
}

dropletIPStr, err := getMetadata(dropletInternalIPMetadataURL)
if err != nil {
return nil, fmt.Errorf("failed to get droplet ip: %s", err)
}
dropletIP := net.ParseIP(dropletIPStr)

dropletName, err := getMetadataDropletName()
if err != nil {
return nil, fmt.Errorf("failed to get droplet name: %s", err)
Expand All @@ -132,7 +124,6 @@ func NewDOCloudProvider() (*DOCloudProvider, error) {
godoClient: godoClient,
ClusterID: clusterID,
dropletID: dropletID,
dropletIP: dropletIP,
dropletName: dropletName,
region: region,
dropletTags: dropletTags,
Expand Down Expand Up @@ -202,10 +193,6 @@ func (d *DOCloudProvider) InstanceID() string {
return d.dropletName
}

func (d *DOCloudProvider) InstanceInternalIP() net.IP {
return d.dropletIP
}

func getMetadataRegion() (string, error) {
return getMetadata(dropletRegionMetadataURL)
}
Expand Down
23 changes: 0 additions & 23 deletions protokube/pkg/protokube/gce_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package protokube

import (
"fmt"
"net"
"strings"

"cloud.google.com/go/compute/metadata"
Expand All @@ -38,7 +37,6 @@ type GCECloudProvider struct {
region string
clusterName string
instanceName string
internalIP net.IP
}

var _ CloudProvider = &GCECloudProvider{}
Expand Down Expand Up @@ -68,11 +66,6 @@ func (a *GCECloudProvider) Project() string {
return a.project
}

// InstanceInternalIP implements CloudProvider InstanceInternalIP
func (a *GCECloudProvider) InstanceInternalIP() net.IP {
return a.internalIP
}

func (a *GCECloudProvider) discoverTags() error {
// Cluster Name
{
Expand Down Expand Up @@ -116,22 +109,6 @@ func (a *GCECloudProvider) discoverTags() error {
klog.Infof("Found instanceName=%q", a.instanceName)
}

// Internal IP
{
internalIP, err := metadata.InternalIP()
if err != nil {
return fmt.Errorf("error querying InternalIP from GCE: %v", err)
}
if internalIP == "" {
return fmt.Errorf("InternalIP from metadata was empty")
}
a.internalIP = net.ParseIP(internalIP)
if a.internalIP == nil {
return fmt.Errorf("InternalIP from metadata was not parseable(%q)", internalIP)
}
klog.Infof("Found internalIP=%q", a.internalIP)
}

return nil
}

Expand Down
7 changes: 0 additions & 7 deletions protokube/pkg/protokube/hetzner_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package protokube
import (
"context"
"fmt"
"net"
"os"

"github.com/hetznercloud/hcloud-go/hcloud"
Expand All @@ -34,7 +33,6 @@ import (
type HetznerCloudProvider struct {
hcloudClient *hcloud.Client
server *hcloud.Server
serverIP net.IP
}

var _ CloudProvider = &HetznerCloudProvider{}
Expand Down Expand Up @@ -77,16 +75,11 @@ func NewHetznerCloudProvider() (*HetznerCloudProvider, error) {
h := &HetznerCloudProvider{
hcloudClient: hcloudClient,
server: server,
serverIP: server.PrivateNet[0].IP,
}

return h, nil
}

func (h HetznerCloudProvider) InstanceInternalIP() net.IP {
return h.serverIP
}

func (h *HetznerCloudProvider) GossipSeeds() (gossip.SeedProvider, error) {
clusterName, ok := h.server.Labels[hetzner.TagKubernetesClusterName]
if !ok {
Expand Down
3 changes: 0 additions & 3 deletions protokube/pkg/protokube/kube_boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package protokube
import (
"context"
"fmt"
"net"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,8 +34,6 @@ type KubeBoot struct {
Channels []string
// InternalDNSSuffix is the dns zone we are living in
InternalDNSSuffix string
// InternalIP is the internal ip address of the node
InternalIP net.IP
// Kubernetes holds a kubernetes client
Kubernetes *KubernetesContext
// Master indicates we are a master node
Expand Down
26 changes: 0 additions & 26 deletions protokube/pkg/protokube/openstack_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package protokube

import (
"fmt"
"net"
"strings"

"k8s.io/klog/v2"
Expand All @@ -36,7 +35,6 @@ type OpenStackCloudProvider struct {
clusterName string
project string
instanceName string
internalIP net.IP
storageZone string
}

Expand Down Expand Up @@ -72,11 +70,6 @@ func (a *OpenStackCloudProvider) Project() string {
return a.meta.ProjectID
}

// InstanceInternalIP implements CloudProvider InstanceInternalIP
func (a *OpenStackCloudProvider) InstanceInternalIP() net.IP {
return a.internalIP
}

func (a *OpenStackCloudProvider) discoverTags() error {
// Cluster Name
{
Expand Down Expand Up @@ -113,25 +106,6 @@ func (a *OpenStackCloudProvider) discoverTags() error {
klog.Infof("Found instanceName=%q", a.instanceName)
}

// Internal IP
{
server, err := a.cloud.GetInstance(strings.TrimSpace(a.meta.ServerID))
if err != nil {
return fmt.Errorf("error getting instance from ID: %v", err)
}
// find kopsNetwork from metadata, fallback to clustername
ifName := a.clusterName
if val, ok := server.Metadata[openstack.TagKopsNetwork]; ok {
ifName = val
}
ip, err := openstack.GetServerFixedIP(server, ifName)
if err != nil {
return fmt.Errorf("error querying InternalIP from name: %v", err)
}
a.internalIP = net.ParseIP(ip)
klog.Infof("Found internalIP=%q", a.internalIP)
}

return nil
}

Expand Down
Loading

0 comments on commit 518b7b8

Please sign in to comment.