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

Conversation

ormergi
Copy link
Contributor

@ormergi ormergi commented Mar 10, 2024

Rebased on (#13, first five commits)

This PR bootstrap Ginkgo e2e test suite and tests the OverlayNetwork corresponding NetworkAttachmentDefinition lifecycle; where the controller create/delete NAD when OverlayNetwork is created/delteted.

The test suite create Namspace "overlay-network-tests" for tests and deletes it when the test suite finish.

How to run tests:
make functests

The imported version is v1.4.0 to complay with k8s api dependencies.

Signed-off-by: Or Mergi <[email protected]>
On update/create of OverlayNetwork, create corresponding
NetworkAttachmentDefinition.

The NetworkAttachmentDefinition is created with OwnerReference [1] pointing to
the reconciled object.
Once the reconciled object is deleted the corresponding net-attach-def object
is deleted as well by the cluster garbage collector.

This change does not set the net-attach-def spec.config, it will be implemented
in follow up commit.

Do not fail when the reconciled OverlayNetwork object is it not found as it may
got deleted just before getting into the reconcile loop.

[1] https://sdk.operatorframework.io/docs/building-operators/ansible/reference/retroactively-owned-resources/

Signed-off-by: Or Mergi <[email protected]>
@ormergi ormergi changed the title E2e nad lifecycle tests: Add E2E test for net-attach-def lifecycle Mar 10, 2024
Setup cluster API client for tests.
Runtime client is necessary to enable creating CRDs (e.g.: OverlayNetwork, NAD).

The test suite create a Kubernetes Namespace "overlay-network-tests" for the tests
and deleted when the test suite finish.

Signed-off-by: Or Mergi <[email protected]>
Test creation and deletion of the OverlayNetwork correspoinding
net-attach-def object according to OverlayNetwork state.

Signed-off-by: Or Mergi <[email protected]>
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()})

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants