-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
composition repo and version + meta composition
- Loading branch information
1 parent
ada2e52
commit 09cb473
Showing
16 changed files
with
646 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements | ||
|
||
type ArtifactOption interface { | ||
ResourceMetaOption | ||
SourceMetaOption | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements | ||
|
||
import ( | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" | ||
) | ||
|
||
type CommonOption interface { | ||
ResourceMetaOption | ||
SourceMetaOption | ||
ReferenceOption | ||
} | ||
|
||
type commonOption struct { | ||
apply func(meta *compdesc.ElementMeta) error | ||
} | ||
|
||
type commonOptionI interface { | ||
apply(*compdesc.ElementMeta) error | ||
} | ||
|
||
func newCommonOption[T commonOptionI](e T) CommonOption { | ||
return commonOption{e.apply} | ||
} | ||
|
||
func (o commonOption) ApplyToResourceMeta(m *compdesc.ResourceMeta) error { | ||
return o.apply(&m.ElementMeta) | ||
} | ||
|
||
func (o commonOption) ApplyToSourceMeta(m *compdesc.SourceMeta) error { | ||
return o.apply(&m.ElementMeta) | ||
} | ||
|
||
func (o commonOption) ApplyToReference(m *compdesc.ComponentReference) error { | ||
return o.apply(&m.ElementMeta) | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type version string | ||
|
||
func (o version) apply(m *compdesc.ElementMeta) error { | ||
m.Version = string(o) | ||
return nil | ||
} | ||
|
||
// WithVersion sets the version of the element. | ||
func WithVersion(v string) CommonOption { | ||
return newCommonOption(version(v)) | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type extraIdentity struct { | ||
id metav1.Identity | ||
} | ||
|
||
func (o *extraIdentity) apply(m *compdesc.ElementMeta) error { | ||
if m.ExtraIdentity == nil { | ||
m.ExtraIdentity = o.id.Copy() | ||
} else { | ||
for n, v := range o.id { | ||
m.ExtraIdentity.Set(n, v) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// WithExtraIdentity adds extra identity properties. | ||
func WithExtraIdentity(extras ...string) CommonOption { | ||
return newCommonOption(&extraIdentity{compdesc.NewExtraIdentity(extras...)}) | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type label struct { | ||
name string | ||
value interface{} | ||
opts []metav1.LabelOption | ||
} | ||
|
||
func (o *label) apply(m *compdesc.ElementMeta) error { | ||
return m.Labels.Set(o.name, o.value, o.opts...) | ||
} | ||
|
||
func WithLabel(name string, value interface{}, opts ...metav1.LabelOption) CommonOption { | ||
return newCommonOption(&label{name, value, opts}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements | ||
|
||
import ( | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" | ||
) | ||
|
||
type ResourceReferenceOption interface { | ||
ResourceMetaOption | ||
ReferenceOption | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type digest metav1.DigestSpec | ||
|
||
func (o *digest) ApplyToReference(m *compdesc.ComponentReference) error { | ||
if !(*metav1.DigestSpec)(o).IsNone() { | ||
m.Digest = (*metav1.DigestSpec)(o).Copy() | ||
} | ||
return nil | ||
} | ||
|
||
func (o *digest) ApplyToResourceMeta(m *compdesc.ResourceMeta) error { | ||
if !(*metav1.DigestSpec)(o).IsNone() { | ||
m.Digest = (*metav1.DigestSpec)(o).Copy() | ||
} | ||
return nil | ||
} | ||
|
||
// WithDigest sets digest information. | ||
// at least one value should be set. | ||
func WithDigest(algo, norm, value string) ResourceReferenceOption { | ||
return &digest{HashAlgorithm: algo, NormalisationAlgorithm: norm, Value: value} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements | ||
|
||
import ( | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
"github.com/open-component-model/ocm/pkg/errors" | ||
) | ||
|
||
type ReferenceOption interface { | ||
ApplyToReference(reference *compdesc.ComponentReference) error | ||
} | ||
|
||
func Reference(name, comp, vers string, opts ...ReferenceOption) (*compdesc.ComponentReference, error) { | ||
m := compdesc.NewComponentReference(name, comp, vers, nil) | ||
list := errors.ErrList() | ||
for _, o := range opts { | ||
if o != nil { | ||
list.Add(o.ApplyToReference(m)) | ||
} | ||
} | ||
return m, list.Result() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements | ||
|
||
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" | ||
"github.com/open-component-model/ocm/pkg/utils" | ||
) | ||
|
||
type ResourceMetaOption interface { | ||
ApplyToResourceMeta(*compdesc.ResourceMeta) error | ||
} | ||
|
||
func ResourceMeta(name, typ string, opts ...ResourceMetaOption) (*compdesc.ResourceMeta, error) { | ||
m := compdesc.NewResourceMeta(name, typ, metav1.LocalRelation) | ||
list := errors.ErrList() | ||
for _, o := range opts { | ||
if o != nil { | ||
list.Add(o.ApplyToResourceMeta(m)) | ||
} | ||
} | ||
return m, list.Result() | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type local bool | ||
|
||
func (o local) ApplyToResourceMeta(m *compdesc.ResourceMeta) error { | ||
if o { | ||
m.Relation = metav1.LocalRelation | ||
} else { | ||
m.Relation = metav1.ExternalRelation | ||
} | ||
return nil | ||
} | ||
|
||
// WithLocalRelation sets the resource relation to metav1.LocalRelation. | ||
func WithLocalRelation(flag ...bool) ResourceMetaOption { | ||
return local(utils.OptionalDefaultedBool(true, flag...)) | ||
} | ||
|
||
// WithExternalRelation sets the resource relation to metav1.ExternalRelation. | ||
func WithExternalRelation(flag ...bool) ResourceMetaOption { | ||
return local(!utils.OptionalDefaultedBool(true, flag...)) | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type srcref struct { | ||
ref metav1.StringMap | ||
labels metav1.Labels | ||
errlist errors.ErrorList | ||
} | ||
|
||
var _ ResourceMetaOption = (*srcref)(nil) | ||
|
||
func (o *srcref) ApplyToResourceMeta(m *compdesc.ResourceMeta) error { | ||
if err := o.errlist.Result(); err != nil { | ||
return err | ||
} | ||
m.SourceRef = append(m.SourceRef, compdesc.SourceRef{IdentitySelector: o.ref.Copy(), Labels: o.labels.Copy()}) | ||
return nil | ||
} | ||
|
||
func (o *srcref) WithLabel(name string, value interface{}, opts ...metav1.LabelOption) *srcref { | ||
r := &srcref{ref: o.ref, labels: o.labels.Copy()} | ||
r.errlist.Add(r.labels.Set(name, value, opts...)) | ||
return r | ||
} | ||
|
||
// WithSourceRef adds a source reference to a resource meta object. | ||
// this is a sequence of name/value pairs. | ||
// Optionally, additional labels can be added with srcref.WithLabel. | ||
func WithSourceRef(sel ...string) *srcref { | ||
return &srcref{ref: metav1.StringMap(metav1.NewExtraIdentity(sel...))} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/open-component-model/ocm/pkg/testutils" | ||
|
||
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/digester/digesters/blob" | ||
me "github.com/open-component-model/ocm/pkg/contexts/ocm/elements" | ||
"github.com/open-component-model/ocm/pkg/signing/hasher/sha256" | ||
) | ||
|
||
type value struct { | ||
Field string `json:"field"` | ||
} | ||
|
||
var _ = Describe("resources", func() { | ||
It("configures resource meta", func() { | ||
m := Must(me.ResourceMeta("name", "type", | ||
me.WithVersion("v1"), | ||
me.WithExtraIdentity("extra", "value"), | ||
me.WithLabel("label", value{"value"}, metav1.WithSigning(), metav1.WithVersion("v1")), | ||
me.WithSourceRef("name", "image").WithLabel("prop", "x"), | ||
me.WithDigest(sha256.Algorithm, blob.GenericBlobDigestV1, "0815"), | ||
)) | ||
Expect(m).To(YAMLEqual(` | ||
name: name | ||
type: type | ||
version: v1 | ||
relation: local | ||
extraIdentity: | ||
extra: value | ||
labels: | ||
- name: label | ||
version: v1 | ||
value: | ||
field: value | ||
signing: true | ||
srcRef: | ||
- identitySelector: | ||
name: image | ||
labels: | ||
- name: prop | ||
value: x | ||
digest: | ||
hashAlgorithm: SHA-256 | ||
normalisationAlgorithm: genericBlobDigest/v1 | ||
value: "0815" | ||
`)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements | ||
|
||
import ( | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
"github.com/open-component-model/ocm/pkg/errors" | ||
) | ||
|
||
type SourceMetaOption interface { | ||
ApplyToSourceMeta(*compdesc.SourceMeta) error | ||
} | ||
|
||
func SourceMeta(name, typ string, opts ...SourceMetaOption) (*compdesc.SourceMeta, error) { | ||
m := compdesc.NewSourceMeta(name, typ) | ||
list := errors.ErrList() | ||
for _, o := range opts { | ||
if o != nil { | ||
list.Add(o.ApplyToSourceMeta(m)) | ||
} | ||
} | ||
return m, list.Result() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package elements_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Element builder") | ||
} |
Oops, something went wrong.