Skip to content

Commit 74d3bd5

Browse files
authored
Merge pull request #5 from nginxinc/areste-changes
Improvements on CLI params and tarball creation
2 parents 1f96b29 + fbfaf52 commit 74d3bd5

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build:
2+
go build -o cmd/kubectl-kic-supportpkg
3+
4+
install: build
5+
sudo cp cmd/kubectl-kic-supportpkg /usr/local/bin

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
# kubectl-kic-supportpkg
1+
# kubectl-kic-supportpkg
2+
3+
A kubectl plugin designed to collect diagnostics information on the designated namespaces.
4+
5+
## Features
6+
7+
The plugin collects the following global and namespace-specific information:
8+
9+
- k8s version, nodes information and CRDs
10+
- pods logs
11+
- list of pods, events, configmaps, services, deployments, statefulsets, replicasets and leases
12+
- k8s metrics
13+
- helm deployments
14+
15+
## Installation
16+
17+
Clone the repo and run `make install`. This will build the binary and copy it on `/usr/local/bin/`.
18+
19+
Verify that the plugin is properly found by `kubectl`:
20+
21+
```
22+
$ kubectl plugin list
23+
The following compatible plugins are available:
24+
25+
/usr/local/bin/kubectl-kic-supportpkg
26+
```
27+
28+
## Usage
29+
30+
The plugin is invoked via `kubectl kic supportpkg` and has only one required flag, `-n` or `--namespace`:
31+
32+
```
33+
$ kubectl kic supportpkg -n default -n nginx-ingress-0
34+
Running job pod-list... OK
35+
Running job collect-pods-logs... OK
36+
Running job events-list... OK
37+
Running job configmap-list... OK
38+
Running job service-list... OK
39+
Running job deployment-list... OK
40+
Running job statefulset-list... OK
41+
Running job replicaset-list... OK
42+
Running job lease-list... OK
43+
Running job k8s-version... OK
44+
Running job crd-info... OK
45+
Running job nodes-info... OK
46+
Running job metrics-information... OK
47+
Running job helm-info... OK
48+
Running job helm-deployments... OK
49+
Supportpkg successfully generated: kic-supportpkg-1711384966.tar.gz
50+
51+
```

cmd/kic-supportpkg.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,25 @@ func Execute() {
2828
fmt.Printf("Running job %s...", job.Name)
2929
err = job.Collect(collector)
3030
if err != nil {
31-
fmt.Printf("Error: %s", err)
31+
fmt.Printf("Error: %s\n", err)
3232
} else {
3333
fmt.Print(" OK\n")
3434
}
3535
}
3636

37-
err = collector.WrapUp()
37+
tarFile, err := collector.WrapUp()
3838
if err != nil {
3939
fmt.Println(fmt.Errorf("error when wrapping up: %s", err))
4040
os.Exit(1)
41+
} else {
42+
fmt.Printf("Supportpkg successfully generated: %s\n", tarFile)
4143
}
4244
},
4345
}
4446

45-
rootCmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "comma-separated list of namespaces to collect information from")
46-
rootCmd.SetUsageTemplate("Usage: \n kic-supportpkg -n namespace1 -n namespace2 ...")
47+
rootCmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "list of namespaces to collect information from")
48+
rootCmd.MarkFlagRequired("namespace")
49+
rootCmd.SetUsageTemplate("Usage: \n kic supportpkg [-n|--namespace] ns1 [-n|--namespace] ns2 ...\n kic supportpkg [-n|--namespace] ns1,ns2 ...\n")
4750

4851
if err := rootCmd.Execute(); err != nil {
4952
fmt.Println(err)

pkg/data_collector/data_collector.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
3535
}
3636

3737
// Find config
38-
home := homedir.HomeDir()
39-
kubeConfig := filepath.Join(home, ".kube", "config")
38+
kubeConfig := os.Getenv("KUBECONFIG")
39+
if kubeConfig == "" {
40+
kubeConfig = filepath.Join(homedir.HomeDir(), ".kube", "config")
41+
}
4042
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
4143

4244
if err != nil {
43-
return nil, fmt.Errorf("unable to create k8s config: %s", err)
45+
return nil, fmt.Errorf("unable to connect to k8s using file %s: %s", kubeConfig, err)
4446
}
4547

4648
dc := DataCollector{
@@ -63,17 +65,15 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
6365
return &dc, nil
6466
}
6567

66-
func (c *DataCollector) WrapUp() error {
68+
func (c *DataCollector) WrapUp() (string, error) {
6769

68-
// Create the tarball file
69-
//wd, _ := os.Getwd()
7070
unixTime := time.Now().Unix()
7171
unixTimeString := strconv.FormatInt(unixTime, 10)
7272
tarballName := fmt.Sprintf("kic-supportpkg-%s.tar.gz", unixTimeString)
7373

7474
file, err := os.Create(tarballName)
7575
if err != nil {
76-
return err
76+
return "", err
7777
}
7878
defer file.Close()
7979

@@ -92,18 +92,22 @@ func (c *DataCollector) WrapUp() error {
9292
if err != nil {
9393
return err
9494
}
95-
header.Name = path
96-
//TODO: correct this to relative path
9795

98-
if err := tw.WriteHeader(header); err != nil {
96+
relativePath, err := filepath.Rel(c.BaseDir, path)
97+
if err != nil {
98+
return err
99+
}
100+
header.Name = relativePath
101+
102+
if err = tw.WriteHeader(header); err != nil {
99103
return err
100104
}
101105

102106
if !info.Mode().IsRegular() {
103107
return nil
104108
}
105109

106-
file, err := os.Open(path)
110+
file, err = os.Open(path)
107111
if err != nil {
108112
return err
109113
}
@@ -116,8 +120,6 @@ func (c *DataCollector) WrapUp() error {
116120

117121
return nil
118122
})
119-
120-
fmt.Println("Tarball created successfully:", tarballName)
121-
os.RemoveAll(c.BaseDir)
122-
return nil
123+
_ = os.RemoveAll(c.BaseDir)
124+
return tarballName, nil
123125
}

pkg/jobs/job_list.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ func JobList() []Job {
5454
return results
5555
},
5656
},
57+
{
58+
Name: "events-list",
59+
Global: false,
60+
Execute: func(dc *data_collector.DataCollector, ctx context.Context) map[string][]byte {
61+
jobResults := make(map[string][]byte)
62+
for _, namespace := range dc.Namespaces {
63+
result, _ := dc.K8sCoreClientSet.CoreV1().Events(namespace).List(ctx, metav1.ListOptions{})
64+
jsonResult, _ := json.MarshalIndent(result, "", " ")
65+
jobResults[path.Join(dc.BaseDir, namespace, "events.json")] = jsonResult
66+
}
67+
68+
return jobResults
69+
},
70+
},
5771
{
5872
Name: "configmap-list",
5973
Global: false,

0 commit comments

Comments
 (0)