From 800a448893edbd76417a44f6077dd344677909ef Mon Sep 17 00:00:00 2001 From: ubombar Date: Tue, 19 Mar 2024 18:11:07 +0100 Subject: [PATCH] Add maxmind to the label manager --- .../labellers/nodelabeller_controller.go | 2 +- internal/labeller/labeller.go | 76 ++++++++++++++++++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/internal/controller/labellers/nodelabeller_controller.go b/internal/controller/labellers/nodelabeller_controller.go index 1eda8d8..96180ca 100644 --- a/internal/controller/labellers/nodelabeller_controller.go +++ b/internal/controller/labellers/nodelabeller_controller.go @@ -58,7 +58,7 @@ func (r *NodeLabellerReconciler) Reconcile(ctx context.Context, req ctrl.Request } // Create the labeller manager - labellerManager, err := labeller.NewLabelManager(ctx, r.Client) + labellerManager, err := labeller.NewLabelManager(ctx, r.Client, r.MaxMind) if err != nil { return ctrl.Result{}, err diff --git a/internal/labeller/labeller.go b/internal/labeller/labeller.go index fda52ac..1a2ba6e 100644 --- a/internal/labeller/labeller.go +++ b/internal/labeller/labeller.go @@ -30,21 +30,89 @@ import ( type LabelManager interface { // This adds the labes to the node object given LabelNode(context.Context, *corev1.Node) error + + // Gets the internal and external ip address + GetNodeIPAddresses(obj *corev1.Node) (string, string) } type labelManager struct { LabelManager - client client.Client + client client.Client + Maxmind MaxMind } -func NewLabelManager(ctx context.Context, client client.Client) (LabelManager, error) { +func NewLabelManager(ctx context.Context, client client.Client, maxmind MaxMind) (LabelManager, error) { return &labelManager{ - client: client, + client: client, + Maxmind: maxmind, }, nil } // This adds the labels to the node and updates it. If any error occures it returnes the error. -func (m *labelManager) LabelNode(context.Context, *corev1.Node) error { +func (m *labelManager) LabelNode(ctx context.Context, node *corev1.Node) error { fmt.Println("Node labeller implementation required...") + + // TODO: Get this part of the code from old repo + // internalIp, externalIp := m.GetNodeIPAddresses(node) + + // Old code... + // // 1. Use the VPNPeer endpoint address if available. + // peer, err := c.edgenetclientset.NetworkingV1alpha1().VPNPeers().Get(context.TODO(), nodeObj.Name, v1.GetOptions{}) + // if err != nil { + // klog.V(4).Infof( + // "Failed to find a matching VPNPeer object for %s: %s. The node IP will be used instead.", + // nodeObj.Name, + // err, + // ) + // } else { + // klog.V(4).Infof("VPNPeer endpoint IP: %s", *peer.Spec.EndpointAddress) + // result = multiproviderManager.GetGeolocationByIP( + // c.maxmindURL, + // c.maxmindAccountID, + // c.maxmindLicenseKey, + // nodeObj.Name, + // *peer.Spec.EndpointAddress, + // ) + // } + + // // 2. Otherwise use the node external IP if available. + // if externalIP != "" && !result { + // klog.V(4).Infof("External IP: %s", externalIP) + // result = multiproviderManager.GetGeolocationByIP( + // c.maxmindURL, + // c.maxmindAccountID, + // c.maxmindLicenseKey, + // nodeObj.Name, + // externalIP, + // ) + // } + + // // 3. Otherwise use the node internal IP if available. + // if internalIP != "" && !result { + // klog.V(4).Infof("Internal IP: %s", internalIP) + // multiproviderManager.GetGeolocationByIP( + // c.maxmindURL, + // c.maxmindAccountID, + // c.maxmindLicenseKey, + // nodeObj.Name, + // internalIP, + // ) + // } + return nil } + +// GetNodeIPAddresses picks up the internal and external IP addresses of the Node +func (m *labelManager) GetNodeIPAddresses(obj *corev1.Node) (string, string) { + internalIP := "" + externalIP := "" + for _, addressesRow := range obj.Status.Addresses { + if addressType := addressesRow.Type; addressType == "InternalIP" { + internalIP = addressesRow.Address + } + if addressType := addressesRow.Type; addressType == "ExternalIP" { + externalIP = addressesRow.Address + } + } + return internalIP, externalIP +}