From ef48d7f7ea5678e38a0236563a8b7224b1353e85 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/host_agent_test.go | 44 -------------- 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 - 9 files changed, 5 insertions(+), 135 deletions(-) diff --git a/agent/host_agent_test.go b/agent/host_agent_test.go index 01f9e2f10..5ba2a82fb 100644 --- a/agent/host_agent_test.go +++ b/agent/host_agent_test.go @@ -7,7 +7,6 @@ package main import ( "context" "fmt" - "net" "os" "os/exec" "runtime" @@ -15,7 +14,6 @@ import ( 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" @@ -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") 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