Skip to content

Commit

Permalink
add e2e test for annotation cni.spidernet.io/network-resource-inject
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhb-nil committed Dec 25, 2024
1 parent 1746a95 commit 671f99f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 28 deletions.
3 changes: 1 addition & 2 deletions pkg/multuscniconfig/multusconfig_mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func mutateSpiderMultusConfig(ctx context.Context, smc *spiderpoolv2beta1.Spider
}

// inject the labels
annos := []string{constant.AnnoPodResourceInject, constant.AnnoNetworkResourceInject}
for _, v := range annos {
for _, v := range []string{constant.AnnoPodResourceInject, constant.AnnoNetworkResourceInject} {
value, ok := smc.Annotations[v]
if !ok {
continue
Expand Down
3 changes: 1 addition & 2 deletions pkg/podmanager/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func (pw *podWebhook) Default(ctx context.Context, obj runtime.Object) error {
mutateLogger.Sugar().Debugf("Request Pod: %+v", *pod)

needInject := false
annos := []string{constant.AnnoPodResourceInject, constant.AnnoNetworkResourceInject}
for _, anno := range annos {
for _, anno := range []string{constant.AnnoPodResourceInject, constant.AnnoNetworkResourceInject} {
if _, ok := pod.Annotations[anno]; ok {
mutateLogger.Sugar().Debugf("Pod %s/%s is annotated with %s, start injecting network resources", pod.Namespace, pod.GenerateName, anno)
needInject = true
Expand Down
28 changes: 8 additions & 20 deletions pkg/podmanager/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,15 @@ func IsStaticIPPod(enableStatefulSet, enableKubevirtStaticIP bool, pod *corev1.P
// Returns:
// - An error if any step in the process fails, nil otherwise
func podNetworkMutatingWebhook(spiderClient crdclientset.Interface, pod *corev1.Pod) error {
annos := []struct {
key string
checkCNI bool
}{
{constant.AnnoPodResourceInject, true},
{constant.AnnoNetworkResourceInject, false},
}

for _, anno := range annos {
multusLabelValue, ok := pod.Annotations[anno.key]
for _, anno := range []string{constant.AnnoPodResourceInject, constant.AnnoNetworkResourceInject} {
multusLabelValue, ok := pod.Annotations[anno]
if !ok {
continue
}
labelSelector := metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: anno.key,
Key: anno,
Operator: metav1.LabelSelectorOpIn,
Values: []string{multusLabelValue},
},
Expand All @@ -112,12 +104,10 @@ func podNetworkMutatingWebhook(spiderClient crdclientset.Interface, pod *corev1.
}

if len(multusConfigs.Items) == 0 {
return fmt.Errorf("No spidermultusconfigs with annotation: %v:%v found", anno.key, multusLabelValue)
return fmt.Errorf("No spidermultusconfigs with annotation: %v:%v found", anno, multusLabelValue)
}

if err = InjectPodNetwork(pod, *multusConfigs, anno.checkCNI); err != nil {
return err
}
return InjectPodNetwork(pod, *multusConfigs)
}

return nil
Expand All @@ -133,13 +123,11 @@ func podNetworkMutatingWebhook(spiderClient crdclientset.Interface, pod *corev1.
//
// Returns:
// - An error if there's an inconsistency in CNI types, nil otherwise
func InjectPodNetwork(pod *corev1.Pod, multusConfigs v2beta1.SpiderMultusConfigList, checkCNI bool) error {
func InjectPodNetwork(pod *corev1.Pod, multusConfigs v2beta1.SpiderMultusConfigList) error {
resourcesMap := make(map[string]bool, len(multusConfigs.Items))
for _, mc := range multusConfigs.Items {
if checkCNI {
if err := DoValidateRdmaResouce(mc); err != nil {
return err
}
if err := DoValidateRdmaResouce(mc); err != nil {
return err
}

// Update the pod's network attachment
Expand Down
8 changes: 4 additions & 4 deletions pkg/podmanager/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var _ = Describe("PodManager utils", Label("pod_manager_utils_test"), func() {
},
},
}
err := podmanager.InjectPodNetwork(pod, multusConfigs, true)
err := podmanager.InjectPodNetwork(pod, multusConfigs)
Expect(err).NotTo(HaveOccurred())
Expect(pod.Annotations[constant.MultusNetworkAttachmentAnnot]).To(Equal("default/config1,default/config2"))

Expand Down Expand Up @@ -187,7 +187,7 @@ var _ = Describe("PodManager utils", Label("pod_manager_utils_test"), func() {
},
},
}
err := podmanager.InjectPodNetwork(pod, multusConfigs, true)
err := podmanager.InjectPodNetwork(pod, multusConfigs)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("No any ippools configured"))
})
Expand Down Expand Up @@ -229,7 +229,7 @@ var _ = Describe("PodManager utils", Label("pod_manager_utils_test"), func() {
},
},
}
err := podmanager.InjectPodNetwork(pod, multusConfigs, true)
err := podmanager.InjectPodNetwork(pod, multusConfigs)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not enable RDMA"))
})
Expand Down Expand Up @@ -278,7 +278,7 @@ var _ = Describe("PodManager utils", Label("pod_manager_utils_test"), func() {
},
}

err := podmanager.InjectPodNetwork(pod, multusConfigs, true)
err := podmanager.InjectPodNetwork(pod, multusConfigs)
Expect(err).NotTo(HaveOccurred())
Expect(pod.Annotations[constant.MultusNetworkAttachmentAnnot]).To(Equal("default/config1,default/config2"))

Expand Down
2 changes: 2 additions & 0 deletions test/doc/spidermultus.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
| M00028 | return an err if rdma is not enabled when spidermutlus with annotation: cni.spidernet.io/rdma-resource-inject | p3 | | done | |
| M00029 | return an err if no ippools config when spidermutlus with annotation: cni.spidernet.io/rdma-resource-inject | p3 | | done | |
| M00030 | return an err if cniType is not in [macvlan,ipvlan,sriov,ib-sriov,ipoib] when spidermutlus with annotation: cni.spidernet.io/rdma-resource-inject | p3 | | done | |
| M00031 | resoucename and ippools config must be both set when spidermutlus with annotation: cni.spidernet.io/network-resource-inject | p3 | | done | |
| M00032 | return an err if resoucename is set without ippools config when spidermutlus with annotation: cni.spidernet.io/network-resource-inject | p3 | | done | |
59 changes: 59 additions & 0 deletions test/e2e/spidermultus/spidermultus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,65 @@ var _ = Describe("test spidermultus", Label("SpiderMultusConfig"), func() {
Expect(err).To(HaveOccurred())
})

It("resoucename and ippools config must be both set when spidermutlus with annotation: cni.spidernet.io/network-resource-inject", Label("M00031"), func() {
var smcName string = "ann-network-resource" + common.GenerateString(10, true)
smc := &spiderpoolv2beta1.SpiderMultusConfig{
ObjectMeta: metav1.ObjectMeta{
Name: smcName,
Namespace: namespace,
Annotations: map[string]string{
constant.AnnoNetworkResourceInject: "test",
},
},
Spec: spiderpoolv2beta1.MultusCNIConfigSpec{
CniType: ptr.To(constant.MacvlanCNI),
MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{
Master: []string{common.NIC1},
EnableRdma: true,
RdmaResourceName: "test",
SpiderpoolConfigPools: &spiderpoolv2beta1.SpiderpoolPools{
IPv4IPPool: []string{"test"},
},
},
EnableCoordinator: ptr.To(true),
CoordinatorConfig: &spiderpoolv2beta1.CoordinatorSpec{
PodRPFilter: nil,
},
},
}
GinkgoWriter.Printf("spidermultus cr: %+v \n", smc)
err := frame.CreateSpiderMultusInstance(smc)
Expect(err).NotTo(HaveOccurred(), "create spiderMultus instance failed: %v\n", err)
})

It("return an err if resoucename is set without ippools config when spidermutlus with annotation: cni.spidernet.io/network-resource-inject", Label("M00032"), func() {
var smcName string = "ann-network-resource" + common.GenerateString(10, true)
smc := &spiderpoolv2beta1.SpiderMultusConfig{
ObjectMeta: metav1.ObjectMeta{
Name: smcName,
Namespace: namespace,
Annotations: map[string]string{
constant.AnnoNetworkResourceInject: "test",
},
},
Spec: spiderpoolv2beta1.MultusCNIConfigSpec{
CniType: ptr.To(constant.MacvlanCNI),
MacvlanConfig: &spiderpoolv2beta1.SpiderMacvlanCniConfig{
Master: []string{common.NIC1},
EnableRdma: true,
RdmaResourceName: "test",
},
EnableCoordinator: ptr.To(true),
CoordinatorConfig: &spiderpoolv2beta1.CoordinatorSpec{
PodRPFilter: nil,
},
},
}
GinkgoWriter.Printf("spidermultus cr: %+v \n", smc)
err := frame.CreateSpiderMultusInstance(smc)
Expect(err).To(HaveOccurred())
})

It("set disableIPAM to true and see if multus's nad has ipam config", Label("M00017"), func() {
var smcName string = "disable-ipam-multus-" + common.GenerateString(10, true)

Expand Down

0 comments on commit 671f99f

Please sign in to comment.