Skip to content

Commit

Permalink
Merge pull request #20 from j-fuentes/report-versions
Browse files Browse the repository at this point in the history
Include preflight-version, and package information in reports
  • Loading branch information
jetstack-bot authored Dec 17, 2019
2 parents 7a2806a + dd1f7dc commit 1557256
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 37 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ DOCKER_IMAGE?=quay.io/jetstack/preflight
DOCKER_IMAGE_TAG?=$(DOCKER_IMAGE):$(VERSION)

define LDFLAGS
-X "github.com/jetstack/preflight/cmd.PreflightVersion=$(VERSION)" \
-X "github.com/jetstack/preflight/cmd.Platform=$(GOOS)/$(GOARCH)" \
-X "github.com/jetstack/preflight/cmd.Commit=$(COMMIT)" \
-X "github.com/jetstack/preflight/cmd.BuildDate=$(DATE)" \
-X "github.com/jetstack/preflight/cmd.GoVersion=$(GOVERSION)"
-X "github.com/jetstack/preflight/pkg/version.PreflightVersion=$(VERSION)" \
-X "github.com/jetstack/preflight/pkg/version.Platform=$(GOOS)/$(GOARCH)" \
-X "github.com/jetstack/preflight/pkg/version.Commit=$(COMMIT)" \
-X "github.com/jetstack/preflight/pkg/version.BuildDate=$(DATE)" \
-X "github.com/jetstack/preflight/pkg/version.GoVersion=$(GOVERSION)"
endef

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

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

build-all-platforms:
$(MAKE) GOOS=linux GOARCH=amd64 ./builds/preflight-linux-amd64
Expand Down
33 changes: 25 additions & 8 deletions api/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@ package api

// Report contains the fields of a Preflight report
type Report struct {
// Unique ID of the report
// Unique ID of the report.
ID string `json:"id"`
// Timestamp indicates when the report was generated
// PreflightVersion indicates the version of preflight this report was generated with.
PreflightVersion string `json:"preflight-version"`
// Timestamp indicates when the report was generated.
Timestamp Time `json:"timestamp"`
// Cluster indicates which was the target of the report
// Cluster indicates which was the target of the report.
Cluster string `json:"cluster"`
// Package indicates which package was used for the report
Package string `json:"package"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Sections []ReportSection `json:"sections,omitempty"`
// Package indicates which package was used for the report. (deprecated)
Package string `json:"package"`
// PackageInformation contains all the information about the package that was used to generate the report.
PackageInformation PackageInformation `json:"package-information"`
// Name is the name of the package that was used for this report.
Name string `json:"name"`
// Description is the description of the package that was used for this report.
Description string `json:"description,omitempty"`
// Sections contains the sections of the package that was used for this report.
Sections []ReportSection `json:"sections,omitempty"`
}

// PackageInformation contains all the details to identify a package.
type PackageInformation struct {
// Namespace the package belongs to.
Namespace string `json:"namespace"`
// ID is the ID of the package.
ID string `json:"id"`
// Version is the version of the package.
Version string `json:"version"`
}

// ReportSection contains the fields of a section inside a Report
Expand Down
25 changes: 6 additions & 19 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@ package cmd
import (
"fmt"

"github.com/jetstack/preflight/pkg/version"

"github.com/spf13/cobra"
)

// PreflightVersion hosts the version of the app. It is injected at build time.
var PreflightVersion = "development"

// Commit is the commit hash of the build
var Commit string

// BuildDate is the date it was built
var BuildDate string

// GoVersion is the go version that was used to compile this
var GoVersion string

// Platform is the target platform this was compiled for
var Platform string

var verbose bool

var versionCmd = &cobra.Command{
Expand All @@ -29,12 +16,12 @@ var versionCmd = &cobra.Command{
Long: `Display preflight version.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Preflight version: ", PreflightVersion, Platform)
fmt.Println("Preflight version: ", version.PreflightVersion, version.Platform)
if verbose {
fmt.Println()
fmt.Println("Commit: ", Commit)
fmt.Println("Built: ", BuildDate)
fmt.Println("Go: ", GoVersion)
fmt.Println("Commit: ", version.Commit)
fmt.Println("Built: ", version.BuildDate)
fmt.Println("Go: ", version.GoVersion)
}
},
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ require (
github.com/gomarkdown/markdown v0.0.0-20191104174740-4d42851d4d5a
github.com/gookit/color v1.2.0
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/open-policy-agent/opa v0.15.0
github.com/pkg/errors v0.8.1
github.com/sergi/go-diff v1.0.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.5.0
github.com/yudai/gojsondiff v1.0.0
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
golang.org/x/arch v0.0.0-20191126211547-368ea8f32fff // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
google.golang.org/api v0.13.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.0-20181025052659-b20a3daf6a39/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -233,6 +237,8 @@ github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCO
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI=
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
Expand Down Expand Up @@ -305,6 +311,7 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
9 changes: 8 additions & 1 deletion pkg/exporter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/packaging"
"github.com/jetstack/preflight/pkg/results"
"github.com/jetstack/preflight/pkg/version"
)

// JSONExporter is an Exporter that outputs the results enriched with metadata in a JSON format
Expand All @@ -23,7 +24,13 @@ func NewJSONExporter() *JSONExporter {
func (e *JSONExporter) Export(ctx context.Context, policyManifest *packaging.PolicyManifest, intermediateJSON []byte, rc *results.ResultCollection) (*bytes.Buffer, error) {
report := api.Report{
// TODO: we are omitting ID, Timestamp and Cluster for now, but it will get fixed with #1
Package: policyManifest.ID,
PreflightVersion: version.PreflightVersion,
Package: policyManifest.ID,
PackageInformation: api.PackageInformation{
Namespace: policyManifest.Namespace,
ID: policyManifest.ID,
Version: policyManifest.PackageVersion,
},
Name: policyManifest.Name,
Description: policyManifest.Description,
Sections: make([]api.ReportSection, len(policyManifest.Sections)),
Expand Down
15 changes: 12 additions & 3 deletions pkg/exporter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import (

func TestJSONExport(t *testing.T) {
pm := &packaging.PolicyManifest{
ID: "test-pkg",
Name: "Test Package",
Description: "This is a test package.",
SchemaVersion: "1.0.0",
Namespace: "test.org",
ID: "test-pkg",
PackageVersion: "1.2.3",
Name: "Test Package",
Description: "This is a test package.",
Sections: []packaging.Section{
packaging.Section{
ID: "section-1",
Expand Down Expand Up @@ -156,6 +159,12 @@ func TestJSONExport(t *testing.T) {
"description": "This is a test package.",
"name": "Test Package",
"package": "test-pkg",
"package-information": {
"id": "test-pkg",
"namespace": "test.org",
"version": "1.2.3"
},
"preflight-version": "development",
"cluster": "",
"timestamp": "0001-01-01T00:00:00Z",
"id": ""
Expand Down
18 changes: 18 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package version

// This variables are injected at build time.

// PreflightVersion hosts the version of the app.
var PreflightVersion = "development"

// Commit is the commit hash of the build
var Commit string

// BuildDate is the date it was built
var BuildDate string

// GoVersion is the go version that was used to compile this
var GoVersion string

// Platform is the target platform this was compiled for
var Platform string

0 comments on commit 1557256

Please sign in to comment.