Skip to content

Commit

Permalink
feat: make external plugin path configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Jun 2, 2023
1 parent 40ec611 commit 06be130
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
10 changes: 7 additions & 3 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,18 @@ func isHostSupported(host string) bool {
return false
}

// getPluginsRoot detects the host system and gets the plugins root based on the host.
// getPluginsRoot gets the plugin root path.
func getPluginsRoot(host string) (pluginsRoot string, err error) {
if !isHostSupported(host) {
// freebsd, openbsd, windows...
return "", fmt.Errorf("host not supported: %v", host)
}

// if user provides specific path, return
if pluginsPath := os.Getenv("KB_PLUGINS_PATH"); pluginsPath != "" {
return pluginsPath, nil
}
// if no specific path, detects the host system and gets the plugins root based on the host.
pluginsRelativePath := filepath.Join("kubebuilder", "plugins")
if xdgHome := os.Getenv("XDG_CONFIG_HOME"); xdgHome != "" {
return filepath.Join(xdgHome, pluginsRelativePath), nil
Expand All @@ -206,9 +211,8 @@ func getPluginsRoot(host string) (pluginsRoot string, err error) {
if err != nil {
return "", fmt.Errorf("error retrieving home dir: %v", err)
}
pluginsRoot = filepath.Join(userHomeDir, pluginsRoot)

return
return filepath.Join(userHomeDir, pluginsRoot), nil
}

// DiscoverExternalPlugins discovers the external plugins in the plugins root directory
Expand Down
84 changes: 60 additions & 24 deletions pkg/cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cli

import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
Expand All @@ -34,6 +35,65 @@ import (
)

var _ = Describe("Discover external plugins", func() {
Context("validate plugins root path", func() {
var (
homePath string = os.Getenv("HOME")
customPath string = "/usr/local/myplugins"
xdghome string = os.Getenv("XDG_CONFIG_HOME")
)

It("based on the host os", func() {
err := os.Unsetenv("KB_PLUGINS_PATH")
Expect(err).To(BeNil())

plgPath, err := getPluginsRoot("darwin")
Expect(err).To(BeNil())
if xdghome != "" {
Expect(plgPath).To(Equal(fmt.Sprintf("%s/kubebuilder/plugins", xdghome)))
AddReportEntry(fmt.Sprintf("Detect XDG_CONFIG_HOME: %s, generated plugin path: %s", xdghome, plgPath), ReportEntryVisibilityAlways)
} else {
Expect(plgPath).To(Equal(fmt.Sprintf("%s/Library/Application Support/kubebuilder/plugins", homePath)))
}

plgPath, err = getPluginsRoot("linux")
Expect(err).To(BeNil())
if xdghome != "" {
Expect(plgPath).To(Equal(fmt.Sprintf("%s/kubebuilder/plugins", xdghome)))
AddReportEntry(fmt.Sprintf("Detect XDG_CONFIG_HOME: %s, generated plugin path: %s", xdghome, plgPath), ReportEntryVisibilityAlways)
} else {
Expect(plgPath).To(Equal(fmt.Sprintf("%s/.config/kubebuilder/plugins", homePath)))
}

plgPath, err = getPluginsRoot("random")
Expect(plgPath).To(Equal(""))
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("host not supported"))
})

It("based on the user given path", func() {
err := os.Setenv("KB_PLUGINS_PATH", customPath)
Expect(err).To(BeNil())

plgPath, err := getPluginsRoot("darwin")
Expect(plgPath).To(Equal(customPath))
Expect(err).To(BeNil())

plgPath, err = getPluginsRoot("linux")
Expect(plgPath).To(Equal(customPath))
Expect(err).To(BeNil())

plgPath, err = getPluginsRoot("random")
Expect(plgPath).To(Equal(""))
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("host not supported"))
})

AfterEach(func() {
err := os.Unsetenv("KB_PLUGINS_PATH")
Expect(err).To(BeNil())
})
})

Context("when plugin executables exist in the expected plugin directories", func() {
const (
filePermissions os.FileMode = 755
Expand Down Expand Up @@ -219,18 +279,6 @@ var _ = Describe("Discover external plugins", func() {
Expect(err).To(Equal(errPluginsRoot))
})

It("should fail for any other host that is not supported", func() {
_, err := getPluginsRoot("darwin")
Expect(err).To(BeNil())

_, err = getPluginsRoot("linux")
Expect(err).To(BeNil())

_, err = getPluginsRoot("random")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("host not supported"))
})

It("should skip parsing of directories if plugins root is not a directory", func() {
retrievePluginsRoot = func(host string) (string, error) {
return "externalplugin.sh", nil
Expand All @@ -240,18 +288,6 @@ var _ = Describe("Discover external plugins", func() {
Expect(err).To(BeNil())
})

It("should fail for any other host that is not supported", func() {
_, err := getPluginsRoot("darwin")
Expect(err).To(BeNil())

_, err = getPluginsRoot("linux")
Expect(err).To(BeNil())

_, err = getPluginsRoot("random")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("host not supported"))
})

It("should return full path to the external plugins without XDG_CONFIG_HOME", func() {
if _, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
err = os.Setenv("XDG_CONFIG_HOME", "")
Expand Down

0 comments on commit 06be130

Please sign in to comment.