Skip to content

Commit

Permalink
handling of cd versions (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft authored Apr 4, 2023
1 parent 8ef0989 commit 456b644
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 40 deletions.
27 changes: 21 additions & 6 deletions cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -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()
Expand All @@ -104,17 +116,20 @@ 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)
}
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"`
Expand Down
37 changes: 24 additions & 13 deletions cmds/ocm/commands/ocmcmds/common/options/schemaoption/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -60,10 +64,17 @@ If the option <code>--scheme</code> is given, the specified component descriptor
`
} else {
s = `
If the option <code>--scheme</code> is given, the component descriptor is converted to specified format for output.
`
If the option <code>--scheme</code> 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 <code>internal</code> 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
}
15 changes: 11 additions & 4 deletions cmds/ocm/commands/ocmcmds/components/add/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,28 @@ Archive. This might be either a directory prepared to host component version
content or a tar/tgz file (see option --type).
If option <code>--create</code> is given, the archive is created first. An
additional option <code>--force</code> will recreate an empty archive if it already exists.
additional option <code>--force</code> will recreate an empty archive if it
already exists.
If option <code>--complete</code> is given all component versions referenced by
the added one, will be added, also. Therefore, the <code>--lookup</code> is required
to specify an OCM repository to lookup the missing component versions. If
additionally the <code>-V</code> 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
<CMD>ocm add sources</CMD>, <CMD>ocm add resources</CMD>, <CMD>ocm add references</CMD>, respectively.
The source, resource and reference list can be composed according to the commands
<CMD>ocm add sources</CMD>, <CMD>ocm add resources</CMD>, <CMD>ocm add references</CMD>,
respectively.
The description file might contain:
- a single component as shown in the example
- a list of components under the key <code>components</code>
- a list of yaml documents with a single component or component list
The optional field <code>meta.configuredSchemaVersion</code> for a component
entry can be used to specify a dedicated serialization format to use for the
component descriptor. If given it overrides the <code>--schema</code> option
of the command. By default v2 is used.
`,
}
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions cmds/ocm/commands/ocmcmds/components/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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...)...,
)
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 12 additions & 4 deletions docs/reference/ocm_add_componentversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,29 @@ Archive. This might be either a directory prepared to host component version
content or a tar/tgz file (see option --type).

If option <code>--create</code> is given, the archive is created first. An
additional option <code>--force</code> will recreate an empty archive if it already exists.
additional option <code>--force</code> will recreate an empty archive if it
already exists.

If option <code>--complete</code> is given all component versions referenced by
the added one, will be added, also. Therefore, the <code>--lookup</code> is required
to specify an OCM repository to lookup the missing component versions. If
additionally the <code>-V</code> 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 <code>components</code>
- a list of yaml documents with a single component or component list

The optional field <code>meta.configuredSchemaVersion</code> for a component
entry can be used to specify a dedicated serialization format to use for the
component descriptor. If given it overrides the <code>--schema</code> option
of the command. By default v2 is used.

The <code>--type</code> option accepts a file format for the
target archive to use. The following formats are supported:
- directory
Expand All @@ -59,7 +66,8 @@ target archive to use. The following formats are supported:
The default format is <code>directory</code>.

If the option <code>--scheme</code> 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:

- <code>ocm.software/v3alpha1</code>:
- <code>v2</code> (default):
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/ocm_create_componentarchive.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ target archive to use. The following formats are supported:
The default format is <code>directory</code>.

If the option <code>--scheme</code> 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:

- <code>ocm.software/v3alpha1</code>:
- <code>v2</code> (default):
Expand Down
8 changes: 6 additions & 2 deletions docs/reference/ocm_get_componentversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>--scheme</code> is given, the component descriptor is converted to specified format for output.
The following schema versions are supported:
If the option <code>--scheme</code> 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 <code>internal</code> the internal representation is shown.
The following schema versions are supported for explicit conversions:

- <code>ocm.software/v3alpha1</code>:
- <code>v2</code>:
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/ocm_logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ The following *realms* are used by the command line tool:
- <code>ocm/accessmethod/ociartifact</code>: access method ociArtifact
- <code>ocm/credentials/dockerconfig</code>: docker config handling as credential repository
- <code>ocm/oci.ocireg</code>: OCI repository handling
- <code>ocm/ocm/toi</code>: TOI logging
- <code>ocm/plugins</code>: OCM plugin handling
- <code>ocm/processing</code>: output processing chains
- <code>ocm/toi</code>: TOI logging
- <code>ocm/transfer</code>: OCM transfer handling


Expand Down
2 changes: 2 additions & 0 deletions pkg/contexts/ocm/compdesc/componentdescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package compdesc
package v2

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/compdesc/versions/v2/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/compdesc/versions/v2/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package compdesc
package v2

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/compdesc/versions/v2/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package compdesc
package v2

import (
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/compdesc/versions/v2/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package compdesc_test
package v2_test

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/compdesc/versions/v2/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

package compdesc
package v2

import (
"encoding/json"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 456b644

Please sign in to comment.