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

simple plugin concept #169

Merged
merged 6 commits into from
Nov 3, 2022
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
28 changes: 12 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ else
GOBIN=$(shell go env GOBIN)
endif

NOW := $(shell date --rfc-3339=seconds | sed 's/ /T/')
BUILD_FLAGS := "-s -w \
-X github.com/open-component-model/ocm/pkg/version.gitVersion=$(EFFECTIVE_VERSION) \
-X github.com/open-component-model/ocm/pkg/version.gitTreeState=$(GIT_TREE_STATE) \
-X github.com/open-component-model/ocm/pkg/version.gitCommit=$(COMMIT) \
-X github.com/open-component-model/ocm/pkg/version.buildDate=$(NOW)"

build: ${SOURCES}
mkdir -p bin
go build -ldflags "-s -w \
-X github.com/open-component-model/ocm/pkg/version.gitVersion=$(EFFECTIVE_VERSION) \
-X github.com/open-component-model/ocm/pkg/version.gitTreeState=$(GIT_TREE_STATE) \
-X github.com/open-component-model/ocm/pkg/version.gitCommit=$(COMMIT) \
-X github.com/open-component-model/ocm/pkg/version.buildDate=$(shell date --rfc-3339=seconds | sed 's/ /T/')" \
-o bin/ocm \
./cmds/ocm

go build -ldflags "-s -w \
-X github.com/open-component-model/ocm/pkg/version.gitVersion=$(EFFECTIVE_VERSION) \
-X github.com/open-component-model/ocm/pkg/version.gitTreeState=$(GIT_TREE_STATE) \
-X github.com/open-component-model/ocm/pkg/version.gitCommit=$(COMMIT) \
-X github.com/open-component-model/ocm/pkg/version.buildDate=$(shell date --rfc-3339=seconds | sed 's/ /T/')" \
-o bin/helminstaller \
./cmds/helminstaller
go build -ldflags $(BUILD_FLAGS) -o bin/ocm ./cmds/ocm
go build -ldflags $(BUILD_FLAGS) -o bin/helminstaller ./cmds/helminstaller
go build -ldflags $(BUILD_FLAGS) -o bin/demo ./cmds/demoplugin


.PHONY: install-requirements
install-requirements:
Expand Down Expand Up @@ -126,4 +122,4 @@ generate-deepcopy: controller-gen ## Generate code containing DeepCopy, DeepCopy
generate-license:
for f in $(shell find . -name "*.go" -o -name "*.sh"); do \
reuse addheader -r --copyright="SAP SE or an SAP affiliate company and Open Component Model contributors." --license="Apache-2.0" $$f --skip-unrecognised; \
done
done
7 changes: 7 additions & 0 deletions cmds/common/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package common

const CONSUMER_TYPE = "demo"
84 changes: 84 additions & 0 deletions cmds/demoplugin/accessmethods/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package accessmethods

import (
out "fmt"
"io"
"os"
"strings"

"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/open-component-model/ocm/cmds/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/oci/identity"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/runtime"
)

const NAME = "demo"
const VERSION = "v1"

type AccessSpec struct {
runtime.ObjectVersionedType `json:",inline"`

Path string `json:"path"`
MediaType string `json:"mediaType,omitempty"`
}

type AccessMethod struct {
ppi.AccessMethodBase
}

var _ ppi.AccessMethod = (*AccessMethod)(nil)

func New() ppi.AccessMethod {
return &AccessMethod{
AccessMethodBase: ppi.MustNewAccessMethodBase(NAME, "", &AccessSpec{}, "demo access to temp files", ""),
}
}

func (a *AccessMethod) Decode(data []byte, unmarshaler runtime.Unmarshaler) (runtime.TypedObject, error) {
if unmarshaler == nil {
unmarshaler = runtime.DefaultYAMLEncoding
}
var spec AccessSpec
err := unmarshaler.Unmarshal(data, &spec)
if err != nil {
return nil, err
}
return &spec, nil
}

func (a *AccessMethod) ValidateSpecification(p ppi.Plugin, spec ppi.AccessSpec) (*ppi.AccessSpecInfo, error) {
var info ppi.AccessSpecInfo

my := spec.(*AccessSpec)

if my.Path == "" {
return nil, out.Errorf("path not specified")
}
if strings.HasPrefix(my.Path, "/") {
return nil, out.Errorf("path must be relative (%s)", my.Path)
}
if my.MediaType == "" {
return nil, out.Errorf("mediaType not specified")
}
info.MediaType = my.MediaType
info.ConsumerId = credentials.ConsumerIdentity{
identity.ID_TYPE: common.CONSUMER_TYPE,
identity.ID_HOSTNAME: "localhost",
identity.ID_PATHPREFIX: my.Path,
}
info.Short = "temp file " + my.Path
info.Hint = "temp file " + my.Path
return &info, nil
}

func (a *AccessMethod) Reader(p ppi.Plugin, spec ppi.AccessSpec, creds credentials.Credentials) (io.ReadCloser, error) {
my := spec.(*AccessSpec)

return os.Open(filepath.Join(os.TempDir(), my.Path))
}
30 changes: 30 additions & 0 deletions cmds/demoplugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package main

import (
"os"

"github.com/open-component-model/ocm/cmds/demoplugin/accessmethods"
"github.com/open-component-model/ocm/cmds/demoplugin/uploaders"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds"
"github.com/open-component-model/ocm/pkg/version"
)

func main() {
p := ppi.NewPlugin("demo", version.Get().String())

p.SetShort("demo plugin")
p.SetLong("plugin providing access to temp files.")

p.RegisterAccessMethod(accessmethods.New())
u := uploaders.New()
p.RegisterUploader("testArtefact", "", u)
err := cmds.NewPluginCommand(p).Execute(os.Args[1:])
if err != nil {
os.Exit(1)
}
}
109 changes: 109 additions & 0 deletions cmds/demoplugin/uploaders/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package uploaders

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/open-component-model/ocm/cmds/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/oci/identity"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/runtime"

"github.com/open-component-model/ocm/cmds/demoplugin/accessmethods"
)

const NAME = "demo"
const VERSION = "v1"

type TargetSpec struct {
runtime.ObjectVersionedType `json:",inline"`

Path string `json:"path"`
}

var types map[string]runtime.TypedObjectDecoder

func init() {
decoder, err := runtime.NewDirectDecoder(&TargetSpec{})
if err != nil {
panic(err)
}
types = map[string]runtime.TypedObjectDecoder{NAME + runtime.VersionSeparator + VERSION: decoder}
}

type Uploader struct {
ppi.UploaderBase
}

var _ ppi.Uploader = (*Uploader)(nil)

func New() ppi.Uploader {
return &Uploader{
UploaderBase: ppi.MustNewUploaderBase("demo", "upload temp files"),
}
}

func (a *Uploader) Decoders() map[string]runtime.TypedObjectDecoder {
return types
}

func (a *Uploader) ValidateSpecification(p ppi.Plugin, spec ppi.UploadTargetSpec) (*ppi.UploadTargetSpecInfo, error) {
var info ppi.UploadTargetSpecInfo
my := spec.(*TargetSpec)

if strings.HasPrefix(my.Path, "/") {
return nil, fmt.Errorf("path must be relative (%s)", my.Path)
}

info.ConsumerId = credentials.ConsumerIdentity{
identity.ID_TYPE: common.CONSUMER_TYPE,
identity.ID_HOSTNAME: "localhost",
identity.ID_PATHPREFIX: my.Path,
}
return &info, nil
}

func (a *Uploader) Writer(p ppi.Plugin, arttype, mediatype, hint string, repo ppi.UploadTargetSpec, creds credentials.Credentials) (io.WriteCloser, ppi.AccessSpecProvider, error) {
var file *os.File
var err error

my := repo.(*TargetSpec)

path := hint
root := os.TempDir()
dir := root
if my.Path != "" {
root = filepath.Join(root, my.Path)
if hint == "" {
path = my.Path
dir = filepath.Join(dir, path)
} else {
path = filepath.Join(my.Path, hint)
dir = filepath.Join(dir, filepath.Dir(path))
}
}

err = os.MkdirAll(dir, 0o700)
if err != nil {
return nil, nil, err
}

if hint == "" {
file, err = os.CreateTemp(root, "demo.*.blob")
} else {
file, err = os.OpenFile(filepath.Join(os.TempDir(), path), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600)
}
if err != nil {
return nil, nil, err
}
writer := NewWriter(file, path, mediatype, hint == "", accessmethods.NAME, accessmethods.VERSION)
return writer, writer.Specification, nil
}
66 changes: 66 additions & 0 deletions cmds/demoplugin/uploaders/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package uploaders

import (
"os"
"path/filepath"

"github.com/open-component-model/ocm/cmds/demoplugin/accessmethods"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
)

type writer = accessio.DigestWriter

type Writer struct {
*writer
file *os.File
path string
rename bool
name string
version string
media string
spec *accessmethods.AccessSpec
}

func NewWriter(file *os.File, path string, media string, rename bool, name, version string) *Writer {
return &Writer{
writer: accessio.NewDefaultDigestWriter(file),
file: file,
path: path,
rename: rename,
name: name,
version: version,
media: media,
}
}

func (w *Writer) Close() error {
err := w.writer.Close()
if err == nil {
n := w.path
if w.rename {
n = filepath.Join(os.TempDir(), n, common.DigestToFileName(w.writer.Digest()))
err := os.Rename(w.file.Name(), n)
if err != nil {
return errors.Wrapf(err, "cannot rename %q to %q", w.file.Name(), n)
}
}
w.spec = &accessmethods.AccessSpec{
ObjectVersionedType: runtime.NewVersionedObjectType(w.name, w.version),
Path: n,
MediaType: w.media,
}
}
return err
}

func (w *Writer) Specification() ppi.AccessSpec {
return w.spec
}
5 changes: 4 additions & 1 deletion cmds/ocm/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
common2 "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/componentarchive"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/components"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/plugins"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/references"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/resources"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/sources"
Expand Down Expand Up @@ -55,6 +56,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr"
datacfg "github.com/open-component-model/ocm/pkg/contexts/datacontext/config/attrs"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/utils"
"github.com/open-component-model/ocm/pkg/errors"
ocmlog "github.com/open-component-model/ocm/pkg/logging"
Expand Down Expand Up @@ -172,6 +174,7 @@ func NewCliCommand(ctx clictx.Context, mod ...func(clictx.Context, *cobra.Comman
cmd.AddCommand(cmdutils.HideCommand(references.NewCommand(opts.Context)))
cmd.AddCommand(cmdutils.HideCommand(sources.NewCommand(opts.Context)))
cmd.AddCommand(cmdutils.HideCommand(components.NewCommand(opts.Context)))
cmd.AddCommand(cmdutils.HideCommand(plugins.NewCommand(opts.Context)))

cmd.AddCommand(cmdutils.HideCommand(cachecmds.NewCommand(opts.Context)))
cmd.AddCommand(cmdutils.HideCommand(ocicmds.NewCommand(opts.Context)))
Expand Down Expand Up @@ -316,7 +319,7 @@ func (o *CLIOptions) Complete() error {
}
err = ctx.ApplyConfig(spec, "cli")
}
return err
return plugincacheattr.Get(o.Context.OCMContext()).RegisterExtensions()
}

func NewVersionCommand(ctx clictx.Context) *cobra.Command {
Expand Down
2 changes: 2 additions & 0 deletions cmds/ocm/commands/ocmcmds/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/componentarchive"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/components"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/ctf"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/plugins"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/references"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/resourceconfig"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/resources"
Expand All @@ -35,6 +36,7 @@ func NewCommand(ctx clictx.Context) *cobra.Command {
cmd.AddCommand(ctf.NewCommand(ctx))
cmd.AddCommand(componentarchive.NewCommand(ctx))
cmd.AddCommand(versions.NewCommand(ctx))
cmd.AddCommand(plugins.NewCommand(ctx))

cmd.AddCommand(topicocmrefs.New(ctx))
return cmd
Expand Down
Loading