Skip to content

Commit 6adce22

Browse files
authored
Merge pull request #3247 from em-r/linux-plugin-discovery-fix
🐛 (API - External Plugins): Fix external plugin discovery on Linux
2 parents 134cc11 + 6ccbc0f commit 6adce22

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

pkg/cli/options.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,44 @@ func parseExternalPluginArgs() (args []string) {
171171
return args
172172
}
173173

174+
// isHostSupported checks whether the host system is supported or not.
175+
func isHostSupported(host string) bool {
176+
for _, platform := range supportedPlatforms {
177+
if host == platform {
178+
return true
179+
}
180+
}
181+
return false
182+
}
183+
174184
// getPluginsRoot detects the host system and gets the plugins root based on the host.
175185
func getPluginsRoot(host string) (pluginsRoot string, err error) {
186+
if !isHostSupported(host) {
187+
// freebsd, openbsd, windows...
188+
return "", fmt.Errorf("host not supported: %v", host)
189+
}
190+
191+
pluginsRelativePath := filepath.Join("kubebuilder", "plugins")
192+
if xdgHome := os.Getenv("XDG_CONFIG_HOME"); xdgHome != "" {
193+
return filepath.Join(xdgHome, pluginsRelativePath), nil
194+
}
195+
176196
switch host {
177197
case "darwin":
178198
logrus.Debugf("Detected host is macOS.")
179-
pluginsRoot = filepath.Join("Library", "Application Support", "kubebuilder", "plugins")
199+
pluginsRoot = filepath.Join("Library", "Application Support", pluginsRelativePath)
180200
case "linux":
181201
logrus.Debugf("Detected host is Linux.")
182-
pluginsRoot = filepath.Join(".config", "kubebuilder", "plugins")
183-
default:
184-
// freebsd, openbsd, windows...
185-
return "", fmt.Errorf("Host not supported: %v", host)
202+
pluginsRoot = filepath.Join(".config", pluginsRelativePath)
186203
}
187-
userHomeDir, err := getHomeDir()
204+
205+
userHomeDir, err := os.UserHomeDir()
188206
if err != nil {
189207
return "", fmt.Errorf("error retrieving home dir: %v", err)
190208
}
191209
pluginsRoot = filepath.Join(userHomeDir, pluginsRoot)
192210

193-
return pluginsRoot, nil
211+
return
194212
}
195213

196214
// DiscoverExternalPlugins discovers the external plugins in the plugins root directory
@@ -286,16 +304,3 @@ func DiscoverExternalPlugins(fs afero.Fs) (ps []plugin.Plugin, err error) {
286304
func isPluginExectuable(mode fs.FileMode) bool {
287305
return mode&0111 != 0
288306
}
289-
290-
// getHomeDir returns $XDG_CONFIG_HOME if set, otherwise $HOME.
291-
func getHomeDir() (string, error) {
292-
var err error
293-
xdgHome := os.Getenv("XDG_CONFIG_HOME")
294-
if xdgHome == "" {
295-
xdgHome, err = os.UserHomeDir()
296-
if err != nil {
297-
return "", err
298-
}
299-
}
300-
return xdgHome, nil
301-
}

pkg/cli/options_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var _ = Describe("Discover external plugins", func() {
228228

229229
_, err = getPluginsRoot("random")
230230
Expect(err).ToNot(BeNil())
231-
Expect(err.Error()).To(ContainSubstring("Host not supported"))
231+
Expect(err.Error()).To(ContainSubstring("host not supported"))
232232
})
233233

234234
It("should skip parsing of directories if plugins root is not a directory", func() {
@@ -249,7 +249,35 @@ var _ = Describe("Discover external plugins", func() {
249249

250250
_, err = getPluginsRoot("random")
251251
Expect(err).ToNot(BeNil())
252-
Expect(err.Error()).To(ContainSubstring("Host not supported"))
252+
Expect(err.Error()).To(ContainSubstring("host not supported"))
253+
})
254+
255+
It("should return full path to the external plugins without XDG_CONFIG_HOME", func() {
256+
if _, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
257+
err = os.Setenv("XDG_CONFIG_HOME", "")
258+
Expect(err).To(BeNil())
259+
}
260+
261+
home := os.Getenv("HOME")
262+
263+
pluginsRoot, err := getPluginsRoot("darwin")
264+
Expect(err).To(BeNil())
265+
expected := filepath.Join(home, "Library", "Application Support", "kubebuilder", "plugins")
266+
Expect(pluginsRoot).To(Equal(expected))
267+
268+
pluginsRoot, err = getPluginsRoot("linux")
269+
Expect(err).To(BeNil())
270+
expected = filepath.Join(home, ".config", "kubebuilder", "plugins")
271+
Expect(pluginsRoot).To(Equal(expected))
272+
})
273+
274+
It("should return full path to the external plugins with XDG_CONFIG_HOME", func() {
275+
err = os.Setenv("XDG_CONFIG_HOME", "/some/random/path")
276+
Expect(err).To(BeNil())
277+
278+
pluginsRoot, err := getPluginsRoot(runtime.GOOS)
279+
Expect(err).To(BeNil())
280+
Expect(pluginsRoot).To(Equal("/some/random/path/kubebuilder/plugins"))
253281
})
254282

255283
It("should return error when home directory is set to empty", func() {

pkg/cli/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
projectVersionsHeader = "Supported project versions"
3030
)
3131

32+
var (
33+
supportedPlatforms = []string{"darwin", "linux"}
34+
)
35+
3236
func (c CLI) newRootCmd() *cobra.Command {
3337
cmd := &cobra.Command{
3438
Use: c.commandName,

0 commit comments

Comments
 (0)