diff --git a/pkg/kube/utils.go b/pkg/kube/utils.go index 40eba84db0..6b3b371d8c 100644 --- a/pkg/kube/utils.go +++ b/pkg/kube/utils.go @@ -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" @@ -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 +} diff --git a/pkg/kube/workload.go b/pkg/kube/workload.go index eaf054b3ec..4d6a2d2923 100644 --- a/pkg/kube/workload.go +++ b/pkg/kube/workload.go @@ -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 } @@ -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 @@ -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 {