Add label selector for plugin
This commit is contained in:
parent
010563df2e
commit
b096bf9ad9
9 changed files with 86 additions and 33 deletions
|
|
@ -32,11 +32,15 @@ import (
|
|||
)
|
||||
|
||||
// ChoosePod finds a pod either by deployment or by name
|
||||
func ChoosePod(flags *genericclioptions.ConfigFlags, podName string, deployment string) (apiv1.Pod, error) {
|
||||
func ChoosePod(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string) (apiv1.Pod, error) {
|
||||
if podName != "" {
|
||||
return GetNamedPod(flags, podName)
|
||||
}
|
||||
|
||||
if selector != "" {
|
||||
return GetLabeledPod(flags, selector)
|
||||
}
|
||||
|
||||
return GetDeploymentPod(flags, deployment)
|
||||
}
|
||||
|
||||
|
|
@ -70,6 +74,20 @@ func GetDeploymentPod(flags *genericclioptions.ConfigFlags, deployment string) (
|
|||
return ings[0], nil
|
||||
}
|
||||
|
||||
// GetDeploymentPod finds a pod from a given deployment
|
||||
func GetLabeledPod(flags *genericclioptions.ConfigFlags, label string) (apiv1.Pod, error) {
|
||||
ings, err := getLabeledPods(flags, label)
|
||||
if err != nil {
|
||||
return apiv1.Pod{}, err
|
||||
}
|
||||
|
||||
if len(ings) == 0 {
|
||||
return apiv1.Pod{}, fmt.Errorf("no pods for label selector %v found in namespace %v", label, util.GetNamespace(flags))
|
||||
}
|
||||
|
||||
return ings[0], nil
|
||||
}
|
||||
|
||||
// GetDeployments returns an array of Deployments
|
||||
func GetDeployments(flags *genericclioptions.ConfigFlags, namespace string) ([]appsv1.Deployment, error) {
|
||||
rawConfig, err := flags.ToRESTConfig()
|
||||
|
|
@ -246,6 +264,30 @@ func getPods(flags *genericclioptions.ConfigFlags) ([]apiv1.Pod, error) {
|
|||
return pods.Items, nil
|
||||
}
|
||||
|
||||
func getLabeledPods(flags *genericclioptions.ConfigFlags, label string) ([]apiv1.Pod, error) {
|
||||
namespace := util.GetNamespace(flags)
|
||||
|
||||
rawConfig, err := flags.ToRESTConfig()
|
||||
if err != nil {
|
||||
return make([]apiv1.Pod, 0), err
|
||||
}
|
||||
|
||||
api, err := corev1.NewForConfig(rawConfig)
|
||||
if err != nil {
|
||||
return make([]apiv1.Pod, 0), err
|
||||
}
|
||||
|
||||
pods, err := api.Pods(namespace).List(metav1.ListOptions{
|
||||
LabelSelector: label,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return make([]apiv1.Pod, 0), err
|
||||
}
|
||||
|
||||
return pods.Items, nil
|
||||
}
|
||||
|
||||
func getDeploymentPods(flags *genericclioptions.ConfigFlags, deployment string) ([]apiv1.Pod, error) {
|
||||
pods, err := getPods(flags)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue