Skip to content
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

Auto-detect the default BYOH interface for hosts #477

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions agent/host_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ package main
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"runtime"
"strings"

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/jackpal/gateway"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
Expand Down Expand Up @@ -199,48 +197,6 @@ var _ = Describe("Agent", func() {
}).Should(Equal(map[string]string{"site": "apac"}))
})

It("should fetch networkstatus when register the BYOHost with the management cluster", func() {
byoHostLookupKey := types.NamespacedName{Name: hostName, Namespace: ns.Name}
defaultIP, err := gateway.DiscoverInterface()
Expect(err).NotTo(HaveOccurred())
Eventually(func() bool {
createdByoHost := &infrastructurev1beta1.ByoHost{}
err := k8sClient.Get(context.TODO(), byoHostLookupKey, createdByoHost)
if err != nil {
return false
}
// check if default ip and networkInterfaceName is right
for _, item := range createdByoHost.Status.Network {
if item.IsDefault {
iface, err := net.InterfaceByName(item.NetworkInterfaceName)
if err != nil {
return false
}

addrs, err := iface.Addrs()
if err != nil {
return false
}

for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.String() == defaultIP.String() {
return true
}
}
}
}
return false
}).Should(BeTrue())

})

It("should only reconcile ByoHost resource that the agent created", func() {
byoHost := builder.ByoHost(ns.Name, "random-second-host").Build()
Expect(k8sClient.Create(context.TODO(), byoHost)).NotTo(HaveOccurred(), "failed to create byohost")
Expand Down
26 changes: 5 additions & 21 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,6 @@ func handleHostRegistration(k8sClient client.Client, hostName string, logger log
return nil
}

func setupTemplateParser() *cloudinit.TemplateParser {
var templateParser *cloudinit.TemplateParser
if registration.LocalHostRegistrar.ByoHostInfo.DefaultNetworkInterfaceName == "" {
templateParser = nil
} else {
templateParser = &cloudinit.TemplateParser{
Template: registration.HostInfo{
DefaultNetworkInterfaceName: registration.LocalHostRegistrar.ByoHostInfo.DefaultNetworkInterfaceName,
},
}
}

return templateParser
}

var (
namespace string
scheme *runtime.Scheme
Expand Down Expand Up @@ -208,12 +193,11 @@ func main() {
}

hostReconciler := &reconciler.HostReconciler{
Client: k8sClient,
CmdRunner: cloudinit.CmdRunner{},
FileWriter: cloudinit.FileWriter{},
TemplateParser: setupTemplateParser(),
Recorder: mgr.GetEventRecorderFor("hostagent-controller"),
K8sInstaller: k8sInstaller,
Client: k8sClient,
CmdRunner: cloudinit.CmdRunner{},
FileWriter: cloudinit.FileWriter{},
Recorder: mgr.GetEventRecorderFor("hostagent-controller"),
K8sInstaller: k8sInstaller,
}

if err = hostReconciler.SetupWithManager(context.TODO(), mgr); err != nil {
Expand Down
58 changes: 0 additions & 58 deletions agent/registration/host_registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ package registration

import (
"context"
"net"

"github.com/jackpal/gateway"
infrastructurev1beta1 "github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/apis/infrastructure/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -66,66 +64,10 @@ func (hr *HostRegistrar) Register(hostName, namespace string, hostLabels map[str
}
}

// run it at startup or reboot
return hr.UpdateNetwork(ctx, byoHost)
}

// UpdateNetwork updates the network interface status for the host
func (hr *HostRegistrar) UpdateNetwork(ctx context.Context, byoHost *infrastructurev1beta1.ByoHost) error {
klog.Info("Add Network Info")
helper, err := patch.NewHelper(byoHost, hr.K8sClient)
if err != nil {
return err
}

byoHost.Status.Network = hr.GetNetworkStatus()

return helper.Patch(ctx, byoHost)
}

// GetNetworkStatus returns the network interface(s) status for the host
func (hr *HostRegistrar) GetNetworkStatus() []infrastructurev1beta1.NetworkStatus {
Network := make([]infrastructurev1beta1.NetworkStatus, 0)

defaultIP, err := gateway.DiscoverInterface()
if err != nil {
return Network
}

ifaces, err := net.Interfaces()
if err != nil {
return Network
}

for _, iface := range ifaces {
netStatus := infrastructurev1beta1.NetworkStatus{}

if iface.Flags&net.FlagUp > 0 {
netStatus.Connected = true
}

netStatus.MACAddr = iface.HardwareAddr.String()
addrs, err := iface.Addrs()
if err != nil {
continue
}

netStatus.NetworkInterfaceName = iface.Name
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.String() == defaultIP.String() {
netStatus.IsDefault = true
hr.ByoHostInfo.DefaultNetworkInterfaceName = netStatus.NetworkInterfaceName
}
netStatus.IPAddrs = append(netStatus.IPAddrs, addr.String())
}
Network = append(Network, netStatus)
}
return Network
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ spec:
value: "true"
- name: vip_address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: vip_interface
value: {{ .DefaultNetworkInterfaceName }}
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ spec:
value: "true"
- name: vip_address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: vip_interface
value: {{ .DefaultNetworkInterfaceName }}
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ spec:
value: "true"
- name: vip_address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: vip_interface
value: {{ .DefaultNetworkInterfaceName }}
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ spec:
value: "true"
- name: vip_address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: vip_interface
value: {{ .DefaultNetworkInterfaceName }}
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ spec:
value: "true"
- name: vip_address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: vip_interface
value: {{ .DefaultNetworkInterfaceName }}
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ spec:
value: "true"
- name: vip_address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: vip_interface
value: {{ .DefaultNetworkInterfaceName }}
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
Expand Down