-
Notifications
You must be signed in to change notification settings - Fork 18
/
registration.go
143 lines (131 loc) · 5.22 KB
/
registration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package registration
import (
"golang.org/x/exp/slices"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/action"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/action/handlers"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
pluginaccess "github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/plugin"
pluginaction "github.com/open-component-model/ocm/pkg/contexts/ocm/actionhandler/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
pluginupload "github.com/open-component-model/ocm/pkg/contexts/ocm/blobhandler/handlers/generic/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/download"
plugindownload "github.com/open-component-model/ocm/pkg/contexts/ocm/download/handlers/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/labels/routingslip/spi"
pluginroutingslip "github.com/open-component-model/ocm/pkg/contexts/ocm/labels/routingslip/types/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/descriptor"
"github.com/open-component-model/ocm/pkg/contexts/ocm/valuemergehandler"
pluginmerge "github.com/open-component-model/ocm/pkg/contexts/ocm/valuemergehandler/handlers/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/valuemergehandler/hpi"
"github.com/open-component-model/ocm/pkg/runtime"
)
// RegisterExtensions registers all the extension provided by the found plugin.
func RegisterExtensions(ctx ocm.Context) error {
pi := plugincacheattr.Get(ctx)
logger := Logger(ctx)
vmreg := valuemergehandler.For(ctx)
for _, n := range pi.PluginNames() {
p := pi.Get(n)
if !p.IsValid() {
continue
}
for _, a := range p.GetDescriptor().Actions {
h, err := pluginaction.New(p, a.Name)
if err != nil {
logger.Error("cannot create action handler for plugin", "plugin", p.Name(), "handler", a.Name)
} else {
for _, s := range a.DefaultSelectors {
err := ctx.AttributesContext().GetActions().Register(h, handlers.ForAction(a.Name), action.Selector(s))
if err != nil {
logger.LogError(err, "cannot register action handler for plugin", "plugin", p.Name(), "handler", a.Name, "selector", s)
}
}
}
}
for _, a := range p.GetDescriptor().ValueMergeHandlers {
h, err := pluginmerge.New(p, a.Name)
if err != nil {
logger.Error("cannot create value merge handler for plugin", "plugin", p.Name(), "handler", a.Name)
} else {
vmreg.RegisterHandler(h)
}
}
for _, m := range p.GetDescriptor().AccessMethods {
name := m.Name
if m.Version != "" {
name = name + runtime.VersionSeparator + m.Version
}
logger.Info("registering access method",
"plugin", p.Name(),
"type", name)
pi.GetContext().AccessMethods().Register(pluginaccess.NewType(name, p, &m))
}
for _, m := range p.GetDescriptor().ValueSets {
if !slices.Contains(m.Purposes, descriptor.PURPOSE_ROUTINGSLIP) {
continue
}
name := m.Name
if m.Version != "" {
name = name + runtime.VersionSeparator + m.Version
}
logger.Info("registering routing slip entry type",
"plugin", p.Name(),
"type", name)
spi.For(pi.GetContext()).Register(pluginroutingslip.NewType(name, p, &m))
}
if p.IsAutoConfigurationEnabled() {
for _, u := range p.GetDescriptor().Uploaders {
for _, c := range u.Constraints {
if c.ContextType != "" && c.RepositoryType != "" && c.MediaType != "" {
hdlr, err := pluginupload.New(p, u.Name, nil)
if err != nil {
logger.Error("cannot create blob handler for plugin", "plugin", p.Name(), "handler", u.Name)
} else {
logger.Info("registering repository blob handler",
"context", c.ContextType+":"+c.RepositoryType,
"plugin", p.Name(),
"handler", u.Name)
ctx.BlobHandlers().Register(hdlr, cpi.ForRepo(c.ContextType, c.RepositoryType), cpi.ForMimeType(c.MediaType))
}
}
}
}
for _, u := range p.GetDescriptor().Downloaders {
for _, c := range u.AutoRegistration {
if c.ArtifactType != "" || c.MediaType != "" {
hdlr, err := plugindownload.New(p, u.Name, nil)
if err != nil {
logger.Error("cannot create download handler for plugin", "plugin", p.Name(), "handler", u.Name)
} else {
logger.Info("registering download handler",
"context", c.ArtifactType+":"+c.MediaType,
"plugin", p.Name(),
"handler", u.Name,
"priority", c.Priority)
opts := &download.HandlerOptions{
HandlerKey: download.HandlerKey{
ArtifactType: c.ArtifactType,
MimeType: c.MediaType,
},
Priority: c.Priority,
}
download.For(ctx).Register(hdlr, opts)
}
}
}
}
}
for _, s := range p.GetDescriptor().LabelMergeSpecifications {
h := vmreg.GetHandler(s.GetAlgorithm())
if h == nil {
logger.Error("cannot assign label merge spec for plugin", "label", s.GetName(), "algorithm", s.GetAlgorithm(), "plugin", p.Name())
} else {
vmreg.AssignHandler(hpi.LabelHint(s.Name, s.Version), &s.MergeAlgorithmSpecification)
}
}
}
return nil
}