-
Notifications
You must be signed in to change notification settings - Fork 49
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
Multi namespace #26
base: master
Are you sure you want to change the base?
Multi namespace #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
bin/ | ||
lib/ | ||
/out/* | ||
/k8sviz.iml | ||
service-account-key.json | ||
config-k8sviz | ||
/*.png | ||
/merged.gv-e | ||
/global.dot | ||
/config-k8sviz.json | ||
/service-account-key2.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,24 +6,23 @@ package main | |
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/mkimuram/k8sviz/pkg/graph" | ||
"github.com/mkimuram/k8sviz/pkg/resources" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
_ "k8s.io/client-go/plugin/pkg/client/auth" | ||
) | ||
|
||
const ( | ||
defaultNamespace = "namespace" | ||
defaultOutFile = "k8sviz.out" | ||
defaultOutDir = "out" | ||
defaultOutType = "dot" | ||
descNamespaceOpt = "namespace to visualize" | ||
descOutFileOpt = "output filename" | ||
descOutDirOpt = "output dir" | ||
descOutTypeOpt = "type of output" | ||
descShortOptSuffix = " (shorthand)" | ||
) | ||
|
@@ -33,7 +32,7 @@ var ( | |
dir string | ||
// Flags | ||
namespace string | ||
outFile string | ||
outDir string | ||
outType string | ||
) | ||
|
||
|
@@ -49,8 +48,8 @@ func init() { | |
} | ||
flag.StringVar(&namespace, "namespace", defaultNamespace, descNamespaceOpt) | ||
flag.StringVar(&namespace, "n", defaultNamespace, descNamespaceOpt+descShortOptSuffix) | ||
flag.StringVar(&outFile, "outfile", defaultOutFile, descOutFileOpt) | ||
flag.StringVar(&outFile, "o", defaultOutFile, descOutFileOpt+descShortOptSuffix) | ||
flag.StringVar(&outDir, "outDir", defaultOutDir, descOutDirOpt) | ||
flag.StringVar(&outDir, "o", defaultOutDir, descOutDirOpt+descShortOptSuffix) | ||
flag.StringVar(&outType, "type", defaultOutType, descOutTypeOpt) | ||
flag.StringVar(&outType, "t", defaultOutType, descOutTypeOpt+descShortOptSuffix) | ||
flag.Parse() | ||
|
@@ -70,11 +69,13 @@ func init() { | |
} | ||
|
||
// test connectivity for k8s cluster and the namespace | ||
_, err = clientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{}) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to get namespace %q: %v\n", namespace, err) | ||
os.Exit(1) | ||
} | ||
//if !strings.Contains(namespace,",") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If namespaces are parsed into the slice(see below), we will be able to check for all the namespaces here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By "below", I meant this comment. It appeared above this comment :) |
||
// _, err = clientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{}) | ||
// if err != nil { | ||
// fmt.Fprintf(os.Stderr, "Failed to get namespace %q: %v\n", namespace, err) | ||
// os.Exit(1) | ||
// } | ||
//} | ||
|
||
dir, err = getBinDir() | ||
if err != nil { | ||
|
@@ -85,23 +86,25 @@ func init() { | |
|
||
func main() { | ||
// Get all resources in the namespace | ||
res, err := resources.NewResources(clientset, namespace) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to get k8s resources: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
g := graph.NewGraph(res, dir) | ||
|
||
if outType == "dot" { | ||
if err := g.WriteDotFile(outFile); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to output %q file with format %q for namespace %q: %v\n", outFile, outType, namespace, err) | ||
words := strings.Split(namespace, ",") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To parse comma separated strings to a slice of string, https://golang.org/src/flag/example_test.go might be a good reference. |
||
for _, ns := range words { | ||
res, err := resources.NewResources(clientset, ns) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to get k8s resources: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} else { | ||
if err := g.PlotDotFile(outFile, outType); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to output %q file with format %q for namespace %q: %v\n", outFile, outType, namespace, err) | ||
os.Exit(1) | ||
|
||
g := graph.NewGraph(res, dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make |
||
if outType == "dot" { | ||
if err := g.WriteDotFile(outDir + "/" + ns + "." + outType); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to output %q file with format %q for namespace %q: %v\n", outDir, outType, namespace, err) | ||
os.Exit(1) | ||
} | ||
} else { | ||
if err := g.PlotDotFile(outDir+"/"+ns+"."+outType, outType); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to output %q file with format %q for namespace %q: %v\n", outDir, outType, namespace, err) | ||
os.Exit(1) | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, you don't need to modify Makefile. Instead, Just call below: