Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pod prefix #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions pkg/cmd/evict_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/rajatjindal/kubectl-evict-pod/pkg/k8s"
"github.com/sirupsen/logrus"
Expand All @@ -20,11 +21,11 @@ type EvictPodOptions struct {
configFlags *genericclioptions.ConfigFlags
iostreams genericclioptions.IOStreams

args []string
podName string
podNames []string
namespace string
kubeclient kubernetes.Interface
printVersion bool
label string
}

// NewEvictPodOptions provides an instance of EvictPodOptions with default values
Expand Down Expand Up @@ -64,19 +65,22 @@ func NewCmdModifySecret(streams genericclioptions.IOStreams) *cobra.Command {
}

cmd.Flags().BoolVar(&o.printVersion, "version", false, "prints version of plugin")
cmd.Flags().StringVar(&o.label, "label", "", "specify a label to evict pods with")
o.configFlags.AddFlags(cmd.Flags())

return cmd
}

// Complete sets all information required for updating the current context
func (o *EvictPodOptions) Complete(cmd *cobra.Command, args []string) error {
o.args = args

if len(o.args) > 0 {
o.podName = o.args[0]
if len(args) == 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apologies for super delay in reviewing this PR. if only label is provided, the len(args) can be 0 right?

cmd.Help()
os.Exit(0)
}

o.podNames = args

config, err := o.configFlags.ToRESTConfig()
if err != nil {
return err
Expand All @@ -93,21 +97,38 @@ func (o *EvictPodOptions) Complete(cmd *cobra.Command, args []string) error {

// Validate ensures that all required arguments and flag values are provided
func (o *EvictPodOptions) Validate() error {
if len(o.args) != 1 {
return fmt.Errorf("only one argument expected. got %d arguments", len(o.args))

if len(o.podNames) > 0 && o.label != "" {
return fmt.Errorf("pod name cannot be provided when a selector is specified")
}

return nil
}

// Run fetches the given secret manifest from the cluster, decodes the payload, opens an editor to make changes, and applies the modified manifest when done
func (o *EvictPodOptions) Run() error {
err := k8s.Evict(o.kubeclient, o.podName, o.namespace)
if err != nil {
return err
var err error

if o.label != "" {

o.podNames, err = k8s.PodsFromLabel(o.kubeclient, o.label, o.namespace)
if err != nil {
return err
}
}

for _, podName := range o.podNames {
// remove the 'pod/' prefix, if it exists
podName = strings.TrimPrefix(podName, "pod/")

err := k8s.Evict(o.kubeclient, podName, o.namespace)
if err != nil {
return err
}

logrus.Infof("pod %s in namespace %s evicted successfully", podName, o.namespace)
}

logrus.Infof("pod %q in namespace %s evicted successfully", o.podName, o.namespace)
return nil
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/k8s/podsFromLabel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package k8s

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

//get pods from labelSelector
func PodsFromLabel(kubeclient kubernetes.Interface, label, namespace string) ([]string, error) {

podList, err := kubeclient.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: label})
if err != nil {
return nil, err
}

var podNames []string

for _, pod := range podList.Items {
podNames = append(podNames, pod.Name)
}

return podNames, nil
}