Plugin select deployment using replicaset name

This commit is contained in:
Alex Kursell 2019-04-01 17:15:43 -04:00
parent b87cc5a1a6
commit 7c6b2a1566
2 changed files with 21 additions and 1 deletions

View file

@ -253,7 +253,7 @@ func getDeploymentPods(flags *genericclioptions.ConfigFlags, deployment string)
ingressPods := make([]apiv1.Pod, 0) ingressPods := make([]apiv1.Pod, 0)
for _, pod := range pods { for _, pod := range pods {
if pod.Spec.Containers[0].Name == deployment { if util.PodInDeployment(pod, deployment) {
ingressPods = append(ingressPods, pod) ingressPods = append(ingressPods, pod)
} }
} }

View file

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
apiv1 "k8s.io/api/core/v1" apiv1 "k8s.io/api/core/v1"
@ -101,6 +102,25 @@ func isVersionLessThan(a, b string) bool {
return aPatch < bPatch 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 // AddPodFlag adds a --pod flag to a cobra command
func AddPodFlag(cmd *cobra.Command) *string { func AddPodFlag(cmd *cobra.Command) *string {
v := "" v := ""