Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kiln bake always adds tile source revision and kiln version to kiln_metadata in product template #414

Merged
merged 7 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ require (
github.com/containerd/containerd v1.6.18 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/cppforlife/go-semi-semantic v0.0.0-20160921010311-576b6af77ae4 // indirect
github.com/crhntr/bijection v0.0.0-20230628013949-46b5c800bc70 // indirect
github.com/cucumber/gherkin-go/v19 v19.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
Expand Down
38 changes: 28 additions & 10 deletions internal/acceptance/bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/pivotal-cf-experimental/gomegamatchers"

"github.com/pivotal-cf/kiln/internal/builder"
)

const (
buildVersion = "0.0.0-dev.0+acceptance"
)

var (
pathToMain string
buildVersion string
pathToMain string
tileSourceRevision string
)

func TestAcceptance(t *testing.T) {
Expand All @@ -32,12 +38,12 @@ func TestAcceptance(t *testing.T) {
}

var _ = BeforeSuite(func() {
buildVersion = fmt.Sprintf("v0.0.0-dev.%d", time.Now().Unix())

var err error
pathToMain, err = gexec.Build("github.com/pivotal-cf/kiln",
"--ldflags", fmt.Sprintf("-X main.version=%s", buildVersion))
Expect(err).NotTo(HaveOccurred())
tileSourceRevision, err = builder.GitMetadataSHA(".", true)
Expect(err).NotTo(HaveOccurred())
})

var _ = AfterSuite(func() {
Expand Down Expand Up @@ -146,7 +152,7 @@ var _ = Describe("bake command", func() {
metadataContents, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())

renderedYAML := fmt.Sprintf(expectedMetadata, diegoSHA1, cfSHA1)
renderedYAML := fmt.Sprintf(expectedMetadata, diegoSHA1, cfSHA1, tileSourceRevision)
Expect(metadataContents).To(HelpfullyMatchYAML(renderedYAML))

archivedMigration1, err := bakedTile.Open("migrations/v1/201603041539_custom_buildpacks.js")
Expand Down Expand Up @@ -217,7 +223,7 @@ var _ = Describe("bake command", func() {
metadataContents, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())

renderedYAML := fmt.Sprintf(expectedMetadataWithMultipleStemcells, cfSHA1)
renderedYAML := fmt.Sprintf(expectedMetadataWithMultipleStemcells, cfSHA1, tileSourceRevision)
Expect(metadataContents).To(HelpfullyMatchYAML(renderedYAML))
})
})
Expand Down Expand Up @@ -260,7 +266,7 @@ var _ = Describe("bake command", func() {
metadataContents, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())

renderedYAML := fmt.Sprintf(expectedMetadataWithStemcellTarball, cfSHA1)
renderedYAML := fmt.Sprintf(expectedMetadataWithStemcellTarball, cfSHA1, tileSourceRevision)
Expect(metadataContents).To(HelpfullyMatchYAML(renderedYAML))
})
})
Expand Down Expand Up @@ -347,7 +353,7 @@ var _ = Describe("bake command", func() {
metadataContents, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())

renderedYAML := fmt.Sprintf(expectedMetadata, diegoSHA1, cfSHA1)
renderedYAML := fmt.Sprintf(expectedMetadata, diegoSHA1, cfSHA1, tileSourceRevision)
Expect(metadataContents).To(HelpfullyMatchYAML(renderedYAML))

archivedMigration1, err := bakedTile.Open("migrations/v1/201603041539_custom_buildpacks.js")
Expand Down Expand Up @@ -454,7 +460,7 @@ var _ = Describe("bake command", func() {
// intervals added to make tests pass. it is taking too long locally
Eventually(session, time.Second*10).Should(gexec.Exit(0))

renderedYAML := fmt.Sprintf(expectedMetadata, diegoSHA1, cfSHA1)
renderedYAML := fmt.Sprintf(expectedMetadata, diegoSHA1, cfSHA1, tileSourceRevision)
Eventually(session.Out.Contents).Should(HelpfullyMatchYAML(renderedYAML))
})
})
Expand Down Expand Up @@ -696,7 +702,7 @@ var _ = Describe("bake command", func() {
metadataContents, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())

renderedYAML := fmt.Sprintf(expectedMetadataWithStemcellCriteria, diegoSHA1, cfSHA1)
renderedYAML := fmt.Sprintf(expectedMetadataWithStemcellCriteria, diegoSHA1, cfSHA1, tileSourceRevision)
Expect(metadataContents).To(HelpfullyMatchYAML(renderedYAML))

Eventually(session.Err).Should(gbytes.Say("Reading release manifests"))
Expand Down Expand Up @@ -867,6 +873,9 @@ some_runtime_configs:
version: some-addon-version
serial: false
selected_value: "235"
kiln_metadata:
kiln_version: 0.0.0-dev.0+acceptance
metadata_git_sha: %s
`

var expectedMetadataWithStemcellCriteria = `---
Expand All @@ -889,6 +898,9 @@ stemcell_criteria:
version: 250.21
requires_cpi: false
enable_patch_security_updates: true
kiln_metadata:
kiln_version: 0.0.0-dev.0+acceptance
metadata_git_sha: %s
`

var expectedMetadataWithMultipleStemcells = `---
Expand All @@ -908,6 +920,9 @@ stemcell_criteria:
additional_stemcells_criteria:
- os: windows
version: "2019.4"
kiln_metadata:
kiln_version: 0.0.0-dev.0+acceptance
metadata_git_sha: %s
`

var expectedMetadataWithStemcellTarball = `---
Expand All @@ -924,4 +939,7 @@ some_releases:
stemcell_criteria:
os: ubuntu-trusty
version: "3215.4"
kiln_metadata:
kiln_version: 0.0.0-dev.0+acceptance
metadata_git_sha: %s
`
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ label: Pivotal Elastic Runtime
stemcell_criteria: $( stemcell "ubuntu-trusty" )
additional_stemcells_criteria:
- $( stemcell "windows" )
kiln_metadata: {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ stemcell_criteria:
version: 250.21
requires_cpi: false
enable_patch_security_updates: true
kiln_metadata: {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ icon_img: $( icon )
product_version: $( version )
label: Pivotal Elastic Runtime
stemcell_criteria: $( stemcell )
kiln_metadata: {}
24 changes: 14 additions & 10 deletions internal/builder/interpolator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"text/template"

"github.com/pivotal-cf/kiln/pkg/proofing"

yamlConverter "github.com/ghodss/yaml"
"gopkg.in/yaml.v2"
)
Expand All @@ -35,6 +37,7 @@ type Interpolator struct{}

type InterpolateInput struct {
Version string
KilnVersion string
BOSHVariables map[string]any
Variables map[string]any
ReleaseManifests map[string]any
Expand All @@ -47,7 +50,7 @@ type InterpolateInput struct {
PropertyBlueprints map[string]any
RuntimeConfigs map[string]any
StubReleases bool
MetadataGitSHA func() (string, error)
MetadataGitSHA string
}

func NewInterpolator() Interpolator {
Expand All @@ -65,7 +68,10 @@ func (i Interpolator) Interpolate(input InterpolateInput, name string, templateY
return nil, err // un-tested
}

return prettyMetadata, nil
return setKilnMetadata(prettyMetadata, KilnMetadata{
KilnVersion: input.KilnVersion,
MetadataGitSHA: input.MetadataGitSHA,
})
}

func (i Interpolator) functions(input InterpolateInput) template.FuncMap {
Expand Down Expand Up @@ -124,11 +130,11 @@ func (i Interpolator) functions(input InterpolateInput) template.FuncMap {

if !ok {
if input.StubReleases {
val = map[string]any{
"name": name,
"version": "UNKNOWN",
"file": fmt.Sprintf("%s-UNKNOWN.tgz", name),
"sha1": "dead8e1ea5e00dead8e1ea5ed00ead8e1ea5e000",
val = proofing.Release{
Name: name,
Version: "UNKNOWN",
File: fmt.Sprintf("%s-UNKNOWN.tgz", name),
SHA1: "dead8e1ea5e00dead8e1ea5ed00ead8e1ea5e000",
}
} else {
return "", fmt.Errorf("could not find release with name '%s'", name)
Expand Down Expand Up @@ -171,9 +177,7 @@ func (i Interpolator) functions(input InterpolateInput) template.FuncMap {
if !ok {
switch key {
case MetadataGitSHAVariable:
if input.MetadataGitSHA != nil {
return input.MetadataGitSHA()
}
return input.MetadataGitSHA, nil
case BuildVersionVariable:
return versionFunc()
}
Expand Down
10 changes: 6 additions & 4 deletions internal/builder/interpolator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"testing"

"github.com/pivotal-cf/kiln/pkg/proofing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/pivotal-cf-experimental/gomegamatchers"
Expand Down Expand Up @@ -59,7 +61,7 @@ selected_value: $( release "some-release" | select "version" )
"some-variable": "some-value",
},
ReleaseManifests: map[string]any{
"some-release": builder.ReleaseManifest{
"some-release": proofing.Release{
Name: "some-release",
Version: "1.2.3",
File: "some-release-1.2.3.tgz",
Expand Down Expand Up @@ -224,7 +226,7 @@ some_form_types:
BeforeEach(func() {
input = builder.InterpolateInput{
ReleaseManifests: map[string]any{
"some-release": builder.ReleaseManifest{
"some-release": proofing.Release{
Name: "some-release",
Version: "1.2.3",
File: "some-release-1.2.3.tgz",
Expand Down Expand Up @@ -289,7 +291,7 @@ stemcell_criteria: $( stemcell )`

input = builder.InterpolateInput{
ReleaseManifests: map[string]any{
"some-release": builder.ReleaseManifest{
"some-release": proofing.Release{
Name: "some-release",
Version: "1.2.3",
File: "some-release-1.2.3.tgz",
Expand Down Expand Up @@ -329,7 +331,7 @@ some_runtime_configs:

input = builder.InterpolateInput{
ReleaseManifests: map[string]any{
"some-release": builder.ReleaseManifest{
"some-release": proofing.Release{
Name: "some-release",
Version: "1.2.3",
File: "some-release-1.2.3.tgz",
Expand Down
54 changes: 54 additions & 0 deletions internal/builder/kiln_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package builder

import (
"bytes"
"fmt"

"github.com/crhntr/yamlutil/yamlnode"
"gopkg.in/yaml.v3"
)

type KilnMetadata struct {
MetadataGitSHA string `yaml:"metadata_git_sha"`
KilnVersion string `yaml:"kiln_version"`
}

func setKilnMetadata(in []byte, kilnMetadata KilnMetadata) ([]byte, error) {
var productTemplate yaml.Node
err := yaml.Unmarshal(in, &productTemplate)
if err != nil {
return nil, fmt.Errorf("failed to parse product template: %w", err)
}

_, hasMetadataVersionKey := yamlnode.LookupKey(&productTemplate, "metadata_version")
if !hasMetadataVersionKey {
return in, nil
}

kilnMetadataValueNode, fieldExists := yamlnode.LookupKey(&productTemplate, "kiln_metadata")
if fieldExists {
fmt.Println(`WARNING: the metadata field "kiln_metadata" is owned by Kiln. You are setting it in your "base.yml". You should not set it anymore.`)
if err := kilnMetadataValueNode.Encode(kilnMetadata); err != nil {
return nil, err
}
} else {
var productTemplatePartial yaml.Node
if err := productTemplatePartial.Encode(struct {
KilnMetadata KilnMetadata `yaml:"kiln_metadata"`
}{
KilnMetadata: kilnMetadata,
}); err != nil {
return nil, fmt.Errorf("failed to encode kiln_metadata: %w", err)
}
productTemplate.Content[0].Content = append(productTemplate.Content[0].Content, productTemplatePartial.Content...)
}

var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err = enc.Encode(productTemplate.Content[0])
if err != nil {
return nil, fmt.Errorf("failed to encode product template: %w", err)
}
return buf.Bytes(), nil
}
31 changes: 31 additions & 0 deletions internal/builder/kiln_metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package builder

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_setKilnMetadata(t *testing.T) {
for _, tt := range []struct{ Name string }{
{Name: "append_kiln_metadata"},
{Name: "replace_kiln_metadata"},
} {
t.Run(tt.Name, func(t *testing.T) {
inputMetadataYML, err := os.ReadFile(filepath.Join("testdata", tt.Name, "input_metadata.yml"))
require.NoError(t, err)
outputMetadataYML, err := os.ReadFile(filepath.Join("testdata", tt.Name, "output_metadata.yml"))
require.NoError(t, err)

result, err := setKilnMetadata(inputMetadataYML, KilnMetadata{
MetadataGitSHA: "some-commit-sha",
KilnVersion: "some-kiln-version",
})
require.NoError(t, err)
assert.Equal(t, string(outputMetadataYML), string(result))
})
}
}
Loading
Loading