From 927235df8ec1462bf074327045a1a3563746fec6 Mon Sep 17 00:00:00 2001 From: Vibhor Chinda Date: Tue, 5 Apr 2022 23:56:56 +0530 Subject: [PATCH] Auto-detect the default BYOH interface for hosts --- agent/main.go | 26 ++------- agent/registration/host_registrar.go | 58 ------------------- .../templates/docker/cluster-template.yaml | 2 - .../templates/docker/cluster-with-kcp.yaml | 2 - .../templates/e2e/cluster-template.yaml | 2 - .../templates/e2e/cluster-with-kcp.yaml | 2 - .../templates/vm/cluster-template.yaml | 2 - .../templates/vm/cluster-with-kcp.yaml | 2 - 8 files changed, 5 insertions(+), 91 deletions(-) diff --git a/agent/main.go b/agent/main.go index dda892d08..8f9e3a280 100644 --- a/agent/main.go +++ b/agent/main.go @@ -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 @@ -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 { diff --git a/agent/registration/host_registrar.go b/agent/registration/host_registrar.go index 884173178..cee66c34f 100644 --- a/agent/registration/host_registrar.go +++ b/agent/registration/host_registrar.go @@ -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" @@ -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 -} diff --git a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-template.yaml b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-template.yaml index 27cb3d5aa..3f066f4ce 100644 --- a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-template.yaml +++ b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-template.yaml @@ -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 diff --git a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-with-kcp.yaml b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-with-kcp.yaml index ed8ab57a9..23c50acfb 100644 --- a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-with-kcp.yaml +++ b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/docker/cluster-with-kcp.yaml @@ -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 diff --git a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-template.yaml b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-template.yaml index 711321863..1837ebd42 100644 --- a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-template.yaml +++ b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-template.yaml @@ -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 diff --git a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-with-kcp.yaml b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-with-kcp.yaml index ef6b85792..1856a0d7c 100644 --- a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-with-kcp.yaml +++ b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/e2e/cluster-with-kcp.yaml @@ -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 diff --git a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-template.yaml b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-template.yaml index 549dd27f5..936ee659f 100644 --- a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-template.yaml +++ b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-template.yaml @@ -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 diff --git a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-with-kcp.yaml b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-with-kcp.yaml index 5caedb6d2..1cc28262a 100644 --- a/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-with-kcp.yaml +++ b/test/e2e/data/infrastructure-provider-byoh/v1beta1/templates/vm/cluster-with-kcp.yaml @@ -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