|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + "k8s.io/client-go/tools/clientcmd" |
| 12 | +) |
| 13 | + |
| 14 | +type testCase struct { |
| 15 | + text string |
| 16 | + apiServerURL string |
| 17 | + context string |
| 18 | + kubeconfigFlag string |
| 19 | + kubeconfigEnv []string |
| 20 | + wantHost string |
| 21 | +} |
| 22 | + |
| 23 | +var _ = Describe("Config", func() { |
| 24 | + |
| 25 | + var dir string |
| 26 | + |
| 27 | + kubeconfigFiles := map[string]string{ |
| 28 | + "kubeconfig-flag": genKubeconfig("from-flag"), |
| 29 | + "kubeconfig-multi-context": genKubeconfig("ctx-1", "ctx-2"), |
| 30 | + "kubeconfig-env-1": genKubeconfig("from-env-1"), |
| 31 | + "kubeconfig-env-2": genKubeconfig("from-env-2"), |
| 32 | + ".kubeconfig": genKubeconfig("from-home"), |
| 33 | + } |
| 34 | + |
| 35 | + BeforeEach(func(done Done) { |
| 36 | + os.Unsetenv(clientcmd.RecommendedConfigPathEnvVar) |
| 37 | + kubeconfig = "" |
| 38 | + apiServerURL = "" |
| 39 | + |
| 40 | + // create temporary directory for test case |
| 41 | + var err error |
| 42 | + dir, err = ioutil.TempDir("", "cr-test") |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + |
| 45 | + // override $HOME/.kube/config |
| 46 | + clientcmd.RecommendedHomeFile = filepath.Join(dir, ".kubeconfig") |
| 47 | + |
| 48 | + close(done) |
| 49 | + }) |
| 50 | + |
| 51 | + AfterEach(func(done Done) { |
| 52 | + err := os.RemoveAll(dir) |
| 53 | + Expect(err).NotTo(HaveOccurred()) |
| 54 | + |
| 55 | + close(done) |
| 56 | + }) |
| 57 | + |
| 58 | + Describe("GetConfigWithContext", func() { |
| 59 | + Context("when kubeconfig files don't exist", func() { |
| 60 | + It("should fail", func(done Done) { |
| 61 | + cfg, err := GetConfigWithContext("") |
| 62 | + Expect(cfg).To(BeNil()) |
| 63 | + Expect(err).To(HaveOccurred()) |
| 64 | + |
| 65 | + close(done) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + Context("when kubeconfig files exist", func() { |
| 70 | + BeforeEach(func(done Done) { |
| 71 | + err := createFiles(kubeconfigFiles, dir) |
| 72 | + Expect(err).NotTo(HaveOccurred()) |
| 73 | + |
| 74 | + close(done) |
| 75 | + }) |
| 76 | + testCases := []testCase{ |
| 77 | + { |
| 78 | + text: "should use the --kubeconfig flag", |
| 79 | + kubeconfigFlag: "kubeconfig-flag", |
| 80 | + wantHost: "from-flag", |
| 81 | + }, |
| 82 | + { |
| 83 | + text: "should use the envvar", |
| 84 | + kubeconfigEnv: []string{"kubeconfig-multi-context"}, |
| 85 | + wantHost: "ctx-1", |
| 86 | + }, |
| 87 | + { |
| 88 | + text: "should use the recommended home file", |
| 89 | + wantHost: "from-home", |
| 90 | + }, |
| 91 | + { |
| 92 | + text: "should prefer the flag over the envvar", |
| 93 | + kubeconfigFlag: "kubeconfig-flag", |
| 94 | + kubeconfigEnv: []string{"kubeconfig-multi-context"}, |
| 95 | + wantHost: "from-flag", |
| 96 | + }, |
| 97 | + { |
| 98 | + text: "should prefer the envar over the recommended home file", |
| 99 | + kubeconfigEnv: []string{"kubeconfig-multi-context"}, |
| 100 | + wantHost: "ctx-1", |
| 101 | + }, |
| 102 | + { |
| 103 | + text: "should allow overriding the API server URL", |
| 104 | + apiServerURL: "override", |
| 105 | + kubeconfigEnv: []string{"kubeconfig-multi-context"}, |
| 106 | + wantHost: "override", |
| 107 | + }, |
| 108 | + { |
| 109 | + text: "should allow overriding the context", |
| 110 | + context: "ctx-2", |
| 111 | + kubeconfigEnv: []string{"kubeconfig-multi-context"}, |
| 112 | + wantHost: "ctx-2", |
| 113 | + }, |
| 114 | + { |
| 115 | + text: "should support a multi-value envvar", |
| 116 | + context: "from-env-2", |
| 117 | + kubeconfigEnv: []string{"kubeconfig-env-1", "kubeconfig-env-2"}, |
| 118 | + wantHost: "from-env-2", |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + for _, testCase := range testCases { |
| 123 | + tc := testCase |
| 124 | + It(tc.text, func(done Done) { |
| 125 | + // set global and environment configs |
| 126 | + setConfigs(tc, dir) |
| 127 | + |
| 128 | + // run the test |
| 129 | + cfg, err := GetConfigWithContext(tc.context) |
| 130 | + Expect(err).NotTo(HaveOccurred()) |
| 131 | + Expect(cfg.Host).To(Equal(tc.wantHost)) |
| 132 | + |
| 133 | + close(done) |
| 134 | + }) |
| 135 | + } |
| 136 | + }) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +func setConfigs(tc testCase, dir string) { |
| 141 | + // Set API Server URL |
| 142 | + apiServerURL = tc.apiServerURL |
| 143 | + |
| 144 | + // Set kubeconfig flag value |
| 145 | + if len(tc.kubeconfigFlag) > 0 { |
| 146 | + kubeconfig = filepath.Join(dir, tc.kubeconfigFlag) |
| 147 | + } |
| 148 | + |
| 149 | + // Set KUBECONFIG env value |
| 150 | + if len(tc.kubeconfigEnv) > 0 { |
| 151 | + kubeconfigEnvPaths := []string{} |
| 152 | + for _, k := range tc.kubeconfigEnv { |
| 153 | + kubeconfigEnvPaths = append(kubeconfigEnvPaths, filepath.Join(dir, k)) |
| 154 | + } |
| 155 | + os.Setenv(clientcmd.RecommendedConfigPathEnvVar, strings.Join(kubeconfigEnvPaths, ":")) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func createFiles(files map[string]string, dir string) error { |
| 160 | + for path, data := range files { |
| 161 | + if err := ioutil.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + } |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +func genKubeconfig(contexts ...string) string { |
| 169 | + var sb strings.Builder |
| 170 | + sb.WriteString(`--- |
| 171 | +apiVersion: v1 |
| 172 | +kind: Config |
| 173 | +clusters: |
| 174 | +`) |
| 175 | + for _, ctx := range contexts { |
| 176 | + sb.WriteString(`- cluster: |
| 177 | + server: ` + ctx + ` |
| 178 | + name: ` + ctx + ` |
| 179 | +`) |
| 180 | + } |
| 181 | + sb.WriteString("contexts:\n") |
| 182 | + for _, ctx := range contexts { |
| 183 | + sb.WriteString(`- context: |
| 184 | + cluster: ` + ctx + ` |
| 185 | + user: ` + ctx + ` |
| 186 | + name: ` + ctx + ` |
| 187 | +`) |
| 188 | + } |
| 189 | + |
| 190 | + sb.WriteString("users:\n") |
| 191 | + for _, ctx := range contexts { |
| 192 | + sb.WriteString(`- name: ` + ctx + ` |
| 193 | +`) |
| 194 | + } |
| 195 | + sb.WriteString("preferences: {}\n") |
| 196 | + if len(contexts) > 0 { |
| 197 | + sb.WriteString("current-context: " + contexts[0] + "\n") |
| 198 | + } |
| 199 | + |
| 200 | + return sb.String() |
| 201 | +} |
0 commit comments