Skip to content

Commit

Permalink
Auto-detect the default BYOH interface for hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
VibhorChinda committed Apr 7, 2022
1 parent b17b4f0 commit ad59020
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 91 deletions.
26 changes: 5 additions & 21 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,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 @@ -206,12 +191,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

0 comments on commit ad59020

Please sign in to comment.