Skip to content

Commit cdf2a8e

Browse files
committed
Chore: Refactor
* Pass DataCollector struct as a reference from main * Improve logic around job result/status
1 parent b3cdd58 commit cdf2a8e

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

cmd/nginx-supportpkg.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,22 @@ import (
3131

3232
func Execute() {
3333

34-
var namespaces []string
3534
var product string
36-
var excludeDBData bool
37-
var excludeTimeSeriesData bool
3835
var jobList []jobs.Job
36+
collector := data_collector.DataCollector{}
3937

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

46-
collector, err := data_collector.NewDataCollector(namespaces...)
44+
err := data_collector.NewDataCollector(&collector)
4745
if err != nil {
4846
fmt.Println(fmt.Errorf("unable to start data collector: %s", err))
4947
os.Exit(1)
5048
}
5149

52-
if excludeDBData {
53-
collector.ExcludeDBData = true
54-
}
55-
if excludeTimeSeriesData {
56-
collector.ExcludeTimeSeriesData = true
57-
}
58-
5950
collector.Logger.Printf("Starting kubectl-nginx-supportpkg - version: %s - build: %s", version.Version, version.Build)
6051
collector.Logger.Printf("Input args are %v", os.Args)
6152

@@ -77,14 +68,14 @@ func Execute() {
7768
failedJobs := 0
7869
for _, job := range jobList {
7970
fmt.Printf("Running job %s...", job.Name)
80-
err, Skipped := job.Collect(collector)
71+
err, Skipped := job.Collect(&collector)
8172
if Skipped {
8273
fmt.Print(" SKIPPED\n")
8374
} else if err != nil {
84-
fmt.Printf(" Error: %s\n", err)
75+
fmt.Printf(" FAILED: %s\n", err)
8576
failedJobs++
8677
} else {
87-
fmt.Print(" OK\n")
78+
fmt.Print(" COMPLETED\n")
8879
}
8980
}
9081

@@ -107,7 +98,7 @@ func Execute() {
10798
},
10899
}
109100

110-
rootCmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "list of namespaces to collect information from")
101+
rootCmd.Flags().StringSliceVarP(&collector.Namespaces, "namespace", "n", []string{}, "list of namespaces to collect information from")
111102
if err := rootCmd.MarkFlagRequired("namespace"); err != nil {
112103
fmt.Println(err)
113104
os.Exit(1)
@@ -119,8 +110,8 @@ func Execute() {
119110
os.Exit(1)
120111
}
121112

122-
rootCmd.Flags().BoolVarP(&excludeDBData, "exclude-db-data", "d", false, "exclude DB data collection")
123-
rootCmd.Flags().BoolVarP(&excludeTimeSeriesData, "exclude-time-series-data", "t", false, "exclude time series data collection")
113+
rootCmd.Flags().BoolVarP(&collector.ExcludeDBData, "exclude-db-data", "d", false, "exclude DB data collection")
114+
rootCmd.Flags().BoolVarP(&collector.ExcludeTimeSeriesData, "exclude-time-series-data", "t", false, "exclude time series data collection")
124115

125116
versionStr := "nginx-supportpkg - version: " + version.Version + " - build: " + version.Build + "\n"
126117
rootCmd.SetVersionTemplate(versionStr)

pkg/data_collector/data_collector.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ type DataCollector struct {
6060
ExcludeTimeSeriesData bool
6161
}
6262

63-
func NewDataCollector(namespaces ...string) (*DataCollector, error) {
63+
func NewDataCollector(collector *DataCollector) error {
6464

6565
tmpDir, err := os.MkdirTemp("", "-pkg-diag")
6666
if err != nil {
67-
return nil, fmt.Errorf("unable to create temp directory: %s", err)
67+
return fmt.Errorf("unable to create temp directory: %s", err)
6868
}
6969

7070
logFile, err := os.OpenFile(filepath.Join(tmpDir, "supportpkg.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
7171
if err != nil {
72-
return nil, fmt.Errorf("unable to create log file: %s", err)
72+
return fmt.Errorf("unable to create log file: %s", err)
7373
}
7474

7575
// Find config
@@ -80,30 +80,27 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
8080
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
8181

8282
if err != nil {
83-
return nil, fmt.Errorf("unable to connect to k8s using file %s: %s", kubeConfig, err)
84-
}
85-
86-
dc := DataCollector{
87-
BaseDir: tmpDir,
88-
Namespaces: namespaces,
89-
LogFile: logFile,
90-
Logger: log.New(logFile, "", log.LstdFlags|log.LUTC|log.Lmicroseconds|log.Lshortfile),
91-
K8sHelmClientSet: make(map[string]helmClient.Client),
83+
return fmt.Errorf("unable to connect to k8s using file %s: %s", kubeConfig, err)
9284
}
85+
// Set up the DataCollector options
86+
collector.BaseDir = tmpDir
87+
collector.LogFile = logFile
88+
collector.Logger = log.New(logFile, "", log.LstdFlags|log.LUTC|log.Lmicroseconds|log.Lshortfile)
89+
collector.K8sHelmClientSet = make(map[string]helmClient.Client)
9390

9491
//Initialize clients
95-
dc.K8sRestConfig = config
96-
dc.K8sCoreClientSet, _ = kubernetes.NewForConfig(config)
97-
dc.K8sCrdClientSet, _ = crdClient.NewForConfig(config)
98-
dc.K8sMetricsClientSet, _ = metricsClient.NewForConfig(config)
99-
for _, namespace := range dc.Namespaces {
100-
dc.K8sHelmClientSet[namespace], _ = helmClient.NewClientFromRestConf(&helmClient.RestConfClientOptions{
92+
collector.K8sRestConfig = config
93+
collector.K8sCoreClientSet, _ = kubernetes.NewForConfig(config)
94+
collector.K8sCrdClientSet, _ = crdClient.NewForConfig(config)
95+
collector.K8sMetricsClientSet, _ = metricsClient.NewForConfig(config)
96+
for _, namespace := range collector.Namespaces {
97+
collector.K8sHelmClientSet[namespace], _ = helmClient.NewClientFromRestConf(&helmClient.RestConfClientOptions{
10198
Options: &helmClient.Options{Namespace: namespace},
10299
RestConfig: config,
103100
})
104101
}
105102

106-
return &dc, nil
103+
return nil
107104
}
108105

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

pkg/jobs/job.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ func (j Job) Collect(dc *data_collector.DataCollector) (error, bool) {
5252
select {
5353
case <-ctx.Done():
5454
dc.Logger.Printf("\tJob %s has timed out: %s\n---\n", j.Name, ctx.Err())
55-
err := fmt.Errorf("Context cancelled: %v", ctx.Err())
56-
return err, false
55+
return fmt.Errorf("Context cancelled: %v", ctx.Err()), false
5756

5857
case jobResults := <-ch:
58+
if jobResults.Skipped {
59+
dc.Logger.Printf("\tJob %s has been skipped\n---\n", j.Name)
60+
return nil, true
61+
}
5962
if jobResults.Error != nil {
6063
dc.Logger.Printf("\tJob %s has failed: %s\n", j.Name, jobResults.Error)
61-
return jobResults.Error, jobResults.Skipped
64+
return jobResults.Error, false
6265
}
6366

6467
for fileName, fileValue := range jobResults.Files {

0 commit comments

Comments
 (0)