-
Notifications
You must be signed in to change notification settings - Fork 1.6k
🐛 (Kubebuilder External Plugins API): fix populate PluginRequest.Universe which is empty #3223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,12 +20,15 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
iofs "io/fs" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/pflag" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/machinery" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/plugin" | ||
|
@@ -102,8 +105,57 @@ func makePluginRequest(req external.PluginRequest, path string) (*external.Plugi | |
return &res, nil | ||
} | ||
|
||
// getUniverseMap is a helper function that is used to read the current directory to build | ||
// the universe map. | ||
// It will return a map[string]string where the keys are relative paths to files in the directory | ||
// and values are the contents, or an error if an issue occurred while reading one of the files. | ||
func getUniverseMap(fs machinery.Filesystem) (map[string]string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @em-r : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @camilamacedo86 - the way I see this is that when passing the path of files, we preserve the universe's file system structure, this way, the external plugin will have access to all scaffolded files the same way it would if we were to scan the root directory of the project from the external plugin. Example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the explanation. |
||
universe := map[string]string{} | ||
|
||
err := afero.Walk(fs.FS, ".", func(path string, info iofs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
file, err := fs.FS.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if err := file.Close(); err != nil { | ||
return | ||
} | ||
}() | ||
|
||
content, err := io.ReadAll(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
universe[path] = string(content) | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return universe, nil | ||
} | ||
|
||
func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, path string) error { | ||
req.Universe = map[string]string{} | ||
var err error | ||
|
||
req.Universe, err = getUniverseMap(fs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := makePluginRequest(req, path) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be possible to add a unit test for this implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes sure, I'll add a unit test for this, thank you for the feedback!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@camilamacedo86 I've added a unit test in fd9cfa, let me know if it needs to be adjusted or to add more test cases.
thank you!