Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Add E2E test for net-attach-def lifecycle #14

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,7 @@ kind-push:

.PHONY: cluster-sync
cluster-sync: undeploy uninstall manifests generate fmt vet docker-build kind-push install deploy

.PHONY: functest
functest:
go test -v ./e2e/...
1 change: 1 addition & 0 deletions config/rbac/net_attach_def_editor_role.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: net-attach-def-editor
Expand Down
70 changes: 68 additions & 2 deletions controllers/overlaynetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ package controllers

import (
"context"
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"

selfservicev1 "github.com/AlonaKaplan/selfserviceoverlay/api/v1"
)
Expand All @@ -47,9 +55,28 @@ type OverlayNetworkReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *OverlayNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
logger := log.FromContext(ctx).WithValues("overlaynetwork", req.NamespacedName)

logger.Info("Reconciling OverlayNetwork")

overlayNetwork := &selfservicev1.OverlayNetwork{}
if err := r.Client.Get(ctx, req.NamespacedName, overlayNetwork); err != nil {
if errors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed to get OverlayNetwrok %q: %v", req.NamespacedName, err)
}

// TODO(user): your logic here
netAttachDef, err := renderNetAttachDef(overlayNetwork)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to render NetworkAttachmentDefinition for OverlayNetwrok %q: %v", req.NamespacedName, err)
}
if err := r.Create(ctx, netAttachDef); err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("NetworkAttachmentDefinition [%q] already exist", req.NamespacedName)
}
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}
Expand All @@ -60,3 +87,42 @@ func (r *OverlayNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&selfservicev1.OverlayNetwork{}).
Complete(r)
}

func renderNetAttachDef(overlayNet *selfservicev1.OverlayNetwork) (*netv1.NetworkAttachmentDefinition, error) {
const netAttachDefKind = "NetworkAttachmentDefinition"
const netAttachDefAPIVer = "v1"

cniNetConf := map[string]interface{}{
"type": "ovn-k8s-cni-overlay",
"name": overlayNet.GetName(),
"netAttachDefName": overlayNet.Namespace + "/" + overlayNet.Name,
"topology": "layer2",
}
cniNetConfRaw, err := json.Marshal(cniNetConf)
if err != nil {
return nil, err
}

return &netv1.NetworkAttachmentDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: netAttachDefAPIVer,
Kind: netAttachDefKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: overlayNet.Name,
Namespace: overlayNet.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: overlayNet.APIVersion,
Kind: overlayNet.Kind,
Name: overlayNet.Name,
UID: overlayNet.UID,
BlockOwnerDeletion: nil,
},
},
},
Spec: netv1.NetworkAttachmentDefinitionSpec{
Config: string(cniNetConfRaw),
},
}, nil
}
81 changes: 81 additions & 0 deletions e2e/net-attach-def_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package e2e

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"

selfservicev1 "github.com/AlonaKaplan/selfserviceoverlay/api/v1"
)

const overlayNetworkKind = "OverlayNetwork"

var _ = Describe("OverlayNetwork controller, net-attach-def lifecycle", func() {
It("should create and delete net-attach-def according to OverlayNetwork state", func() {
By("Create test OverlayNetwork instance")
ovrlyNet := newTestOverlayNetwork(TestsNamespace, "test")
Expect(RuntimeClient.Create(context.Background(), ovrlyNet)).To(Succeed())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run the tests a project admin and not cluster-admin.

Chatgpt example of how it should be done-
Create a kubeconfig file for the project admin. Save it as kubeconfig-admin.yaml:
yaml
Copy code
apiVersion: v1
kind: Config
clusters:

  • name: your-cluster-name
    cluster:
    server: https://your-cluster-server
    certificate-authority-data:
    users:
  • name: project-admin
    user:
    client-certificate-data:
    client-key-data:
    contexts:
  • name: project-admin-context
    context:
    cluster: your-cluster-name
    user: project-admin
    current-context: project-admin-context
    Replace placeholders like your-cluster-name, https://your-cluster-server, , , and with the actual values.

Update your test code to use this custom kubeconfig file:
go
Copy code
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
)

func getClientForProjectAdmin() (kubernetes.Interface, error) {
adminKubeconfigPath := "/path/to/kubeconfig-admin.yaml" // Update with the correct path

// Use the in-cluster config if running within a cluster, otherwise use the specified kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", adminKubeconfigPath)
if err != nil {
	return nil, err
}

mgr, err := ctrl.NewManager(config, ctrlOptions)
if err != nil {
	return nil, err
}

return client.New(mgr.GetConfig(), client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()})

}


By("Assert a corresponding net-attach-def has been created")
nad := &netv1.NetworkAttachmentDefinition{}
nadKey := types.NamespacedName{Namespace: ovrlyNet.Namespace, Name: ovrlyNet.Name}
Eventually(func() error {
return RuntimeClient.Get(context.Background(), nadKey, nad)
}).Should(Succeed(), "1m", "3s")

By("Assert overlay-network corresponding net-attach-def object")
assertOverlayNetworkNetAttachDef(ovrlyNet, nad)

By("Delete test OverlayNetwork instance")
Expect(RuntimeClient.Delete(context.Background(), ovrlyNet)).To(Succeed())

By("Assert the corresponding net-attach-def is deleted by and not exist")
Eventually(func() bool {
return errors.IsNotFound(RuntimeClient.Get(context.Background(), nadKey, nad))
}).Should(BeTrue(), "1m", "3s",
"the overlay-network corresponding net-attach-def should be disposed")
})
})

func assertOverlayNetworkNetAttachDef(overlyNet *selfservicev1.OverlayNetwork, nad *netv1.NetworkAttachmentDefinition) {
expectedNetAttachName := overlyNet.Namespace + "/" + overlyNet.Name
expectedCniNetConf := fmt.Sprintf(`{
"name":"test",
"type":"ovn-k8s-cni-overlay",
"netAttachDefName": "%s",
"topology":"layer2"
}`, expectedNetAttachName)

Expect(nad.Spec.Config).To(MatchJSON(expectedCniNetConf))

expectedOwnerReference := metav1.OwnerReference{
APIVersion: selfservicev1.GroupVersion.String(), // "self.service.ovn.org/netv1",
Kind: overlayNetworkKind,
Name: overlyNet.Name,
UID: overlyNet.UID,
}

Expect(nad.ObjectMeta.OwnerReferences).To(Equal([]metav1.OwnerReference{expectedOwnerReference}))
}

func newTestOverlayNetwork(namespace, name string) *selfservicev1.OverlayNetwork {
return &selfservicev1.OverlayNetwork{
TypeMeta: metav1.TypeMeta{
Kind: overlayNetworkKind,
APIVersion: selfservicev1.GroupVersion.Version,
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: selfservicev1.OverlayNetworkSpec{},
}
}
71 changes: 71 additions & 0 deletions e2e/selfserviceoverlay_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package e2e

import (
"context"
"testing"

. "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/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"

selfservicev1 "github.com/AlonaKaplan/selfserviceoverlay/api/v1"
)

const TestsNamespace = "overlay-network-tests"

var (
KubeClient *kubernetes.Clientset
RuntimeClient client.Client
)

func TestSelfserviceoverlay(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Selfserviceoverlay Suite")
}

var _ = BeforeSuite(func() {
cfg, err := config.GetConfig()
Expect(err).ToNot(HaveOccurred(), "failed to get configuration for existing cluster")
Expect(cfg).ToNot(BeNil())

err = selfservicev1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = netv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

KubeClient, err = kubernetes.NewForConfig(cfg)
Expect(KubeClient).ToNot(BeNil())

RuntimeClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(RuntimeClient).ToNot(BeNil())

By("create tests namespace")
testNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: TestsNamespace}}
_, err = KubeClient.CoreV1().Namespaces().Create(context.Background(), testNamespace, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
})

var _ = AfterSuite(func() {
By("delete tests namespace")
err := KubeClient.CoreV1().Namespaces().Delete(context.Background(), TestsNamespace, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

By("wait for tests namespace to dispose")
Eventually(func() bool {
_, err := KubeClient.CoreV1().Namespaces().Get(context.Background(), TestsNamespace, metav1.GetOptions{})
return errors.IsNotFound(err)
}, "30s", "1s").Should(BeTrue(), "tests namespace should be disposed")
})
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/AlonaKaplan/selfserviceoverlay
go 1.19

require (
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
k8s.io/apimachinery v0.25.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 h1:VzM3TYHDgqPkettiP6I6q2jOeQFL4nrJM+UcAc4f6Fs=
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0/go.mod h1:nqCI7aelBJU61wiBeeZWJ6oi4bJy5nrjkM6lWIMA4j0=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
selfservicev1 "github.com/AlonaKaplan/selfserviceoverlay/api/v1"
"github.com/AlonaKaplan/selfserviceoverlay/controllers"
//+kubebuilder:scaffold:imports

netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned/scheme"
)

var (
Expand All @@ -45,6 +47,8 @@ func init() {

utilruntime.Must(selfservicev1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme

utilruntime.Must(netv1.AddToScheme(scheme))
}

func main() {
Expand Down