Skip to content

Commit

Permalink
Merge pull request #443 from DataDog/fricounet/upstream/fix-pv-deleti…
Browse files Browse the repository at this point in the history
…on-node

Fix: [node-cleanup] Check node label to know if pv is linked to a deleted node
  • Loading branch information
k8s-ci-robot authored Aug 9, 2024
2 parents 9d34194 + 09efb94 commit b6f4650
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
14 changes: 13 additions & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
Expand Down Expand Up @@ -488,10 +490,20 @@ func GetVolumeMode(volUtil util.VolumeUtil, fullPath string) (v1.PersistentVolum
}

// NodeExists checks to see if a Node exists in the Indexer of a NodeLister.
// It tries to get the node and if it fails, it uses the well known label
// `kubernetes.io/hostname` to find the Node.
func NodeExists(nodeLister corelisters.NodeLister, nodeName string) (bool, error) {
_, err := nodeLister.Get(nodeName)
if errors.IsNotFound(err) {
return false, nil
req, err := labels.NewRequirement(NodeLabelKey, selection.Equals, []string{nodeName})
if err != nil {
return false, err
}
nodes, err := nodeLister.List(labels.NewSelector().Add(*req))
if err != nil {
return false, err
}
return len(nodes) > 0, nil
}
return err == nil, err
}
Expand Down
23 changes: 18 additions & 5 deletions pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,17 @@ func TestGetVolumeMode(t *testing.T) {
}

func TestNodeExists(t *testing.T) {
node := &v1.Node{
nodeName := "test-node"
nodeWithName := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test-node",
Name: nodeName,
},
}
nodeWithLabel := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
NodeLabelKey: nodeName,
},
},
}

Expand All @@ -491,12 +499,17 @@ func TestNodeExists(t *testing.T) {
expectedResult bool
}{
{
nodeAdded: node,
nodeQueried: node,
nodeAdded: nodeWithName,
nodeQueried: nodeWithName,
expectedResult: true,
},
{
nodeAdded: nodeWithLabel,
nodeQueried: nodeWithName,
expectedResult: true,
},
{
nodeQueried: node,
nodeQueried: nodeWithName,
expectedResult: false,
},
}
Expand Down

0 comments on commit b6f4650

Please sign in to comment.