Skip to content

Commit

Permalink
feat: always add tile source revision and kiln version to kiln_metada…
Browse files Browse the repository at this point in the history
…ta in product template
  • Loading branch information
crhntr committed Jul 20, 2023
1 parent cb8096e commit 12a446a
Show file tree
Hide file tree
Showing 16 changed files with 482 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/cloudfoundry/bosh-cli v6.4.1+incompatible
github.com/cloudfoundry/bosh-utils v0.0.0-20210130100352-ab14c90ad9f2
github.com/cppforlife/go-patch v0.2.0
github.com/crhntr/yamlutil v0.0.0-20230523004714-d7a84a7a5d64
github.com/crhntr/yamlutil v0.0.0-20230524174859-4e4388958875
github.com/cucumber/godog v0.12.5
github.com/cucumber/messages-go/v16 v16.0.1
github.com/docker/docker v23.0.0-rc.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ github.com/cppforlife/go-semi-semantic v0.0.0-20160921010311-576b6af77ae4 h1:J+g
github.com/cppforlife/go-semi-semantic v0.0.0-20160921010311-576b6af77ae4/go.mod h1:socxpf5+mELPbosI149vWpNlHK6mbfWFxSWOoSndXR8=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crhntr/yamlutil v0.0.0-20230523004714-d7a84a7a5d64 h1:v9TB0YLlfTV7nZtDt8Amb9ml/udpjSE63P+0qmfK6aA=
github.com/crhntr/yamlutil v0.0.0-20230523004714-d7a84a7a5d64/go.mod h1:ksg8an2nXLcrZReC9jceNV/Wj9OPSrN4YIH3qYRCswo=
github.com/crhntr/yamlutil v0.0.0-20230524174859-4e4388958875 h1:AqLZ3TsDKb96pHcL5/SgUAlq7hM26OKFTFs0E1SozxU=
github.com/crhntr/yamlutil v0.0.0-20230524174859-4e4388958875/go.mod h1:ksg8an2nXLcrZReC9jceNV/Wj9OPSrN4YIH3qYRCswo=
github.com/cucumber/gherkin-go/v19 v19.0.3 h1:mMSKu1077ffLbTJULUfM5HPokgeBcIGboyeNUof1MdE=
github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw=
github.com/cucumber/godog v0.12.5 h1:FZIy6VCfMbmGHts9qd6UjBMT9abctws/pQYO/ZcwOVs=
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 @@ -12,6 +12,8 @@ import (
"testing"
"time"

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

"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"

Expand All @@ -20,9 +22,13 @@ import (
. "github.com/pivotal-cf-experimental/gomegamatchers"
)

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 @@ -147,7 +153,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 @@ -218,7 +224,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 @@ -261,7 +267,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 @@ -350,7 +356,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 @@ -458,7 +464,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 @@ -701,7 +707,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 @@ -873,6 +879,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 @@ -895,6 +904,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 @@ -914,6 +926,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 @@ -930,4 +945,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: {}
15 changes: 14 additions & 1 deletion internal/builder/interpolator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 @@ -55,6 +56,15 @@ func NewInterpolator() Interpolator {
}

func (i Interpolator) Interpolate(input InterpolateInput, name string, templateYAML []byte) ([]byte, error) {
var gitMetadataSHA string
if input.MetadataGitSHA != nil {
sha, err := input.MetadataGitSHA()
if err != nil {
return nil, err
}
gitMetadataSHA = sha
}

interpolatedYAML, err := i.interpolate(input, name, templateYAML)
if err != nil {
return nil, err
Expand All @@ -65,7 +75,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: gitMetadataSHA,
})
}

func (i Interpolator) functions(input InterpolateInput) template.FuncMap {
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,omitempty"`
KilnVersion string `yaml:"kiln_version,omitempty"`
}

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: kiln_metadata is not set by kiln please remove it from your product template")
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))
})
}
}
83 changes: 83 additions & 0 deletions internal/builder/testdata/append_kiln_metadata/input_metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
description: Serves a "Hello World" server.
icon_image: some-image
form_types: []
job_types:
- description: HTTP Server
dynamic_ip: 0
instance_definition:
configurable: true
constraints:
max: 1
min: 0
default: 1
label: Instances
name: instances
type: integer
label: Server
max_in_flight: 1
name: hello-server
resource_definitions:
- configurable: true
constraints:
min: 1024
default: 1024
label: RAM
name: ram
type: integer
- configurable: true
constraints:
min: 2000
default: 4000
label: Ephemeral Disk
name: ephemeral_disk
type: integer
- configurable: false
constraints:
min: 2000
default: 4000
label: Persistent Disk
name: persistent_disk
type: integer
- configurable: true
constraints:
min: 1
default: 1
label: CPU
name: cpu
type: integer
resource_label: Server
single_az_only: true
static_ip: 1
templates:
- manifest: |
job-properties:
port: 8080
name: hello-server
release: hello-release
- manifest: {}
name: bpm
release: bpm
label: Hello
metadata_version: 2.7.0
minimum_version_for_upgrade: 0.1.0
name: hello
product_version: 0.1.2
property_blueprints: []
provides_product_versions:
- name: hello
version: 0.1.2
rank: 90
releases:
- file: hello-release-v0.1.4-ubuntu-xenial-621.256.tgz
name: hello-release
sha1: c471ac6371eb8fc24508b14d9a49a44f9a5ef98c
version: v0.1.4
- file: bpm-1.1.18-ubuntu-xenial-621.256.tgz
name: bpm
sha1: 476c516e0644564838c025b165560bb24102fe6f
version: 1.1.18
runtime_configs: []
serial: false
stemcell_criteria:
os: ubuntu-xenial
version: "621.256"
Loading

0 comments on commit 12a446a

Please sign in to comment.