Skip to content

Commit

Permalink
Add support for specifying Kubernetes context via --context flag
Browse files Browse the repository at this point in the history
  • Loading branch information
uozalp committed Aug 27, 2024
1 parent 114791e commit 3857e72
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

This app is a plugin for `kubectl`, it allows you to duplicate a running Pod and auto-exec into. The list of Pods is filterable, and you can select the namespace you want.

You can also set these parameters for customization of the duplicata:
You can also set these parameters for customization of the duplicate:
- `cpu`
- `memory`
- `ttl`

Already created duplicatas remain 4h (by default) and you can exec into them as long they're running.
Already created duplicates remain 4h (by default) and you can exec into them as long they're running.

## Requirements

Expand Down Expand Up @@ -41,7 +41,7 @@ Flags:
### Install
Download latest release from https://github.com/qonto/kubectl-duplicate/releases and extract `kubectl-duplicate` into your `/usr/loca/bin` and run `chmod -x /usr/local/bin/kubectl-duplicate`.
Download latest release from https://github.com/qonto/kubectl-duplicate/releases and extract `kubectl-duplicate` into your `/usr/local/bin` and run `chmod -x /usr/local/bin/kubectl-duplicate`.
### Build
Expand Down Expand Up @@ -69,14 +69,14 @@ xattr -d com.apple.quarantine /usr/local/bin/kubectl-duplicate
falcosidekick-5f44cb5bff-jh9wk
```
* List pods with already created duplicatas:
* List pods with already created duplicates:
```shell
Search: █
? Pods:
falcosidekick-duplicata-nglvr-f569r [duplicata]
> falcosidekick-duplicata-kzx9z-kpjh6 [duplicata]
falcosidekick-duplicata-mtb9x-lb29p [duplicata]
falcosidekick-duplicate-nglvr-f569r [duplicate]
> falcosidekick-duplicate-kzx9z-kpjh6 [duplicate]
falcosidekick-duplicate-mtb9x-lb29p [duplicate]
falcosidekick-5f44cb5bff-94sqc
falcosidekick-5f44cb5bff-jh9wk
falcosidekick-ui-867f5d6f7-76lfx
Expand Down
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
var config cfg.Configuration
var version string
var commit string
var noAutoExec bool
var kubeContext string

func init() {
allnamespaces := kingpin.Flag("all-namespaces", "All Namespaces").Bool()
Expand All @@ -32,9 +34,14 @@ func init() {
config.CPU = kingpin.Flag("cpu", "CPU Request for the duplicated Pod").Short('c').String()
config.Memory = kingpin.Flag("memory", "Memory Request for the duplicated Pod").Short('m').String()
config.Kubeconfig = kingpin.Flag("kubeconfig", "Kube config file (override by env var KUBECONFIG").Short('k').Default(os.Getenv("HOME") + "/.kube/config").ExistingFile()
noAutoExecFlag := kingpin.Flag("no-auto-exec", "Do not automatically exec in to the container").Bool()
contextFlag := kingpin.Flag("context", "Kubernetes context to use").String()
v := kingpin.Flag("version", "Print version").Short('v').Bool()
kingpin.Parse()

noAutoExec = *noAutoExecFlag
kubeContext = *contextFlag

if *v {
fmt.Printf("Version: %s\nCommit: %s\n", version, commit)
os.Exit(0)
Expand All @@ -47,10 +54,13 @@ func init() {
if kc := os.Getenv("KUBECONFIG"); kc != "" {
*config.Kubeconfig = kc
}

}

func main() {
kubeconfig, err := clientcmd.BuildConfigFromFlags("", *config.Kubeconfig)
loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: *config.Kubeconfig}
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: kubeContext}
kubeconfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides).ClientConfig()
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -85,11 +95,13 @@ func main() {
s.Stop()
}

startShell(pod.Name, container.Name, config.Namespace)
if !noAutoExec {
startShell(pod.Name, container.Name, config.Namespace)
}
}

func startShell(pod, container string, namespace *string) {
cmd := exec.Command(os.Getenv("SHELL"), "-c", "kubectl attach "+pod+" -n "+*namespace+" -t -i -c "+*namespace+"-"+container+"-exec")
cmd := exec.Command(os.Getenv("SHELL"), "-c", "kubectl attach "+pod+" -n "+*namespace+" -t -i -c "+container+"-exec")
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
Expand Down
6 changes: 3 additions & 3 deletions pkg/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"k8s.io/client-go/kubernetes"
)

// Job creates the Job for Duplicata
// Job creates the Job for duplicate
func Job(clientset *kubernetes.Clientset, config config.Configuration, deployment appsv1.Deployment, container corev1.Container) (*batchv1.Job, error) {
execAction := new(corev1.ExecAction)
execAction.Command = []string{"true"}
Expand Down Expand Up @@ -51,7 +51,7 @@ func Job(clientset *kubernetes.Clientset, config config.Configuration, deploymen

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
GenerateName: deployment.ObjectMeta.Name + "-duplicata-",
GenerateName: deployment.ObjectMeta.Name + "-duplicate-",
Annotations: map[string]string{
"end-at": endAt.Format("2006-01-02 15:04:05"),
},
Expand All @@ -65,7 +65,7 @@ func Job(clientset *kubernetes.Clientset, config config.Configuration, deploymen
BackoffLimit: &backoffLimit,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
GenerateName: deployment.ObjectMeta.Name + "-duplicata-",
GenerateName: deployment.ObjectMeta.Name + "-duplicate-",
Annotations: map[string]string{
"end-at": endAt.Format("2006-01-02 15:04:05"),
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func PodsForJob(clientset *kubernetes.Clientset, config config.Configuration, jo
func sort(pods []corev1.Pod) []corev1.Pod {
var sortedPods []corev1.Pod
for _, i := range pods {
if strings.Contains(i.ObjectMeta.Name, "-duplicata-") {
if strings.Contains(i.ObjectMeta.Name, "-duplicate-") {
sortedPods = append(sortedPods, i)
}
}
for _, i := range pods {
if !strings.Contains(i.ObjectMeta.Name, "-duplicata-") {
if !strings.Contains(i.ObjectMeta.Name, "-duplicate-") {
sortedPods = append(sortedPods, i)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func Pod(pods []corev1.Pod, match string) corev1.Pod {

templates := &promptui.SelectTemplates{
// Label: ` `,
Active: `{{ "> " | cyan | bold }}{{ .ObjectMeta.Name | cyan | bold }}{{if (index .ObjectMeta.Annotations "end-at")}}{{ " [duplicata]" }}{{ end }}`,
Inactive: ` {{ .ObjectMeta.Name }}{{if (index .ObjectMeta.Annotations "end-at")}}{{ " [duplicata]" }}{{ end }}`,
Active: `{{ "> " | cyan | bold }}{{ .ObjectMeta.Name | cyan | bold }}{{if (index .ObjectMeta.Annotations "end-at")}}{{ " [duplicate]" }}{{ end }}`,
Inactive: ` {{ .ObjectMeta.Name }}{{if (index .ObjectMeta.Annotations "end-at")}}{{ " [duplicate]" }}{{ end }}`,
Details: `
{{if (index .ObjectMeta.Annotations "end-at")}}{{ " End: " }}{{ index .ObjectMeta.Annotations "end-at" | bold }}{{ end }}`,
}
Expand Down

0 comments on commit 3857e72

Please sign in to comment.