From 692fac40e9f989ef4387585f0b91a455db97bbf3 Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Wed, 8 Jan 2025 16:14:22 +0100 Subject: [PATCH] revert defaulting during internal version creation --- .../versions/ocm.software/v3alpha1/default.go | 27 +++++++++++++++++++ api/ocm/compdesc/versions/v2/default.go | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/api/ocm/compdesc/versions/ocm.software/v3alpha1/default.go b/api/ocm/compdesc/versions/ocm.software/v3alpha1/default.go index 979c2b09ea..b05e5cfd65 100644 --- a/api/ocm/compdesc/versions/ocm.software/v3alpha1/default.go +++ b/api/ocm/compdesc/versions/ocm.software/v3alpha1/default.go @@ -1,6 +1,7 @@ package v3alpha1 import ( + v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" "ocm.software/ocm/api/utils/runtime" ) @@ -19,5 +20,31 @@ func (cd *ComponentDescriptor) Default() error { cd.Spec.Resources = make([]Resource, 0) } + DefaultResources(cd) return nil } + +// DefaultResources defaults a list of resources. +// The version of the component is defaulted for local resources that do not contain a version. +// adds the version as identity if the resource identity would clash otherwise. +func DefaultResources(component *ComponentDescriptor) { + for i, res := range component.Spec.Resources { + if res.Relation == v1.LocalRelation && len(res.Version) == 0 { + component.Spec.Resources[i].Version = component.GetVersion() + } + + id := res.GetIdentity(component.Spec.Resources) + if v, ok := id[SystemIdentityVersion]; ok { + if res.ExtraIdentity == nil { + // due to compatibility with very old faulty original implementation + // we cannot default the version for a not existing identity map. + // res is copy of the resource and setting this field here never worked + // as intended. + } else { + if _, ok := res.ExtraIdentity[SystemIdentityVersion]; !ok { + res.ExtraIdentity[SystemIdentityVersion] = v + } + } + } + } +} diff --git a/api/ocm/compdesc/versions/v2/default.go b/api/ocm/compdesc/versions/v2/default.go index 08ea24a1a3..635e3c8951 100644 --- a/api/ocm/compdesc/versions/v2/default.go +++ b/api/ocm/compdesc/versions/v2/default.go @@ -1,6 +1,7 @@ package v2 import ( + v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1" "ocm.software/ocm/api/utils/runtime" ) @@ -19,5 +20,31 @@ func (cd *ComponentDescriptor) Default() error { cd.Resources = make([]Resource, 0) } + DefaultResources(cd) return nil } + +// DefaultResources defaults a list of resources. +// The version of the component is defaulted for local resources that do not contain a version. +// adds the version as identity if the resource identity would clash otherwise. +func DefaultResources(component *ComponentDescriptor) { + for i, res := range component.Resources { + if res.Relation == v1.LocalRelation && len(res.Version) == 0 { + component.Resources[i].Version = component.GetVersion() + } + + id := res.GetIdentity(component.Resources) + if v, ok := id[SystemIdentityVersion]; ok { + if res.ExtraIdentity == nil { + // due to compatibility with very old faulty original implementation + // we cannot default the version for a not existing identity map. + // res is copy of the resource and setting this field here never worked + // as intended. + } else { + if _, ok := res.ExtraIdentity[SystemIdentityVersion]; !ok { + res.ExtraIdentity[SystemIdentityVersion] = v + } + } + } + } +}