Skip to content

Commit

Permalink
Add nodelabeller implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ubombar committed Mar 19, 2024
1 parent e3bb39d commit e629aa3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(),
Expand Down
17 changes: 16 additions & 1 deletion internal/controller/labellers/nodelabeller_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions internal/labeller/labeller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package labeller

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion internal/labeller/maxmind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e629aa3

Please sign in to comment.