Skip to content

Commit 4c91202

Browse files
committed
Feat: Generate a manifest file for ihealth ingestion
* Job.Collect now returns filenames in addition to error and skipped status * Some files from the nim-dqlite-job were written directly in the job instead of being returned as part of the JobResult, this was corrected in a previous commit * Added GenerateManifest and associated structures to generate a manifest.json file for iHealth ingestion
1 parent ccfcb82 commit 4c91202

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

cmd/nginx-supportpkg.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"os"
2424
"path/filepath"
2525
"slices"
26-
"strings"
2726
"time"
2827

2928
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
@@ -70,49 +69,39 @@ func Execute() {
7069
if collector.AllNamespacesExist() {
7170
failedJobs := 0
7271
totalJobs := len(jobList)
73-
var jobTimings []data_collector.JobTiming
72+
var jobTimings []data_collector.JobInfo
7473

7574
for _, job := range jobList {
7675
fmt.Printf("Running job %s...", job.Name)
7776

78-
// Record job start time
77+
// Record job start and end time to calculate duration
7978
jobStartTime := time.Now()
80-
jobResult := job.Collect(&collector)
81-
82-
// Record job end time and calculate duration
79+
err, skipped, files := job.Collect(&collector)
8380
jobEndTime := time.Now()
8481
duration := jobEndTime.Sub(jobStartTime)
8582

86-
// Create job timing record
87-
files := make([]string, 0, len(jobResult.Files))
88-
for filename := range jobResult.Files {
89-
if len(filename) > 0 {
90-
packagePath := strings.TrimPrefix(filename, collector.BaseDir)
91-
files = append(files, packagePath)
92-
}
93-
}
94-
95-
jobTiming := data_collector.JobTiming{
83+
// Create job info record
84+
jobInfo := data_collector.JobInfo{
9685
Name: job.Name,
97-
StartTime: jobStartTime.UTC().Format(time.RFC3339),
98-
EndTime: jobEndTime.UTC().Format(time.RFC3339),
86+
StartTime: jobStartTime.UTC().Format(time.RFC3339Nano),
87+
EndTime: jobEndTime.UTC().Format(time.RFC3339Nano),
9988
Duration: duration.String(),
10089
Files: files,
10190
}
10291

103-
if jobResult.Skipped {
92+
if skipped {
10493
fmt.Print(" SKIPPED\n")
105-
} else if jobResult.Error != nil {
106-
fmt.Printf(" FAILED: %s\n", jobResult.Error)
94+
} else if err != nil {
95+
fmt.Printf(" FAILED: %s\n", err)
10796
failedJobs++
10897
} else {
10998
fmt.Print(" COMPLETED\n")
11099
}
111100

112-
jobTimings = append(jobTimings, jobTiming)
101+
jobTimings = append(jobTimings, jobInfo)
113102
}
114103

115-
// Generate manifest with job timings - UPDATE THIS LINE
104+
// Generate manifest with job timings
116105
manifestData, err := collector.GenerateManifest(product, startTime, totalJobs, failedJobs, jobTimings)
117106
if err != nil {
118107
fmt.Printf("Warning: Failed to generate manifest: %v\n", err)

pkg/data_collector/data_collector.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type SubPackage struct {
110110
ID string `json:"id,omitempty"`
111111
}
112112

113-
type JobTiming struct {
113+
type JobInfo struct {
114114
Name string `json:"name"`
115115
StartTime string `json:"start_time"`
116116
EndTime string `json:"end_time"`
@@ -168,7 +168,7 @@ func (c *DataCollector) WrapUp(product string) (string, error) {
168168
unixTime := time.Now().Unix()
169169
unixTimeString := strconv.FormatInt(unixTime, 10)
170170
tarballName := fmt.Sprintf("%s-supportpkg-%s.tar.gz", product, unixTimeString)
171-
tarballRootDirName := fmt.Sprintf("%s-supportpkg-%s", product, unixTimeString)
171+
tarballRootDirName := "."
172172

173173
err := c.LogFile.Close()
174174
if err != nil {
@@ -327,12 +327,12 @@ func (c *DataCollector) AllNamespacesExist() bool {
327327
return allExist
328328
}
329329

330-
func (c *DataCollector) GenerateManifest(product string, startTime time.Time, jobsRun, jobsFailed int, jobTimings []JobTiming) ([]byte, error) {
330+
func (c *DataCollector) GenerateManifest(product string, startTime time.Time, jobsRun, jobsFailed int, jobTimings []JobInfo) ([]byte, error) {
331331
manifest := Manifest{
332332
Version: "1.2", // Match the schema version
333333
Timestamp: TimestampInfo{
334-
Start: startTime.UTC().Format(time.RFC3339),
335-
Stop: time.Now().UTC().Format(time.RFC3339),
334+
Start: startTime.UTC().Format(time.RFC3339Nano),
335+
Stop: time.Now().UTC().Format(time.RFC3339Nano),
336336
},
337337
PackageType: "root", // As defined in schema enum
338338
RootDir: ".",

pkg/jobs/job.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"os"
2525
"path/filepath"
26+
"strings"
2627
"time"
2728

2829
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
@@ -40,7 +41,7 @@ type JobResult struct {
4041
Skipped bool
4142
}
4243

43-
func (j Job) Collect(dc *data_collector.DataCollector) JobResult {
44+
func (j Job) Collect(dc *data_collector.DataCollector) (error, bool, []string) {
4445
ch := make(chan JobResult, 1)
4546

4647
ctx, cancel := context.WithTimeout(context.Background(), j.Timeout)
@@ -52,32 +53,44 @@ func (j Job) Collect(dc *data_collector.DataCollector) JobResult {
5253
select {
5354
case <-ctx.Done():
5455
dc.Logger.Printf("\tJob %s has timed out: %s\n---\n", j.Name, ctx.Err())
55-
return JobResult{Error: fmt.Errorf("Context cancelled: %v", ctx.Err()), Skipped: false}
56+
return fmt.Errorf("Context cancelled: %v", ctx.Err()), false, nil
5657

5758
case jobResults := <-ch:
59+
files := j.GetFilesFromJobResult(dc, jobResults)
5860
if jobResults.Skipped {
5961
dc.Logger.Printf("\tJob %s has been skipped\n---\n", j.Name)
60-
return JobResult{Error: nil, Skipped: true}
62+
return nil, true, files
6163
}
6264
if jobResults.Error != nil {
6365
dc.Logger.Printf("\tJob %s has failed: %s\n", j.Name, jobResults.Error)
64-
return JobResult{Error: jobResults.Error, Skipped: false}
66+
return jobResults.Error, false, files
6567
}
6668

6769
for fileName, fileValue := range jobResults.Files {
6870
err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
6971
if err != nil {
70-
return JobResult{Error: fmt.Errorf("MkdirAll failed: %v", err), Skipped: jobResults.Skipped}
72+
return fmt.Errorf("MkdirAll failed: %v", err), jobResults.Skipped, files
7173
}
7274
file, _ := os.Create(fileName)
7375
_, err = file.Write(fileValue)
7476
if err != nil {
75-
return JobResult{Error: fmt.Errorf("Write failed: %v", err), Skipped: jobResults.Skipped}
77+
return fmt.Errorf("Write failed: %v", err), jobResults.Skipped, files
7678
}
7779
_ = file.Close()
7880
dc.Logger.Printf("\tJob %s wrote %d bytes to %s\n", j.Name, len(fileValue), fileName)
7981
}
8082
dc.Logger.Printf("\tJob %s completed successfully\n---\n", j.Name)
81-
return JobResult{Files: jobResults.Files, Error: nil, Skipped: false}
83+
return nil, false, files
8284
}
8385
}
86+
87+
func (j Job) GetFilesFromJobResult(dc *data_collector.DataCollector, jobResult JobResult) []string {
88+
files := make([]string, 0, len(jobResult.Files))
89+
for filename := range jobResult.Files {
90+
if len(filename) > 0 {
91+
packagePath := strings.TrimPrefix(filename, dc.BaseDir)
92+
files = append(files, packagePath)
93+
}
94+
}
95+
return files
96+
}

0 commit comments

Comments
 (0)