-
Notifications
You must be signed in to change notification settings - Fork 2
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
ormergi
wants to merge
8
commits into
AlonaKaplan:main
Choose a base branch
from
ormergi:e2e-nad-lifecycle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3094d71
rbac, net-attach-def: Add missing APIVersion
ormergi b6c5ab5
go modules: Go get network-attachment-definition-client
ormergi a796172
controller, client: Add NetworkAttachmetDefinition to scheme
ormergi 994c678
controller, reconciler: Create NetworkAttachmentDefinition
ormergi 5e6a2f6
controller, nad: Add basic CNI network config for overlay network
ormergi 9cb43b9
tests,e2e: Bootstrap ginkgo e2e tests
ormergi 71f3422
tests, e2e: Bootstrap test suite
ormergi cbc99bf
tests, e2e: Test net-attach-def lifecycle
ormergi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
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{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
cluster:
server: https://your-cluster-server
certificate-authority-data:
users:
user:
client-certificate-data:
client-key-data:
contexts:
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
}