Skip to content

Commit

Permalink
Reference Hint Check fixed + alignment of configure function (#672)
Browse files Browse the repository at this point in the history
## Description

Some fixes:
- align different configure functions and switch to config context
- fix reference hint check for same resource

## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature
- [x] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] 🧑‍💻 Code Refactor
- [ ] 🔥 Performance Improvements
- [x] ✅ Test
- [ ] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert

## Related Tickets & Documents

<!-- 
Please use this format link issue numbers: Fixes #123

https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->
- Related Issue # (issue)
- Closes # (issue)
- Fixes # (issue)
> Remove if not applicable

## Screenshots

<!-- Visual changes require screenshots -->


## Added tests?

- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help
- [ ] Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration


## Added to documentation?

- [ ] 📜 README.md
- [ ] 🙅 no documentation needed

## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
mandelsoft authored Feb 23, 2024
1 parent 137c796 commit 929d0ea
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 60 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
57 changes: 7 additions & 50 deletions pkg/contexts/config/configutils/configure.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,20 @@
package configutils

import (
"fmt"
"os"
"strings"

_ "github.com/open-component-model/ocm/pkg/contexts/datacontext/config"

"github.com/mandelsoft/spiff/features"
"github.com/mandelsoft/spiff/spiffing"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/vfs"

"github.com/open-component-model/ocm/pkg/contexts/config"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/contexts/ocm/utils"
)

func Configure(path string) error {
return ConfigureContext(config.DefaultContext(), path)
func Configure(path string, fss ...vfs.FileSystem) error {
_, err := utils.Configure(config.DefaultContext(), path, fss...)
return err
}

func ConfigureContext(ctxp config.ContextProvider, path string) error {
ctx := config.FromProvider(ctxp)

h, _ := os.UserHomeDir()
if path == "" {
if h != "" {
cfg := h + "/.ocmconfig"
if ok, err := vfs.FileExists(osfs.New(), cfg); ok && err == nil {
path = cfg
}
}
}

if path != "" {
if strings.HasPrefix(path, "~"+string(os.PathSeparator)) {
if len(h) == 0 {
return fmt.Errorf("no home directory found for resolving path of ocm config file %q", path)
}
path = h + path[1:]
}
data, err := vfs.ReadFile(osfs.New(), path)
if err != nil {
return errors.Wrapf(err, "cannot read ocm config file %q", path)
}

sctx := spiffing.New().WithFeatures(features.INTERPOLATION, features.CONTROL)
data, err = spiffing.Process(sctx, spiffing.NewSourceData(path, data))
if err != nil {
return errors.Wrapf(err, "processing ocm config %q", path)
}
cfg, err := ctx.GetConfigForData(data, nil)
if err != nil {
return errors.Wrapf(err, "invalid ocm config file %q", path)
}
err = ctx.ApplyConfig(cfg, path)
if err != nil {
return errors.Wrapf(err, "cannot apply ocm config %q", path)
}
}
return nil
func ConfigureContext(ctxp config.ContextProvider, path string, fss ...vfs.FileSystem) error {
_, err := utils.Configure(ctxp, path, fss...)
return err
}
16 changes: 12 additions & 4 deletions pkg/contexts/ocm/utils/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mandelsoft/spiff/spiffing"
"github.com/mandelsoft/vfs/pkg/vfs"

"github.com/open-component-model/ocm/pkg/contexts/config"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/utils/defaultconfigregistry"
"github.com/open-component-model/ocm/pkg/errors"
Expand All @@ -19,10 +20,17 @@ const DEFAULT_OCM_CONFIG = ".ocmconfig"

const DEFAULT_OCM_CONFIG_DIR = ".ocm"

func Configure(ctx ocm.Context, path string, fss ...vfs.FileSystem) (ocm.Context, error) {
func Configure(ctx config.ContextProvider, path string, fss ...vfs.FileSystem) (ocm.Context, error) {
var ocmctx ocm.Context

fs := utils.FileSystem(fss...)
if ctx == nil {
ctx = ocm.DefaultContext()
ocmctx = ocm.DefaultContext()
ctx = ocmctx
} else {
if c, ok := ctx.(ocm.Context); ok {
ocmctx = c
}
}
h, _ := os.UserHomeDir()
if path == "" {
Expand Down Expand Up @@ -66,10 +74,10 @@ func Configure(ctx ocm.Context, path string, fss ...vfs.FileSystem) (ocm.Context
}
}
}
return ctx, nil
return ocmctx, nil
}

func ConfigureByData(ctx ocm.Context, data []byte, info string) error {
func ConfigureByData(ctx config.ContextProvider, data []byte, info string) error {
var err error

sctx := spiffing.New().WithFeatures(features.INTERPOLATION, features.CONTROL)
Expand Down

0 comments on commit 929d0ea

Please sign in to comment.