Skip to content

Commit

Permalink
šŸ› (Kubebuilder External Plugins API): fix populate PluginRequest.Univā€¦
Browse files Browse the repository at this point in the history
ā€¦erse which is empty (#3223)

* fix: populate PluginRequest.Universe

* test: add unit test for getUniverseMap

* refactor: fix linting errors
  • Loading branch information
em-r committed Feb 19, 2023
1 parent 4238c3f commit 8538e78
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
55 changes: 55 additions & 0 deletions pkg/plugins/external/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,61 @@ var _ = Describe("Run external plugin using Scaffold", func() {
checkMetadata()
})
})

Context("Helper functions for Sending request to external plugin and parsing response", func() {
It("getUniverseMap should return path to content mapping of all files in Filesystem", func() {
fs := machinery.Filesystem{
FS: afero.NewMemMapFs(),
}

files := []struct {
path string
name string
content string
}{
{
path: "./",
name: "file",
content: "level 0 file",
},
{
path: "dir/",
name: "file",
content: "level 1 file",
},
{
path: "dir/subdir",
name: "file",
content: "level 2 file",
},
}

// create files in Filesystem
for _, file := range files {
err := fs.FS.MkdirAll(file.path, 0o700)
Expect(err).ToNot(HaveOccurred())

f, err := fs.FS.Create(filepath.Join(file.path, file.name))
Expect(err).ToNot(HaveOccurred())

_, err = f.Write([]byte(file.content))
Expect(err).ToNot(HaveOccurred())

err = f.Close()
Expect(err).ToNot(HaveOccurred())
}

universe, err := getUniverseMap(fs)

Expect(err).ToNot(HaveOccurred())
Expect(len(universe)).To(Equal(len(files)))

for _, file := range files {
content := universe[filepath.Join(file.path, file.name)]
Expect(content).To(Equal(file.content))
}
})
})
})

func getFlags() []external.Flag {
Expand Down
54 changes: 53 additions & 1 deletion pkg/plugins/external/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
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 {
Expand Down

0 comments on commit 8538e78

Please sign in to comment.