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

Commit 14700ca

Browse files
templecloudowainlewis
authored andcommitted
Added override mechanism for kubeconfig location. (#154)
Allow the location of the kubeconfig file needed by the driver to be configured with an environment variable.
1 parent f6ad85b commit 14700ca

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ You can set these in the environment to override the default values.
111111
`/usr/libexec/kubernetes/kubelet-plugins/volume/exec/oracle~oci`)
112112
* `OCI_FLEXD_CONFIG_DIRECTORY` - Directory where the driver configuration lives (Default:
113113
`/usr/libexec/kubernetes/kubelet-plugins/volume/exec/oracle~oci`)
114+
* `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.
114115

115116
## OCI Policies
116117

pkg/oci/driver/driver.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ func GetConfigPath() string {
9696
return filepath.Join(path, "config.yaml")
9797
}
9898

99+
// GetKubeconfigPath gets the override path of the 'kubeconfig'. This override
100+
// can be uses to explicitly set the name and location of the kubeconfig file
101+
// via the OCI_FLEXD_KUBECONFIG_PATH environment variable. If this value is not
102+
// specified then the default GetConfigDirectory mechanism is used.
103+
func GetKubeconfigPath() string {
104+
kcp := os.Getenv("OCI_FLEXD_KUBECONFIG_PATH")
105+
if kcp == "" {
106+
kcp = fmt.Sprintf("%s/%s", strings.TrimRight(GetConfigDirectory(), "/"), "kubeconfig")
107+
}
108+
return kcp
109+
}
110+
99111
// Init checks that we have the appropriate credentials and metadata API access
100112
// on driver initialisation.
101113
func (d OCIFlexvolumeDriver) Init() flexvolume.DriverStatus {
@@ -137,9 +149,9 @@ func deriveVolumeOCID(regionKey string, volumeName string) string {
137149
}
138150

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

144156
c, err := clientcmd.BuildConfigFromFlags("", fp)
145157
if err != nil {

pkg/oci/driver/driver_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,49 @@ func TestGetConfigPath(t *testing.T) {
7878

7979
}
8080
}
81+
82+
func TestGetKubeconfigPath(t *testing.T) {
83+
testCases := map[string]struct {
84+
envvar string
85+
value string
86+
expected string
87+
}{
88+
"default": {
89+
envvar: "",
90+
value: "",
91+
expected: "/usr/libexec/kubernetes/kubelet-plugins/volume/exec/oracle~oci/kubeconfig",
92+
},
93+
"custom config dir": {
94+
envvar: "OCI_FLEXD_CONFIG_DIRECTORY",
95+
value: "/foo/bar",
96+
expected: "/foo/bar/kubeconfig",
97+
},
98+
"custom config dir with trailing path seperator": {
99+
envvar: "OCI_FLEXD_CONFIG_DIRECTORY",
100+
value: "/foo/bar/",
101+
expected: "/foo/bar/kubeconfig",
102+
},
103+
"override kubeconfig path": {
104+
envvar: "OCI_FLEXD_KUBECONFIG_PATH",
105+
value: "/etc/kubevar/kubeconfig",
106+
expected: "/etc/kubevar/kubeconfig",
107+
},
108+
}
109+
110+
for name, tc := range testCases {
111+
t.Run(name, func(t *testing.T) {
112+
// idk if we need this but figure it can't hurt
113+
original := os.Getenv(tc.envvar)
114+
defer os.Setenv(tc.envvar, original)
115+
116+
// set env var value for the test.
117+
os.Setenv(tc.envvar, tc.value)
118+
119+
result := GetKubeconfigPath()
120+
if result != tc.expected {
121+
t.Errorf("GetKubeconfigPath() = %q ; wanted %q", result, tc.expected)
122+
}
123+
})
124+
125+
}
126+
}

0 commit comments

Comments
 (0)