Skip to content

Commit f1c382e

Browse files
authored
Revert "Cleanup routing peer deployment creation" (#159)
Reverts #155
1 parent a20570b commit f1c382e

1 file changed

Lines changed: 164 additions & 72 deletions

File tree

internal/controller/nbroutingpeer_controller.go

Lines changed: 164 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import (
1111
appsv1 "k8s.io/api/apps/v1"
1212
corev1 "k8s.io/api/core/v1"
1313
"k8s.io/apimachinery/pkg/api/errors"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
ctrl "sigs.k8s.io/controller-runtime"
1616
"sigs.k8s.io/controller-runtime/pkg/client"
17-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1817
"sigs.k8s.io/controller-runtime/pkg/handler"
1918

2019
"github.com/go-logr/logr"
@@ -105,7 +104,7 @@ func (r *NBRoutingPeerReconciler) Reconcile(ctx context.Context, req ctrl.Reques
105104
}
106105

107106
logger.Info("NBRoutingPeer: Checking deployment")
108-
err = r.handleDeployment(ctx, nbrp)
107+
err = r.handleDeployment(ctx, req, nbrp, logger)
109108
if err != nil {
110109
return ctrl.Result{}, err
111110
}
@@ -115,93 +114,167 @@ func (r *NBRoutingPeerReconciler) Reconcile(ctx context.Context, req ctrl.Reques
115114
}
116115

117116
// handleDeployment reconcile routing peer Deployment
118-
func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, nbrp *netbirdiov1.NBRoutingPeer) error {
117+
func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl.Request, nbrp *netbirdiov1.NBRoutingPeer, logger logr.Logger) error {
118+
routingPeerDeployment := appsv1.Deployment{}
119+
err := r.Client.Get(ctx, req.NamespacedName, &routingPeerDeployment)
120+
if err != nil && !errors.IsNotFound(err) {
121+
logger.Error(errKubernetesAPI, "error getting Deployment", "err", err)
122+
nbrp.Status.Conditions = netbirdiov1.NBConditionFalse("internalError", fmt.Sprintf("error getting Deployment: %v", err))
123+
return err
124+
}
125+
119126
labels := r.DefaultLabels
120127
maps.Copy(labels, nbrp.Spec.Labels)
121128
podLabels := labels
122129
podLabels["app.kubernetes.io/name"] = "netbird-router"
123130

124-
var replicas int32 = 3
125-
if nbrp.Spec.Replicas != nil {
126-
replicas = *nbrp.Spec.Replicas
127-
}
128-
129-
securityContext := &corev1.SecurityContext{
130-
Capabilities: &corev1.Capabilities{
131-
Add: []corev1.Capability{
132-
"NET_ADMIN",
133-
},
134-
},
135-
}
136-
if nbrp.Spec.Privileged != nil && *nbrp.Spec.Privileged {
137-
securityContext.Privileged = nbrp.Spec.Privileged
138-
}
139-
140-
dep := &appsv1.Deployment{
141-
ObjectMeta: metav1.ObjectMeta{
142-
Namespace: nbrp.Namespace,
143-
Name: nbrp.Name,
144-
},
145-
}
146-
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, dep, func() error {
147-
dep.ObjectMeta.Labels = labels
148-
dep.ObjectMeta.Annotations = nbrp.Spec.Annotations
149-
dep.Spec = appsv1.DeploymentSpec{
150-
Replicas: &replicas,
151-
Selector: &metav1.LabelSelector{
152-
MatchLabels: map[string]string{
153-
"app.kubernetes.io/name": "netbird-router",
131+
// Create deployment
132+
if errors.IsNotFound(err) {
133+
var replicas int32 = 3
134+
if nbrp.Spec.Replicas != nil {
135+
replicas = *nbrp.Spec.Replicas
136+
}
137+
routingPeerDeployment = appsv1.Deployment{
138+
ObjectMeta: v1.ObjectMeta{
139+
Name: nbrp.Name,
140+
Namespace: nbrp.Namespace,
141+
OwnerReferences: []v1.OwnerReference{
142+
{
143+
APIVersion: netbirdiov1.GroupVersion.Identifier(),
144+
Kind: "NBRoutingPeer",
145+
Name: nbrp.Name,
146+
UID: nbrp.UID,
147+
BlockOwnerDeletion: util.Ptr(true),
148+
},
154149
},
150+
Labels: labels,
151+
Annotations: nbrp.Spec.Annotations,
155152
},
156-
Template: corev1.PodTemplateSpec{
157-
ObjectMeta: metav1.ObjectMeta{
158-
Labels: podLabels,
153+
Spec: appsv1.DeploymentSpec{
154+
Replicas: &replicas,
155+
Selector: &v1.LabelSelector{
156+
MatchLabels: map[string]string{
157+
"app.kubernetes.io/name": "netbird-router",
158+
},
159159
},
160-
Spec: corev1.PodSpec{
161-
NodeSelector: nbrp.Spec.NodeSelector,
162-
Tolerations: nbrp.Spec.Tolerations,
163-
Containers: []corev1.Container{
164-
{
165-
Name: "netbird",
166-
Image: r.ClientImage,
167-
Env: []corev1.EnvVar{
168-
{
169-
Name: "NB_SETUP_KEY",
170-
ValueFrom: &corev1.EnvVarSource{
171-
SecretKeyRef: &corev1.SecretKeySelector{
172-
LocalObjectReference: corev1.LocalObjectReference{
173-
Name: nbrp.Name,
160+
Template: corev1.PodTemplateSpec{
161+
ObjectMeta: v1.ObjectMeta{
162+
Labels: podLabels,
163+
},
164+
Spec: corev1.PodSpec{
165+
NodeSelector: nbrp.Spec.NodeSelector,
166+
Tolerations: nbrp.Spec.Tolerations,
167+
Containers: []corev1.Container{
168+
{
169+
Name: "netbird",
170+
Image: r.ClientImage,
171+
Env: []corev1.EnvVar{
172+
{
173+
Name: "NB_SETUP_KEY",
174+
ValueFrom: &corev1.EnvVarSource{
175+
SecretKeyRef: &corev1.SecretKeySelector{
176+
LocalObjectReference: corev1.LocalObjectReference{
177+
Name: nbrp.Name,
178+
},
179+
Key: "setupKey",
174180
},
175-
Key: "setupKey",
176181
},
177182
},
183+
{
184+
Name: "NB_MANAGEMENT_URL",
185+
Value: r.ManagementURL,
186+
},
178187
},
179-
{
180-
Name: "NB_MANAGEMENT_URL",
181-
Value: r.ManagementURL,
182-
},
188+
SecurityContext: r.buildSecurityContext(nbrp),
189+
Resources: nbrp.Spec.Resources,
190+
VolumeMounts: nbrp.Spec.VolumeMounts,
183191
},
184-
SecurityContext: securityContext,
185-
Resources: nbrp.Spec.Resources,
186-
VolumeMounts: nbrp.Spec.VolumeMounts,
187192
},
193+
Volumes: nbrp.Spec.Volumes,
188194
},
189-
Volumes: nbrp.Spec.Volumes,
190195
},
191196
},
192197
}
193198

194-
err := controllerutil.SetControllerReference(nbrp, dep, r.Scheme())
199+
err = r.Client.Create(ctx, &routingPeerDeployment)
195200
if err != nil {
201+
logger.Error(errKubernetesAPI, "error creating Deployment", "err", err)
202+
nbrp.Status.Conditions = netbirdiov1.NBConditionFalse("internalError", fmt.Sprintf("error creating Deployment: %v", err))
203+
return err
204+
}
205+
} else if err == nil {
206+
updatedDeployment := routingPeerDeployment.DeepCopy()
207+
updatedDeployment.ObjectMeta.Name = nbrp.Name
208+
updatedDeployment.ObjectMeta.Namespace = nbrp.Namespace
209+
updatedDeployment.ObjectMeta.OwnerReferences = []v1.OwnerReference{
210+
{
211+
APIVersion: netbirdiov1.GroupVersion.Identifier(),
212+
Kind: "NBRoutingPeer",
213+
Name: nbrp.Name,
214+
UID: nbrp.UID,
215+
BlockOwnerDeletion: util.Ptr(true),
216+
},
217+
}
218+
updatedDeployment.ObjectMeta.Labels = labels
219+
for k, v := range nbrp.Spec.Annotations {
220+
updatedDeployment.ObjectMeta.Annotations[k] = nbrp.Spec.Annotations[v]
221+
}
222+
var replicas int32 = 3
223+
if nbrp.Spec.Replicas != nil {
224+
replicas = *nbrp.Spec.Replicas
225+
}
226+
updatedDeployment.Spec.Replicas = &replicas
227+
updatedDeployment.Spec.Selector = &v1.LabelSelector{
228+
MatchLabels: map[string]string{
229+
"app.kubernetes.io/name": "netbird-router",
230+
},
231+
}
232+
updatedDeployment.Spec.Template.Spec.Tolerations = nbrp.Spec.Tolerations
233+
updatedDeployment.Spec.Template.Spec.NodeSelector = nbrp.Spec.NodeSelector
234+
updatedDeployment.Spec.Template.ObjectMeta.Labels = podLabels
235+
updatedDeployment.Spec.Template.Spec.Volumes = nbrp.Spec.Volumes
236+
if len(updatedDeployment.Spec.Template.Spec.Containers) != 1 {
237+
updatedDeployment.Spec.Template.Spec.Containers = []corev1.Container{{}}
238+
}
239+
updatedDeployment.Spec.Template.Spec.Containers[0].Name = "netbird"
240+
updatedDeployment.Spec.Template.Spec.Containers[0].Image = r.ClientImage
241+
updatedDeployment.Spec.Template.Spec.Containers[0].Env = []corev1.EnvVar{
242+
{
243+
Name: "NB_SETUP_KEY",
244+
ValueFrom: &corev1.EnvVarSource{
245+
SecretKeyRef: &corev1.SecretKeySelector{
246+
LocalObjectReference: corev1.LocalObjectReference{
247+
Name: nbrp.Name,
248+
},
249+
Key: "setupKey",
250+
},
251+
},
252+
},
253+
{
254+
Name: "NB_MANAGEMENT_URL",
255+
Value: r.ManagementURL,
256+
},
257+
}
258+
updatedDeployment.Spec.Template.Spec.Containers[0].SecurityContext = r.buildSecurityContext(nbrp)
259+
updatedDeployment.Spec.Template.Spec.Containers[0].Resources = nbrp.Spec.Resources
260+
updatedDeployment.Spec.Template.Spec.Containers[0].VolumeMounts = nbrp.Spec.VolumeMounts
261+
262+
patch := client.StrategicMergeFrom(&routingPeerDeployment)
263+
bs, _ := patch.Data(updatedDeployment)
264+
// To ensure no useless patching is done to the deployment being watched
265+
// Minimum patch size is 2 for "{}"
266+
if len(bs) <= 2 {
267+
return nil
268+
}
269+
err = r.Client.Patch(ctx, updatedDeployment, patch)
270+
if err != nil {
271+
logger.Error(errKubernetesAPI, "error updating Deployment", "err", err)
272+
nbrp.Status.Conditions = netbirdiov1.NBConditionFalse("internalError", fmt.Sprintf("error updating Deployment: %v", err))
196273
return err
197274
}
198-
199-
return nil
200-
})
201-
if err != nil {
202-
return err
203275
}
204-
return nil
276+
277+
return err
205278
}
206279

207280
// handleRouter reconcile network routing peer in NetBird management API
@@ -283,10 +356,10 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
283356
nbrp.Status.SetupKeyID = &setupKey.Id
284357

285358
skSecret := corev1.Secret{
286-
ObjectMeta: metav1.ObjectMeta{
359+
ObjectMeta: v1.ObjectMeta{
287360
Name: nbrp.Name,
288361
Namespace: nbrp.Namespace,
289-
OwnerReferences: []metav1.OwnerReference{
362+
OwnerReferences: []v1.OwnerReference{
290363
{
291364
APIVersion: netbirdiov1.GroupVersion.Identifier(),
292365
Kind: "NBRoutingPeer",
@@ -393,10 +466,10 @@ func (r *NBRoutingPeerReconciler) handleGroup(ctx context.Context, req ctrl.Requ
393466

394467
if errors.IsNotFound(err) {
395468
nbGroup = netbirdiov1.NBGroup{
396-
ObjectMeta: metav1.ObjectMeta{
469+
ObjectMeta: v1.ObjectMeta{
397470
Name: nbrp.Name,
398471
Namespace: nbrp.Namespace,
399-
OwnerReferences: []metav1.OwnerReference{
472+
OwnerReferences: []v1.OwnerReference{
400473
{
401474
APIVersion: netbirdiov1.GroupVersion.Identifier(),
402475
Kind: "NBRoutingPeer",
@@ -570,11 +643,30 @@ func (r *NBRoutingPeerReconciler) handleDelete(ctx context.Context, req ctrl.Req
570643
return ctrl.Result{}, nil
571644
}
572645

646+
// buildSecurityContext creates the appropriate SecurityContext based on the NBRoutingPeer spec
647+
func (r *NBRoutingPeerReconciler) buildSecurityContext(nbrp *netbirdiov1.NBRoutingPeer) *corev1.SecurityContext {
648+
securityContext := &corev1.SecurityContext{
649+
Capabilities: &corev1.Capabilities{
650+
Add: []corev1.Capability{
651+
"NET_ADMIN",
652+
},
653+
},
654+
}
655+
656+
// Set privileged mode if specified
657+
if nbrp.Spec.Privileged != nil && *nbrp.Spec.Privileged {
658+
securityContext.Privileged = nbrp.Spec.Privileged
659+
}
660+
661+
return securityContext
662+
}
663+
573664
// SetupWithManager sets up the controller with the Manager.
574665
func (r *NBRoutingPeerReconciler) SetupWithManager(mgr ctrl.Manager) error {
575666
return ctrl.NewControllerManagedBy(mgr).
576667
For(&netbirdiov1.NBRoutingPeer{}).
577-
Owns(&appsv1.Deployment{}).
668+
Named("nbroutingpeer").
669+
Watches(&appsv1.Deployment{}, handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &netbirdiov1.NBRoutingPeer{})).
578670
Watches(&corev1.Secret{}, handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &netbirdiov1.NBRoutingPeer{})).
579671
Watches(&netbirdiov1.NBGroup{}, handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &netbirdiov1.NBRoutingPeer{})).
580672
Complete(r)

0 commit comments

Comments
 (0)