-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: create evict pod binary
- Loading branch information
Showing
6 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
bin | ||
.DS_Store | ||
|
||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/dhenkel92/kubectl-pdb/pkg/cmd" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
func main() { | ||
flags := pflag.NewFlagSet("kubectl-evict", pflag.ExitOnError) | ||
pflag.CommandLine = flags | ||
|
||
conf := genericclioptions.NewConfigFlags(true) | ||
conf.AddFlags(flags) | ||
|
||
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr} | ||
|
||
rootCmd := &cobra.Command{ | ||
Use: "evict", | ||
Short: "Utility to evict a pod from a node", | ||
} | ||
rootCmd.AddCommand(cmd.NewCmdEvictPod(streams, conf)) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/dhenkel92/kubectl-pdb/pkg/kube" | ||
"github.com/dhenkel92/kubectl-pdb/pkg/utils" | ||
"github.com/spf13/cobra" | ||
policyv1 "k8s.io/api/policy/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
) | ||
|
||
type EvictPodOptions struct { | ||
genericclioptions.IOStreams | ||
|
||
configFlags *genericclioptions.ConfigFlags | ||
Namespace string | ||
PodName string | ||
DryRun bool | ||
Output string | ||
} | ||
|
||
func NewEvictPodOptions(streams genericclioptions.IOStreams, configFlags *genericclioptions.ConfigFlags) *EvictPodOptions { | ||
return &EvictPodOptions{ | ||
IOStreams: streams, | ||
configFlags: configFlags, | ||
} | ||
} | ||
|
||
func NewCmdEvictPod(streams genericclioptions.IOStreams, configFlags *genericclioptions.ConfigFlags) *cobra.Command { | ||
o := NewEvictPodOptions(streams, configFlags) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "pod <pod_name>", | ||
Short: "Utility to evict a pod from a node", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := o.Complete(cmd, args); err != nil { | ||
return err | ||
} | ||
if err := o.Validate(); err != nil { | ||
return err | ||
} | ||
if err := o.Run(); err != nil { | ||
return utils.HandleRunError(streams, err) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&o.Output, "output", "o", "json", "Output format. One of: json|yaml") | ||
cmd.Flags().BoolVar(&o.DryRun, "dry-run", true, "If true, only print the object that would be sent, without sending it.") | ||
|
||
return cmd | ||
} | ||
|
||
func (o *EvictPodOptions) Complete(cmd *cobra.Command, args []string) error { | ||
var err error | ||
if len(args) != 1 { | ||
return fmt.Errorf("pod name is required") | ||
} | ||
o.PodName = args[0] | ||
|
||
o.Namespace, _, err = o.configFlags.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *EvictPodOptions) GetPrinter() (printers.ResourcePrinter, error) { | ||
switch o.Output { | ||
case "json": | ||
return &printers.JSONPrinter{}, nil | ||
case "yaml": | ||
return &printers.YAMLPrinter{}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("invalid output '%s'", o.Output) | ||
} | ||
|
||
func (o *EvictPodOptions) Validate() error { | ||
switch o.Output { | ||
case "json", "yaml": | ||
default: | ||
return fmt.Errorf("invalid output '%s'", o.Output) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *EvictPodOptions) Run() error { | ||
ctx := context.Background() | ||
|
||
kubeClient, err := kube.New(o.configFlags) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pod, err := kubeClient.GetClientset().CoreV1().Pods(o.Namespace).Get(ctx, o.PodName, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dryRunOpts := []string{} | ||
if o.DryRun { | ||
dryRunOpts = append(dryRunOpts, "All") | ||
} | ||
|
||
eviction := policyv1.Eviction{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Eviction", | ||
APIVersion: policyv1.SchemeGroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pod.Name, | ||
Namespace: pod.Namespace, | ||
}, | ||
DeleteOptions: &metav1.DeleteOptions{ | ||
DryRun: dryRunOpts, | ||
}, | ||
} | ||
|
||
err = kubeClient.GetClientset().CoreV1().Pods(o.Namespace).EvictV1(ctx, &eviction) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w := printers.GetNewTabWriter(o.Out) | ||
printer, err := o.GetPrinter() | ||
if err := printer.PrintObj(&eviction, w); err != nil { | ||
return err | ||
} | ||
|
||
return w.Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package utils | ||
|
||
import ( | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
) | ||
|
||
func HandleRunError(streams genericclioptions.IOStreams, err error) error { | ||
w := printers.GetNewTabWriter(streams.ErrOut) | ||
w.Write([]byte(err.Error())) | ||
return w.Flush() | ||
} |