Skip to content

Commit

Permalink
add integration test for pod with pvc has node-affinity to non-existe…
Browse files Browse the repository at this point in the history
…nt/illegal nodes

Signed-off-by: joey <zchengjoey@gmail.com>
  • Loading branch information
chengjoey committed May 2, 2024
1 parent 82cd82a commit 25ffa37
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/scheduler/testing/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,20 @@ func (p *PersistentVolumeWrapper) HostPathVolumeSource(src *v1.HostPathVolumeSou
return p
}

// NodeAffinityIn creates a HARD node affinity (with the operator In)
// // and injects into the pv.
func (p *PersistentVolumeWrapper) NodeAffinityIn(key string, vals []string) *PersistentVolumeWrapper {
if p.Spec.NodeAffinity == nil {
p.Spec.NodeAffinity = &v1.VolumeNodeAffinity{}
}
if p.Spec.NodeAffinity.Required == nil {
p.Spec.NodeAffinity.Required = &v1.NodeSelector{}
}
nodeSelector := MakeNodeSelector().In(key, vals).Obj()
p.Spec.NodeAffinity.Required.NodeSelectorTerms = append(p.Spec.NodeAffinity.Required.NodeSelectorTerms, nodeSelector.NodeSelectorTerms...)
return p
}

// ResourceClaimWrapper wraps a ResourceClaim inside.
type ResourceClaimWrapper struct{ resourcev1alpha2.ResourceClaim }

Expand Down
48 changes: 48 additions & 0 deletions test/integration/scheduler/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -2036,6 +2037,53 @@ func TestUnschedulablePodBecomesSchedulable(t *testing.T) {
return deletePod(cs, "pod-to-be-deleted", ns)
},
},
{
name: "pod with pvc has node-affinity to non-existent/illegal nodes",
init: func(cs kubernetes.Interface, ns string) error {
storage := v1.VolumeResourceRequirements{Requests: v1.ResourceList{v1.ResourceStorage: resource.MustParse("1Mi")}}
volType := v1.HostPathDirectoryOrCreate
pv, err := testutils.CreatePV(cs, st.MakePersistentVolume().
Name("pv-has-non-existent-nodes").
AccessModes([]v1.PersistentVolumeAccessMode{v1.ReadWriteOncePod}).
Capacity(storage.Requests).
HostPathVolumeSource(&v1.HostPathVolumeSource{Path: "/tmp", Type: &volType}).
NodeAffinityIn("kubernetes.io/hostname", []string{"node-available", string(uuid.NewUUID()), string(uuid.NewUUID())}). // one node exist, two don't
Obj())
if err != nil {
return fmt.Errorf("cannot create pv: %w", err)
}
_, err = testutils.CreatePVC(cs, st.MakePersistentVolumeClaim().
Name("pvc-has-non-existent-nodes").
Namespace(ns).
Annotation(volume.AnnBindCompleted, "true").
VolumeName(pv.Name).
AccessModes([]v1.PersistentVolumeAccessMode{v1.ReadWriteOncePod}).
Resources(storage).
Obj())
if err != nil {
return fmt.Errorf("cannot create pvc: %w", err)
}
return nil
},
pod: &testutils.PausePodConfig{
Name: "pod-with-pvc-has-non-existent-nodes",
Volumes: []v1.Volume{{
Name: "volume",
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: "pvc-has-non-existent-nodes",
},
},
}},
},
update: func(cs kubernetes.Interface, ns string) error {
_, err := createNode(cs, st.MakeNode().Label("kubernetes.io/hostname", "node-available").Name("node-available").Obj())
if err != nil {
return fmt.Errorf("cannot create node: %w", err)
}
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 25ffa37

Please sign in to comment.