Skip to content

Commit f52876e

Browse files
authored
fix: use kubernetes-client/go-base for kubeconfig marshaling (#1955)
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent 1661a15 commit f52876e

11 files changed

+158
-67
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ require (
1414
github.com/etdub/goparsetime v0.0.0-20160315173935-ea17b0ac3318 // indirect
1515
github.com/fatih/color v1.12.0
1616
github.com/getsentry/raven-go v0.2.0
17+
github.com/ghodss/yaml v1.0.0
1718
github.com/gorilla/websocket v1.4.2
1819
github.com/hashicorp/go-version v1.3.0
1920
github.com/karrick/tparse v2.4.2+incompatible
21+
github.com/kubernetes-client/go-base v0.0.0-20190205182333-3d0e39759d98
2022
github.com/mattn/go-colorable v0.1.8
2123
github.com/mattn/go-isatty v0.0.13
2224
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210707121459-c4899e298c98

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGE
9292
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
9393
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
9494
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
95+
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
9596
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
9697
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
9798
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -203,6 +204,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
203204
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
204205
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
205206
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
207+
github.com/kubernetes-client/go-base v0.0.0-20190205182333-3d0e39759d98 h1:ZMIkOkl/Bg5H4EJI7zbjVXAo4rV0QJOGz2U5A0xUmZU=
208+
github.com/kubernetes-client/go-base v0.0.0-20190205182333-3d0e39759d98/go.mod h1:HPlr4uJEfrxar3JUY9cmXs3oooPjTLO6nEaEAIt5LI8=
206209
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
207210
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
208211
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=

internal/namespaces/k8s/v1/custom_kubeconfig_get.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"reflect"
66

7+
"github.com/ghodss/yaml"
8+
api "github.com/kubernetes-client/go-base/config/api"
79
"github.com/scaleway/scaleway-cli/internal/core"
810
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
911
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -58,10 +60,22 @@ func k8sKubeconfigGetRun(ctx context.Context, argsI interface{}) (i interface{},
5860
client := core.ExtractClient(ctx)
5961
apiK8s := k8s.NewAPI(client)
6062

61-
kubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
63+
apiKubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
6264
if err != nil {
6365
return nil, err
6466
}
6567

66-
return string(kubeconfig.GetRaw()), nil
68+
var kubeconfig api.Config
69+
70+
err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
config, err := yaml.Marshal(kubeconfig)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
return string(config), nil
6781
}

internal/namespaces/k8s/v1/custom_kubeconfig_get_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"testing"
55

66
"github.com/alecthomas/assert"
7+
"github.com/ghodss/yaml"
8+
api "github.com/kubernetes-client/go-base/config/api"
79
"github.com/scaleway/scaleway-cli/internal/core"
8-
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
910
)
1011

1112
func Test_GetKubeconfig(t *testing.T) {
@@ -19,7 +20,9 @@ func Test_GetKubeconfig(t *testing.T) {
1920
Check: core.TestCheckCombine(
2021
core.TestCheckGolden(),
2122
func(t *testing.T, ctx *core.CheckFuncCtx) {
22-
assert.Equal(t, ctx.Result.(string), string(ctx.Meta["Kubeconfig"].(*k8s.Kubeconfig).GetRaw()))
23+
config, err := yaml.Marshal(ctx.Meta["Kubeconfig"].(api.Config))
24+
assert.Equal(t, err, nil)
25+
assert.Equal(t, ctx.Result.(string), string(config))
2326
},
2427
core.TestCheckExitCode(0),
2528
),

internal/namespaces/k8s/v1/custom_kubeconfig_helpers.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"runtime"
99
"strings"
1010

11+
"github.com/ghodss/yaml"
12+
api "github.com/kubernetes-client/go-base/config/api"
1113
"github.com/scaleway/scaleway-cli/internal/core"
12-
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
13-
"gopkg.in/yaml.v2"
1414
)
1515

1616
// get the path to the wanted kubeconfig on disk
@@ -36,14 +36,14 @@ func getKubeconfigPath(ctx context.Context) (string, error) {
3636
return kubeconfigPath, nil
3737
}
3838

39-
func openAndUnmarshalKubeconfig(kubeconfigPath string) (*k8s.Kubeconfig, error) {
39+
func openAndUnmarshalKubeconfig(kubeconfigPath string) (*api.Config, error) {
4040
// getting the existing file
4141
file, err := ioutil.ReadFile(kubeconfigPath)
4242
if err != nil {
4343
return nil, err
4444
}
4545

46-
var kubeconfig k8s.Kubeconfig
46+
var kubeconfig api.Config
4747

4848
err = yaml.Unmarshal(file, &kubeconfig)
4949
if err != nil {
@@ -53,7 +53,7 @@ func openAndUnmarshalKubeconfig(kubeconfigPath string) (*k8s.Kubeconfig, error)
5353
return &kubeconfig, nil
5454
}
5555

56-
func marshalAndWriteKubeconfig(kubeconfig *k8s.Kubeconfig, kubeconfigPath string) error {
56+
func marshalAndWriteKubeconfig(kubeconfig *api.Config, kubeconfigPath string) error {
5757
newKubeconfig, err := yaml.Marshal(*kubeconfig)
5858
if err != nil {
5959
return err

internal/namespaces/k8s/v1/custom_kubeconfig_install.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"path"
88
"reflect"
99

10+
"github.com/ghodss/yaml"
11+
api "github.com/kubernetes-client/go-base/config/api"
1012
"github.com/scaleway/scaleway-cli/internal/core"
1113
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
1214
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -74,7 +76,13 @@ func k8sKubeconfigInstallRun(ctx context.Context, argsI interface{}) (i interfac
7476
apiK8s := k8s.NewAPI(client)
7577

7678
// get the wanted kubeconfig
77-
kubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
79+
apiKubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
80+
if err != nil {
81+
return nil, err
82+
}
83+
var kubeconfig api.Config
84+
85+
err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig)
7886
if err != nil {
7987
return nil, err
8088
}
@@ -115,7 +123,7 @@ func k8sKubeconfigInstallRun(ctx context.Context, argsI interface{}) (i interfac
115123
}
116124
}
117125
if !clusterFoundInExistingKubeconfig {
118-
existingKubeconfig.Clusters = append(existingKubeconfig.Clusters, &k8s.KubeconfigClusterWithName{
126+
existingKubeconfig.Clusters = append(existingKubeconfig.Clusters, api.NamedCluster{
119127
Name: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
120128
Cluster: kubeconfig.Clusters[0].Cluster,
121129
})
@@ -126,36 +134,38 @@ func k8sKubeconfigInstallRun(ctx context.Context, argsI interface{}) (i interfac
126134
for _, kubeconfigContext := range existingKubeconfig.Contexts {
127135
if kubeconfigContext.Name == kubeconfig.Contexts[0].Name+"-"+request.ClusterID {
128136
contextFoundInExistingKubeconfig = true
129-
kubeconfigContext.Context = k8s.KubeconfigContext{
130-
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
131-
User: kubeconfig.Users[0].Name + "-" + request.ClusterID,
137+
kubeconfigContext.Context = api.Context{
138+
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
139+
AuthInfo: kubeconfig.AuthInfos[0].Name,
132140
}
133141
break
134142
}
135143
}
136144
if !contextFoundInExistingKubeconfig {
137-
existingKubeconfig.Contexts = append(existingKubeconfig.Contexts, &k8s.KubeconfigContextWithName{
145+
existingKubeconfig.Contexts = append(existingKubeconfig.Contexts, api.NamedContext{
138146
Name: kubeconfig.Contexts[0].Name + "-" + request.ClusterID,
139-
Context: k8s.KubeconfigContext{
140-
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
141-
User: kubeconfig.Users[0].Name + "-" + request.ClusterID,
147+
Context: api.Context{
148+
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
149+
AuthInfo: kubeconfig.AuthInfos[0].Name + "-" + request.ClusterID,
142150
},
143151
})
144152
}
145153

146154
// loop through all users and insert the wanted one if it does not exist
147155
userFoundInExistingKubeconfig := false
148-
for _, user := range existingKubeconfig.Users {
149-
if user.Name == kubeconfig.Users[0].Name+"-"+request.ClusterID {
156+
for _, user := range existingKubeconfig.AuthInfos {
157+
if user.Name == kubeconfig.AuthInfos[0].Name+"-"+request.ClusterID {
150158
userFoundInExistingKubeconfig = true
151-
user.User = kubeconfig.Users[0].User
159+
user.AuthInfo.Username = kubeconfig.AuthInfos[0].AuthInfo.Username
152160
break
153161
}
154162
}
155163
if !userFoundInExistingKubeconfig {
156-
existingKubeconfig.Users = append(existingKubeconfig.Users, &k8s.KubeconfigUserWithName{
157-
Name: kubeconfig.Users[0].Name + "-" + request.ClusterID,
158-
User: kubeconfig.Users[0].User,
164+
existingKubeconfig.AuthInfos = append(existingKubeconfig.AuthInfos, api.NamedAuthInfo{
165+
Name: kubeconfig.AuthInfos[0].Name + "-" + request.ClusterID,
166+
AuthInfo: api.AuthInfo{
167+
Username: kubeconfig.AuthInfos[0].AuthInfo.Username,
168+
},
159169
})
160170
}
161171

0 commit comments

Comments
 (0)