@@ -30,6 +30,7 @@ import (
3030 "sigs.k8s.io/kubebuilder/v4/pkg/config"
3131 cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
3232 "sigs.k8s.io/kubebuilder/v4/pkg/machinery"
33+ "sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
3334 "sigs.k8s.io/kubebuilder/v4/pkg/model/stage"
3435 "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3536 golangv4 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4"
@@ -89,6 +90,56 @@ func (s *pluginChainCapturingSubcommand) SetPluginChain(chain []string) {
8990 s .pluginChain = append ([]string (nil ), chain ... )
9091}
9192
93+ type testCreateAPIPlugin struct {
94+ name string
95+ version plugin.Version
96+ subcommand * testCreateAPISubcommand
97+ projectVers []config.Version
98+ }
99+
100+ func newTestCreateAPIPlugin (name string , version plugin.Version ) testCreateAPIPlugin {
101+ return testCreateAPIPlugin {
102+ name : name ,
103+ version : version ,
104+ subcommand : & testCreateAPISubcommand {},
105+ projectVers : []config.Version {{Number : 3 }},
106+ }
107+ }
108+
109+ func (p testCreateAPIPlugin ) Name () string { return p .name }
110+ func (p testCreateAPIPlugin ) Version () plugin.Version { return p .version }
111+ func (p testCreateAPIPlugin ) SupportedProjectVersions () []config.Version { return p .projectVers }
112+ func (p testCreateAPIPlugin ) GetCreateAPISubcommand () plugin.CreateAPISubcommand {
113+ return p .subcommand
114+ }
115+
116+ type testCreateAPISubcommand struct {}
117+
118+ func (s * testCreateAPISubcommand ) InjectResource (* resource.Resource ) error {
119+ return nil
120+ }
121+
122+ func (s * testCreateAPISubcommand ) Scaffold (machinery.Filesystem ) error {
123+ return nil
124+ }
125+
126+ type fakeStore struct {
127+ cfg config.Config
128+ }
129+
130+ func (f * fakeStore ) New (config.Version ) error { return nil }
131+ func (f * fakeStore ) Load () error { return nil }
132+ func (f * fakeStore ) LoadFrom (string ) error { return nil }
133+ func (f * fakeStore ) Save () error { return nil }
134+ func (f * fakeStore ) SaveTo (string ) error { return nil }
135+ func (f * fakeStore ) Config () config.Config { return f .cfg }
136+
137+ type captureSubcommand struct {
138+ lastChain []string
139+ }
140+
141+ func (c * captureSubcommand ) Scaffold (machinery.Filesystem ) error { return nil }
142+
92143var _ = Describe ("CLI" , func () {
93144 var (
94145 c * CLI
@@ -103,6 +154,83 @@ var _ = Describe("CLI", func() {
103154 projectVersion = config.Version {Number : 3 }
104155 })
105156
157+ Describe ("filterSubcommands" , func () {
158+ It ("propagates bundle keys to wrapped subcommands" , func () {
159+ bundleVersion := plugin.Version {Number : 1 , Stage : stage .Alpha }
160+
161+ fooPlugin := newTestCreateAPIPlugin ("deploy-image.go.kubebuilder.io" , plugin.Version {Number : 1 , Stage : stage .Alpha })
162+ barPlugin := newTestCreateAPIPlugin ("deploy-image.go.kubebuilder.io" , plugin.Version {Number : 1 , Stage : stage .Alpha })
163+
164+ fooBundle , err := plugin .NewBundleWithOptions (
165+ plugin .WithName ("deploy-image.foo.example.com" ),
166+ plugin .WithVersion (bundleVersion ),
167+ plugin .WithPlugins (fooPlugin ),
168+ )
169+ Expect (err ).NotTo (HaveOccurred ())
170+
171+ barBundle , err := plugin .NewBundleWithOptions (
172+ plugin .WithName ("deploy-image.bar.example.com" ),
173+ plugin .WithVersion (bundleVersion ),
174+ plugin .WithPlugins (barPlugin ),
175+ )
176+ Expect (err ).NotTo (HaveOccurred ())
177+
178+ c .resolvedPlugins = []plugin.Plugin {fooBundle , barBundle }
179+
180+ tuples := c .filterSubcommands (
181+ func (p plugin.Plugin ) bool {
182+ _ , isCreateAPI := p .(plugin.CreateAPI )
183+ return isCreateAPI
184+ },
185+ func (p plugin.Plugin ) plugin.Subcommand {
186+ return p .(plugin.CreateAPI ).GetCreateAPISubcommand ()
187+ },
188+ )
189+
190+ Expect (tuples ).To (HaveLen (2 ))
191+ Expect (tuples [0 ].key ).To (Equal ("deploy-image.go.kubebuilder.io/v1-alpha" ))
192+ Expect (tuples [0 ].configKey ).To (Equal ("deploy-image.foo.example.com/v1-alpha" ))
193+ Expect (tuples [1 ].key ).To (Equal ("deploy-image.go.kubebuilder.io/v1-alpha" ))
194+ Expect (tuples [1 ].configKey ).To (Equal ("deploy-image.bar.example.com/v1-alpha" ))
195+ })
196+ })
197+
198+ Describe ("executionHooksFactory" , func () {
199+ It ("temporarily reorders the plugin chain while invoking bundled subcommands" , func () {
200+ cfg := cfgv3 .New ()
201+ Expect (cfg .SetPluginChain ([]string {
202+ "deploy-image.foo.example.com/v1-alpha" ,
203+ "deploy-image.bar.example.com/v1-alpha" ,
204+ })).To (Succeed ())
205+
206+ store := & fakeStore {cfg : cfg }
207+ first := & captureSubcommand {}
208+ second := & captureSubcommand {}
209+
210+ factory := executionHooksFactory {
211+ store : store ,
212+ subcommands : []keySubcommandTuple {
213+ {configKey : "deploy-image.foo.example.com/v1-alpha" , subcommand : first },
214+ {configKey : "deploy-image.bar.example.com/v1-alpha" , subcommand : second },
215+ },
216+ errorMessage : "test" ,
217+ }
218+
219+ callErr := factory .forEach (func (sub plugin.Subcommand ) error {
220+ cs := sub .(* captureSubcommand )
221+ cs .lastChain = append ([]string (nil ), store .Config ().GetPluginChain ()... )
222+ return nil
223+ }, "scaffold" )
224+ Expect (callErr ).NotTo (HaveOccurred ())
225+ Expect (first .lastChain [0 ]).To (Equal ("deploy-image.foo.example.com/v1-alpha" ))
226+ Expect (second .lastChain [0 ]).To (Equal ("deploy-image.bar.example.com/v1-alpha" ))
227+ Expect (store .Config ().GetPluginChain ()).To (Equal ([]string {
228+ "deploy-image.foo.example.com/v1-alpha" ,
229+ "deploy-image.bar.example.com/v1-alpha" ,
230+ }))
231+ })
232+ })
233+
106234 Context ("buildCmd" , func () {
107235 var projectFile string
108236
0 commit comments