Skip to content

Commit

Permalink
fix resource reference hint check
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Feb 23, 2024
1 parent 7706092 commit d8f1323
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
20 changes: 14 additions & 6 deletions cmds/ocm/commands/ocmcmds/common/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"

_ "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types"
v1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"

"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -44,15 +45,15 @@ type ResourceSpecHandler interface {
Set(v ocm.ComponentVersionAccess, r addhdlrs.Element, acc compdesc.AccessSpec) error
}

func CheckHint(v ocm.ComponentVersionAccess, acc compdesc.AccessSpec) error {
err := checkHint(v, "source", compdesc.SourceArtifacts, acc)
func CheckHint(v ocm.ComponentVersionAccess, elem addhdlrs.Element, acc compdesc.AccessSpec) error {
err := checkHint(v, "source", elem, compdesc.SourceArtifacts, acc)
if err != nil {
return err
}
return checkHint(v, "resource", compdesc.ResourceArtifacts, acc)
return checkHint(v, "resource", elem, compdesc.ResourceArtifacts, acc)
}

func checkHint(v ocm.ComponentVersionAccess, typ string, artacc compdesc.ArtifactAccess, acc compdesc.AccessSpec) error {
func checkHint(v ocm.ComponentVersionAccess, typ string, elem addhdlrs.Element, artacc compdesc.ArtifactAccess, acc compdesc.AccessSpec) error {
spec, err := v.GetContext().AccessSpecForSpec(acc)
if err != nil {
return err
Expand All @@ -64,13 +65,20 @@ func checkHint(v ocm.ComponentVersionAccess, typ string, artacc compdesc.Artifac
if local.ReferenceName == "" {
return nil
}
elemid := elem.Spec().GetRawIdentity()
if elemid[v1.SystemIdentityVersion] == ComponentVersionTag {
elemid[v1.SystemIdentityVersion] = v.GetVersion()
}
accessor := artacc(v.GetDescriptor())
for i := 0; i < accessor.Len(); i++ {
a := accessor.GetArtifact(i)
other, err := v.GetContext().AccessSpecForSpec(a.GetAccess())
if err != nil {
continue
}
if elemid.Equals(a.GetMeta().GetRawIdentity()) {
continue
}
olocal, ok := other.(*localblob.AccessSpec)
if !ok {
continue
Expand Down Expand Up @@ -486,14 +494,14 @@ func ProcessElements(ictx inputs.Context, cv ocm.ComponentVersionAccess, elems [
acc, err = cv.AddBlob(blob, elem.Type(), hint, nil)
blob.Close()
if err == nil {
err = CheckHint(cv, acc)
err = CheckHint(cv, elem, acc)
if err == nil {
err = h.Set(cv, elem, acc)
}
}
} else {
acc := elem.Input().Access
err = CheckHint(cv, acc)
err = CheckHint(cv, elem, acc)
if err == nil {
err = h.Set(cv, elem, acc)
}
Expand Down
16 changes: 16 additions & 0 deletions cmds/ocm/commands/ocmcmds/resources/add/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ var _ = Describe("Add resources", func() {
Expect(acc.(*ociartifact.AccessSpec).ImageReference).To(Equal("ghcr.io/mandelsoft/pause:v0.1.0"))
})

It("re-adds external image", func() {
Expect(env.Execute("add", "resources", "--skip-digest-generation", "--file", ARCH, "/testdata/helm2.yaml")).To(Succeed())
Expect(env.Execute("add", "resources", "--skip-digest-generation", "--file", ARCH, "/testdata/helm2.yaml")).To(Succeed())

data, err := env.ReadFile(env.Join(ARCH, comparch.ComponentDescriptorFileName))
Expect(err).To(Succeed())
cd, err := compdesc.Decode(data)
Expect(err).To(Succeed())
Expect(len(cd.Resources)).To(Equal(1))
})

It("rejects duplicate hinte", func() {
Expect(env.Execute("add", "resources", "--skip-digest-generation", "--file", ARCH, "/testdata/helm.yaml")).To(Succeed())
ExpectError(env.Execute("add", "resources", "--skip-digest-generation", "--file", ARCH, "/testdata/helm2.yaml")).To(MatchError("cannot add resource \"chart2\"(/testdata/helm2.yaml[1][1]): reference name (hint) \"test.de/x/mandelsoft/testchart:0.1.0\" with base media type application/vnd.oci.image.manifest.v1 already used for resource chart:v1"))
})

Context("resource by options", func() {
It("adds simple text blob", func() {
meta := `
Expand Down
7 changes: 7 additions & 0 deletions cmds/ocm/commands/ocmcmds/resources/add/testdata/helm2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: chart2
type: helmChart
input:
type: helm
path: testchart
repository: mandelsoft/testchart

0 comments on commit d8f1323

Please sign in to comment.