@@ -21,7 +21,7 @@ import (
21
21
"fmt"
22
22
"log/slog"
23
23
"os"
24
- "os/exec "
24
+ "path/filepath "
25
25
"strings"
26
26
27
27
"sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/common"
@@ -144,14 +144,31 @@ func (opts *Generate) Validate() error {
144
144
return fmt .Errorf ("error getting input path %q: %w" , opts .InputDir , err )
145
145
}
146
146
147
- _ , err = exec . LookPath ( "kubebuilder" )
147
+ _ , err = getExecutablePath ( )
148
148
if err != nil {
149
- return fmt . Errorf ( "kubebuilder not found in the path: %w" , err )
149
+ return err
150
150
}
151
151
152
152
return nil
153
153
}
154
154
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
+
155
172
// Helper function to create the output directory.
156
173
func createDirectory (outputDir string ) error {
157
174
if err := os .MkdirAll (outputDir , 0o755 ); err != nil {
@@ -171,7 +188,11 @@ func changeWorkingDirectory(outputDir string) error {
171
188
// Initializes the project with Kubebuilder.
172
189
func kubebuilderInit (s store.Store ) error {
173
190
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 {
175
196
return fmt .Errorf ("failed to run kubebuilder init command: %w" , err )
176
197
}
177
198
0 commit comments