mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
Add unit tests to new controllers and fix minor bugs (#12)
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
netbird "github.com/netbirdio/netbird/management/client/rest"
|
||||
)
|
||||
|
||||
// nolint:unused
|
||||
@@ -22,17 +21,16 @@ import (
|
||||
var nbgrouplog = logf.Log.WithName("nbgroup-resource")
|
||||
|
||||
// SetupNBGroupWebhookWithManager registers the webhook for NBGroup in the manager.
|
||||
func SetupNBGroupWebhookWithManager(mgr ctrl.Manager, managementURL, apiKey string) error {
|
||||
func SetupNBGroupWebhookWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewWebhookManagedBy(mgr).For(&netbirdiov1.NBGroup{}).
|
||||
WithValidator(&NBGroupCustomValidator{netbird: netbird.New(managementURL, apiKey), client: mgr.GetClient()}).
|
||||
WithValidator(&NBGroupCustomValidator{client: mgr.GetClient()}).
|
||||
Complete()
|
||||
}
|
||||
|
||||
// NBGroupCustomValidator struct is responsible for validating the NBGroup resource
|
||||
// when it is created, updated, or deleted.
|
||||
type NBGroupCustomValidator struct {
|
||||
netbird *netbird.Client
|
||||
client client.Client
|
||||
client client.Client
|
||||
}
|
||||
|
||||
var _ webhook.CustomValidator = &NBGroupCustomValidator{}
|
||||
|
||||
@@ -3,9 +3,11 @@ package v1
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
// TODO (user): Add any additional imports if needed
|
||||
)
|
||||
|
||||
var _ = Describe("NBGroup Webhook", func() {
|
||||
@@ -16,41 +18,154 @@ var _ = Describe("NBGroup Webhook", func() {
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
Skip("Not implemented yet")
|
||||
obj = &netbirdiov1.NBGroup{}
|
||||
oldObj = &netbirdiov1.NBGroup{}
|
||||
validator = NBGroupCustomValidator{}
|
||||
validator = NBGroupCustomValidator{
|
||||
client: k8sClient,
|
||||
}
|
||||
Expect(validator).NotTo(BeNil(), "Expected validator to be initialized")
|
||||
Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized")
|
||||
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
|
||||
// TODO (user): Add any setup logic common to all tests
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO (user): Add any teardown logic common to all tests
|
||||
})
|
||||
|
||||
Context("When creating or updating NBGroup under Validating Webhook", func() {
|
||||
// TODO (user): Add logic for validating webhooks
|
||||
// Example:
|
||||
// It("Should deny creation if a required field is missing", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = ""
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).Error().To(HaveOccurred())
|
||||
// })
|
||||
//
|
||||
// It("Should admit creation if all required fields are present", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = "valid_value"
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("Should validate updates correctly", func() {
|
||||
// By("simulating a valid update scenario")
|
||||
// oldObj.SomeRequiredField = "updated_value"
|
||||
// obj.SomeRequiredField = "updated_value"
|
||||
// Expect(validator.ValidateUpdate(ctx, oldObj, obj)).To(BeNil())
|
||||
// })
|
||||
It("should allow creation", func() {
|
||||
Expect(validator.ValidateCreate(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
It("should allow update", func() {
|
||||
Expect(validator.ValidateUpdate(ctx, oldObj, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
When("There are no owners", func() {
|
||||
It("should allow deletion", func() {
|
||||
obj = &netbirdiov1.NBGroup{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
OwnerReferences: nil,
|
||||
},
|
||||
}
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("There deleted owners", func() {
|
||||
It("should allow deletion", func() {
|
||||
obj = &netbirdiov1.NBGroup{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
OwnerReferences: []v1.OwnerReference{
|
||||
{
|
||||
APIVersion: netbirdiov1.GroupVersion.Identifier(),
|
||||
Kind: "NBResource",
|
||||
Name: "notexist",
|
||||
UID: obj.UID,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("NBResource owner exists", func() {
|
||||
BeforeEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "isexist",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: netbirdiov1.NBResourceSpec{
|
||||
Name: "test1",
|
||||
NetworkID: "test2",
|
||||
Address: "test3",
|
||||
Groups: []string{"test"},
|
||||
},
|
||||
}
|
||||
|
||||
Expect(k8sClient.Create(ctx, nbResource)).To(Succeed())
|
||||
|
||||
obj = &netbirdiov1.NBGroup{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
OwnerReferences: []v1.OwnerReference{
|
||||
{
|
||||
APIVersion: netbirdiov1.GroupVersion.Identifier(),
|
||||
Kind: nbResource.Kind,
|
||||
Name: nbResource.Name,
|
||||
UID: nbResource.UID,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
AfterEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "isexist"}, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(nbResource.Finalizers) > 0 {
|
||||
nbResource.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, nbResource)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should deny deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("NBRoutingPeer owner exists", func() {
|
||||
BeforeEach(func() {
|
||||
nbrp := &netbirdiov1.NBRoutingPeer{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "isexist",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: netbirdiov1.NBRoutingPeerSpec{},
|
||||
}
|
||||
|
||||
Expect(k8sClient.Create(ctx, nbrp)).To(Succeed())
|
||||
|
||||
obj = &netbirdiov1.NBGroup{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
OwnerReferences: []v1.OwnerReference{
|
||||
{
|
||||
APIVersion: netbirdiov1.GroupVersion.Identifier(),
|
||||
Kind: nbrp.Kind,
|
||||
Name: nbrp.Name,
|
||||
UID: nbrp.UID,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
AfterEach(func() {
|
||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "isexist"}, nbrp)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(nbrp.Finalizers) > 0 {
|
||||
nbrp.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, nbrp)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, nbrp)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should deny deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
@@ -15,7 +16,6 @@ import (
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
"github.com/netbirdio/kubernetes-operator/internal/controller"
|
||||
netbird "github.com/netbirdio/netbird/management/client/rest"
|
||||
)
|
||||
|
||||
// nolint:unused
|
||||
@@ -23,17 +23,16 @@ import (
|
||||
var nbresourcelog = logf.Log.WithName("nbresource-resource")
|
||||
|
||||
// SetupNBResourceWebhookWithManager registers the webhook for NBResource in the manager.
|
||||
func SetupNBResourceWebhookWithManager(mgr ctrl.Manager, managementURL, apiKey string) error {
|
||||
func SetupNBResourceWebhookWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewWebhookManagedBy(mgr).For(&netbirdiov1.NBResource{}).
|
||||
WithValidator(&NBResourceCustomValidator{netbird: netbird.New(managementURL, apiKey), client: mgr.GetClient()}).
|
||||
WithValidator(&NBResourceCustomValidator{client: mgr.GetClient()}).
|
||||
Complete()
|
||||
}
|
||||
|
||||
// NBResourceCustomValidator struct is responsible for validating the NBResource resource
|
||||
// when it is created, updated, or deleted.
|
||||
type NBResourceCustomValidator struct {
|
||||
netbird *netbird.Client
|
||||
client client.Client
|
||||
client client.Client
|
||||
}
|
||||
|
||||
var _ webhook.CustomValidator = &NBResourceCustomValidator{}
|
||||
@@ -58,6 +57,9 @@ func (v *NBResourceCustomValidator) ValidateDelete(ctx context.Context, obj runt
|
||||
|
||||
var svc corev1.Service
|
||||
err := v.client.Get(ctx, types.NamespacedName{Namespace: nbresource.Namespace, Name: nbresource.Name}, &svc)
|
||||
if errors.IsNotFound(err) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -3,9 +3,13 @@ package v1
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
// TODO (user): Add any additional imports if needed
|
||||
)
|
||||
|
||||
var _ = Describe("NBResource Webhook", func() {
|
||||
@@ -16,41 +20,103 @@ var _ = Describe("NBResource Webhook", func() {
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
Skip("Not implemented yet")
|
||||
obj = &netbirdiov1.NBResource{}
|
||||
oldObj = &netbirdiov1.NBResource{}
|
||||
validator = NBResourceCustomValidator{}
|
||||
Expect(validator).NotTo(BeNil(), "Expected validator to be initialized")
|
||||
Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized")
|
||||
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
|
||||
// TODO (user): Add any setup logic common to all tests
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO (user): Add any teardown logic common to all tests
|
||||
validator = NBResourceCustomValidator{
|
||||
client: k8sClient,
|
||||
}
|
||||
})
|
||||
|
||||
Context("When creating or updating NBResource under Validating Webhook", func() {
|
||||
// TODO (user): Add logic for validating webhooks
|
||||
// Example:
|
||||
// It("Should deny creation if a required field is missing", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = ""
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).Error().To(HaveOccurred())
|
||||
// })
|
||||
//
|
||||
// It("Should admit creation if all required fields are present", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = "valid_value"
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("Should validate updates correctly", func() {
|
||||
// By("simulating a valid update scenario")
|
||||
// oldObj.SomeRequiredField = "updated_value"
|
||||
// obj.SomeRequiredField = "updated_value"
|
||||
// Expect(validator.ValidateUpdate(ctx, oldObj, obj)).To(BeNil())
|
||||
// })
|
||||
})
|
||||
It("should allow creation", func() {
|
||||
Expect(validator.ValidateCreate(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
It("should allow update", func() {
|
||||
Expect(validator.ValidateUpdate(ctx, oldObj, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
When("No services are exposed", func() {
|
||||
BeforeEach(func() {
|
||||
obj.Name = "maw"
|
||||
obj.Namespace = "default"
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "ne",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Protocol: corev1.ProtocolTCP,
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt32(80),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, svc)).To(Succeed())
|
||||
})
|
||||
AfterEach(func() {
|
||||
svc := &netbirdiov1.NBResource{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "ne"}, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(svc.Finalizers) > 0 {
|
||||
svc.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, svc)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should allow deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("A service is exposed", func() {
|
||||
BeforeEach(func() {
|
||||
obj.Name = "maw"
|
||||
obj.Namespace = "default"
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "maw",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{
|
||||
"netbird.io/expose": "true",
|
||||
},
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Protocol: corev1.ProtocolTCP,
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt32(80),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, svc)).To(Succeed())
|
||||
})
|
||||
AfterEach(func() {
|
||||
svc := &corev1.Service{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "maw"}, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(svc.Finalizers) > 0 {
|
||||
svc.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, svc)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should deny deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
netbird "github.com/netbirdio/netbird/management/client/rest"
|
||||
)
|
||||
|
||||
// nolint:unused
|
||||
@@ -20,17 +19,16 @@ import (
|
||||
var nbroutingpeerlog = logf.Log.WithName("nbroutingpeer-resource")
|
||||
|
||||
// SetupNBRoutingPeerWebhookWithManager registers the webhook for NBRoutingPeer in the manager.
|
||||
func SetupNBRoutingPeerWebhookWithManager(mgr ctrl.Manager, managementURL, apiKey string) error {
|
||||
func SetupNBRoutingPeerWebhookWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewWebhookManagedBy(mgr).For(&netbirdiov1.NBRoutingPeer{}).
|
||||
WithValidator(&NBRoutingPeerCustomValidator{netbird: netbird.New(managementURL, apiKey), client: mgr.GetClient()}).
|
||||
WithValidator(&NBRoutingPeerCustomValidator{client: mgr.GetClient()}).
|
||||
Complete()
|
||||
}
|
||||
|
||||
// NBRoutingPeerCustomValidator struct is responsible for validating the NBRoutingPeer resource
|
||||
// when it is created, updated, or deleted.
|
||||
type NBRoutingPeerCustomValidator struct {
|
||||
netbird *netbird.Client
|
||||
client client.Client
|
||||
client client.Client
|
||||
}
|
||||
|
||||
var _ webhook.CustomValidator = &NBRoutingPeerCustomValidator{}
|
||||
@@ -63,11 +61,13 @@ func (v *NBRoutingPeerCustomValidator) ValidateDelete(ctx context.Context, obj r
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resourceValidator := &NBResourceCustomValidator{client: v.client}
|
||||
|
||||
for _, r := range nbResources.Items {
|
||||
if r.Spec.NetworkID == *nbroutingpeer.Status.NetworkID {
|
||||
err = v.client.Delete(ctx, &r, client.DryRunAll)
|
||||
_, err = resourceValidator.ValidateDelete(ctx, &r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s/%s: %w", r.Namespace, r.Name, err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,14 @@ package v1
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
// TODO (user): Add any additional imports if needed
|
||||
"github.com/netbirdio/kubernetes-operator/internal/util"
|
||||
)
|
||||
|
||||
var _ = Describe("NBRoutingPeer Webhook", func() {
|
||||
@@ -16,41 +21,222 @@ var _ = Describe("NBRoutingPeer Webhook", func() {
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
Skip("Not implemented yet")
|
||||
obj = &netbirdiov1.NBRoutingPeer{}
|
||||
oldObj = &netbirdiov1.NBRoutingPeer{}
|
||||
validator = NBRoutingPeerCustomValidator{}
|
||||
Expect(validator).NotTo(BeNil(), "Expected validator to be initialized")
|
||||
Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized")
|
||||
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
|
||||
// TODO (user): Add any setup logic common to all tests
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO (user): Add any teardown logic common to all tests
|
||||
validator = NBRoutingPeerCustomValidator{
|
||||
client: k8sClient,
|
||||
}
|
||||
})
|
||||
|
||||
Context("When creating or updating NBRoutingPeer under Validating Webhook", func() {
|
||||
// TODO (user): Add logic for validating webhooks
|
||||
// Example:
|
||||
// It("Should deny creation if a required field is missing", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = ""
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).Error().To(HaveOccurred())
|
||||
// })
|
||||
//
|
||||
// It("Should admit creation if all required fields are present", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = "valid_value"
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("Should validate updates correctly", func() {
|
||||
// By("simulating a valid update scenario")
|
||||
// oldObj.SomeRequiredField = "updated_value"
|
||||
// obj.SomeRequiredField = "updated_value"
|
||||
// Expect(validator.ValidateUpdate(ctx, oldObj, obj)).To(BeNil())
|
||||
// })
|
||||
It("should allow creation", func() {
|
||||
Expect(validator.ValidateCreate(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
It("should allow update", func() {
|
||||
Expect(validator.ValidateUpdate(ctx, oldObj, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
When("No NBResources Exist", func() {
|
||||
It("should allow deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("Deleteable NBResources Exist", func() {
|
||||
BeforeEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "isexist",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: netbirdiov1.NBResourceSpec{
|
||||
Name: "test1",
|
||||
NetworkID: "test2",
|
||||
Address: "test3",
|
||||
Groups: []string{"test"},
|
||||
},
|
||||
}
|
||||
|
||||
Expect(k8sClient.Create(ctx, nbResource)).To(Succeed())
|
||||
|
||||
obj = &netbirdiov1.NBRoutingPeer{
|
||||
Status: netbirdiov1.NBRoutingPeerStatus{
|
||||
NetworkID: util.Ptr("test2"),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "isexist"}, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(nbResource.Finalizers) > 0 {
|
||||
nbResource.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, nbResource)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should allow deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("Exposed Services for Network Exist", func() {
|
||||
BeforeEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "maw",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: netbirdiov1.NBResourceSpec{
|
||||
Name: "test1",
|
||||
NetworkID: "test2",
|
||||
Address: "test3",
|
||||
Groups: []string{"test"},
|
||||
},
|
||||
}
|
||||
|
||||
Expect(k8sClient.Create(ctx, nbResource)).To(Succeed())
|
||||
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "maw",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{
|
||||
"netbird.io/expose": "true",
|
||||
},
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Protocol: corev1.ProtocolTCP,
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt32(80),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, svc)).To(Succeed())
|
||||
|
||||
obj = &netbirdiov1.NBRoutingPeer{
|
||||
Status: netbirdiov1.NBRoutingPeerStatus{
|
||||
NetworkID: util.Ptr("test2"),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "maw"}, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(nbResource.Finalizers) > 0 {
|
||||
nbResource.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, nbResource)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
|
||||
svc := &corev1.Service{}
|
||||
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "maw"}, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(svc.Finalizers) > 0 {
|
||||
svc.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, svc)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should deny deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("Exposed NBResources do not belong to network", func() {
|
||||
BeforeEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "maw",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: netbirdiov1.NBResourceSpec{
|
||||
Name: "test1",
|
||||
NetworkID: "test5",
|
||||
Address: "test3",
|
||||
Groups: []string{"test"},
|
||||
},
|
||||
}
|
||||
|
||||
Expect(k8sClient.Create(ctx, nbResource)).To(Succeed())
|
||||
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "maw",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{
|
||||
"netbird.io/expose": "true",
|
||||
},
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Protocol: corev1.ProtocolTCP,
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt32(80),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, svc)).To(Succeed())
|
||||
|
||||
obj = &netbirdiov1.NBRoutingPeer{
|
||||
Status: netbirdiov1.NBRoutingPeerStatus{
|
||||
NetworkID: util.Ptr("test2"),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
nbResource := &netbirdiov1.NBResource{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "maw"}, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(nbResource.Finalizers) > 0 {
|
||||
nbResource.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, nbResource)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, nbResource)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
|
||||
svc := &corev1.Service{}
|
||||
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "maw"}, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(svc.Finalizers) > 0 {
|
||||
svc.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, svc)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should allow deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
@@ -30,11 +31,6 @@ var _ = Describe("NBSetupKey Webhook", func() {
|
||||
validator = NBSetupKeyCustomValidator{
|
||||
client: k8sClient,
|
||||
}
|
||||
Expect(validator).NotTo(BeNil(), "Expected validator to be initialized")
|
||||
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
})
|
||||
|
||||
Context("When creating or updating NBSetupKey under Validating Webhook", func() {
|
||||
@@ -119,6 +115,96 @@ var _ = Describe("NBSetupKey Webhook", func() {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Context("Delete", func() {
|
||||
When("No pods exist with annotation", func() {
|
||||
BeforeEach(func() {
|
||||
pod := &corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "notannotated",
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{Name: "test", Image: "test"},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, pod)).To(Succeed())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
pod := &corev1.Pod{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "notannotated"}, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(pod.Finalizers) > 0 {
|
||||
pod.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, pod)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("should allow delete", func() {
|
||||
obj = &netbirdiov1.NBSetupKey{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("Pods exist with annotation", func() {
|
||||
BeforeEach(func() {
|
||||
pod := &corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "annotated",
|
||||
Annotations: map[string]string{
|
||||
setupKeyAnnotation: "test",
|
||||
},
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{Name: "test", Image: "test"},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, pod)).To(Succeed())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
pod := &corev1.Pod{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "annotated"}, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(pod.Finalizers) > 0 {
|
||||
pod.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, pod)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("should deny delete", func() {
|
||||
obj = &netbirdiov1.NBSetupKey{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -124,13 +124,13 @@ var _ = BeforeSuite(func() {
|
||||
err = SetupNBSetupKeyWebhookWithManager(mgr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = SetupNBResourceWebhookWithManager(mgr, "", "")
|
||||
err = SetupNBResourceWebhookWithManager(mgr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = SetupNBRoutingPeerWebhookWithManager(mgr, "", "")
|
||||
err = SetupNBRoutingPeerWebhookWithManager(mgr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = SetupNBGroupWebhookWithManager(mgr, "", "")
|
||||
err = SetupNBGroupWebhookWithManager(mgr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// +kubebuilder:scaffold:webhook
|
||||
|
||||
Reference in New Issue
Block a user