Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use kubernetes-client/go-base for kubeconfig marshaling #1955

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ require (
github.com/etdub/goparsetime v0.0.0-20160315173935-ea17b0ac3318 // indirect
github.com/fatih/color v1.12.0
github.com/getsentry/raven-go v0.2.0
github.com/ghodss/yaml v1.0.0
github.com/gorilla/websocket v1.4.2
github.com/hashicorp/go-version v1.3.0
github.com/karrick/tparse v2.4.2+incompatible
github.com/kubernetes-client/go-base v0.0.0-20190205182333-3d0e39759d98
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.13
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210707121459-c4899e298c98
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGE
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -203,6 +204,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kubernetes-client/go-base v0.0.0-20190205182333-3d0e39759d98 h1:ZMIkOkl/Bg5H4EJI7zbjVXAo4rV0QJOGz2U5A0xUmZU=
github.com/kubernetes-client/go-base v0.0.0-20190205182333-3d0e39759d98/go.mod h1:HPlr4uJEfrxar3JUY9cmXs3oooPjTLO6nEaEAIt5LI8=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
Expand Down
18 changes: 16 additions & 2 deletions internal/namespaces/k8s/v1/custom_kubeconfig_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"reflect"

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

kubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
apiKubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
if err != nil {
return nil, err
}

return string(kubeconfig.GetRaw()), nil
var kubeconfig api.Config

err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig)
if err != nil {
return nil, err
}

config, err := yaml.Marshal(kubeconfig)
if err != nil {
return nil, err
}

return string(config), nil
}
7 changes: 5 additions & 2 deletions internal/namespaces/k8s/v1/custom_kubeconfig_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"testing"

"github.com/alecthomas/assert"
"github.com/ghodss/yaml"
api "github.com/kubernetes-client/go-base/config/api"
"github.com/scaleway/scaleway-cli/internal/core"
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
)

func Test_GetKubeconfig(t *testing.T) {
Expand All @@ -19,7 +20,9 @@ func Test_GetKubeconfig(t *testing.T) {
Check: core.TestCheckCombine(
core.TestCheckGolden(),
func(t *testing.T, ctx *core.CheckFuncCtx) {
assert.Equal(t, ctx.Result.(string), string(ctx.Meta["Kubeconfig"].(*k8s.Kubeconfig).GetRaw()))
config, err := yaml.Marshal(ctx.Meta["Kubeconfig"].(api.Config))
assert.Equal(t, err, nil)
assert.Equal(t, ctx.Result.(string), string(config))
},
core.TestCheckExitCode(0),
),
Expand Down
10 changes: 5 additions & 5 deletions internal/namespaces/k8s/v1/custom_kubeconfig_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"runtime"
"strings"

"github.com/ghodss/yaml"
api "github.com/kubernetes-client/go-base/config/api"
"github.com/scaleway/scaleway-cli/internal/core"
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
"gopkg.in/yaml.v2"
)

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

func openAndUnmarshalKubeconfig(kubeconfigPath string) (*k8s.Kubeconfig, error) {
func openAndUnmarshalKubeconfig(kubeconfigPath string) (*api.Config, error) {
// getting the existing file
file, err := ioutil.ReadFile(kubeconfigPath)
if err != nil {
return nil, err
}

var kubeconfig k8s.Kubeconfig
var kubeconfig api.Config

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

func marshalAndWriteKubeconfig(kubeconfig *k8s.Kubeconfig, kubeconfigPath string) error {
func marshalAndWriteKubeconfig(kubeconfig *api.Config, kubeconfigPath string) error {
newKubeconfig, err := yaml.Marshal(*kubeconfig)
if err != nil {
return err
Expand Down
40 changes: 25 additions & 15 deletions internal/namespaces/k8s/v1/custom_kubeconfig_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path"
"reflect"

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

// get the wanted kubeconfig
kubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
apiKubeconfig, err := apiK8s.GetClusterKubeConfig(kubeconfigRequest)
if err != nil {
return nil, err
}
var kubeconfig api.Config

err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,7 +123,7 @@ func k8sKubeconfigInstallRun(ctx context.Context, argsI interface{}) (i interfac
}
}
if !clusterFoundInExistingKubeconfig {
existingKubeconfig.Clusters = append(existingKubeconfig.Clusters, &k8s.KubeconfigClusterWithName{
existingKubeconfig.Clusters = append(existingKubeconfig.Clusters, api.NamedCluster{
Name: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
Cluster: kubeconfig.Clusters[0].Cluster,
})
Expand All @@ -126,36 +134,38 @@ func k8sKubeconfigInstallRun(ctx context.Context, argsI interface{}) (i interfac
for _, kubeconfigContext := range existingKubeconfig.Contexts {
if kubeconfigContext.Name == kubeconfig.Contexts[0].Name+"-"+request.ClusterID {
contextFoundInExistingKubeconfig = true
kubeconfigContext.Context = k8s.KubeconfigContext{
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
User: kubeconfig.Users[0].Name + "-" + request.ClusterID,
kubeconfigContext.Context = api.Context{
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
AuthInfo: kubeconfig.AuthInfos[0].Name,
}
break
}
}
if !contextFoundInExistingKubeconfig {
existingKubeconfig.Contexts = append(existingKubeconfig.Contexts, &k8s.KubeconfigContextWithName{
existingKubeconfig.Contexts = append(existingKubeconfig.Contexts, api.NamedContext{
Name: kubeconfig.Contexts[0].Name + "-" + request.ClusterID,
Context: k8s.KubeconfigContext{
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
User: kubeconfig.Users[0].Name + "-" + request.ClusterID,
Context: api.Context{
Cluster: kubeconfig.Clusters[0].Name + "-" + request.ClusterID,
AuthInfo: kubeconfig.AuthInfos[0].Name + "-" + request.ClusterID,
},
})
}

// loop through all users and insert the wanted one if it does not exist
userFoundInExistingKubeconfig := false
for _, user := range existingKubeconfig.Users {
if user.Name == kubeconfig.Users[0].Name+"-"+request.ClusterID {
for _, user := range existingKubeconfig.AuthInfos {
if user.Name == kubeconfig.AuthInfos[0].Name+"-"+request.ClusterID {
userFoundInExistingKubeconfig = true
user.User = kubeconfig.Users[0].User
user.AuthInfo.Username = kubeconfig.AuthInfos[0].AuthInfo.Username
break
}
}
if !userFoundInExistingKubeconfig {
existingKubeconfig.Users = append(existingKubeconfig.Users, &k8s.KubeconfigUserWithName{
Name: kubeconfig.Users[0].Name + "-" + request.ClusterID,
User: kubeconfig.Users[0].User,
existingKubeconfig.AuthInfos = append(existingKubeconfig.AuthInfos, api.NamedAuthInfo{
Name: kubeconfig.AuthInfos[0].Name + "-" + request.ClusterID,
AuthInfo: api.AuthInfo{
Username: kubeconfig.AuthInfos[0].AuthInfo.Username,
},
})
}

Expand Down
Loading