Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Added override mechanism for kubeconfig. This allows the full path to… #154

Merged
merged 3 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ You can set these in the environment to override the default values.
`/usr/libexec/kubernetes/kubelet-plugins/volume/exec/oracle~oci`)
* `OCI_FLEXD_CONFIG_DIRECTORY` - Directory where the driver configuration lives (Default:
`/usr/libexec/kubernetes/kubelet-plugins/volume/exec/oracle~oci`)
* `OCI_FLEXD_KUBECONFIG_PATH` - An override to allow the fully qualified path of the kubeconfig resource file to be specified. This take precedence over additional configuration.

## OCI Policies

Expand Down
16 changes: 14 additions & 2 deletions pkg/oci/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func GetConfigPath() string {
return filepath.Join(path, "config.yaml")
}

// GetKubeconfigPath gets the override path of the 'kubeconfig'. This override
// can be uses to explicitly set the name and location of the kubeconfig file
// via the OCI_FLEXD_KUBECONFIG environment variable. If this value is not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OCI_FLEXD_KUBECONFIG => OCI_FLEXD_KUBECONFIG_PATH

// specified then the default GetConfigDirectory mechanism is used.
func GetKubeconfigPath() string {
kcp := os.Getenv("OCI_FLEXD_KUBECONFIG_PATH")
if kcp == "" {
kcp = fmt.Sprintf("%s/%s", strings.TrimRight(GetConfigDirectory(), "/"), "kubeconfig")
}
return kcp
}

// Init checks that we have the appropriate credentials and metadata API access
// on driver initialisation.
func (d OCIFlexvolumeDriver) Init() flexvolume.DriverStatus {
Expand Down Expand Up @@ -137,9 +149,9 @@ func deriveVolumeOCID(regionKey string, volumeName string) string {
}

// constructKubeClient uses a kubeconfig layed down by a secret via deploy.sh to return
// a kube clientset
// a kube clientset.
func constructKubeClient() (*kubernetes.Clientset, error) {
fp := GetConfigDirectory() + "/kubeconfig"
fp := GetKubeconfigPath()

c, err := clientcmd.BuildConfigFromFlags("", fp)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions pkg/oci/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,49 @@ func TestGetConfigPath(t *testing.T) {

}
}

func TestGetKubeconfigPath(t *testing.T) {
testCases := map[string]struct {
envvar string
value string
expected string
}{
"default": {
envvar: "",
value: "",
expected: "/usr/libexec/kubernetes/kubelet-plugins/volume/exec/oracle~oci/kubeconfig",
},
"custom config dir": {
envvar: "OCI_FLEXD_CONFIG_DIRECTORY",
value: "/foo/bar",
expected: "/foo/bar/kubeconfig",
},
"custom config dir with trailing path seperator": {
envvar: "OCI_FLEXD_CONFIG_DIRECTORY",
value: "/foo/bar/",
expected: "/foo/bar/kubeconfig",
},
"override kubeconfig path": {
envvar: "OCI_FLEXD_KUBECONFIG_PATH",
value: "/etc/kubevar/kubeconfig",
expected: "/etc/kubevar/kubeconfig",
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
// idk if we need this but figure it can't hurt
original := os.Getenv(tc.envvar)
defer os.Setenv(tc.envvar, original)

// set env var value for the test.
os.Setenv(tc.envvar, tc.value)

result := GetKubeconfigPath()
if result != tc.expected {
t.Errorf("GetKubeconfigPath() = %q ; wanted %q", result, tc.expected)
}
})

}
}