-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathbundle.go
124 lines (100 loc) · 3.43 KB
/
bundle.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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
"fmt"
"sigs.k8s.io/kubebuilder/v4/pkg/config"
)
type bundle struct {
name string
version Version
plugins []Plugin
supportedProjectVersions []config.Version
deprecateWarning string
}
// BundleOption define the options to create the bundle
type BundleOption func(*bundle)
// WithName allow set the name of the Bundle Plugin
func WithName(name string) BundleOption {
return func(opts *bundle) {
opts.name = name
}
}
// WithVersion allow set the version of the Bundle Plugin
func WithVersion(version Version) BundleOption {
return func(opts *bundle) {
opts.version = version
}
}
// WithPlugins allow set the plugins which will be used in the composition for the Bundle Plugin
func WithPlugins(plugins ...Plugin) BundleOption {
return func(opts *bundle) {
opts.plugins = plugins
}
}
// WithDeprecationMessage allow set a deprecate message when needed
func WithDeprecationMessage(msg string) BundleOption {
return func(opts *bundle) {
opts.deprecateWarning = msg
}
}
// NewBundleWithOptions creates a new Bundle with the provided BundleOptions.
// The list of supported project versions is computed from the provided plugins in options.
func NewBundleWithOptions(opts ...BundleOption) (Bundle, error) {
bundleOpts := bundle{}
for _, opts := range opts {
opts(&bundleOpts)
}
supportedProjectVersions := CommonSupportedProjectVersions(bundleOpts.plugins...)
if len(supportedProjectVersions) == 0 {
return nil, fmt.Errorf("in order to bundle plugins, they must all support at least one common project version")
}
// Plugins may be bundles themselves, so unbundle here
// NOTE(Adirio): unbundling here ensures that Bundle.Plugin always returns a flat list of Plugins instead of also
// including Bundles, and therefore we don't have to use a recursive algorithm when resolving.
allPlugins := make([]Plugin, 0, len(bundleOpts.plugins))
for _, plugin := range bundleOpts.plugins {
if pluginBundle, isBundle := plugin.(Bundle); isBundle {
allPlugins = append(allPlugins, pluginBundle.Plugins()...)
} else {
allPlugins = append(allPlugins, plugin)
}
}
return bundle{
name: bundleOpts.name,
version: bundleOpts.version,
plugins: allPlugins,
supportedProjectVersions: supportedProjectVersions,
deprecateWarning: bundleOpts.deprecateWarning,
}, nil
}
// Name implements Plugin
func (b bundle) Name() string {
return b.name
}
// Version implements Plugin
func (b bundle) Version() Version {
return b.version
}
// SupportedProjectVersions implements Plugin
func (b bundle) SupportedProjectVersions() []config.Version {
return b.supportedProjectVersions
}
// Plugins implements Bundle
func (b bundle) Plugins() []Plugin {
return b.plugins
}
// DeprecationWarning return the warning message
func (b bundle) DeprecationWarning() string {
return b.deprecateWarning
}