Skip to content

Commit a63d468

Browse files
committed
enter testpod
1 parent d129042 commit a63d468

3 files changed

Lines changed: 70 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ Edit the configuration file to set default image and shell to execute, as well a
2020
| ---- | ----------- |
2121
| `--image` | Overrides the default image from your template. |
2222
| `--shell` | Overrides the default shell from your template. |
23+
| `--enter-mine` | Enters an existing testpod managed by you. |
24+
| `--enter-any` | Enters an existing testpod managed by anyone. |
2325
| `--dry-run` | Prints the rendered manifests instead of applying them to Kubernetes. |

k8s.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ type PortBlock struct {
5050
EndPort int `yaml:"endPort"`
5151
}
5252

53-
func MakeManifestFromTemplate(name string, tpl Template) (string, error) {
53+
func MakeManifestFromTemplate(managedBy, name string, tpl Template) (string, error) {
5454
if len(name) == 0 {
5555
return "", fmt.Errorf("name cannot be empty")
5656
}
5757

5858
matchLabels := map[string]string{
59-
"app.kubernetes.io/name": "go-testpod",
60-
"app.kubernetes.io/instance": name,
59+
"app.kubernetes.io/name": "go-testpod",
60+
"app.kubernetes.io/instance": name,
61+
"app.kubernetes.io/managed-by": managedBy,
6162
}
6263

6364
var podManifest PodManifest

main.go

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
var (
1313
flagImageOverride = flag.String("image", "", "set to override default image from template")
1414
flagShellOverride = flag.String("shell", "", "set to override default shell from template")
15+
flagEnterMine = flag.Bool("enter-mine", false, "open another shell in existing testpod managed by you")
16+
flagEnterAny = flag.Bool("enter-any", false, "open another shell in existing testpod managed by anyone")
1517
flagDryRun = flag.Bool("dry-run", false, "print manifest instead of applying it to kubernetes")
1618

1719
tempKubeconfigPath string
@@ -25,6 +27,12 @@ func main() {
2527
fmt.Println("ERR: read template:", err)
2628
os.Exit(1)
2729
}
30+
if len(*flagImageOverride) > 0 {
31+
tpl.DefaultImage = *flagImageOverride
32+
}
33+
if len(*flagShellOverride) > 0 {
34+
tpl.DefaultShell = *flagShellOverride
35+
}
2836

2937
if err := execTemplate(tpl); err != nil {
3038
fmt.Println("ERR:", err)
@@ -59,16 +67,46 @@ func execTemplate(tpl Template) error {
5967
if err != nil {
6068
return fmt.Errorf("get hostname: %w", err)
6169
}
62-
podName := "testpod-" + hostname + "-" + time.Now().Format("20060102-150405")
70+
managedBy := hostname
6371

64-
if len(*flagImageOverride) > 0 {
65-
tpl.DefaultImage = *flagImageOverride
66-
}
67-
if len(*flagShellOverride) > 0 {
68-
tpl.DefaultShell = *flagShellOverride
72+
if *flagEnterMine || *flagEnterAny {
73+
if *flagEnterMine && *flagEnterAny {
74+
return fmt.Errorf("cannot set flags --enter-mine and --enter-any at the same time")
75+
}
76+
77+
matchLabel := "app.kubernetes.io/managed-by"
78+
matchValue := managedBy
79+
if *flagEnterAny {
80+
matchLabel = "app.kubernetes.io/name"
81+
matchValue = "go-testpod"
82+
}
83+
84+
pods, err := kubectlGetPodNames(matchLabel, matchValue)
85+
if err != nil {
86+
return fmt.Errorf("list running pods: %w", err)
87+
}
88+
if len(pods) == 0 {
89+
return fmt.Errorf("no suitable testpods running in selected context")
90+
}
91+
if len(pods) > 1 {
92+
return fmt.Errorf("multiple suitable testpods running in selected context")
93+
}
94+
95+
if *flagDryRun {
96+
fmt.Println("dry-run: skip entering pod", pods[0])
97+
return nil
98+
}
99+
100+
fmt.Println("enter running pod", pods[0])
101+
if err := kubectlExec(pods[0], tpl.DefaultShell); err != nil {
102+
return fmt.Errorf("exec into Pod: %w", err)
103+
}
104+
return nil
69105
}
70106

71-
manifestData, err := MakeManifestFromTemplate(podName, tpl)
107+
podName := "testpod-" + hostname + "-" + time.Now().Format("20060102-150405")
108+
109+
manifestData, err := MakeManifestFromTemplate(managedBy, podName, tpl)
72110
if err != nil {
73111
return fmt.Errorf("render manifest: %w", err)
74112
}
@@ -108,6 +146,25 @@ func execTemplate(tpl Template) error {
108146
return nil
109147
}
110148

149+
func kubectlGetPodNames(label, value string) ([]string, error) {
150+
cmd := exec.Command("kubectl", "get", "pods", "-l", label+"="+value, "--no-headers", "-o", "custom-columns=:metadata.name")
151+
cmd.Env = os.Environ()
152+
cmd.Env = append(cmd.Env, "KUBECONFIG="+tempKubeconfigPath)
153+
out, err := cmd.CombinedOutput()
154+
fmt.Println(strings.TrimSpace(string(out)))
155+
if err != nil {
156+
return nil, err
157+
}
158+
podNames := make([]string, 0)
159+
for _, part := range strings.Split(string(out), "\n") {
160+
part = strings.TrimSpace(part)
161+
if len(part) > 0 {
162+
podNames = append(podNames, part)
163+
}
164+
}
165+
return podNames, nil
166+
}
167+
111168
func kubectlApply(manifestData string) error {
112169
cmd := exec.Command("kubectl", "apply", "-f", "-")
113170
cmd.Env = os.Environ()

0 commit comments

Comments
 (0)