Skip to content

Commit 834761d

Browse files
committed
fix: populate PluginRequest.Universe
1 parent 9c46344 commit 834761d

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

pkg/plugins/external/helpers.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"io"
24+
iofs "io/fs"
2325
"os"
2426
"os/exec"
2527
"path/filepath"
2628
"strconv"
2729
"strings"
2830

31+
"github.com/spf13/afero"
2932
"github.com/spf13/pflag"
3033
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
3134
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -102,8 +105,52 @@ func makePluginRequest(req external.PluginRequest, path string) (*external.Plugi
102105
return &res, nil
103106
}
104107

108+
// getUniverseMap is a helper function that is used to read the current directory to build
109+
// the universe map.
110+
// It will return a map[string]string where the keys are relative paths to files in the directory
111+
// and values are the contents, or an error if an issue occured while reading one of the files.
112+
func getUniverseMap(fs machinery.Filesystem) (map[string]string, error) {
113+
universe := map[string]string{}
114+
115+
err := afero.Walk(fs.FS, ".", func(path string, info iofs.FileInfo, err error) error {
116+
if err != nil {
117+
return err
118+
}
119+
120+
if info.IsDir() {
121+
return nil
122+
}
123+
124+
file, err := fs.FS.Open(path)
125+
if err != nil {
126+
return err
127+
}
128+
defer file.Close()
129+
130+
content, err := io.ReadAll(file)
131+
if err != nil {
132+
return err
133+
}
134+
135+
universe[path] = string(content)
136+
137+
return nil
138+
})
139+
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
return universe, nil
145+
}
146+
105147
func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, path string) error {
106-
req.Universe = map[string]string{}
148+
var err error
149+
150+
req.Universe, err = getUniverseMap(fs)
151+
if err != nil {
152+
return err
153+
}
107154

108155
res, err := makePluginRequest(req, path)
109156
if err != nil {

0 commit comments

Comments
 (0)