@@ -20,12 +20,15 @@ import (
20
20
"bytes"
21
21
"encoding/json"
22
22
"fmt"
23
+ "io"
24
+ iofs "io/fs"
23
25
"os"
24
26
"os/exec"
25
27
"path/filepath"
26
28
"strconv"
27
29
"strings"
28
30
31
+ "github.com/spf13/afero"
29
32
"github.com/spf13/pflag"
30
33
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
31
34
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -102,8 +105,52 @@ func makePluginRequest(req external.PluginRequest, path string) (*external.Plugi
102
105
return & res , nil
103
106
}
104
107
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
+
105
147
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
+ }
107
154
108
155
res , err := makePluginRequest (req , path )
109
156
if err != nil {
0 commit comments