Skip to content
Draft
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,88 @@
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": nil})
Expect(err).To(HaveOccurred())
})

It("should inject node selector terms when runtimeInfo has fuse node selectors", func() {
plugin, err := NewPlugin(cl, "")
Expect(err).NotTo(HaveOccurred())

runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
Expect(err).NotTo(HaveOccurred())
runtimeInfo.SetFuseNodeSelector(map[string]string{"fluid.io/fuse": "true"})

Check failure on line 103 in pkg/webhook/plugins/requirenodewithfuse/require_node_with_fuse_test.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "fluid.io/fuse" 3 times.

See more on https://sonarcloud.io/project/issues?id=fluid-cloudnative_fluid&issues=AZ1KzDXZHZwaKbLfBuTa&open=AZ1KzDXZHZwaKbLfBuTa&pullRequest=5757

shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": runtimeInfo})
Expect(err).NotTo(HaveOccurred())
Expect(shouldStop).To(BeFalse())
Expect(pod.Spec.Affinity).NotTo(BeNil())
Expect(pod.Spec.Affinity.NodeAffinity).NotTo(BeNil())
terms := pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
Expect(terms).To(HaveLen(1))
Expect(terms[0].MatchExpressions).To(HaveLen(1))
Expect(terms[0].MatchExpressions[0].Key).To(Equal("fluid.io/fuse"))
Expect(terms[0].MatchExpressions[0].Operator).To(Equal(corev1.NodeSelectorOpIn))
Expect(terms[0].MatchExpressions[0].Values).To(ConsistOf("true"))
})

// InjectNodeSelectorTerms appends fuse MatchExpressions only into NodeSelectorTerms[0].
// A pod with pre-existing terms (A) OR (B) becomes (A AND fuse) OR (B) after injection —
// this is the known upstream semantic: term B can still match a node without fuse.
It("should append fuse match expression into the first existing node selector term when pod already has multiple required node affinity terms", func() {
const fuseKey = "fluid.io/fuse"
const termAKey = "zone"
const termBKey = "region"

pod.Spec.Affinity = &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{Key: termAKey, Operator: corev1.NodeSelectorOpIn, Values: []string{"us-east-1a"}},
},
},
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{Key: termBKey, Operator: corev1.NodeSelectorOpIn, Values: []string{"us-east-1"}},
},
},
},
},
},
}

plugin, err := NewPlugin(cl, "")
Expect(err).NotTo(HaveOccurred())

runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
Expect(err).NotTo(HaveOccurred())
runtimeInfo.SetFuseNodeSelector(map[string]string{fuseKey: "true"})

shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": runtimeInfo})
Expect(err).NotTo(HaveOccurred())
Expect(shouldStop).To(BeFalse())

terms := pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
// The two original terms must be preserved; no new term is added.
Expect(terms).To(HaveLen(2))
// Term[0] gains the fuse expression appended alongside the original zone expression.
Expect(terms[0].MatchExpressions).To(HaveLen(2))
Expect(terms[0].MatchExpressions).To(ContainElement(corev1.NodeSelectorRequirement{
Key: termAKey,
Operator: corev1.NodeSelectorOpIn,
Values: []string{"us-east-1a"},
}))
Expect(terms[0].MatchExpressions).To(ContainElement(corev1.NodeSelectorRequirement{
Key: fuseKey,
Operator: corev1.NodeSelectorOpIn,
Values: []string{"true"},
}))
// Term[1] is left unmodified — it does NOT receive the fuse expression.
Expect(terms[1].MatchExpressions).To(HaveLen(1))
Expect(terms[1].MatchExpressions[0]).To(Equal(corev1.NodeSelectorRequirement{
Key: termBKey,
Operator: corev1.NodeSelectorOpIn,
Values: []string{"us-east-1"},
}))
})
})
})
Loading