-
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.
extension point for pub/sub support (#832)
#### What this PR does / why we need it: - extension point to support publish/subscribe systems. - fix repo type for composition repositories For some doc look at go doc for package pkg/contexts/ocm/pubsub. #### Which issue(s) this PR fixes: --------- Co-authored-by: Fabian Burth <[email protected]>
- Loading branch information
1 parent
0cec0c2
commit 5cc3a97
Showing
54 changed files
with
2,291 additions
and
63 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
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,23 @@ | ||
package pubsub | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names" | ||
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/pubsub/get" | ||
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/pubsub/set" | ||
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils" | ||
"github.com/open-component-model/ocm/pkg/contexts/clictx" | ||
) | ||
|
||
var Names = names.PubSub | ||
|
||
// NewCommand creates a new pubsub command. | ||
func NewCommand(ctx clictx.Context) *cobra.Command { | ||
cmd := utils.MassageCommand(&cobra.Command{ | ||
Short: "Commands acting on sub/sub specifications", | ||
}, Names...) | ||
cmd.AddCommand(get.NewCommand(ctx, get.Verb)) | ||
cmd.AddCommand(set.NewCommand(ctx, set.Verb)) | ||
return cmd | ||
} |
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,132 @@ | ||
package get | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mandelsoft/goutils/sliceutils" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names" | ||
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs" | ||
"github.com/open-component-model/ocm/cmds/ocm/pkg/output" | ||
"github.com/open-component-model/ocm/cmds/ocm/pkg/processing" | ||
"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/cpi" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/pubsub" | ||
) | ||
|
||
var ( | ||
Names = names.PubSub | ||
Verb = verbs.Get | ||
) | ||
|
||
type Command struct { | ||
utils.BaseCommand | ||
|
||
RepoSpecs []string | ||
} | ||
|
||
var _ utils.OCMCommand = (*Command)(nil) | ||
|
||
// NewCommand creates a new pubsub command. | ||
func NewCommand(ctx clictx.Context, names ...string) *cobra.Command { | ||
return utils.SetupCommand(&Command{BaseCommand: utils.NewBaseCommand(ctx, output.OutputOptions(outputs))}, utils.Names(Names, names...)...) | ||
} | ||
|
||
func (o *Command) ForName(name string) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "{<ocm repository>}", | ||
Short: "Get the pubsub spec for an ocm repository", | ||
Long: ` | ||
A repository may be able to store a publish/subscribe specification | ||
to propagate the creation or update of component version. | ||
If such an implementation is available and a specification is | ||
assigned to the repository, it is shown. The specification | ||
can be set with the <CMD>ocm set pubsub</CMD>. | ||
`, | ||
} | ||
} | ||
|
||
func (o *Command) Complete(args []string) error { | ||
o.RepoSpecs = args | ||
return nil | ||
} | ||
|
||
func (o *Command) Run() error { | ||
return utils.HandleOutputsFor("repository spec", output.From(o), o.transform, o.RepoSpecs...) | ||
} | ||
|
||
func (o *Command) transform(in string) *Repo { | ||
var spec cpi.RepositorySpec | ||
rs := &Repo{RepoSpec: in} | ||
u, err := ocm.ParseRepo(in) | ||
if err == nil { | ||
spec, err = o.OCMContext().MapUniformRepositorySpec(&u) | ||
} | ||
if err == nil { | ||
rs.Repo, err = o.OCMContext().RepositoryForSpec(spec) | ||
} | ||
if err == nil { | ||
rs.Spec, err = pubsub.SpecForRepo(rs.Repo) | ||
} | ||
if err != nil { | ||
rs.Error = err.Error() | ||
} | ||
return rs | ||
} | ||
|
||
type Repo struct { | ||
RepoSpec string `json:"repository"` | ||
Repo cpi.Repository `json:"-"` | ||
Spec pubsub.PubSubSpec `json:"pubsub,omitempty"` | ||
Error string `json:"error,omitempty"` | ||
} | ||
|
||
var _ output.Manifest = (*Repo)(nil) | ||
|
||
func (r *Repo) AsManifest() interface{} { | ||
return r | ||
} | ||
|
||
var outputs = output.NewOutputs(getRegular).AddManifestOutputs() | ||
|
||
func TableOutput(opts *output.Options, mapping processing.MappingFunction) *output.TableOutput { | ||
return &output.TableOutput{ | ||
Headers: output.Fields("REPOSITORY", "PUBSUBTYPE", "ERROR"), | ||
Options: opts, | ||
Mapping: mapping, | ||
} | ||
} | ||
|
||
func getRegular(opts *output.Options) output.Output { | ||
return TableOutput(opts, mapGetRegularOutput).New() | ||
} | ||
|
||
func mapGetRegularOutput(e interface{}) interface{} { | ||
r := e.(*Repo) | ||
if r.Error != "" { | ||
return output.Fields(r.RepoSpec, "", r.Error) | ||
} | ||
if r.Spec == nil { | ||
return output.Fields(r.RepoSpec, "-", "") | ||
} | ||
list := sliceutils.Slice[string]{} | ||
Add(r.Repo.GetContext(), r.Spec, &list) | ||
strings.Join(list, ", ") | ||
|
||
return output.Fields(r.RepoSpec, strings.Join(list, ", "), "") | ||
} | ||
|
||
func Add(ctx cpi.Context, s pubsub.PubSubSpec, slice *sliceutils.Slice[string]) { | ||
if s == nil { | ||
return | ||
} | ||
slice.Add(s.GetKind()) | ||
if u, ok := s.(pubsub.Unwrapable); ok { | ||
for _, n := range u.Unwrap(ctx) { | ||
Add(ctx, n, slice) | ||
} | ||
} | ||
} |
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,106 @@ | ||
package get_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
. "github.com/mandelsoft/goutils/testutils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/open-component-model/ocm/cmds/ocm/testhelper" | ||
|
||
"github.com/open-component-model/ocm/pkg/common/accessio" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/pubsub" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/pubsub/providers/ocireg" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/ctf" | ||
"github.com/open-component-model/ocm/pkg/runtime" | ||
) | ||
|
||
const ARCH = "ctf" | ||
const ARCH2 = "ctf2" | ||
|
||
var _ = Describe("Test Environment", func() { | ||
var env *TestEnv | ||
|
||
BeforeEach(func() { | ||
env = NewTestEnv() | ||
env.OCMCommonTransport(ARCH, accessio.FormatDirectory) | ||
env.OCMCommonTransport(ARCH2, accessio.FormatDirectory) | ||
attr := pubsub.For(env) | ||
attr.ProviderRegistry.Register(ctf.Type, &ocireg.Provider{}) | ||
attr.TypeScheme.Register(pubsub.NewPubSubType[*Spec](Type)) | ||
attr.TypeScheme.Register(pubsub.NewPubSubType[*Spec](TypeV1)) | ||
|
||
repo := Must(ctf.Open(env, ctf.ACC_WRITABLE, ARCH, 0o600, env)) | ||
defer repo.Close() | ||
MustBeSuccessful(pubsub.SetForRepo(repo, NewSpec("testtarget"))) | ||
}) | ||
|
||
AfterEach(func() { | ||
env.Cleanup() | ||
}) | ||
|
||
It("get pubsub", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("get", "pubsub", ARCH)) | ||
Expect(buf.String()).To(StringEqualTrimmedWithContext(` | ||
REPOSITORY PUBSUBTYPE ERROR | ||
ctf test | ||
`)) | ||
}) | ||
|
||
It("get pubsub list", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("get", "pubsub", ARCH, ARCH2, "ARCH2")) | ||
Expect(buf.String()).To(StringEqualTrimmedWithContext(` | ||
REPOSITORY PUBSUBTYPE ERROR | ||
ctf test | ||
ctf2 - | ||
ARCH2 repository "ARCH2" is unknown | ||
`)) | ||
}) | ||
|
||
It("get pubsub yaml", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("get", "pubsub", ARCH, "-o", "yaml")) | ||
Expect(buf.String()).To(StringEqualTrimmedWithContext(` | ||
--- | ||
pubsub: | ||
target: testtarget | ||
type: test | ||
repository: ctf | ||
`)) | ||
}) | ||
}) | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
const ( | ||
Type = "test" | ||
TypeV1 = Type + runtime.VersionSeparator + "v1" | ||
) | ||
|
||
type Spec struct { | ||
runtime.ObjectVersionedType | ||
Target string `json:"target"` | ||
} | ||
|
||
var ( | ||
_ pubsub.PubSubSpec = (*Spec)(nil) | ||
) | ||
|
||
func NewSpec(target string) *Spec { | ||
return &Spec{runtime.NewVersionedObjectType(Type), target} | ||
} | ||
|
||
func (s *Spec) PubSubMethod(repo ocm.Repository) (pubsub.PubSubMethod, error) { | ||
return nil, nil | ||
} | ||
|
||
func (s *Spec) Describe(_ ocm.Context) string { | ||
return fmt.Sprintf("test pubsub") | ||
} |
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,13 @@ | ||
package get_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "get pubsub Test Suite") | ||
} |
Oops, something went wrong.