diff --git a/api/ocm/compdesc/componentdescriptor.go b/api/ocm/compdesc/componentdescriptor.go index 13e73e4f4..2d80753e8 100644 --- a/api/ocm/compdesc/componentdescriptor.go +++ b/api/ocm/compdesc/componentdescriptor.go @@ -233,10 +233,7 @@ func (o *ElementMeta) AddExtraIdentity(identity metav1.Identity) { // GetIdentity returns the identity of the object. func (o *ElementMeta) GetIdentity(accessor ElementListAccessor) metav1.Identity { - identity := o.ExtraIdentity.Copy() - if identity == nil { - identity = metav1.Identity{} - } + identity := o.GetExtraIdentity() identity[SystemIdentityName] = o.Name if identity.Get(SystemIdentityVersion) == "" && accessor != nil { found := false @@ -266,7 +263,7 @@ func (o *ElementMeta) GetRawIdentity() metav1.Identity { identity = metav1.Identity{} } identity[SystemIdentityName] = o.Name - if o.Version != "" { + if _, ok := identity[SystemIdentityVersion]; !ok && o.Version != "" { identity[SystemIdentityVersion] = o.Version } return identity @@ -279,8 +276,9 @@ func (o *ElementMeta) GetMatchBaseIdentity() metav1.Identity { identity = metav1.Identity{} } identity[SystemIdentityName] = o.Name - identity[SystemIdentityVersion] = o.Version - + if _, ok := identity[SystemIdentityVersion]; !ok { + identity[SystemIdentityVersion] = o.Version + } return identity } diff --git a/api/ocm/compdesc/id_test.go b/api/ocm/compdesc/id_test.go new file mode 100644 index 000000000..82fced9e6 --- /dev/null +++ b/api/ocm/compdesc/id_test.go @@ -0,0 +1,57 @@ +package compdesc_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "ocm.software/ocm/api/ocm/compdesc" + metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" +) + +var _ = Describe("Extra Identity Test Environment", func() { + It("handles complete defaulting", func() { + resources := compdesc.Resources{ + compdesc.Resource{ + ResourceMeta: compdesc.ResourceMeta{ + ElementMeta: compdesc.ElementMeta{ + Name: "res", + Version: "v1", + }, + }, + }, + compdesc.Resource{ + ResourceMeta: compdesc.ResourceMeta{ + ElementMeta: compdesc.ElementMeta{ + Name: "res", + Version: "v2", + }, + }, + }, + } + Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v1"))) + Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v2"))) + }) + + It("handles partial defaulting", func() { + resources := compdesc.Resources{ + compdesc.Resource{ + ResourceMeta: compdesc.ResourceMeta{ + ElementMeta: compdesc.ElementMeta{ + Name: "res", + Version: "v1", + ExtraIdentity: metav1.NewExtraIdentity(compdesc.SystemIdentityVersion, "v1"), + }, + }, + }, + compdesc.Resource{ + ResourceMeta: compdesc.ResourceMeta{ + ElementMeta: compdesc.ElementMeta{ + Name: "res", + Version: "v2", + }, + }, + }, + } + Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v1"))) + Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v2"))) + }) +}) diff --git a/api/ocm/compdesc/versions/ocm.software/v3alpha1/componentdescriptor.go b/api/ocm/compdesc/versions/ocm.software/v3alpha1/componentdescriptor.go index 8808fc3ec..e3967971b 100644 --- a/api/ocm/compdesc/versions/ocm.software/v3alpha1/componentdescriptor.go +++ b/api/ocm/compdesc/versions/ocm.software/v3alpha1/componentdescriptor.go @@ -129,24 +129,33 @@ func (o *ElementMeta) SetExtraIdentity(identity metav1.Identity) { o.ExtraIdentity = identity } +// GetExtraIdentity gets the extra identity of the object. +func (o *ElementMeta) GetExtraIdentity() metav1.Identity { + if o.ExtraIdentity == nil { + return metav1.Identity{} + } + return o.ExtraIdentity.Copy() +} + // GetIdentity returns the identity of the object. func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity { - identity := o.ExtraIdentity.Copy() - if identity == nil { - identity = metav1.Identity{} - } + identity := o.GetExtraIdentity() identity[SystemIdentityName] = o.Name - if accessor != nil { + if identity.Get(SystemIdentityVersion) == "" && accessor != nil { found := false l := accessor.Len() for i := 0; i < l; i++ { m := accessor.Get(i).GetMeta() - if m.Name == o.Name && m.ExtraIdentity.Equals(o.ExtraIdentity) { - if found { - identity[SystemIdentityVersion] = o.Version - break + if m.GetName() == o.Name { + mid := m.GetExtraIdentity() + mid.Remove(SystemIdentityVersion) + if mid.Equals(o.ExtraIdentity) { + if found { + identity[SystemIdentityVersion] = o.Version + break + } + found = true } - found = true } } } diff --git a/api/ocm/compdesc/versions/ocm.software/v3alpha1/id_test.go b/api/ocm/compdesc/versions/ocm.software/v3alpha1/id_test.go new file mode 100644 index 000000000..5b19fdfc5 --- /dev/null +++ b/api/ocm/compdesc/versions/ocm.software/v3alpha1/id_test.go @@ -0,0 +1,50 @@ +package v3alpha1_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" + me "ocm.software/ocm/api/ocm/compdesc/versions/ocm.software/v3alpha1" +) + +var _ = Describe("Extra Identity Test Environment", func() { + It("handles complete defaulting", func() { + resources := me.Resources{ + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v1", + }, + }, + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v2", + }, + }, + } + Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1"))) + Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2"))) + }) + + It("handles partial defaulting", func() { + resources := me.Resources{ + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v1", + ExtraIdentity: metav1.NewExtraIdentity(me.SystemIdentityVersion, "v1"), + }, + }, + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v2", + }, + }, + } + Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1"))) + Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2"))) + }) +}) diff --git a/api/ocm/compdesc/versions/ocm.software/v3alpha1/suite_test.go b/api/ocm/compdesc/versions/ocm.software/v3alpha1/suite_test.go new file mode 100644 index 000000000..08ddd9a41 --- /dev/null +++ b/api/ocm/compdesc/versions/ocm.software/v3alpha1/suite_test.go @@ -0,0 +1,13 @@ +package v3alpha1_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConfig(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "v3alpha1 Test Suite") +} diff --git a/api/ocm/compdesc/versions/ocm.software/v3alpha1/validation_test.go b/api/ocm/compdesc/versions/ocm.software/v3alpha1/validation_test.go index 223ed5d25..8a02f6cc8 100644 --- a/api/ocm/compdesc/versions/ocm.software/v3alpha1/validation_test.go +++ b/api/ocm/compdesc/versions/ocm.software/v3alpha1/validation_test.go @@ -1,8 +1,6 @@ package v3alpha1_test import ( - "testing" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" . "github.com/onsi/gomega/gstruct" @@ -17,11 +15,6 @@ import ( "ocm.software/ocm/api/utils/runtime" ) -func TestConfig(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "V2 Test Suite") -} - var _ = Describe("Validation", func() { testutils.TestCompName(jsonscheme.ResourcesComponentDescriptorOcmV3SchemaYamlBytes()) diff --git a/api/ocm/compdesc/versions/v2/componentdescriptor.go b/api/ocm/compdesc/versions/v2/componentdescriptor.go index d57f96714..c448036d3 100644 --- a/api/ocm/compdesc/versions/v2/componentdescriptor.go +++ b/api/ocm/compdesc/versions/v2/componentdescriptor.go @@ -174,24 +174,33 @@ func (o *ElementMeta) SetExtraIdentity(identity metav1.Identity) { o.ExtraIdentity = identity } +// GetExtraIdentity gets the extra identity of the object. +func (o *ElementMeta) GetExtraIdentity() metav1.Identity { + if o.ExtraIdentity == nil { + return metav1.Identity{} + } + return o.ExtraIdentity.Copy() +} + // GetIdentity returns the identity of the object. func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity { - identity := o.ExtraIdentity.Copy() - if identity == nil { - identity = metav1.Identity{} - } + identity := o.GetExtraIdentity() identity[SystemIdentityName] = o.Name - if accessor != nil { + if identity.Get(SystemIdentityVersion) == "" && accessor != nil { found := false l := accessor.Len() for i := 0; i < l; i++ { m := accessor.Get(i).GetMeta() - if m.Name == o.Name && m.ExtraIdentity.Equals(o.ExtraIdentity) { - if found { - identity[SystemIdentityVersion] = o.Version - break + if m.GetName() == o.Name { + mid := m.GetExtraIdentity() + mid.Remove(SystemIdentityVersion) + if mid.Equals(o.ExtraIdentity) { + if found { + identity[SystemIdentityVersion] = o.Version + break + } + found = true } - found = true } } } @@ -209,7 +218,7 @@ func (o *ElementMeta) GetRawIdentity() metav1.Identity { identity = metav1.Identity{} } identity[SystemIdentityName] = o.Name - if o.Version != "" { + if _, ok := identity[SystemIdentityVersion]; !ok && o.Version != "" { identity[SystemIdentityVersion] = o.Version } return identity diff --git a/api/ocm/compdesc/versions/v2/id_test.go b/api/ocm/compdesc/versions/v2/id_test.go new file mode 100644 index 000000000..01b2db66a --- /dev/null +++ b/api/ocm/compdesc/versions/v2/id_test.go @@ -0,0 +1,50 @@ +package v2_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" + me "ocm.software/ocm/api/ocm/compdesc/versions/v2" +) + +var _ = Describe("Extra Identity Test Environment", func() { + It("handles complete defaulting", func() { + resources := me.Resources{ + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v1", + }, + }, + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v2", + }, + }, + } + Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1"))) + Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2"))) + }) + + It("handles partial defaulting", func() { + resources := me.Resources{ + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v1", + ExtraIdentity: metav1.NewExtraIdentity(me.SystemIdentityVersion, "v1"), + }, + }, + me.Resource{ + ElementMeta: me.ElementMeta{ + Name: "res", + Version: "v2", + }, + }, + } + Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1"))) + Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2"))) + }) +}) diff --git a/api/ocm/compdesc/versions/v2/suite_test.go b/api/ocm/compdesc/versions/v2/suite_test.go new file mode 100644 index 000000000..3329f170f --- /dev/null +++ b/api/ocm/compdesc/versions/v2/suite_test.go @@ -0,0 +1,13 @@ +package v2_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConfig(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "v2 Test Suite") +} diff --git a/api/ocm/compdesc/versions/v2/validation_test.go b/api/ocm/compdesc/versions/v2/validation_test.go index dd49fbe5c..0784f2aee 100644 --- a/api/ocm/compdesc/versions/v2/validation_test.go +++ b/api/ocm/compdesc/versions/v2/validation_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "strings" - "testing" . "github.com/mandelsoft/goutils/testutils" . "github.com/onsi/ginkgo/v2" @@ -22,11 +21,6 @@ import ( "ocm.software/ocm/api/utils/runtime" ) -func TestConfig(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "V2 Test Suite") -} - var _ = Describe("Validation", func() { testutils.TestCompName(jsonscheme.ResourcesComponentDescriptorV2SchemaYamlBytes())