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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace support #1

Closed
FZambia opened this issue Jul 9, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@FZambia
Copy link

commented Jul 9, 2018

Hello, very useful tool - thanks! 馃憤

We actively use k8s namespaces to deploy services so having support for them would be really nice.

Locally I quickly solved this for myself in this way:

diff --git a/main.go b/main.go
index db868b8..6bd2fa8 100644
--- a/main.go
+++ b/main.go
@@ -33,7 +33,7 @@ var (
                                failx("need at least one target")
                        }
 
-                       podsList, err := exec.Command("kubectl", "get", "pod").CombinedOutput()
+                       podsList, err := exec.Command("kubectl", "get", "pod", "--all-namespaces").CombinedOutput()
                        if err != nil {
                                failx(err)
                        }
@@ -48,16 +48,24 @@ var (
                                                "get",
                                                "pod",
                                                "--no-headers=true",
+                                               "--all-namespaces",
                                                "-o",
-                                               "custom-columns=:metadata.name",
+                                               "custom-columns=:metadata.name,:metadata.namespace",
                                        }
 
-                                       podNames, _ := exec.Command("kubectl", args...).CombinedOutput()
-                                       re := regexp.MustCompile(t[0] + ".*")
-                                       targetList := re.FindAllString(string(podNames), -1)
-                                       if len(targetList) > 0 {
-                                               addcmd := exec.Command("kubectl", "port-forward", targetList[0], t[1]+":"+t[2])
-                                               cs = append(cs, addcmd)
+                                       pods, _ := exec.Command("kubectl", args...).CombinedOutput()
+                                       rows := strings.Split(string(pods), "\n")
+                                       for _, row := range rows {
+                                               parts := strings.Fields(row)
+                                               if len(parts) != 2 {
+                                                       continue
+                                               }
+                                               re := regexp.MustCompile(t[0] + ".*")
+                                               targetList := re.FindAllString(parts[0], -1)
+                                               if len(targetList) > 0 {
+                                                       addcmd := exec.Command("kubectl", "port-forward", "-n", parts[1], targetList[0], t[1]+":"+t[2])
+                                                       cs = append(cs, addcmd)
+                                               }
                                        }
                                } else {
                                        fail("invalid target", c)

But this is a quick solution - it will output all pods on start for example, you might not need this.

@flowerinthenight

This comment has been minimized.

Copy link
Owner

commented Jul 9, 2018

Actually, this is very useful. If you can PR this, it would be much appreciated.

@FZambia

This comment has been minimized.

Copy link
Author

commented Jul 9, 2018

Sorry, I don't have enough time unfortunately...

BTW I think you could simplify code a lot using -o json option of kubectl which outputs information in JSON so you don't need to parse output manually.

@flowerinthenight

This comment has been minimized.

Copy link
Owner

commented Jul 10, 2018

It's okay. Thanks anyway. If I can find time myself, I'll add these codes.

You're right. Better yet, using the go client is probably the best. Unfortunately, I don't have the time either.

fkei added a commit to fkei/kubepfm that referenced this issue Sep 19, 2018

@flowerinthenight

This comment has been minimized.

Copy link
Owner

commented Feb 19, 2019

I merged the code above since I needed it. Closing this issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can鈥檛 perform that action at this time.