@@ -28,6 +28,7 @@ import (
2828 "github.com/spf13/afero"
2929 "github.com/spf13/pflag"
3030
31+ "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
3132 "sigs.k8s.io/kubebuilder/v4/pkg/machinery"
3233 "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3334 "sigs.k8s.io/kubebuilder/v4/pkg/plugin/external"
@@ -75,6 +76,28 @@ func (m *mockInValidOsWdGetter) GetCurrentDir() (string, error) {
7576 return "" , fmt .Errorf ("error getting current directory" )
7677}
7778
79+ // mockConfigOutputGetter captures the request to verify config is passed
80+ type mockConfigOutputGetter struct {
81+ capturedRequest * external.PluginRequest
82+ }
83+
84+ var _ ExecOutputGetter = & mockConfigOutputGetter {}
85+
86+ func (m * mockConfigOutputGetter ) GetExecOutput (reqBytes []byte , _ string ) ([]byte , error ) {
87+ // Capture the request for verification
88+ m .capturedRequest = & external.PluginRequest {}
89+ if err := json .Unmarshal (reqBytes , m .capturedRequest ); err != nil {
90+ return nil , fmt .Errorf ("error unmarshalling request: %w" , err )
91+ }
92+
93+ return []byte (`{
94+ "command": "init",
95+ "error": false,
96+ "error_msg": "none",
97+ "universe": {"LICENSE": "Apache 2.0 License\n"}
98+ }` ), nil
99+ }
100+
78101type mockValidFlagOutputGetter struct {}
79102
80103func (m * mockValidFlagOutputGetter ) GetExecOutput (_ []byte , _ string ) ([]byte , error ) {
@@ -754,6 +777,133 @@ var _ = Describe("Run external plugin using Scaffold", func() {
754777 }
755778 })
756779 })
780+
781+ Context ("with config injection" , func () {
782+ const filePerm os.FileMode = 755
783+ var (
784+ pluginFileName string
785+ args []string
786+ f afero.File
787+ fs machinery.Filesystem
788+ mockGetter * mockConfigOutputGetter
789+ cfg * v3.Cfg
790+
791+ err error
792+ )
793+
794+ BeforeEach (func () {
795+ mockGetter = & mockConfigOutputGetter {}
796+ outputGetter = mockGetter
797+ currentDirGetter = & mockValidOsWdGetter {}
798+ fs = machinery.Filesystem {
799+ FS : afero .NewMemMapFs (),
800+ }
801+
802+ pluginFileName = "externalPlugin.sh"
803+ pluginFilePath := filepath .Join ("tmp" , "externalPlugin" , pluginFileName )
804+
805+ err = fs .FS .MkdirAll (filepath .Dir (pluginFilePath ), filePerm )
806+ Expect (err ).ToNot (HaveOccurred ())
807+
808+ f , err = fs .FS .Create (pluginFilePath )
809+ Expect (err ).ToNot (HaveOccurred ())
810+ Expect (f ).ToNot (BeNil ())
811+
812+ _ , err = fs .FS .Stat (pluginFilePath )
813+ Expect (err ).ToNot (HaveOccurred ())
814+
815+ args = []string {"--domain" , "example.com" }
816+
817+ // Create a config instance
818+ cfg = & v3.Cfg {
819+ Version : v3 .Version ,
820+ Domain : "test.domain" ,
821+ Repository : "github.com/test/repo" ,
822+ Name : "test-project" ,
823+ }
824+ })
825+
826+ It ("should pass config to external plugin on init subcommand" , func () {
827+ i := initSubcommand {
828+ Path : pluginFileName ,
829+ Args : args ,
830+ config : cfg ,
831+ }
832+
833+ err = i .Scaffold (fs )
834+ Expect (err ).ToNot (HaveOccurred ())
835+
836+ // Verify that config was captured in the request
837+ Expect (mockGetter .capturedRequest ).ToNot (BeNil ())
838+ Expect (mockGetter .capturedRequest .Config ).ToNot (BeNil ())
839+ Expect (mockGetter .capturedRequest .Config ["domain" ]).To (Equal ("test.domain" ))
840+ Expect (mockGetter .capturedRequest .Config ["repo" ]).To (Equal ("github.com/test/repo" ))
841+ Expect (mockGetter .capturedRequest .Config ["projectName" ]).To (Equal ("test-project" ))
842+ })
843+
844+ It ("should pass config to external plugin on create api subcommand" , func () {
845+ c := createAPISubcommand {
846+ Path : pluginFileName ,
847+ Args : args ,
848+ config : cfg ,
849+ }
850+
851+ err = c .Scaffold (fs )
852+ Expect (err ).ToNot (HaveOccurred ())
853+
854+ // Verify that config was captured in the request
855+ Expect (mockGetter .capturedRequest ).ToNot (BeNil ())
856+ Expect (mockGetter .capturedRequest .Config ).ToNot (BeNil ())
857+ Expect (mockGetter .capturedRequest .Config ["domain" ]).To (Equal ("test.domain" ))
858+ })
859+
860+ It ("should pass config to external plugin on create webhook subcommand" , func () {
861+ c := createWebhookSubcommand {
862+ Path : pluginFileName ,
863+ Args : args ,
864+ config : cfg ,
865+ }
866+
867+ err = c .Scaffold (fs )
868+ Expect (err ).ToNot (HaveOccurred ())
869+
870+ // Verify that config was captured in the request
871+ Expect (mockGetter .capturedRequest ).ToNot (BeNil ())
872+ Expect (mockGetter .capturedRequest .Config ).ToNot (BeNil ())
873+ Expect (mockGetter .capturedRequest .Config ["domain" ]).To (Equal ("test.domain" ))
874+ })
875+
876+ It ("should pass config to external plugin on edit subcommand" , func () {
877+ e := editSubcommand {
878+ Path : pluginFileName ,
879+ Args : args ,
880+ config : cfg ,
881+ }
882+
883+ err = e .Scaffold (fs )
884+ Expect (err ).ToNot (HaveOccurred ())
885+
886+ // Verify that config was captured in the request
887+ Expect (mockGetter .capturedRequest ).ToNot (BeNil ())
888+ Expect (mockGetter .capturedRequest .Config ).ToNot (BeNil ())
889+ Expect (mockGetter .capturedRequest .Config ["domain" ]).To (Equal ("test.domain" ))
890+ })
891+
892+ It ("should handle nil config gracefully" , func () {
893+ i := initSubcommand {
894+ Path : pluginFileName ,
895+ Args : args ,
896+ config : nil ,
897+ }
898+
899+ err = i .Scaffold (fs )
900+ Expect (err ).ToNot (HaveOccurred ())
901+
902+ // Verify that request was made but config is nil
903+ Expect (mockGetter .capturedRequest ).ToNot (BeNil ())
904+ Expect (mockGetter .capturedRequest .Config ).To (BeNil ())
905+ })
906+ })
757907})
758908
759909func getFlags () []external.Flag {
0 commit comments