From 456b644b1b1ce4885b38d67333fbc194736152c6 Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Tue, 4 Apr 2023 14:21:42 +0200 Subject: [PATCH] handling of cd versions (#298) --- .../ocmcmds/common/addhdlrs/comp/elements.go | 27 +++++++++++--- .../common/options/schemaoption/option.go | 37 ++++++++++++------- .../commands/ocmcmds/components/add/cmd.go | 15 ++++++-- .../commands/ocmcmds/components/get/cmd.go | 12 +++++- docs/reference/ocm_add_componentversions.md | 16 ++++++-- docs/reference/ocm_create_componentarchive.md | 3 +- docs/reference/ocm_get_componentversions.md | 8 +++- docs/reference/ocm_logging.md | 2 +- .../ocm/compdesc/componentdescriptor.go | 2 + .../versions/v2/componentdescriptor.go | 2 +- .../ocm/compdesc/versions/v2/default.go | 2 +- .../ocm/compdesc/versions/v2/signing.go | 2 +- .../ocm/compdesc/versions/v2/validation.go | 2 +- .../compdesc/versions/v2/validation_test.go | 2 +- .../ocm/compdesc/versions/v2/version.go | 2 +- .../versions/v2/zz_generated.deepcopy.go | 2 +- 16 files changed, 96 insertions(+), 40 deletions(-) diff --git a/cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go b/cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go index add8ec9d59..e40816dcb2 100644 --- a/cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go +++ b/cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go @@ -7,6 +7,7 @@ package comp import ( "fmt" + "github.com/open-component-model/ocm/cmds/ocm/pkg/utils" . "github.com/open-component-model/ocm/pkg/finalizer" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common" @@ -15,7 +16,6 @@ import ( "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/addhdlrs/rscs" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/addhdlrs/srcs" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs" - utils2 "github.com/open-component-model/ocm/cmds/ocm/pkg/utils" "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" @@ -30,12 +30,13 @@ const ( type ResourceSpecHandler struct { version string + schema string } var _ common.ResourceSpecHandler = (*ResourceSpecHandler)(nil) -func NewResourceSpecHandler(v string) *ResourceSpecHandler { - return &ResourceSpecHandler{v} +func NewResourceSpecHandler(v string, schema string) *ResourceSpecHandler { + return &ResourceSpecHandler{version: v, schema: schema} } func (*ResourceSpecHandler) Key() string { @@ -62,7 +63,7 @@ func (*ResourceSpecHandler) Set(v ocm.ComponentVersionAccess, r addhdlrs.Element return fmt.Errorf("not supported for components") } -func (*ResourceSpecHandler) Add(ctx clictx.Context, ictx inputs.Context, elem addhdlrs.Element, repo ocm.Repository) (err error) { +func (h *ResourceSpecHandler) Add(ctx clictx.Context, ictx inputs.Context, elem addhdlrs.Element, repo ocm.Repository) (err error) { var final Finalizer defer final.FinalizeWithErrorPropagation(&err) @@ -84,6 +85,17 @@ func (*ResourceSpecHandler) Add(ctx clictx.Context, ictx inputs.Context, elem ad cd := cv.GetDescriptor() + schema := h.schema + if r.Meta.ConfiguredVersion != "" { + schema = r.Meta.ConfiguredVersion + } + if schema != "" { + if compdesc.DefaultSchemes[schema] == nil { + return errors.ErrUnknown(errors.KIND_SCHEMAVERSION, schema) + } + cd.Metadata.ConfiguredVersion = schema + } + cd.Labels = r.Labels cd.Provider = r.Provider cd.CreationTime = metav1.NewTimestampP() @@ -104,7 +116,7 @@ func (*ResourceSpecHandler) Add(ctx clictx.Context, ictx inputs.Context, elem ad } func handle[T addhdlrs.ElementSpec](ctx clictx.Context, ictx inputs.Context, si addhdlrs.SourceInfo, cv ocm.ComponentVersionAccess, specs []T, h common.ResourceSpecHandler) error { - key := utils2.Plural(h.Key(), 0) + key := utils.Plural(h.Key(), 0) elems, err := addhdlrs.MapSpecsToElems(ctx, ictx, si.Sub(key), specs, h) if err != nil { return errors.Wrapf(err, key) @@ -112,9 +124,12 @@ func handle[T addhdlrs.ElementSpec](ctx clictx.Context, ictx inputs.Context, si return common.ProcessElements(ictx, cv, elems, h) } -//////////////////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////////////////// type ResourceSpec struct { + // Meta enabled to specify information for the serialization + Meta compdesc.Metadata `json:"meta"` + metav1.ObjectMeta `json:",inline"` // Sources defines sources that produced the component Sources []*srcs.ResourceSpec `json:"sources"` diff --git a/cmds/ocm/commands/ocmcmds/common/options/schemaoption/option.go b/cmds/ocm/commands/ocmcmds/common/options/schemaoption/option.go index 9b59a7e9cc..b82f41a209 100644 --- a/cmds/ocm/commands/ocmcmds/common/options/schemaoption/option.go +++ b/cmds/ocm/commands/ocmcmds/common/options/schemaoption/option.go @@ -12,6 +12,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/errors" + utils2 "github.com/open-component-model/ocm/pkg/utils" ) func From(o options.OptionSetProvider) *Option { @@ -20,13 +21,14 @@ func From(o options.OptionSetProvider) *Option { return opt } -func New(def string) *Option { - return &Option{Defaulted: def} +func New(def string, internal ...bool) *Option { + return &Option{Defaulted: def, internal: utils2.Optional(internal...)} } type Option struct { Defaulted string Schema string + internal bool } func (o *Option) AddFlags(fs *pflag.FlagSet) { @@ -38,15 +40,17 @@ func (o *Option) Complete() error { o.Schema = o.Defaulted } if o.Schema != "" { - s := compdesc.DefaultSchemes[o.Schema] - if s == nil { - s = compdesc.DefaultSchemes[metav1.GROUP+"/"+o.Schema] - if s != nil { - o.Schema = metav1.GROUP + "/" + o.Schema + if o.Schema != compdesc.InternalSchemaVersion || !o.internal { + s := compdesc.DefaultSchemes[o.Schema] + if s == nil { + s = compdesc.DefaultSchemes[metav1.GROUP+"/"+o.Schema] + if s != nil { + o.Schema = metav1.GROUP + "/" + o.Schema + } + } + if s == nil { + return errors.ErrUnknown(errors.KIND_SCHEMAVERSION, o.Schema) } - } - if s == nil { - return errors.ErrUnknown(errors.KIND_SCHEMAVERSION, o.Schema) } } return nil @@ -60,10 +64,17 @@ If the option --scheme is given, the specified component descriptor ` } else { s = ` -If the option --scheme is given, the component descriptor is converted to specified format for output. -` +If the option --scheme is given, the component descriptor +is converted to the specified format for output. If no format is given +the storage format of the actual descriptor is used or, for new ones v2 +is used.` + } + if o.internal { + s += ` +With internal the internal representation is shown.` } - s += `The following schema versions are supported: + s += ` +The following schema versions are supported for explicit conversions: ` + utils.FormatList(o.Defaulted, compdesc.DefaultSchemes.Names()...) return s } diff --git a/cmds/ocm/commands/ocmcmds/components/add/cmd.go b/cmds/ocm/commands/ocmcmds/components/add/cmd.go index d409a85c08..c037196231 100644 --- a/cmds/ocm/commands/ocmcmds/components/add/cmd.go +++ b/cmds/ocm/commands/ocmcmds/components/add/cmd.go @@ -117,7 +117,8 @@ Archive. This might be either a directory prepared to host component version content or a tar/tgz file (see option --type). If option --create is given, the archive is created first. An -additional option --force will recreate an empty archive if it already exists. +additional option --force will recreate an empty archive if it +already exists. If option --complete is given all component versions referenced by the added one, will be added, also. Therefore, the --lookup is required @@ -125,13 +126,19 @@ to specify an OCM repository to lookup the missing component versions. If additionally the -V is given, the resources of those additional components will be added by value. -The source, resource and reference list can be composed according the commands -ocm add sources, ocm add resources, ocm add references, respectively. +The source, resource and reference list can be composed according to the commands +ocm add sources, ocm add resources, ocm add references, +respectively. The description file might contain: - a single component as shown in the example - a list of components under the key components - a list of yaml documents with a single component or component list + +The optional field meta.configuredSchemaVersion for a component +entry can be used to specify a dedicated serialization format to use for the +component descriptor. If given it overrides the --schema option +of the command. By default v2 is used. `, } } @@ -186,7 +193,7 @@ func (o *Command) Run() error { printer := common2.NewPrinter(o.Context.StdOut()) fs := o.Context.FileSystem() - h := comp.NewResourceSpecHandler(o.Version) + h := comp.NewResourceSpecHandler(o.Version, schemaoption.From(o).Schema) elems, ictx, err := addhdlrs.ProcessDescriptions(o.Context, printer, templateroption.From(o).Options, h, o.Elements) if err != nil { return err diff --git a/cmds/ocm/commands/ocmcmds/components/get/cmd.go b/cmds/ocm/commands/ocmcmds/components/get/cmd.go index 5846e357b9..e4a7e32a26 100644 --- a/cmds/ocm/commands/ocmcmds/components/get/cmd.go +++ b/cmds/ocm/commands/ocmcmds/components/get/cmd.go @@ -25,6 +25,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" + compdescv2 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/versions/v2" ) var ( @@ -44,7 +45,7 @@ func NewCommand(ctx clictx.Context, names ...string) *cobra.Command { &Command{BaseCommand: utils.NewBaseCommand(ctx, versionconstraintsoption.New(), repooption.New(), output.OutputOptions(outputs, closureoption.New( - "component reference", output.Fields("IDENTITY"), options.Not(output.Selected("tree")), addIdentityField), lookupoption.New(), schemaoption.New(""), + "component reference", output.Fields("IDENTITY"), options.Not(output.Selected("tree")), addIdentityField), lookupoption.New(), schemaoption.New("", true), ))}, utils.Names(Names, names...)..., ) @@ -106,11 +107,18 @@ func TableOutput(opts *output.Options, mapping processing.MappingFunction, wide func Format(opts *output.Options) processing.ProcessChain { o := schemaoption.From(opts) - if o.Schema == "" { + if o.Schema == compdesc.InternalSchemaVersion { return nil } return processing.Map(func(in interface{}) interface{} { desc := comphdlr.Elem(in).GetDescriptor() + schema := o.Schema + if schema == "" { + schema = desc.SchemaVersion() + } + if schema == "" { + schema = compdescv2.SchemaVersion + } out, err := compdesc.Convert(desc, compdesc.SchemaVersion(o.Schema)) if err != nil { return struct { diff --git a/docs/reference/ocm_add_componentversions.md b/docs/reference/ocm_add_componentversions.md index ddd3896bc3..a8ca5130a7 100644 --- a/docs/reference/ocm_add_componentversions.md +++ b/docs/reference/ocm_add_componentversions.md @@ -34,7 +34,8 @@ Archive. This might be either a directory prepared to host component version content or a tar/tgz file (see option --type). If option --create is given, the archive is created first. An -additional option --force will recreate an empty archive if it already exists. +additional option --force will recreate an empty archive if it +already exists. If option --complete is given all component versions referenced by the added one, will be added, also. Therefore, the --lookup is required @@ -42,14 +43,20 @@ to specify an OCM repository to lookup the missing component versions. If additionally the -V is given, the resources of those additional components will be added by value. -The source, resource and reference list can be composed according the commands -[ocm add sources](ocm_add_sources.md), [ocm add resources](ocm_add_resources.md), [ocm add references](ocm_add_references.md), respectively. +The source, resource and reference list can be composed according to the commands +[ocm add sources](ocm_add_sources.md), [ocm add resources](ocm_add_resources.md), [ocm add references](ocm_add_references.md), +respectively. The description file might contain: - a single component as shown in the example - a list of components under the key components - a list of yaml documents with a single component or component list +The optional field meta.configuredSchemaVersion for a component +entry can be used to specify a dedicated serialization format to use for the +component descriptor. If given it overrides the --schema option +of the command. By default v2 is used. + The --type option accepts a file format for the target archive to use. The following formats are supported: - directory @@ -59,7 +66,8 @@ target archive to use. The following formats are supported: The default format is directory. If the option --scheme is given, the specified component descriptor format is used/generated. -The following schema versions are supported: + +The following schema versions are supported for explicit conversions: - ocm.software/v3alpha1: - v2 (default): diff --git a/docs/reference/ocm_create_componentarchive.md b/docs/reference/ocm_create_componentarchive.md index e3b33b4c5f..4896d3a909 100644 --- a/docs/reference/ocm_create_componentarchive.md +++ b/docs/reference/ocm_create_componentarchive.md @@ -34,7 +34,8 @@ target archive to use. The following formats are supported: The default format is directory. If the option --scheme is given, the specified component descriptor format is used/generated. -The following schema versions are supported: + +The following schema versions are supported for explicit conversions: - ocm.software/v3alpha1: - v2 (default): diff --git a/docs/reference/ocm_get_componentversions.md b/docs/reference/ocm_get_componentversions.md index f12191c45c..340297490f 100644 --- a/docs/reference/ocm_get_componentversions.md +++ b/docs/reference/ocm_get_componentversions.md @@ -87,8 +87,12 @@ contains a single component version. Therefore, in this scenario this option must always be specified to be able to follow component references. -If the option --scheme is given, the component descriptor is converted to specified format for output. -The following schema versions are supported: +If the option --scheme is given, the component descriptor +is converted to the specified format for output. If no format is given +the storage format of the actual descriptor is used or, for new ones v2 +is used. +With internal the internal representation is shown. +The following schema versions are supported for explicit conversions: - ocm.software/v3alpha1: - v2: diff --git a/docs/reference/ocm_logging.md b/docs/reference/ocm_logging.md index 41341cbe00..6c0ce36885 100644 --- a/docs/reference/ocm_logging.md +++ b/docs/reference/ocm_logging.md @@ -22,9 +22,9 @@ The following *realms* are used by the command line tool: - ocm/accessmethod/ociartifact: access method ociArtifact - ocm/credentials/dockerconfig: docker config handling as credential repository - ocm/oci.ocireg: OCI repository handling - - ocm/ocm/toi: TOI logging - ocm/plugins: OCM plugin handling - ocm/processing: output processing chains + - ocm/toi: TOI logging - ocm/transfer: OCM transfer handling diff --git a/pkg/contexts/ocm/compdesc/componentdescriptor.go b/pkg/contexts/ocm/compdesc/componentdescriptor.go index 444e75f7fb..1a348ea942 100644 --- a/pkg/contexts/ocm/compdesc/componentdescriptor.go +++ b/pkg/contexts/ocm/compdesc/componentdescriptor.go @@ -14,6 +14,8 @@ import ( "github.com/open-component-model/ocm/pkg/runtime" ) +const InternalSchemaVersion = "internal" + var NotFound = errors.ErrNotFound() const KIND_REFERENCE = "component reference" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/componentdescriptor.go b/pkg/contexts/ocm/compdesc/versions/v2/componentdescriptor.go index 320903db90..0483ce8be3 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/componentdescriptor.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/componentdescriptor.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package compdesc +package v2 import ( "errors" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/default.go b/pkg/contexts/ocm/compdesc/versions/v2/default.go index 67e9566117..a899a6b757 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/default.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/default.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package compdesc +package v2 import ( v1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/signing.go b/pkg/contexts/ocm/compdesc/versions/v2/signing.go index 28a70253b4..29b4f13d1d 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/signing.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/signing.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package compdesc +package v2 import ( "fmt" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/validation.go b/pkg/contexts/ocm/compdesc/versions/v2/validation.go index 959f9dcf32..e1f2b003e1 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/validation.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/validation.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package compdesc +package v2 import ( "k8s.io/apimachinery/pkg/util/validation/field" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/validation_test.go b/pkg/contexts/ocm/compdesc/versions/v2/validation_test.go index 66955e5a6e..0143a925b1 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/validation_test.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/validation_test.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package compdesc_test +package v2_test import ( "testing" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/version.go b/pkg/contexts/ocm/compdesc/versions/v2/version.go index 0ab2988ddb..b5e56b53a9 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/version.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/version.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -package compdesc +package v2 import ( "encoding/json" diff --git a/pkg/contexts/ocm/compdesc/versions/v2/zz_generated.deepcopy.go b/pkg/contexts/ocm/compdesc/versions/v2/zz_generated.deepcopy.go index 89c748c947..96e96af8f4 100644 --- a/pkg/contexts/ocm/compdesc/versions/v2/zz_generated.deepcopy.go +++ b/pkg/contexts/ocm/compdesc/versions/v2/zz_generated.deepcopy.go @@ -7,7 +7,7 @@ // Code generated by controller-gen. DO NOT EDIT. -package compdesc +package v2 import ( "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"