Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

component constructor with references field #1054

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package comp

import (
"fmt"

"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/finalizer"
"github.com/mandelsoft/goutils/set"
Expand All @@ -21,9 +23,13 @@ func ProcessComponents(ctx clictx.Context, ictx inputs.Context, repo ocm.Reposit

for _, elem := range elems {
if r, ok := elem.Spec().(*ResourceSpec); ok {
if len(r.References) > 0 && len(r.OldReferences) > 0 {
return fmt.Errorf("only field references or componentReferences (deprecated) is possible")
}
list.Add(addhdlrs.ValidateElementSpecIdentities("resource", elem.Source().String(), sliceutils.Convert[addhdlrs.ElementSpec](r.Resources)))
list.Add(addhdlrs.ValidateElementSpecIdentities("source", elem.Source().String(), sliceutils.Convert[addhdlrs.ElementSpec](r.Sources)))
list.Add(addhdlrs.ValidateElementSpecIdentities("reference", elem.Source().String(), sliceutils.Convert[addhdlrs.ElementSpec](r.References)))
list.Add(addhdlrs.ValidateElementSpecIdentities("reference", elem.Source().String(), sliceutils.Convert[addhdlrs.ElementSpec](r.OldReferences)))
}
}
if err := list.Result(); err != nil {
Expand Down
13 changes: 12 additions & 1 deletion cmds/ocm/commands/ocmcmds/common/addhdlrs/comp/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,18 @@ func (h *ResourceSpecHandler) Add(ctx clictx.Context, ictx inputs.Context, elem
if err != nil {
return err
}

if len(r.References) > 0 && len(r.OldReferences) > 0 {
return fmt.Errorf("only field references or componentReferences (deprecated) is possible")
}
err = handle(ctx, ictx, elem.Source(), cv, r.References, h.refhandler)
if err != nil {
return err
}
err = handle(ctx, ictx, elem.Source(), cv, r.OldReferences, h.refhandler)
if err != nil {
return err
}
return comp.AddVersion(cv)
}

Expand All @@ -169,7 +177,10 @@ type ResourceSpec struct {
// Sources defines sources that produced the component
Sources []*srcs.ResourceSpec `json:"sources"`
// References references component dependencies that can be resolved in the current context.
References []*refs.ResourceSpec `json:"componentReferences"`
References []*refs.ResourceSpec `json:"references"`
// OldReferences references component dependencies that can be resolved in the current context.
// Deprecated: use field References.
OldReferences []*refs.ResourceSpec `json:"componentReferences"`
// Resources defines all resources that are created by the component and by a third party.
Resources []*rscs.ResourceSpec `json:"resources"`
}
Expand Down
8 changes: 7 additions & 1 deletion cmds/ocm/commands/ocmcmds/components/add/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"ocm.software/ocm/api/ocm/extensions/accessmethods/ociartifact"
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/repositories/ctf"
ocmutils "ocm.software/ocm/api/ocm/ocmutils"
"ocm.software/ocm/api/ocm/ocmutils"
"ocm.software/ocm/api/ocm/valuemergehandler/handlers/defaultmerge"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
Expand Down Expand Up @@ -92,6 +92,12 @@ var _ = Describe("Test Environment", func() {
CheckComponent(env, nil)
})

It("creates ctf and adds component (deprecated)", func() {
Expect(env.Execute("add", "c", "-fc", "--file", ARCH, "testdata/component-constructor-old.yaml")).To(Succeed())
Expect(env.DirExists(ARCH)).To(BeTrue())
CheckComponent(env, nil)
})

It("creates ctf and adds components", func() {
Expect(env.Execute("add", "c", "-fc", "--file", ARCH, "--version", "1.0.0", "testdata/component-constructor.yaml")).To(Succeed())
Expect(env.DirExists(ARCH)).To(BeTrue())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: ocm.software/demo/test
version: 1.0.0
provider:
name: ocm.software
labels:
- name: city
value: Karlsruhe
labels:
- name: purpose
value: test

resources:
- name: text
type: PlainText
labels:
- name: city
value: Karlsruhe
merge:
algorithm: default
config:
overwrite: inbound
input:
type: file
path: testdata
- name: data
type: PlainText
input:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resources:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
references:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resources:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
references:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ resources:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
references:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ resources:
type: binary
data: IXN0cmluZ2RhdGE=

componentReferences:
references:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ components:
input:
type: binary
data: IXN0cmluZ2RhdGE=
componentReferences:
references:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ components:
input:
type: binary
data: IXN0cmluZ2RhdGE=
componentReferences:
references:
- name: ref
version: v1
componentName: github.com/mandelsoft/test2
2 changes: 1 addition & 1 deletion components/helmdemo/component-constructor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ components:
name: (( values.PROVIDER))
# use all platforms and create a resource for each
# ADD back once https://github.com/open-component-model/ocm/issues/1041 is fixed
componentReferences:
references:
- name: installer
componentName: (( values.HELMINSTCOMP ))
version: (( values.HELMINSTVERSION ))
Expand Down
2 changes: 1 addition & 1 deletion components/subchartsdemo/component-constructor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ components:
version: ${VERSION}
provider:
name: ${PROVIDER}
componentReferences:
references:
- name: echoserver
componentName: ${COMPONENT_PREFIX}/echoserver
version: "${ECHO_VERSION}"
Expand Down