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

Commit 530b04a

Browse files
committed
cli/cmd: refactor getKubeconfig() to return kubeconfig file content
Rather than kubeconfig file path. This will allow changing this function to pull kubeconfig content from Terraform output rather than from assets directory, which will resolve #608. Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
1 parent d98d7d8 commit 530b04a

File tree

6 files changed

+87
-57
lines changed

6 files changed

+87
-57
lines changed

cli/cmd/cluster-apply.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019

2120
"github.com/pkg/errors"
2221
log "github.com/sirupsen/logrus"
@@ -80,14 +79,12 @@ func runClusterApply(cmd *cobra.Command, args []string) {
8079

8180
fmt.Printf("\nYour configurations are stored in %s\n", assetDir)
8281

83-
kubeconfigPath := assetsKubeconfig(assetDir)
84-
85-
kubeconfigContent, err := ioutil.ReadFile(kubeconfigPath) // #nosec G304
82+
kubeconfig, err := getKubeconfig()
8683
if err != nil {
87-
ctxLogger.Fatalf("Failed to read kubeconfig file: %q: %v", kubeconfigPath, err)
84+
ctxLogger.Fatalf("Failed to get kubeconfig: %v", err)
8885
}
8986

90-
if err := verifyCluster(kubeconfigContent, p.Meta().ExpectedNodes); err != nil {
87+
if err := verifyCluster(kubeconfig, p.Meta().ExpectedNodes); err != nil {
9188
ctxLogger.Fatalf("Verify cluster: %v", err)
9289
}
9390

@@ -96,7 +93,7 @@ func runClusterApply(cmd *cobra.Command, args []string) {
9693
fmt.Printf("\nEnsuring that cluster controlplane is up to date.\n")
9794

9895
cu := controlplaneUpdater{
99-
kubeconfig: kubeconfigContent,
96+
kubeconfig: kubeconfig,
10097
assetDir: assetDir,
10198
ctxLogger: *ctxLogger,
10299
ex: *ex,
@@ -125,7 +122,7 @@ func runClusterApply(cmd *cobra.Command, args []string) {
125122
ctxLogger.Println("Applying component configuration")
126123

127124
if len(componentsToApply) > 0 {
128-
if err := applyComponents(lokoConfig, kubeconfigContent, componentsToApply...); err != nil {
125+
if err := applyComponents(lokoConfig, kubeconfig, componentsToApply...); err != nil {
129126
ctxLogger.Fatalf("Applying component configuration failed: %v", err)
130127
}
131128
}

cli/cmd/component-apply.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019

2120
log "github.com/sirupsen/logrus"
2221
"github.com/spf13/cobra"
@@ -64,12 +63,7 @@ func runApply(cmd *cobra.Command, args []string) {
6463
contextLogger.Fatalf("Error in finding kubeconfig file: %s", err)
6564
}
6665

67-
kubeconfigContent, err := ioutil.ReadFile(kubeconfig) // #nosec G304
68-
if err != nil {
69-
contextLogger.Fatalf("Failed to read kubeconfig file: %q: %v", kubeconfig, err)
70-
}
71-
72-
if err := applyComponents(lokoConfig, kubeconfigContent, componentsToApply...); err != nil {
66+
if err := applyComponents(lokoConfig, kubeconfig, componentsToApply...); err != nil {
7367
contextLogger.Fatal(err)
7468
}
7569
}

cli/cmd/component-delete.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"strings"
2120

2221
log "github.com/sirupsen/logrus"
@@ -92,12 +91,7 @@ func runDelete(cmd *cobra.Command, args []string) {
9291
contextLogger.Fatalf("Error in finding kubeconfig file: %s", err)
9392
}
9493

95-
kubeconfigContent, err := ioutil.ReadFile(kubeconfig) // #nosec G304
96-
if err != nil {
97-
contextLogger.Fatalf("Failed to read kubeconfig file: %q: %v", kubeconfig, err)
98-
}
99-
100-
if err := deleteComponents(kubeconfigContent, componentsObjects...); err != nil {
94+
if err := deleteComponents(kubeconfig, componentsObjects...); err != nil {
10195
contextLogger.Fatal(err)
10296
}
10397
}

cli/cmd/health.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"text/tabwriter"
2221

@@ -48,12 +47,7 @@ func runHealth(cmd *cobra.Command, args []string) {
4847
contextLogger.Fatalf("Error in finding kubeconfig file: %s", err)
4948
}
5049

51-
kubeconfigContent, err := ioutil.ReadFile(kubeconfig) // #nosec G304
52-
if err != nil {
53-
contextLogger.Fatalf("Failed to read kubeconfig file: %v", err)
54-
}
55-
56-
cs, err := k8sutil.NewClientset(kubeconfigContent)
50+
cs, err := k8sutil.NewClientset(kubeconfig)
5751
if err != nil {
5852
contextLogger.Fatalf("Error in creating setting up Kubernetes client: %q", err)
5953
}

cli/cmd/utils.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cmd
1616

1717
import (
1818
"fmt"
19+
"io/ioutil"
1920
"os"
2021
"path/filepath"
2122

@@ -92,17 +93,20 @@ func getAssetDir() (string, error) {
9293
return cfg.Meta().AssetDir, nil
9394
}
9495

95-
// expandKubeconfigPath tries to expand ~ in the given kubeconfig path.
96-
// However, if that fails, it just returns original path as the best effort.
97-
func expandKubeconfigPath(path string) string {
96+
func getKubeconfig() ([]byte, error) {
97+
path, err := getKubeconfigPath()
98+
if err != nil {
99+
return nil, fmt.Errorf("failed getting kubeconfig path: %w", err)
100+
}
101+
98102
if expandedPath, err := homedir.Expand(path); err == nil {
99-
return expandedPath
103+
path = expandedPath
100104
}
101105

102106
// homedir.Expand is too restrictive for the ~ prefix,
103107
// i.e., it errors on "~somepath" which is a valid path,
104-
// so just return the original path.
105-
return path
108+
// so just read from the original path.
109+
return ioutil.ReadFile(path) // #nosec G304
106110
}
107111

108112
// getKubeconfig finds the kubeconfig to be used. The precedence is the following:
@@ -112,7 +116,7 @@ func expandKubeconfigPath(path string) string {
112116
// - Asset directory from cluster configuration.
113117
// - KUBECONFIG environment variable.
114118
// - ~/.kube/config path, which is the default for kubectl.
115-
func getKubeconfig() (string, error) {
119+
func getKubeconfigPath() (string, error) {
116120
assetKubeconfig, err := assetsKubeconfigPath()
117121
if err != nil {
118122
return "", fmt.Errorf("reading kubeconfig path from configuration failed: %w", err)
@@ -125,18 +129,13 @@ func getKubeconfig() (string, error) {
125129
defaultKubeconfigPath,
126130
}
127131

128-
return expandKubeconfigPath(pickString(paths...)), nil
129-
}
130-
131-
// pickString returns first non-empty string.
132-
func pickString(options ...string) string {
133-
for _, option := range options {
134-
if option != "" {
135-
return option
132+
for _, path := range paths {
133+
if path != "" {
134+
return path, nil
136135
}
137136
}
138137

139-
return ""
138+
return "", nil
140139
}
141140

142141
// assetsKubeconfigPath reads the lokocfg configuration and returns

cli/cmd/utils_internal_test.go

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"io/ioutil"
1919
"os"
2020
"path/filepath"
21+
"reflect"
2122
"testing"
2223

2324
"github.com/spf13/viper"
@@ -29,6 +30,10 @@ type kubeconfigSources struct {
2930
configFile string
3031
}
3132

33+
const (
34+
tmpPattern = "lokoctl-tests-"
35+
)
36+
3237
func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) {
3338
// Ensure viper flag is NOT empty.
3439
viper.Set(kubeconfigFlag, k.flag)
@@ -48,7 +53,7 @@ func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) {
4853
}
4954

5055
// Ensure there is no lokocfg configuration in working directory.
51-
tmpDir, err := ioutil.TempDir("", "lokoctl-tests-")
56+
tmpDir, err := ioutil.TempDir("", tmpPattern)
5257
if err != nil {
5358
t.Fatalf("creating tmp dir: %v", err)
5459
}
@@ -71,7 +76,54 @@ func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) {
7176
}
7277
}
7378

74-
func TestGetKubeconfigFlag(t *testing.T) {
79+
func TestGetKubeconfigBadConfig(t *testing.T) {
80+
k := &kubeconfigSources{
81+
configFile: `cluster "packet" {
82+
asset_dir = "/foo"
83+
}`,
84+
}
85+
86+
prepareKubeconfigSource(t, k)
87+
88+
kubeconfig, err := getKubeconfig()
89+
if err == nil {
90+
t.Errorf("getting kubeconfig with bad configuration should fail")
91+
}
92+
93+
if kubeconfig != nil {
94+
t.Fatalf("if getting kubeconfig fails, empty content should be returned")
95+
}
96+
}
97+
98+
func TestGetKubeconfig(t *testing.T) {
99+
expectedContent := []byte("foo")
100+
101+
f, err := ioutil.TempFile("", tmpPattern)
102+
if err != nil {
103+
t.Fatalf("creating temp file should succeed, got: %v", err)
104+
}
105+
106+
if err := ioutil.WriteFile(f.Name(), expectedContent, 0600); err != nil {
107+
t.Fatalf("writing temp file %q should succeed, got: %v", f.Name(), err)
108+
}
109+
110+
k := &kubeconfigSources{
111+
env: f.Name(),
112+
}
113+
114+
prepareKubeconfigSource(t, k)
115+
116+
kubeconfig, err := getKubeconfig()
117+
if err != nil {
118+
t.Fatalf("getting kubeconfig: %v", err)
119+
}
120+
121+
if !reflect.DeepEqual(kubeconfig, expectedContent) {
122+
t.Fatalf("expected %q, got %q", expectedContent, kubeconfig)
123+
}
124+
}
125+
126+
func TestGetKubeconfigPathFlag(t *testing.T) {
75127
expectedPath := "/foo"
76128

77129
k := &kubeconfigSources{
@@ -99,7 +151,7 @@ func TestGetKubeconfigFlag(t *testing.T) {
99151

100152
prepareKubeconfigSource(t, k)
101153

102-
kubeconfig, err := getKubeconfig()
154+
kubeconfig, err := getKubeconfigPath()
103155
if err != nil {
104156
t.Fatalf("getting kubeconfig: %v", err)
105157
}
@@ -109,7 +161,7 @@ func TestGetKubeconfigFlag(t *testing.T) {
109161
}
110162
}
111163

112-
func TestGetKubeconfigConfigFile(t *testing.T) {
164+
func TestGetKubeconfigPathConfigFile(t *testing.T) {
113165
expectedPath := assetsKubeconfig("/foo")
114166

115167
k := &kubeconfigSources{
@@ -136,7 +188,7 @@ func TestGetKubeconfigConfigFile(t *testing.T) {
136188

137189
prepareKubeconfigSource(t, k)
138190

139-
kubeconfig, err := getKubeconfig()
191+
kubeconfig, err := getKubeconfigPath()
140192
if err != nil {
141193
t.Fatalf("getting kubeconfig: %v", err)
142194
}
@@ -146,7 +198,7 @@ func TestGetKubeconfigConfigFile(t *testing.T) {
146198
}
147199
}
148200

149-
func TestGetKubeconfigBadConfigFile(t *testing.T) {
201+
func TestGetKubeconfigPathBadConfigFile(t *testing.T) {
150202
expectedPath := ""
151203

152204
k := &kubeconfigSources{
@@ -157,7 +209,7 @@ func TestGetKubeconfigBadConfigFile(t *testing.T) {
157209

158210
prepareKubeconfigSource(t, k)
159211

160-
kubeconfig, err := getKubeconfig()
212+
kubeconfig, err := getKubeconfigPath()
161213
if err == nil {
162214
t.Errorf("getting kubeconfig with bad configuration should fail")
163215
}
@@ -167,7 +219,7 @@ func TestGetKubeconfigBadConfigFile(t *testing.T) {
167219
}
168220
}
169221

170-
func TestGetKubeconfigEnvVariable(t *testing.T) {
222+
func TestGetKubeconfigPathEnvVariable(t *testing.T) {
171223
expectedPath := "/foo"
172224

173225
k := &kubeconfigSources{
@@ -176,7 +228,7 @@ func TestGetKubeconfigEnvVariable(t *testing.T) {
176228

177229
prepareKubeconfigSource(t, k)
178230

179-
kubeconfig, err := getKubeconfig()
231+
kubeconfig, err := getKubeconfigPath()
180232
if err != nil {
181233
t.Fatalf("getting kubeconfig: %v", err)
182234
}
@@ -186,14 +238,14 @@ func TestGetKubeconfigEnvVariable(t *testing.T) {
186238
}
187239
}
188240

189-
func TestGetKubeconfigDefault(t *testing.T) {
190-
expectedPath := expandKubeconfigPath(defaultKubeconfigPath)
241+
func TestGetKubeconfigPathDefault(t *testing.T) {
242+
expectedPath := defaultKubeconfigPath
191243

192244
k := &kubeconfigSources{}
193245

194246
prepareKubeconfigSource(t, k)
195247

196-
kubeconfig, err := getKubeconfig()
248+
kubeconfig, err := getKubeconfigPath()
197249
if err != nil {
198250
t.Fatalf("getting kubeconfig: %v", err)
199251
}

0 commit comments

Comments
 (0)