Skip to content

Commit

Permalink
Find a UID match for pods with multiple ownerReferences (#3209)
Browse files Browse the repository at this point in the history
Signed-off-by: Alwyn Kik <[email protected]>
  • Loading branch information
Alveel authored Nov 1, 2024
1 parent f958887 commit 43381b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
13 changes: 13 additions & 0 deletions pkg/kube/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/kanisterio/errkit"
osversioned "github.com/openshift/client-go/apps/clientset/versioned"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg/field"
Expand Down Expand Up @@ -195,3 +197,14 @@ func AddLabelsToPodOptionsFromContext(
}
}
}

// uidInOwnerRefs returns true if one of the ownerReferences of an object matches the
// provided UID.
func uidInOwnerRefs(ownerReferences []metav1.OwnerReference, uid types.UID) bool {
for _, ownerRef := range ownerReferences {
if ownerRef.UID == uid {
return true
}
}
return false
}
17 changes: 3 additions & 14 deletions pkg/kube/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,9 @@ func FetchReplicationController(cli kubernetes.Interface, namespace string, uid
}

for _, rc := range repCtrls.Items {
if len(rc.OwnerReferences) != 1 {
if !uidInOwnerRefs(rc.OwnerReferences, uid) {
continue
}

if rc.OwnerReferences[0].UID != uid {
continue
}

if rc.Annotations[ReplicationControllerRevisionAnnotation] != revision {
continue
}
Expand All @@ -319,12 +314,7 @@ func FetchReplicaSet(cli kubernetes.Interface, namespace string, uid types.UID,
return nil, errkit.Wrap(err, "Could not list ReplicaSets")
}
for _, rs := range rss.Items {
// We ignore ReplicaSets without a single owner.
if len(rs.OwnerReferences) != 1 {
continue
}
// We ignore ReplicaSets owned by other deployments.
if rs.OwnerReferences[0].UID != uid {
if !uidInOwnerRefs(rs.OwnerReferences, uid) {
continue
}
// We ignore older ReplicaSets
Expand All @@ -344,8 +334,7 @@ func FetchPods(cli kubernetes.Interface, namespace string, uid types.UID) (runni
return nil, nil, errkit.Wrap(err, "Could not list Pods")
}
for _, pod := range pods.Items {
if len(pod.OwnerReferences) != 1 ||
pod.OwnerReferences[0].UID != uid {
if !uidInOwnerRefs(pod.OwnerReferences, uid) {
continue
}
if pod.Status.Phase != corev1.PodRunning {
Expand Down

0 comments on commit 43381b8

Please sign in to comment.