@@ -21,7 +21,7 @@ import (
2121 "fmt"
2222 "log/slog"
2323 "os"
24- "os/exec "
24+ "path/filepath "
2525 "strings"
2626
2727 "sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/common"
@@ -144,14 +144,31 @@ func (opts *Generate) Validate() error {
144144 return fmt .Errorf ("error getting input path %q: %w" , opts .InputDir , err )
145145 }
146146
147- _ , err = exec . LookPath ( "kubebuilder" )
147+ _ , err = getExecutablePath ( )
148148 if err != nil {
149- return fmt . Errorf ( "kubebuilder not found in the path: %w" , err )
149+ return err
150150 }
151151
152152 return nil
153153}
154154
155+ // Helper function to get the PATH of binary.
156+ func getExecutablePath () (string , error ) {
157+ execPath , err := os .Executable ()
158+ if err != nil {
159+ return "" , fmt .Errorf ("kubebuilder executable not found: %w" , err )
160+ }
161+
162+ realPath , err := filepath .EvalSymlinks (execPath )
163+ if err != nil {
164+ slog .Warn ("Unable to resolve symbolic link" , "execPath" , execPath , "error" , err )
165+ // Fallback to execPath
166+ return execPath , nil
167+ }
168+
169+ return realPath , nil
170+ }
171+
155172// Helper function to create the output directory.
156173func createDirectory (outputDir string ) error {
157174 if err := os .MkdirAll (outputDir , 0o755 ); err != nil {
@@ -171,7 +188,11 @@ func changeWorkingDirectory(outputDir string) error {
171188// Initializes the project with Kubebuilder.
172189func kubebuilderInit (s store.Store ) error {
173190 args := append ([]string {"init" }, getInitArgs (s )... )
174- if err := util .RunCmd ("kubebuilder init" , "kubebuilder" , args ... ); err != nil {
191+ execPath , err := getExecutablePath ()
192+ if err != nil {
193+ return err
194+ }
195+ if err := util .RunCmd ("kubebuilder init" , execPath , args ... ); err != nil {
175196 return fmt .Errorf ("failed to run kubebuilder init command: %w" , err )
176197 }
177198
0 commit comments