Skip to content

Commit

Permalink
Merge pull request #3955 from Shopify/plugin-choose-deployment
Browse files Browse the repository at this point in the history
Plugin select deployment using replicaset name
  • Loading branch information
k8s-ci-robot authored Apr 2, 2019
2 parents b87cc5a + 7c6b2a1 commit d1fbf84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/plugin/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
20 changes: 20 additions & 0 deletions cmd/plugin/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/spf13/cobra"
apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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 := ""
Expand Down

0 comments on commit d1fbf84

Please sign in to comment.