Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from achimweigel/target-value-label
Browse files Browse the repository at this point in the history
Prevent loss of target value label
  • Loading branch information
robertgraeff authored Feb 21, 2022
2 parents 1d6b3df + 75e00cb commit a1d7b32
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/imagevector.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,47 @@ func (ip *imageParser) Parse(ctx context.Context, image ImageEntry) error {
// add resource
id := ip.cd.GetResourceIndex(res)
if id != -1 {
if err := preventLossOfTargetVersionLabel(&ip.cd.Resources[id], &res); err != nil {
return err
}

ip.cd.Resources[id] = cdutils.MergeResources(ip.cd.Resources[id], res)
} else {
ip.cd.Resources = append(ip.cd.Resources, res)
}
return nil
}

// preventLossOfTargetVersionLabel throws an error if the provided resources both have a target version label
// with different values. In this case, one of the labels would get lost if the resources are merged.
func preventLossOfTargetVersionLabel(res1, res2 *cdv2.Resource) error {
var (
targetVersion1 string
targetVersion2 string
)

hasLabel1, err := getLabel(res1.Labels, TargetVersionLabel, &targetVersion1)
if err != nil {
return err
}

hasLabel2, err := getLabel(res2.Labels, TargetVersionLabel, &targetVersion2)
if err != nil {
return err
}

if hasLabel1 && hasLabel2 && targetVersion1 != targetVersion2 {
tag := res2.IdentityObjectMeta.GetIdentity()[TagExtraIdentity]

return fmt.Errorf(`there is more than one target version expression specified for name %q and tag %q. `+
`A solution might be to combine the target version expressions by using a range, for example: `+
`targetVersion: ">= 1.18, < 1.22"`,
res2.Name, tag)
}

return nil
}

// ImageEntryIsGenericDependency checks if the image entry should be parsed as generic dependency
func ImageEntryIsGenericDependency(image ImageEntry, opts *ParseImageOptions) bool {
// favor labels over deprecated cli flags
Expand Down
13 changes: 13 additions & 0 deletions pkg/imagevector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ var _ = Describe("Add", func() {
}))
})

It("should throw an error in case of different target version labels for the same name and tag", func() {
ivReader, err := os.Open("./testdata/resources/12-multi-targetversion-same-name-and-tag.yaml")
Expect(err).ToNot(HaveOccurred())
defer func() {
Expect(ivReader.Close()).ToNot(HaveOccurred())
}()

cd := readComponentDescriptor("./testdata/00-component/component-descriptor.yaml")

err = pkg.ParseImageVector(context.TODO(), nil, cd, ivReader, &pkg.ParseImageOptions{})
Expect(err).To(HaveOccurred())
})

Context("ComponentReferences", func() {

Context("should add image sources that match a given pattern as component reference", func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
images:
- name: metrics-server
sourceRepository: github.com/kubernetes-incubator/metrics-server
repository: k8s.gcr.io/metrics-server/metrics-server
tag: v0.4.1
targetVersion: 1.10.x
- name: metrics-server
sourceRepository: github.com/kubernetes-incubator/metrics-server
repository: k8s.gcr.io/metrics-server/metrics-server
tag: v0.4.1
targetVersion: 1.11.x

0 comments on commit a1d7b32

Please sign in to comment.