diff --git a/cmd/main.go b/cmd/main.go index ef3b0d3..0a295f8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -138,6 +138,7 @@ func main() { // Try to read the maxmind accountid, and token from the file. maxmind, err := labeller.NewMaxMindFromSecret() + // If the error is not nil then we cannot get the maxmind config and we should not start node labeller reconcilier. disableNodeLabeller := err != nil @@ -148,7 +149,7 @@ func main() { // Setup reconcilers, we might want to add the list of reconcilers. This part is auto generated. // If you want to add the functionality to disable reconcilers, put it inside an if. // WARNING: This part is semi-auto-generated! By default you cannot disable reconcilers since they are - // generated autside an if clause. + // generated autside an if clause. ADD YOUR IF CLAUSE MANUALLY! if !disabledReconcilers.Contains("Tenant") { if err = (&multitenancycontroller.TenantReconciler{ Client: mgr.GetClient(), diff --git a/internal/controller/labellers/nodelabeller_controller.go b/internal/controller/labellers/nodelabeller_controller.go index 366ad52..1eda8d8 100644 --- a/internal/controller/labellers/nodelabeller_controller.go +++ b/internal/controller/labellers/nodelabeller_controller.go @@ -20,6 +20,7 @@ import ( "context" "github.com/edgenet-project/edgenet-software/internal/labeller" + "github.com/edgenet-project/edgenet-software/internal/utils" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -49,8 +50,22 @@ type NodeLabellerReconciler struct { func (r *NodeLabellerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) + // Get the node directly node := &corev1.Node{} - if err := r.Client.Get(ctx, req.NamespacedName, node); err != nil { + + if err := utils.GetResource(ctx, r.Client, node, req.NamespacedName); err != nil { + return ctrl.Result{}, err + } + + // Create the labeller manager + labellerManager, err := labeller.NewLabelManager(ctx, r.Client) + + if err != nil { + return ctrl.Result{}, err + } + + // Label the node + if err := labellerManager.LabelNode(ctx, node); err != nil { return ctrl.Result{}, err } diff --git a/internal/labeller/labeller.go b/internal/labeller/labeller.go index 328d0f7..fda52ac 100644 --- a/internal/labeller/labeller.go +++ b/internal/labeller/labeller.go @@ -18,6 +18,7 @@ package labeller import ( "context" + "fmt" corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,5 +45,6 @@ func NewLabelManager(ctx context.Context, client client.Client) (LabelManager, e // 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 { + fmt.Println("Node labeller implementation required...") return nil } diff --git a/internal/labeller/maxmind.go b/internal/labeller/maxmind.go index a6b530a..12c09ca 100644 --- a/internal/labeller/maxmind.go +++ b/internal/labeller/maxmind.go @@ -24,7 +24,7 @@ func NewMaxMindFromSecret() (MaxMind, error) { return nil, errors.New("not yet implemented") } -// This is for +// This is for performing a lookup func (mm maxMind) MaxMindLookup(address string) (*geoip2.Response, error) { req, err := http.NewRequest("GET", mm.url+address, nil) if err != nil { diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 491aea6..8291f78 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -118,6 +118,15 @@ func GetResourceWithFinalizer(ctx context.Context, c client.Client, obj client.O return !obj.GetDeletionTimestamp().IsZero(), reconcile.Result{Requeue: false}, nil } +// Gets the object from using the client. Unlike the "GetResourceWithFinalizer" function, it doesn't add a finalizer to the object. +func GetResource(ctx context.Context, c client.Client, obj client.Object, namespacedName types.NamespacedName) error { + // Get the object from the cluster + if err := c.Get(ctx, namespacedName, obj); err != nil { + return err + } + return nil +} + // Normally when a Kubernetes object is deleted it is no longer accessible from the etcd. To retrieve the last state // of the object finalizers are used. GetResourceWithFinalizer function adds a finalizer to the resource if not present. // These finalizers are then can used to keep the object in the cluster after it is marked for deletion.