mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
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:
@@ -31,6 +31,7 @@ type NBResourceReconciler struct {
|
||||
ManagementURL string
|
||||
AllowAutomaticPolicyCreation bool
|
||||
ClusterName string
|
||||
DefaultLabels map[string]string
|
||||
netbird *netbird.Client
|
||||
}
|
||||
|
||||
@@ -128,6 +129,7 @@ func (r *NBResourceReconciler) handlePolicyCreate(ctx context.Context, nbResourc
|
||||
Name: generatedName,
|
||||
Annotations: map[string]string{"netbird.io/generated-by": req.NamespacedName.String()},
|
||||
Finalizers: []string{"netbird.io/cleanup"},
|
||||
Labels: r.DefaultLabels,
|
||||
},
|
||||
Spec: netbirdiov1.NBPolicySpec{
|
||||
Name: name,
|
||||
@@ -148,6 +150,7 @@ func (r *NBResourceReconciler) handlePolicyCreate(ctx context.Context, nbResourc
|
||||
if nbPolicy.Annotations == nil {
|
||||
nbPolicy.Annotations = make(map[string]string)
|
||||
}
|
||||
nbPolicy.Labels = r.DefaultLabels
|
||||
nbPolicy.Annotations["netbird.io/generated-by"] = req.NamespacedName.String()
|
||||
nbPolicy.Spec = netbirdiov1.NBPolicySpec{
|
||||
Name: name,
|
||||
@@ -505,6 +508,7 @@ func (r *NBResourceReconciler) handleGroups(ctx context.Context, req ctrl.Reques
|
||||
},
|
||||
},
|
||||
Finalizers: []string{"netbird.io/group-cleanup", "netbird.io/resource-cleanup"},
|
||||
Labels: r.DefaultLabels,
|
||||
},
|
||||
Spec: netbirdiov1.NBGroupSpec{
|
||||
Name: groupName,
|
||||
|
||||
@@ -46,10 +46,11 @@ var _ = Describe("NBResource Controller", func() {
|
||||
server = httptest.NewServer(mux)
|
||||
netbirdClient = netbird.New(server.URL, "ABC")
|
||||
controllerReconciler = &NBResourceReconciler{
|
||||
Client: k8sClient,
|
||||
Scheme: k8sClient.Scheme(),
|
||||
netbird: netbirdClient,
|
||||
ClusterName: "kubernetes",
|
||||
Client: k8sClient,
|
||||
Scheme: k8sClient.Scheme(),
|
||||
netbird: netbirdClient,
|
||||
ClusterName: "kubernetes",
|
||||
DefaultLabels: map[string]string{"dog": "bark"},
|
||||
}
|
||||
|
||||
By("creating the custom resource for the Kind NBResource")
|
||||
@@ -125,6 +126,7 @@ var _ = Describe("NBResource Controller", func() {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
nbGroup := &netbirdiov1.NBGroup{}
|
||||
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "meow"}, nbGroup)).To(Succeed())
|
||||
Expect(nbGroup.Labels).To(HaveKeyWithValue("dog", "bark"))
|
||||
nbGroup.Status.GroupID = util.Ptr("test")
|
||||
Expect(k8sClient.Status().Update(ctx, nbGroup)).To(Succeed())
|
||||
})
|
||||
@@ -453,6 +455,7 @@ var _ = Describe("NBResource Controller", func() {
|
||||
nbPolicy := &netbirdiov1.NBPolicy{}
|
||||
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: nbresource.Status.PolicyNameMapping[policyGenName]}, nbPolicy)).To(Succeed())
|
||||
Expect(nbPolicy.Status.ManagedServiceList).To(ContainElement("default/test-resource"))
|
||||
Expect(nbPolicy.Labels).To(HaveKeyWithValue("dog", "bark"))
|
||||
})
|
||||
|
||||
When("Source groups is not defined", func() {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -52,6 +52,7 @@ var _ = Describe("NBRoutingPeer Controller", func() {
|
||||
netbird: netbirdClient,
|
||||
ClientImage: "netbirdio/netbird:latest",
|
||||
ClusterName: "kubernetes",
|
||||
DefaultLabels: make(map[string]string),
|
||||
NamespacedNetworks: false,
|
||||
}
|
||||
|
||||
@@ -436,6 +437,7 @@ var _ = Describe("NBRoutingPeer Controller", func() {
|
||||
Expect(k8sClient.Create(ctx, secret)).To(Succeed())
|
||||
})
|
||||
It("should create group and requeue to get its ID", func() {
|
||||
controllerReconciler.DefaultLabels = map[string]string{"dog": "bark"}
|
||||
res, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
@@ -445,6 +447,7 @@ var _ = Describe("NBRoutingPeer Controller", func() {
|
||||
group := &netbirdiov1.NBGroup{}
|
||||
Expect(k8sClient.Get(ctx, typeNamespacedName, group)).To(Succeed())
|
||||
Expect(group.Spec.Name).To(Equal(controllerReconciler.ClusterName))
|
||||
Expect(group.Labels).To(HaveKeyWithValue("dog", "bark"))
|
||||
|
||||
group.Status.GroupID = util.Ptr("test")
|
||||
Expect(k8sClient.Status().Update(ctx, group)).To(Succeed())
|
||||
@@ -846,6 +849,25 @@ var _ = Describe("NBRoutingPeer Controller", func() {
|
||||
Expect(deployment.Spec.Template.Spec.Containers[0].Image).To(Equal(controllerReconciler.ClientImage))
|
||||
})
|
||||
})
|
||||
|
||||
When("Default labels exist", func() {
|
||||
It("should add labels to Deployment and Pod metadata", func() {
|
||||
controllerReconciler.DefaultLabels = map[string]string{
|
||||
"cat": "meow",
|
||||
"dog": "bark",
|
||||
}
|
||||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
deployment := &appsv1.Deployment{}
|
||||
Expect(k8sClient.Get(ctx, typeNamespacedName, deployment)).To(Succeed())
|
||||
Expect(deployment.Labels).To(HaveKeyWithValue("cat", "meow"))
|
||||
Expect(deployment.Labels).To(HaveKeyWithValue("dog", "bark"))
|
||||
})
|
||||
})
|
||||
|
||||
When("Deployment is out-of-date", func() {
|
||||
It("should update deployment", func() {
|
||||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
|
||||
@@ -27,6 +27,7 @@ type ServiceReconciler struct {
|
||||
ClusterDNS string
|
||||
NamespacedNetworks bool
|
||||
ControllerNamespace string
|
||||
DefaultLabels map[string]string
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -138,6 +139,7 @@ func (r *ServiceReconciler) exposeService(ctx context.Context, req ctrl.Request,
|
||||
Name: "router",
|
||||
Namespace: routerNamespace,
|
||||
Finalizers: []string{"netbird.io/cleanup"},
|
||||
Labels: r.DefaultLabels,
|
||||
},
|
||||
Spec: netbirdiov1.NBRoutingPeerSpec{},
|
||||
}
|
||||
@@ -205,6 +207,7 @@ func (r *ServiceReconciler) reconcileNBResource(nbResource *netbirdiov1.NBResour
|
||||
|
||||
nbResource.ObjectMeta.Name = req.Name
|
||||
nbResource.ObjectMeta.Namespace = req.Namespace
|
||||
nbResource.ObjectMeta.Labels = r.DefaultLabels
|
||||
nbResource.Finalizers = []string{"netbird.io/cleanup"}
|
||||
nbResource.Spec.Name = resourceName
|
||||
nbResource.Spec.NetworkID = *routingPeer.Status.NetworkID
|
||||
|
||||
@@ -67,6 +67,7 @@ var _ = Describe("Service Controller", func() {
|
||||
NamespacedNetworks: false,
|
||||
ClusterDNS: "svc.cluster.local",
|
||||
ControllerNamespace: "default",
|
||||
DefaultLabels: map[string]string{"dog": "bark"},
|
||||
}
|
||||
})
|
||||
|
||||
@@ -144,6 +145,7 @@ var _ = Describe("Service Controller", func() {
|
||||
Expect(res.RequeueAfter).NotTo(BeZero())
|
||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: typeNamespacedName.Namespace, Name: "router"}, nbrp)).To(Succeed())
|
||||
Expect(nbrp.Labels).To(HaveKeyWithValue("dog", "bark"))
|
||||
res, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
@@ -203,6 +205,7 @@ var _ = Describe("Service Controller", func() {
|
||||
Expect(nbResource.Spec.PolicyName).To(BeEmpty())
|
||||
Expect(nbResource.Spec.TCPPorts).To(BeEmpty())
|
||||
Expect(nbResource.Spec.UDPPorts).To(BeEmpty())
|
||||
Expect(nbResource.Labels).To(HaveKeyWithValue("dog", "bark"))
|
||||
})
|
||||
})
|
||||
When("policy is specified", func() {
|
||||
|
||||
Reference in New Issue
Block a user