Skip to content

Commit

Permalink
Merge pull request #6 from nginxinc/areste
Browse files Browse the repository at this point in the history
Add support for multiple products
  • Loading branch information
mrajagopal authored Jun 20, 2024
2 parents 183a8b5 + 3ebb186 commit 54f3d25
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build:
go build -o cmd/kubectl-nic-supportpkg
go build -o cmd/kubectl-nginx_supportpkg

install: build
sudo cp cmd/kubectl-nic-supportpkg /usr/local/bin
sudo cp cmd/kubectl-nginx_supportpkg /usr/local/bin
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# nginx-supportpkg-for-k8s

A kubectl plugin designed to collect NIC diagnostics information on the designated namespaces.
A kubectl plugin designed to collect diagnostics information on any NGINX product running on k8s.

## Supported products

Currently, [NIC](https://github.com/nginxinc/kubernetes-ingress) is the only supported product.

## Features

The plugin collects the following global and namespace-specific information:
Depending on the product, the plugin might collect some or all of the following global and namespace-specific information:

- k8s version, nodes information and CRDs
- pods logs
- list of pods, events, configmaps, services, deployments, statefulsets, replicasets and leases
- k8s metrics
- helm deployments
- `nginx -T` output from the NIC pods
- `nginx -T` output from NGINX pods

The plugin DOES NOT collect secrets or coredums.
The plugin DOES NOT collect secrets or coredumps.

## Installation

Expand All @@ -26,7 +30,7 @@ Verify that the plugin is properly found by `kubectl`:
$ kubectl plugin list
The following compatible plugins are available:
/usr/local/bin/kubectl-nic-supportpkg
/usr/local/bin/kubectl-nginx_supportpkg
```

### Downloading the binary
Expand All @@ -39,15 +43,19 @@ Decompress the tarball and copy the binary somewhere in your `$PATH`. Make sure
$ kubectl plugin list
The following compatible plugins are available:
/path/to/plugin/kubectl-nic-supportpkg
/path/to/plugin/kubectl-nginx_supportpkg
```

## Usage

The plugin is invoked via `kubectl nic supportpkg` and has only one required flag, `-n` or `--namespace`:
The plugin is invoked via `kubectl nginx-supportpkg` and has two required flags:

* `-n` or `--namespace` indicates the namespace(s) where the product is running.
* `-p` or `--product` indicates the product to collect information from.


```
$ kubectl nic supportpkg -n default -n nginx-ingress-0
$ kubectl nginx-supportpkg -n default -n nginx-ingress-0 -p nic
Running job pod-list... OK
Running job collect-pods-logs... OK
Running job events-list... OK
Expand Down
33 changes: 25 additions & 8 deletions cmd/nic-supportpkg.go → cmd/nginx-supportpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import (
func Execute() {

var namespaces []string
var product string
var jobList []jobs.Job

var rootCmd = &cobra.Command{
Use: "nic-supportpkg",
Short: "nic-supportpkg - a tool to create Ingress Controller diagnostics package",
Long: `nic-supportpkg - a tool to create Ingress Controller diagnostics package`,
Use: "nginx-supportpkg",
Short: "nginx-supportpkg - a tool to create Ingress Controller diagnostics package",
Long: `nginx-supportpkg - a tool to create Ingress Controller diagnostics package`,
Run: func(cmd *cobra.Command, args []string) {

collector, err := data_collector.NewDataCollector(namespaces...)
Expand All @@ -43,10 +45,18 @@ func Execute() {
os.Exit(1)
}

collector.Logger.Printf("Starting kubectl-nic-suportpkg - version: %s - build: %s", version.Version, version.Build)
collector.Logger.Printf("Starting kubectl-nginx-suportpkg - version: %s - build: %s", version.Version, version.Build)

if collector.AllNamespacesExist() == true {
for _, job := range jobs.JobList() {
switch product {
case "nic":
jobList = jobs.NICJobList()
default:
fmt.Printf("Error: product must be in the following list: [nic]\n")
os.Exit(1)
}

if collector.AllNamespacesExist() {
for _, job := range jobList {
fmt.Printf("Running job %s...", job.Name)
err = job.Collect(collector)
if err != nil {
Expand All @@ -56,7 +66,7 @@ func Execute() {
}
}

tarFile, err := collector.WrapUp()
tarFile, err := collector.WrapUp(product)
if err != nil {
fmt.Println(fmt.Errorf("error when wrapping up: %s", err))
os.Exit(1)
Expand All @@ -74,7 +84,14 @@ func Execute() {
fmt.Println(err)
os.Exit(1)
}
rootCmd.SetUsageTemplate("Usage: \n nic supportpkg [-n|--namespace] ns1 [-n|--namespace] ns2 ...\n nic supportpkg [-n|--namespace] ns1,ns2 ...\n")

rootCmd.Flags().StringVarP(&product, "product", "p", "", "products to collect information from")
if err := rootCmd.MarkFlagRequired("product"); err != nil {
fmt.Println(err)
os.Exit(1)
}

rootCmd.SetUsageTemplate("Usage: \n nginx-supportpkg [-n|--namespace] ns1 [-n|--namespace] ns2 [-p|--product] nic...\n nginx-supportpkg [-n|--namespace] ns1,ns2 [-p|--product] nic...\n")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/data_collector/data_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
return &dc, nil
}

func (c *DataCollector) WrapUp() (string, error) {
func (c *DataCollector) WrapUp(product string) (string, error) {

unixTime := time.Now().Unix()
unixTimeString := strconv.FormatInt(unixTime, 10)
tarballName := fmt.Sprintf("nic-supportpkg-%s.tar.gz", unixTimeString)
tarballName := fmt.Sprintf("%s-supportpkg-%s.tar.gz", product, unixTimeString)
tarballRootDirName := fmt.Sprintf("nic-supportpkg-%s", unixTimeString)

err := c.LogFile.Close()
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/job_list.go → pkg/jobs/nic_job_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"time"
)

func JobList() []Job {
func NICJobList() []Job {
jobList := []Job{
{
Name: "pod-list",
Expand Down

0 comments on commit 54f3d25

Please sign in to comment.