Skip to content
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
19 changes: 19 additions & 0 deletions internal/carvel/baker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/pivotal-cf/kiln/internal/carvel/models"
"github.com/pivotal-cf/kiln/pkg/cargo"
"github.com/pivotal-cf/kiln/pkg/proofing"

"github.com/hashicorp/go-version"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -89,6 +90,9 @@ func (b *baker) Bake(source string) error {
if err != nil {
return err
}
if err := validateVariables(b.metadata.Variables); err != nil {
return err
}

ver, err := b.GetVersion()
if err != nil {
Expand Down Expand Up @@ -137,6 +141,9 @@ func (b *baker) BakeFromLockfile(source string, releaseLock cargo.BOSHReleaseTar
if err != nil {
return err
}
if err := validateVariables(b.metadata.Variables); err != nil {
return err
}

ver, err := b.GetVersion()
if err != nil {
Expand Down Expand Up @@ -248,6 +255,18 @@ func (b *baker) progress(message string) {
_, _ = fmt.Fprintln(b.progressWriter, message)
}

func validateVariables(vars []proofing.Variable) error {
var errs []error
for i, v := range vars {
if v.Name == "" {
errs = append(errs, fmt.Errorf("variables[%d]: missing required field 'name'", i))
} else if v.Type == "" {
errs = append(errs, fmt.Errorf("variables[%d] (%q): missing required field 'type'", i, v.Name))
}
}
return errors.Join(errs...)
}

func (b *baker) generateBoshReleaseDir() error {
dirName := path.Join(b.source, ".boshrelease")
err := os.RemoveAll(dirName)
Expand Down
59 changes: 51 additions & 8 deletions internal/carvel/baker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
. "github.com/onsi/gomega"
"github.com/pivotal-cf/kiln/internal/carvel/models"
"github.com/pivotal-cf/kiln/pkg/cargo"
"github.com/pivotal-cf/kiln/pkg/proofing"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -140,12 +141,12 @@ var _ = Describe("Carvel Baker", func() {
subject = NewBaker()
subject.SetWriter(GinkgoWriter)
})
AfterEach(func() {
// Clean up the temp directory
if inputPath != "" {
_ = os.RemoveAll(filepath.Dir(inputPath))
}
})
AfterEach(func() {
// Clean up the temp directory
if inputPath != "" {
_ = os.RemoveAll(filepath.Dir(inputPath))
}
})
JustBeforeEach(func() {
err = subject.Bake(inputPath)
})
Expand All @@ -169,7 +170,11 @@ var _ = Describe("Carvel Baker", func() {
Expect(outMeta.Serial).To(BeFalse())
Expect(outMeta.PropertyBlueprints).To(HaveLen(2))
Expect(outMeta.FormTypes).To(HaveLen(1))
Expect(outMeta.Variables).To(BeEmpty())
Expect(outMeta.Variables).To(HaveLen(1))
Expect(outMeta.Variables[0].Name).To(Equal("sample-tile-ca"))
Expect(outMeta.Variables[0].Type).To(Equal("certificate"))
Expect(outMeta.Variables[0].Options).To(HaveKeyWithValue("common_name", "Sample Tile CA"))
Expect(outMeta.Variables[0].Options).To(HaveKeyWithValue("is_ca", true))
Expect(outMeta.Releases).To(HaveLen(1))
Expect(outMeta.Releases[0]).To(ContainSubstring("k8s-tile-test"))
Expect(outMeta.InstanceGroups).To(HaveLen(0))
Expand Down Expand Up @@ -297,7 +302,7 @@ var _ = Describe("Carvel Baker", func() {
`$( property "admin_password" )`,
},
FormTypes: []string{`$( form "db_props" )`},
Variables: []string{},
Variables: []proofing.Variable{},
PackageInstalls: []string{`$( package "test-install" )`},
}
yamlData, err := yaml.Marshal(&m)
Expand Down Expand Up @@ -591,4 +596,42 @@ var _ = Describe("Carvel Baker", func() {
Expect(nonEmpty).To(Equal(5))
})
})

Context("validateVariables", func() {
It("passes for an empty list", func() {
err := validateVariables([]proofing.Variable{})
Expect(err).NotTo(HaveOccurred())
})

It("passes for a valid certificate variable", func() {
err := validateVariables([]proofing.Variable{
{
Name: "/cf/diego-instance-identity-root-ca-2-6",
Type: "certificate",
Options: map[string]any{
"common_name": "Diego Instance Identity Root CA",
"is_ca": true,
"duration": 1095,
},
},
})
Expect(err).NotTo(HaveOccurred())
})

It("errors when name is empty", func() {
err := validateVariables([]proofing.Variable{
{Name: "", Type: "certificate"},
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("missing required field 'name'"))
})

It("errors when type is empty", func() {
err := validateVariables([]proofing.Variable{
{Name: "my-var", Type: ""},
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("missing required field 'type'"))
})
})
})
28 changes: 15 additions & 13 deletions internal/carvel/models/metadata.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package models

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

type Metadata struct {
Name string `yaml:"name"`
ProductVersion string `yaml:"product_version"`
IconImage string `yaml:"icon_image"`
Label string `yaml:"label"`
MetadataVersion string `yaml:"metadata_version"`
MinimumVersionForUpgrade string `yaml:"minimum_version_for_upgrade"`
Rank int `yaml:"rank"`
Serial bool `yaml:"serial"`
PropertyBlueprints []string `yaml:"property_blueprints"`
FormTypes []string `yaml:"form_types"`
Variables []string `yaml:"variables"`
PackageInstalls []string `yaml:"package_installs"`
CompatibleKubernetesDistributions []ProductVersion `yaml:"compatible_kubernetes_distributions,omitempty"`
Name string `yaml:"name"`
ProductVersion string `yaml:"product_version"`
IconImage string `yaml:"icon_image"`
Label string `yaml:"label"`
MetadataVersion string `yaml:"metadata_version"`
MinimumVersionForUpgrade string `yaml:"minimum_version_for_upgrade"`
Rank int `yaml:"rank"`
Serial bool `yaml:"serial"`
PropertyBlueprints []string `yaml:"property_blueprints"`
FormTypes []string `yaml:"form_types"`
Variables []proofing.Variable `yaml:"variables"`
PackageInstalls []string `yaml:"package_installs"`
CompatibleKubernetesDistributions []ProductVersion `yaml:"compatible_kubernetes_distributions,omitempty"`
}
36 changes: 19 additions & 17 deletions internal/carvel/models/metadata_out.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package models

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

type MetadataOut struct {
Name string `yaml:"name"`
ProductVersion string `yaml:"product_version"`
IconImage string `yaml:"icon_image"`
Label string `yaml:"label"`
MetadataVersion string `yaml:"metadata_version"`
MinimumVersionForUpgrade string `yaml:"minimum_version_for_upgrade"`
Rank int `yaml:"rank"`
Serial bool `yaml:"serial"`
PropertyBlueprints []string `yaml:"property_blueprints"`
FormTypes []string `yaml:"form_types"`
Variables []string `yaml:"variables"`
InstanceGroups []string `yaml:"job_types"`
StemcellCriteria StemcellCriteria `yaml:"stemcell_criteria"`
Releases []string `yaml:"releases"`
RuntimeConfigs []string `yaml:"runtime_configs"`
RequiresKubernetes bool `yaml:"requires_kubernetes"`
CompatibleKubernetesDistributions []ProductVersion `yaml:"compatible_kubernetes_distributions"`
Name string `yaml:"name"`
ProductVersion string `yaml:"product_version"`
IconImage string `yaml:"icon_image"`
Label string `yaml:"label"`
MetadataVersion string `yaml:"metadata_version"`
MinimumVersionForUpgrade string `yaml:"minimum_version_for_upgrade"`
Rank int `yaml:"rank"`
Serial bool `yaml:"serial"`
PropertyBlueprints []string `yaml:"property_blueprints"`
FormTypes []string `yaml:"form_types"`
Variables []proofing.Variable `yaml:"variables"`
InstanceGroups []string `yaml:"job_types"`
StemcellCriteria StemcellCriteria `yaml:"stemcell_criteria"`
Releases []string `yaml:"releases"`
RuntimeConfigs []string `yaml:"runtime_configs"`
RequiresKubernetes bool `yaml:"requires_kubernetes"`
CompatibleKubernetesDistributions []ProductVersion `yaml:"compatible_kubernetes_distributions"`
}

type StemcellCriteria struct {
Expand Down
8 changes: 7 additions & 1 deletion internal/carvel/testdata/sample-tile/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ property_blueprints:
- $( property "admin_password" )
form_types:
- $( form "db_props" )
variables: []
variables:
- name: sample-tile-ca
type: certificate
options:
common_name: Sample Tile CA
is_ca: true
duration: 730
package_installs:
- $( package "test-install" )
compatible_kubernetes_distributions:
Expand Down
Loading