Skip to content

Commit 1557256

Browse files
authored
Merge pull request #20 from j-fuentes/report-versions
Include preflight-version, and package information in reports
2 parents 7a2806a + dd1f7dc commit 1557256

File tree

8 files changed

+84
-37
lines changed

8 files changed

+84
-37
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ DOCKER_IMAGE?=quay.io/jetstack/preflight
99
DOCKER_IMAGE_TAG?=$(DOCKER_IMAGE):$(VERSION)
1010

1111
define LDFLAGS
12-
-X "github.com/jetstack/preflight/cmd.PreflightVersion=$(VERSION)" \
13-
-X "github.com/jetstack/preflight/cmd.Platform=$(GOOS)/$(GOARCH)" \
14-
-X "github.com/jetstack/preflight/cmd.Commit=$(COMMIT)" \
15-
-X "github.com/jetstack/preflight/cmd.BuildDate=$(DATE)" \
16-
-X "github.com/jetstack/preflight/cmd.GoVersion=$(GOVERSION)"
12+
-X "github.com/jetstack/preflight/pkg/version.PreflightVersion=$(VERSION)" \
13+
-X "github.com/jetstack/preflight/pkg/version.Platform=$(GOOS)/$(GOARCH)" \
14+
-X "github.com/jetstack/preflight/pkg/version.Commit=$(COMMIT)" \
15+
-X "github.com/jetstack/preflight/pkg/version.BuildDate=$(DATE)" \
16+
-X "github.com/jetstack/preflight/pkg/version.GoVersion=$(GOVERSION)"
1717
endef
1818

1919
GO_BUILD:=go build -ldflags '$(LDFLAGS)'
@@ -40,7 +40,7 @@ lint: vet
4040
cd $(ROOT_DIR) && golint
4141

4242
./builds/preflight-$(GOOS)-$(GOARCH):
43-
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o ./builds/preflight-$(GOOS)-$(GOARCH) .
43+
GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) -o ./builds/preflight-$(GOOS)-$(GOARCH) .
4444

4545
build-all-platforms:
4646
$(MAKE) GOOS=linux GOARCH=amd64 ./builds/preflight-linux-amd64

api/report.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@ package api
22

33
// Report contains the fields of a Preflight report
44
type Report struct {
5-
// Unique ID of the report
5+
// Unique ID of the report.
66
ID string `json:"id"`
7-
// Timestamp indicates when the report was generated
7+
// PreflightVersion indicates the version of preflight this report was generated with.
8+
PreflightVersion string `json:"preflight-version"`
9+
// Timestamp indicates when the report was generated.
810
Timestamp Time `json:"timestamp"`
9-
// Cluster indicates which was the target of the report
11+
// Cluster indicates which was the target of the report.
1012
Cluster string `json:"cluster"`
11-
// Package indicates which package was used for the report
12-
Package string `json:"package"`
13-
Name string `json:"name"`
14-
Description string `json:"description,omitempty"`
15-
Sections []ReportSection `json:"sections,omitempty"`
13+
// Package indicates which package was used for the report. (deprecated)
14+
Package string `json:"package"`
15+
// PackageInformation contains all the information about the package that was used to generate the report.
16+
PackageInformation PackageInformation `json:"package-information"`
17+
// Name is the name of the package that was used for this report.
18+
Name string `json:"name"`
19+
// Description is the description of the package that was used for this report.
20+
Description string `json:"description,omitempty"`
21+
// Sections contains the sections of the package that was used for this report.
22+
Sections []ReportSection `json:"sections,omitempty"`
23+
}
24+
25+
// PackageInformation contains all the details to identify a package.
26+
type PackageInformation struct {
27+
// Namespace the package belongs to.
28+
Namespace string `json:"namespace"`
29+
// ID is the ID of the package.
30+
ID string `json:"id"`
31+
// Version is the version of the package.
32+
Version string `json:"version"`
1633
}
1734

1835
// ReportSection contains the fields of a section inside a Report

cmd/version.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@ package cmd
33
import (
44
"fmt"
55

6+
"github.com/jetstack/preflight/pkg/version"
7+
68
"github.com/spf13/cobra"
79
)
810

9-
// PreflightVersion hosts the version of the app. It is injected at build time.
10-
var PreflightVersion = "development"
11-
12-
// Commit is the commit hash of the build
13-
var Commit string
14-
15-
// BuildDate is the date it was built
16-
var BuildDate string
17-
18-
// GoVersion is the go version that was used to compile this
19-
var GoVersion string
20-
21-
// Platform is the target platform this was compiled for
22-
var Platform string
23-
2411
var verbose bool
2512

2613
var versionCmd = &cobra.Command{
@@ -29,12 +16,12 @@ var versionCmd = &cobra.Command{
2916
Long: `Display preflight version.
3017
`,
3118
Run: func(cmd *cobra.Command, args []string) {
32-
fmt.Println("Preflight version: ", PreflightVersion, Platform)
19+
fmt.Println("Preflight version: ", version.PreflightVersion, version.Platform)
3320
if verbose {
3421
fmt.Println()
35-
fmt.Println("Commit: ", Commit)
36-
fmt.Println("Built: ", BuildDate)
37-
fmt.Println("Go: ", GoVersion)
22+
fmt.Println("Commit: ", version.Commit)
23+
fmt.Println("Built: ", version.BuildDate)
24+
fmt.Println("Go: ", version.GoVersion)
3825
}
3926
},
4027
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ require (
88
github.com/gomarkdown/markdown v0.0.0-20191104174740-4d42851d4d5a
99
github.com/gookit/color v1.2.0
1010
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
11+
github.com/mattn/go-colorable v0.1.4 // indirect
1112
github.com/open-policy-agent/opa v0.15.0
1213
github.com/pkg/errors v0.8.1
1314
github.com/sergi/go-diff v1.0.0 // indirect
1415
github.com/spf13/cobra v0.0.5
1516
github.com/spf13/viper v1.5.0
1617
github.com/yudai/gojsondiff v1.0.0
1718
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
19+
github.com/yudai/pp v2.0.1+incompatible // indirect
1820
golang.org/x/arch v0.0.0-20191126211547-368ea8f32fff // indirect
1921
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
2022
google.golang.org/api v0.13.0

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
146146
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
147147
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
148148
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
149+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
150+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
151+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
152+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
149153
github.com/mattn/go-runewidth v0.0.0-20181025052659-b20a3daf6a39/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
150154
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
151155
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
@@ -233,6 +237,8 @@ github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCO
233237
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
234238
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
235239
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
240+
github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI=
241+
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
236242
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
237243
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
238244
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
@@ -305,6 +311,7 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
305311
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
306312
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
307313
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
314+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
308315
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
309316
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
310317
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

pkg/exporter/json.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/jetstack/preflight/api"
99
"github.com/jetstack/preflight/pkg/packaging"
1010
"github.com/jetstack/preflight/pkg/results"
11+
"github.com/jetstack/preflight/pkg/version"
1112
)
1213

1314
// JSONExporter is an Exporter that outputs the results enriched with metadata in a JSON format
@@ -23,7 +24,13 @@ func NewJSONExporter() *JSONExporter {
2324
func (e *JSONExporter) Export(ctx context.Context, policyManifest *packaging.PolicyManifest, intermediateJSON []byte, rc *results.ResultCollection) (*bytes.Buffer, error) {
2425
report := api.Report{
2526
// TODO: we are omitting ID, Timestamp and Cluster for now, but it will get fixed with #1
26-
Package: policyManifest.ID,
27+
PreflightVersion: version.PreflightVersion,
28+
Package: policyManifest.ID,
29+
PackageInformation: api.PackageInformation{
30+
Namespace: policyManifest.Namespace,
31+
ID: policyManifest.ID,
32+
Version: policyManifest.PackageVersion,
33+
},
2734
Name: policyManifest.Name,
2835
Description: policyManifest.Description,
2936
Sections: make([]api.ReportSection, len(policyManifest.Sections)),

pkg/exporter/json_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import (
1414

1515
func TestJSONExport(t *testing.T) {
1616
pm := &packaging.PolicyManifest{
17-
ID: "test-pkg",
18-
Name: "Test Package",
19-
Description: "This is a test package.",
17+
SchemaVersion: "1.0.0",
18+
Namespace: "test.org",
19+
ID: "test-pkg",
20+
PackageVersion: "1.2.3",
21+
Name: "Test Package",
22+
Description: "This is a test package.",
2023
Sections: []packaging.Section{
2124
packaging.Section{
2225
ID: "section-1",
@@ -156,6 +159,12 @@ func TestJSONExport(t *testing.T) {
156159
"description": "This is a test package.",
157160
"name": "Test Package",
158161
"package": "test-pkg",
162+
"package-information": {
163+
"id": "test-pkg",
164+
"namespace": "test.org",
165+
"version": "1.2.3"
166+
},
167+
"preflight-version": "development",
159168
"cluster": "",
160169
"timestamp": "0001-01-01T00:00:00Z",
161170
"id": ""

pkg/version/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package version
2+
3+
// This variables are injected at build time.
4+
5+
// PreflightVersion hosts the version of the app.
6+
var PreflightVersion = "development"
7+
8+
// Commit is the commit hash of the build
9+
var Commit string
10+
11+
// BuildDate is the date it was built
12+
var BuildDate string
13+
14+
// GoVersion is the go version that was used to compile this
15+
var GoVersion string
16+
17+
// Platform is the target platform this was compiled for
18+
var Platform string

0 commit comments

Comments
 (0)