Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate detection for add commands #564

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ import (
)

func ProcessComponents(ctx clictx.Context, ictx inputs.Context, repo ocm.Repository, complete ocm.ComponentVersionResolver, thdlr transferhandler.TransferHandler, h *ResourceSpecHandler, elems []addhdlrs.Element) (err error) {
list := errors.ErrorList{}

for _, elem := range elems {
if r, ok := elem.Spec().(*ResourceSpec); ok {
list.Add(addhdlrs.ValdateElementSpecIdentities("resource", elem.Source().String(), generics.ConvertSliceTo[addhdlrs.ElementSpec](r.Resources)))
list.Add(addhdlrs.ValdateElementSpecIdentities("source", elem.Source().String(), generics.ConvertSliceTo[addhdlrs.ElementSpec](r.Sources)))
list.Add(addhdlrs.ValdateElementSpecIdentities("reference", elem.Source().String(), generics.ConvertSliceTo[addhdlrs.ElementSpec](r.References)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is an elements source (elem.Source())? Confusing name (especially since "Sources" actually are a core model element - is the core model element related to this function? If so, how?)

The package addhdlrs (where this function is from) contains generally quite generic coding with non-tangible namings and no comments to support the understanding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added docu

}
}
if err := list.Result(); err != nil {
return err
}

index := generics.Set[common.NameVersion]{}
for _, elem := range elems {
if r, ok := elem.Spec().(*ResourceSpec); ok {
Expand Down
4 changes: 4 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ type ResourceSpec struct {

var _ addhdlrs.ElementSpec = (*ResourceSpec)(nil)

func (r *ResourceSpec) GetRawIdentity() metav1.Identity {
return metav1.NewIdentity(r.Name, metav1.SystemIdentityVersion, r.Version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this constant called SystemIdentityVersion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SystemIdentityVersion and SystemIdentityName are the two predefined identity properties use to identify elements (resources, sources, referebces) in CDs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added docu to their const definitions.

}

func (r *ResourceSpec) Info() string {
return fmt.Sprintf("component %s:%s", r.Name, r.Version)
}
Expand Down
2 changes: 2 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/generics"
Expand Down Expand Up @@ -79,6 +80,7 @@ type ElementSpec interface {
GetName() string
GetVersion() string
SetVersion(string)
GetRawIdentity() metav1.Identity
Info() string
Validate(ctx clictx.Context, input *ResourceInput) error
}
Expand Down
5 changes: 5 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/refs/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
compdescv2 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/versions/v2"
"github.com/open-component-model/ocm/pkg/runtime"
)
Expand Down Expand Up @@ -70,6 +71,10 @@ type ResourceSpec struct {

var _ addhdlrs.ElementSpec = (*ResourceSpec)(nil)

func (r *ResourceSpec) GetRawIdentity() metav1.Identity {
return r.ElementMeta.GetRawIdentity()
}

func (r *ResourceSpec) Info() string {
return fmt.Sprintf("reference %s: %s", r.ComponentName, r.GetRawIdentity())
}
Expand Down
4 changes: 4 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/rscs/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ type ResourceSpec struct {

var _ addhdlrs.ElementSpec = (*ResourceSpec)(nil)

func (r *ResourceSpec) GetRawIdentity() metav1.Identity {
return r.ElementMeta.GetRawIdentity()
}

func (r *ResourceSpec) Info() string {
return fmt.Sprintf("resource %s: %s", r.Type, r.GetRawIdentity())
}
Expand Down
5 changes: 5 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/srcs/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
compdescv2 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/versions/v2"
"github.com/open-component-model/ocm/pkg/runtime"
)
Expand Down Expand Up @@ -70,6 +71,10 @@ type ResourceSpec struct {

var _ addhdlrs.ElementSpec = (*ResourceSpec)(nil)

func (r *ResourceSpec) GetRawIdentity() metav1.Identity {
return r.ElementMeta.GetRawIdentity()
}

func (r *ResourceSpec) Info() string {
return fmt.Sprintf("source %s: %s", r.Type, r.GetRawIdentity())
}
Expand Down
37 changes: 37 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func ProcessDescriptions(ctx clictx.Context, printer common2.Printer, templ temp
}
elems = append(elems, tmp...)
}
err := ValdateElementIdentities(h.Key(), elems)
if err != nil {
return nil, nil, err
}
ictx.Printf("found %d %s\n", len(elems), utils.Plural(h.Key(), len(elems)))
return elems, ictx, nil
}
Expand Down Expand Up @@ -279,6 +283,39 @@ func Validate(r *ResourceInput, ctx inputs.Context, inputFilePath string) error
return allErrs.ToAggregate()
}

func ValdateElementIdentities(kind string, elems []Element) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Suggested change
func ValdateElementIdentities(kind string, elems []Element) error {
func ValidateElementIdentities(kind string, elems []Element) error {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

list := errors.ErrList()
ids := map[string]SourceInfo{}
for _, r := range elems {
var i interface{}
err := runtime.DefaultYAMLEncoding.Unmarshal(r.Data(), &i)
if err != nil {
return errors.Wrapf(err, "cannot eval data %q", string(r.Data()))
}
id := r.Spec().GetRawIdentity()
dig := id.Digest()
if s, ok := ids[string(dig)]; ok {
list.Add(fmt.Errorf("duplicate %s identity %s (%s and %s)", kind, id, r.Source(), s))
}
ids[string(dig)] = r.Source()
}
return list.Result()
}

func ValdateElementSpecIdentities(kind string, src string, elems []ElementSpec) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Suggested change
func ValdateElementSpecIdentities(kind string, src string, elems []ElementSpec) error {
func ValidateElementSpecIdentities(kind string, src string, elems []ElementSpec) error {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the src string supposed to be?

Please choose a more tangible parameter name or add helpful comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added doc to various types and functions

list := errors.ErrList()
ids := map[string]int{}
for i, r := range elems {
id := r.GetRawIdentity()
dig := id.Digest()
if s, ok := ids[string(dig)]; ok {
list.Add(fmt.Errorf("duplicate %s identity %s (%s index %d and %d)", kind, id, src, i+1, s+1))
}
ids[string(dig)] = i
}
return list.Result()
}

func PrintElements(p common2.Printer, elems []Element, outfile string, fss ...vfs.FileSystem) error {
if outfile != "" && outfile != "-" {
f, err := accessio.FileSystem(fss...).OpenFile(outfile, vfs.O_TRUNC|vfs.O_CREATE|vfs.O_WRONLY, 0o644)
Expand Down
23 changes: 23 additions & 0 deletions cmds/ocm/commands/ocmcmds/components/add/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ var _ = Describe("Test Environment", func() {
CheckComponent(env, nil)
})

Context("failures", func() {
It("rejects adding duplicate components", func() {
ExpectError(env.Execute("add", "c", "-fc", "--file", ARCH, "--version", "1.0.0", "testdata/components-dup.yaml")).To(
MatchError(`duplicate component identity "name"="ocm.software/demo/test","version"="1.0.0" (testdata/components-dup.yaml[1][2] and testdata/components-dup.yaml[1][1])`),
)
})
It("rejects adding duplicate resources", func() {
ExpectError(env.Execute("add", "c", "-fc", "--file", ARCH, "--version", "1.0.0", "testdata/component-dup-res.yaml")).To(
MatchError(`duplicate resource identity "name"="text" (testdata/component-dup-res.yaml[1][1] index 3 and 1)`),
)
})
It("rejects adding duplicate source", func() {
ExpectError(env.Execute("add", "c", "-fc", "--file", ARCH, "--version", "1.0.0", "testdata/component-dup-src.yaml")).To(
MatchError(`duplicate source identity "name"="source" (testdata/component-dup-src.yaml[1][1] index 2 and 1)`),
)
})
It("rejects adding duplicate reference", func() {
ExpectError(env.Execute("add", "c", "-fc", "--file", ARCH, "--version", "1.0.0", "testdata/component-dup-ref.yaml")).To(
MatchError(`duplicate reference identity "name"="ref","version"="v1" (testdata/component-dup-ref.yaml[1][1] index 2 and 1)`),
)
})
})

Context("with completion", func() {
var ldesc *artdesc.Descriptor

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: ocm.software/demo/test
version: 1.0.0
provider:
name: ocm.software
labels:
- name: city
value: Karlsruhe
labels:
- name: purpose
value: test

resources:
- name: text
type: PlainText
labels:
- name: city
value: Karlsruhe
merge:
algorithm: default
config:
overwrite: inbound
input:
type: file
path: testdata
- name: data
type: PlainText
input:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
- name: ref
version: v1
componentName: github.com/mandelsoft/test3

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: ocm.software/demo/test
version: 1.0.0
provider:
name: ocm.software
labels:
- name: city
value: Karlsruhe
labels:
- name: purpose
value: test

resources:
- name: text
type: PlainText
labels:
- name: city
value: Karlsruhe
merge:
algorithm: default
config:
overwrite: inbound
input:
type: file
path: testdata
- name: data
type: PlainText
input:
type: binary
data: IXN0cmluZ2RhdGE=
- name: text
type: PlainText
input:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: ocm.software/demo/test
version: 1.0.0
provider:
name: ocm.software
labels:
- name: city
value: Karlsruhe
labels:
- name: purpose
value: test

sources:
- name: source
type: DirectoryTree
input:
type: binary
data: IXN0cmluZ2RhdGE=
- name: source
type: DirectoryTree
input:
type: binary
data: IXN0cmluZ2RhdGE=

resources:
- name: text
type: PlainText
labels:
- name: city
value: Karlsruhe
merge:
algorithm: default
config:
overwrite: inbound
input:
type: file
path: testdata
- name: data
type: PlainText
input:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
components:
- name: ocm.software/demo/test
provider:
name: ocm.software
labels:
- name: city
value: Karlsruhe
labels:
- name: purpose
value: test

resources:
- name: text
type: PlainText
labels:
- name: city
value: Karlsruhe
merge:
algorithm: default
config:
overwrite: inbound
input:
type: file
path: testdata
- name: data
type: PlainText
input:
type: binary
data: IXN0cmluZ2RhdGE=
componentReferences:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2

- name: ocm.software/demo/test
provider:
name: ocm.software
labels:
- name: city
value: Karlsruhe
9 changes: 9 additions & 0 deletions cmds/ocm/commands/ocmcmds/references/add/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
"github.com/open-component-model/ocm/pkg/testutils"
)

const ARCH = "/tmp/ca"
Expand Down Expand Up @@ -175,4 +176,12 @@ labels:
})
})
})

Context("failures", func() {
It("rejects adding duplicate ref", func() {
testutils.ExpectError(env.Execute("add", "references", "--file", ARCH, "/testdata/references-dup.yaml")).To(
MatchError(`duplicate reference identity "name"="testdata","version"="v1.1.1" (/testdata/references-dup.yaml[1][2] and /testdata/references-dup.yaml[1][1])`),
)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
references:
- name: testdata
componentName: github.com/mandelsoft/ref
version: v1.1.1
- name: testdata
componentName: github.com/mandelsoft/ref2
version: v1.1.1
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/oauth2 v0.9.0
golang.org/x/text v0.13.0
google.golang.org/api v0.128.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.12.2
k8s.io/api v0.27.3
Expand Down Expand Up @@ -196,6 +197,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
Expand Down Expand Up @@ -303,7 +305,6 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.3 // indirect
Expand Down
Loading
Loading