Add feature to add default labels to all resources (#62)

Fixes #41 
Thanks to @mhartmann-jaconi for the Helm changes in #42
This commit is contained in:
M. Essam
2025-10-07 13:59:50 +03:00
committed by GitHub
parent 1b36287705
commit 4b98bf9ff9
10 changed files with 94 additions and 10 deletions
@@ -31,6 +31,7 @@ type NBRoutingPeerReconciler struct {
APIKey string
ManagementURL string
NamespacedNetworks bool
DefaultLabels map[string]string
netbird *netbird.Client
}
@@ -122,6 +123,13 @@ func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl
return err
}
labels := r.DefaultLabels
for k, v := range nbrp.Spec.Labels {
labels[k] = v
}
podLabels := labels
podLabels["app.kubernetes.io/name"] = "netbird-router"
// Create deployment
if errors.IsNotFound(err) {
var replicas int32 = 3
@@ -141,7 +149,7 @@ func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl
BlockOwnerDeletion: util.Ptr(true),
},
},
Labels: nbrp.Spec.Labels,
Labels: labels,
Annotations: nbrp.Spec.Annotations,
},
Spec: appsv1.DeploymentSpec{
@@ -153,9 +161,7 @@ func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl
},
Template: corev1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"app.kubernetes.io/name": "netbird-router",
},
Labels: podLabels,
},
Spec: corev1.PodSpec{
NodeSelector: nbrp.Spec.NodeSelector,
@@ -217,7 +223,7 @@ func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl
BlockOwnerDeletion: util.Ptr(true),
},
}
updatedDeployment.ObjectMeta.Labels = nbrp.Spec.Labels
updatedDeployment.ObjectMeta.Labels = labels
for k, v := range nbrp.Spec.Annotations {
updatedDeployment.ObjectMeta.Annotations[k] = nbrp.Spec.Annotations[v]
}
@@ -233,6 +239,7 @@ func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl
}
updatedDeployment.Spec.Template.Spec.Tolerations = nbrp.Spec.Tolerations
updatedDeployment.Spec.Template.Spec.NodeSelector = nbrp.Spec.NodeSelector
updatedDeployment.Spec.Template.ObjectMeta.Labels = podLabels
updatedDeployment.Spec.Template.Spec.Volumes = nbrp.Spec.Volumes
updatedDeployment.Spec.Template.ObjectMeta.Labels = map[string]string{
"app.kubernetes.io/name": "netbird-router",
@@ -378,6 +385,7 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
BlockOwnerDeletion: util.Ptr(true),
},
},
Labels: r.DefaultLabels,
},
StringData: map[string]string{
"setupKey": setupKey.Key,
@@ -402,7 +410,7 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
return &ctrl.Result{}, err
}
if (err != nil && strings.Contains(err.Error(), "not found")) || setupKey.Revoked {
if err != nil || setupKey.Revoked {
if setupKey != nil && setupKey.Revoked {
err = r.netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
@@ -480,6 +488,7 @@ func (r *NBRoutingPeerReconciler) handleGroup(ctx context.Context, req ctrl.Requ
},
},
Finalizers: []string{"netbird.io/group-cleanup", "netbird.io/routing-peer-cleanup"},
Labels: r.DefaultLabels,
},
Spec: netbirdiov1.NBGroupSpec{
Name: networkName,