Skip to content

Commit

Permalink
Report the git commit hash in groupversion_info_gen.go (#150)
Browse files Browse the repository at this point in the history
This will help us trace a generated package back to the code that
produced it. The git commit hash (and whether the tree is dirty) is
fed into the build from the Makefile.
  • Loading branch information
babbageclunk authored Jun 18, 2020
1 parent bd2bad2 commit db0082d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
5 changes: 3 additions & 2 deletions hack/generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PACKAGE = github.com/Azure/k8s-infra/hack/generator
DATE ?= $(shell date +%FT%T%z)
VERSION ?= $(shell git rev-list -1 HEAD)
SHORT_VERSION ?= $(shell git rev-parse --short HEAD)
TREE_STATE ?= $(if $(shell git status --porcelain),dirty,clean)
CRD_OPTIONS ?= "crd:crdVersions=v1"

include ../../tools.mk
Expand All @@ -29,8 +30,8 @@ rm-apis: ; $(info $(M) removing previously generated apis…)
$(Q) rm -rf apis

.PHONY: build
build: tidy fmt vet lint ; $(info $(M) buiding ./bin/$(APP))
$(Q) $(GO) build -ldflags "-X $(PACKAGE)/cmd.GitCommit=$(VERSION)" -o ./bin/$(APP)
build: tidy fmt vet lint ; $(info $(M) building ./bin/$(APP))
$(Q) $(GO) build -ldflags "-X $(PACKAGE)/pkg/codegen.GitCommit=$(SHORT_VERSION) -X $(PACKAGE)/pkg/codegen.GitTreeState=$(TREE_STATE)" -o ./bin/$(APP)

.PHONY: lint
lint: $(GOLANGCI_LINT) ; $(info $(M) running golangci configured linters…) ## Lint codebase
Expand Down
13 changes: 8 additions & 5 deletions hack/generator/pkg/astmodel/package_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import (

// PackageDefinition is the definition of a package
type PackageDefinition struct {
GroupName string
PackageName string
GroupName string
PackageName string
GeneratorVersion string

definitions []TypeDefiner
}

// NewPackageDefinition constructs a new package definition
func NewPackageDefinition(groupName string, packageName string) *PackageDefinition {
return &PackageDefinition{groupName, packageName, nil}
func NewPackageDefinition(groupName string, packageName string, genVersion string) *PackageDefinition {
return &PackageDefinition{groupName, packageName, genVersion, nil}
}

func (pkgDef *PackageDefinition) Definitions() []TypeDefiner {
Expand Down Expand Up @@ -210,6 +211,7 @@ Licensed under the MIT license.
*/
// Code generated by k8s-infra-gen. DO NOT EDIT.
// Generator version: {{.GeneratorVersion}}
// Package {{.PackageName}} contains API Schema definitions for the {{.GroupName}} {{.PackageName}} API group
// +kubebuilder:object:generate=true
Expand All @@ -234,7 +236,8 @@ var (
AddToScheme = SchemeBuilder.AddToScheme
localSchemeBuilder = SchemeBuilder.SchemeBuilder
)`))
)
`))

func emitGroupVersionFile(pkgDef *PackageDefinition, outputDir string) error {
buf := &bytes.Buffer{}
Expand Down
34 changes: 34 additions & 0 deletions hack/generator/pkg/astmodel/package_definition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package astmodel

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
)

func TestPackageGroupVersion_IncludesGeneratorVersion(t *testing.T) {
g := NewGomegaWithT(t)

pkg := NewPackageDefinition("longest-johns", "santiana", "latest-version")

destDir, err := ioutil.TempDir("", "package_definition_test")
g.Expect(err).To(BeNil())
defer os.RemoveAll(destDir)

fileCount, err := pkg.EmitDefinitions(destDir)
g.Expect(err).To(BeNil())
g.Expect(fileCount).To(Equal(0))

gvFile := filepath.Join(destDir, "groupversion_info_gen.go")
data, err := ioutil.ReadFile(gvFile)
g.Expect(err).To(BeNil())
g.Expect(string(data)).To(ContainSubstring("// Generator version: latest-version"))
}
8 changes: 5 additions & 3 deletions hack/generator/pkg/codegen/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/Azure/k8s-infra/hack/generator/pkg/astmodel"
"github.com/Azure/k8s-infra/hack/generator/pkg/config"
"github.com/Azure/k8s-infra/hack/generator/pkg/jsonast"
"github.com/xeipuuv/gojsonreference"
"github.com/bmatcuk/doublestar"
"github.com/xeipuuv/gojsonreference"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -54,6 +54,7 @@ func NewCodeGenerator(configurationFile string) (*CodeGenerator, error) {

// Generate produces the Go code corresponding to the configured JSON schema in the given output folder
func (generator *CodeGenerator) Generate(ctx context.Context) error {
klog.V(1).Infof("Generator version: %v", combinedVersion())
klog.V(0).Infof("Loading JSON schema %v", generator.configuration.SchemaURL)
schema, err := loadSchema(ctx, generator.configuration.SchemaURL)
if err != nil {
Expand Down Expand Up @@ -137,7 +138,7 @@ func (generator *CodeGenerator) MarkLatestResourceVersionsForStorage(

for _, pkg := range pkgs {

resultPkg := astmodel.NewPackageDefinition(pkg.GroupName, pkg.PackageName)
resultPkg := astmodel.NewPackageDefinition(pkg.GroupName, pkg.PackageName, pkg.GeneratorVersion)
for _, def := range pkg.Definitions() {
// see if it is a resource (only struct definitions can be resources)
if structDef, ok := def.(*astmodel.StructDefinition); ok && structDef.IsResource() {
Expand Down Expand Up @@ -246,6 +247,7 @@ func (generator *CodeGenerator) FilterDefinitions(
func (generator *CodeGenerator) CreatePackagesForDefinitions(
definitions []astmodel.TypeDefiner) ([]*astmodel.PackageDefinition, error) {

genVersion := combinedVersion()
packages := make(map[astmodel.PackageReference]*astmodel.PackageDefinition)
for _, def := range definitions {
defName := def.Name()
Expand All @@ -258,7 +260,7 @@ func (generator *CodeGenerator) CreatePackagesForDefinitions(
if pkg, ok := packages[pkgRef]; ok {
pkg.AddDefinition(def)
} else {
pkg = astmodel.NewPackageDefinition(groupName, pkgName)
pkg = astmodel.NewPackageDefinition(groupName, pkgName, genVersion)
pkg.AddDefinition(def)
packages[pkgRef] = pkg
}
Expand Down
24 changes: 24 additions & 0 deletions hack/generator/pkg/codegen/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package codegen

import (
"fmt"
)

var (
// GitCommit and GitTreeState are populated by the Makefile.
GitCommit string
GitTreeState string
)

func combinedVersion() string {
result := GitCommit
if GitTreeState != "clean" {
result += fmt.Sprintf(" (tree is %s)", GitTreeState)
}
return result
}

0 comments on commit db0082d

Please sign in to comment.