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

chore: Add unit tests for nv-ipam-cni objects rendering and sync #995

Merged
merged 1 commit into from
Jul 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
k8s.io/api v0.29.7
k8s.io/apimachinery v0.29.7
k8s.io/client-go v0.29.7
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.17.5
sigs.k8s.io/yaml v1.4.0
)
Expand Down Expand Up @@ -117,7 +118,6 @@ require (
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e // indirect
k8s.io/kubectl v0.29.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.15.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.15.0 // indirect
Expand Down
109 changes: 90 additions & 19 deletions pkg/state/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import (

mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1"
clustertype_mocks "github.com/Mellanox/network-operator/pkg/clustertype/mocks"
"github.com/Mellanox/network-operator/pkg/consts"
"github.com/Mellanox/network-operator/pkg/state"
"github.com/Mellanox/network-operator/pkg/staticconfig"
staticconfig_mocks "github.com/Mellanox/network-operator/pkg/staticconfig/mocks"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -43,6 +45,9 @@ import (

const (
hostDeviceNetworkResourceNamePrefix = "nvidia.com/"
defaultTestRepository = "myRepo"
defaultTestImage = "myImage"
defaultTestVersion = "myVersion"
)

func getTestCatalog() state.InfoCatalog {
Expand Down Expand Up @@ -120,42 +125,52 @@ func assertCommonPodTemplateFields(template *corev1.PodTemplateSpec, image *mell
// Container Resources
Expect(template.Spec.Containers[0].Resources.Limits).To(Equal(image.ContainerResources[0].Limits))
Expect(template.Spec.Containers[0].Resources.Requests).To(Equal(image.ContainerResources[0].Requests))

Expect(template.Spec.Tolerations).To(ContainElements(
corev1.Toleration{
Key: "nvidia.com/gpu",
Operator: "Exists",
Value: "",
Effect: "NoSchedule",
TolerationSeconds: nil,
},
))
}

func assertCommonDeploymentFields(u *unstructured.Unstructured, image *mellanoxv1alpha1.ImageSpec) {
func assertCommonDeploymentFieldsFromUnstructured(u *unstructured.Unstructured, image *mellanoxv1alpha1.ImageSpec) {
d := &appsv1.Deployment{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), d)
Expect(err).ToNot(HaveOccurred())
assertCommonDeploymentFields(d, image)
}

func assertCommonDeploymentFields(d *appsv1.Deployment, image *mellanoxv1alpha1.ImageSpec) {
assertCommonPodTemplateFields(&d.Spec.Template, image)
}

func assertCommonDaemonSetFields(u *unstructured.Unstructured,
func assertCommonDaemonSetFieldsFromUnstructured(u *unstructured.Unstructured,
image *mellanoxv1alpha1.ImageSpec, policy *mellanoxv1alpha1.NicClusterPolicy) {
ds := &appsv1.DaemonSet{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), ds)
Expect(err).ToNot(HaveOccurred())
assertCommonDaemonSetFields(ds, image, policy)
}

func assertCommonDaemonSetFields(ds *appsv1.DaemonSet, image *mellanoxv1alpha1.ImageSpec,
policy *mellanoxv1alpha1.NicClusterPolicy) {
assertCommonPodTemplateFields(&ds.Spec.Template, image)
// Tolerations

Expect(ds.Spec.Template.Spec.Tolerations).To(ContainElements(
corev1.Toleration{Key: "first-taint"},
corev1.Toleration{
Key: "nvidia.com/gpu",
Operator: "Exists",
Value: "",
Effect: "NoSchedule",
TolerationSeconds: nil,
},
))

// NodeAffinity
Expect(ds.Spec.Template.Spec.Affinity.NodeAffinity).To(Equal(policy.Spec.NodeAffinity))
}

func getTestImageSpec() *mellanoxv1alpha1.ImageSpec {
return &mellanoxv1alpha1.ImageSpec{
Image: "image-one",
Repository: "repository",
Version: "five",
Image: defaultTestImage,
Repository: defaultTestRepository,
Version: defaultTestVersion,
ImagePullSecrets: []string{"secret-one", "secret-two"},
}
}
Expand All @@ -180,10 +195,14 @@ func isNamespaced(obj *unstructured.Unstructured) bool {
obj.GetKind() != "ValidatingWebhookConfiguration"
}

func assertCNIBinDirForDS(u *unstructured.Unstructured) {
func assertCNIBinDirForDSFromUnstructured(u *unstructured.Unstructured) {
ds := &appsv1.DaemonSet{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), ds)
Expect(err).ToNot(HaveOccurred())
assertCNIBinDirForDS(ds)
}

func assertCNIBinDirForDS(ds *appsv1.DaemonSet) {
for i := range ds.Spec.Template.Spec.Volumes {
vol := ds.Spec.Template.Spec.Volumes[i]
if vol.Name == "cnibin" {
Expand Down Expand Up @@ -222,10 +241,10 @@ func GetManifestObjectsTest(ctx context.Context, cr *mellanoxv1alpha1.NicCluster
}
switch got[i].GetKind() {
case "DaemonSet":
assertCommonDaemonSetFields(got[i], imageSpec, cr)
assertCNIBinDirForDS(got[i])
assertCommonDaemonSetFieldsFromUnstructured(got[i], imageSpec, cr)
assertCNIBinDirForDSFromUnstructured(got[i])
case "Deployment":
assertCommonDeploymentFields(got[i], imageSpec)
assertCommonDeploymentFieldsFromUnstructured(got[i], imageSpec)
}
}
}
Expand All @@ -252,3 +271,55 @@ func getTestClusterPolicyWithBaseFields() *mellanoxv1alpha1.NicClusterPolicy {
},
}
}

func getKindState(ctx context.Context, c client.Client, objs []*unstructured.Unstructured,
targetKind string) (state.SyncState, error) {
reqLogger := log.FromContext(ctx)
reqLogger.V(consts.LogLevelInfo).Info("Checking related object states")
for _, obj := range objs {
if obj.GetKind() != targetKind {
continue
}
found := obj.DeepCopy()
err := c.Get(
ctx, types.NamespacedName{Name: found.GetName(), Namespace: found.GetNamespace()}, found)
if err != nil {
if k8serrors.IsNotFound(err) {
return state.SyncStateNotReady, nil
}
return state.SyncStateNotReady, fmt.Errorf("failed to get object: %w", err)
}

buf, err := found.MarshalJSON()
if err != nil {
return state.SyncStateNotReady, fmt.Errorf("failed to marshall unstructured daemonset object: %w", err)
}

switch obj.GetKind() {
case "DaemonSet":
ds := &appsv1.DaemonSet{}
if err = json.Unmarshal(buf, ds); err != nil {
return state.SyncStateNotReady, fmt.Errorf("failed to unmarshall to daemonset object: %w", err)
}
if ds.Status.DesiredNumberScheduled != 0 && ds.Status.DesiredNumberScheduled == ds.Status.NumberAvailable &&
ds.Status.UpdatedNumberScheduled == ds.Status.NumberAvailable {
return state.SyncStateReady, nil
}
return state.SyncStateNotReady, nil
case "Deployment":
d := &appsv1.Deployment{}
if err = json.Unmarshal(buf, d); err != nil {
return state.SyncStateNotReady, fmt.Errorf("failed to unmarshall to deployment object: %w", err)
}

if d.Status.ObservedGeneration > 0 && d.Status.Replicas == d.Status.ReadyReplicas &&
d.Status.UpdatedReplicas == d.Status.AvailableReplicas {
return state.SyncStateReady, nil
}
return state.SyncStateNotReady, nil
default:
return state.SyncStateNotReady, fmt.Errorf("unsupported target kind")
}
}
return state.SyncStateNotReady, fmt.Errorf("objects list does not contain the specified target kind")
}
Loading
Loading