From 7c6b2a15662328b2f28da5c0372eb0b10f079041 Mon Sep 17 00:00:00 2001 From: Alex Kursell Date: Mon, 1 Apr 2019 17:15:43 -0400 Subject: [PATCH] Plugin select deployment using replicaset name --- cmd/plugin/request/request.go | 2 +- cmd/plugin/util/util.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/plugin/request/request.go b/cmd/plugin/request/request.go index 1e4938479..61e2a5b22 100644 --- a/cmd/plugin/request/request.go +++ b/cmd/plugin/request/request.go @@ -253,7 +253,7 @@ func getDeploymentPods(flags *genericclioptions.ConfigFlags, deployment string) ingressPods := make([]apiv1.Pod, 0) for _, pod := range pods { - if pod.Spec.Containers[0].Name == deployment { + if util.PodInDeployment(pod, deployment) { ingressPods = append(ingressPods, pod) } } diff --git a/cmd/plugin/util/util.go b/cmd/plugin/util/util.go index 913eeeb38..1832fca01 100644 --- a/cmd/plugin/util/util.go +++ b/cmd/plugin/util/util.go @@ -20,6 +20,7 @@ import ( "fmt" "regexp" "strconv" + "strings" "github.com/spf13/cobra" apiv1 "k8s.io/api/core/v1" @@ -101,6 +102,25 @@ func isVersionLessThan(a, b string) bool { return aPatch < bPatch } +// PodInDeployment returns whether a pod is part of a deployment with the given name +// a pod is considered to be in {deployment} if it is owned by a replicaset with a name of format {deployment}-otherchars +func PodInDeployment(pod apiv1.Pod, deployment string) bool { + for _, owner := range pod.OwnerReferences { + if owner.Controller == nil || !*owner.Controller || owner.Kind != "ReplicaSet" { + continue + } + + if strings.Count(owner.Name, "-") != strings.Count(deployment, "-")+1 { + continue + } + + if strings.HasPrefix(owner.Name, deployment+"-") { + return true + } + } + return false +} + // AddPodFlag adds a --pod flag to a cobra command func AddPodFlag(cmd *cobra.Command) *string { v := ""