From ce1dea338fb12511e595e0307151df1b32d2870a Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Thu, 2 Sep 2021 16:29:11 +0200 Subject: [PATCH] fix: use kubernetes-client/go-base for kubeconfig marshaling Signed-off-by: Patrik Cyvoct --- go.mod | 2 + go.sum | 3 + .../k8s/v1/custom_kubeconfig_get.go | 18 ++++- .../k8s/v1/custom_kubeconfig_get_test.go | 7 +- .../k8s/v1/custom_kubeconfig_helpers.go | 10 +-- .../k8s/v1/custom_kubeconfig_install.go | 40 +++++++---- .../k8s/v1/custom_kubeconfig_install_test.go | 69 ++++++++++++------- .../k8s/v1/custom_kubeconfig_uninstall.go | 12 ++-- .../v1/custom_kubeconfig_uninstall_test.go | 11 +-- internal/namespaces/k8s/v1/helpers_test.go | 42 +++++++++-- .../test-get-kubeconfig-simple.golden | 11 +-- 11 files changed, 158 insertions(+), 67 deletions(-) diff --git a/go.mod b/go.mod index b7989197c6..c8202c3679 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a5469e02bd..d87f756461 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_get.go b/internal/namespaces/k8s/v1/custom_kubeconfig_get.go index e9e14f7aec..f6f4dac576 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_get.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_get.go @@ -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" @@ -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 } diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_get_test.go b/internal/namespaces/k8s/v1/custom_kubeconfig_get_test.go index 5edfa27ade..936a5e5168 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_get_test.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_get_test.go @@ -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) { @@ -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), ), diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_helpers.go b/internal/namespaces/k8s/v1/custom_kubeconfig_helpers.go index 6fbd8b9e33..687139f25a 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_helpers.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_helpers.go @@ -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 @@ -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 { @@ -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 diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_install.go b/internal/namespaces/k8s/v1/custom_kubeconfig_install.go index dca84c1e28..10a64e97ba 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_install.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_install.go @@ -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" @@ -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 } @@ -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, }) @@ -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, + }, }) } diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_install_test.go b/internal/namespaces/k8s/v1/custom_kubeconfig_install_test.go index 7e423e06e6..dd01db39c8 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_install_test.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_install_test.go @@ -7,9 +7,10 @@ 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/scale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1RzZXbWx3QmFUcVU5S0ZSUFBSVTlxNDA4WTJjR2RXVzRkTXM0cWRaSlkxYUg1QjNJUDVBb25PCnhwSkVOMFRadGluaGlnaXUvbVkza3NzQkFvR0JBUFZDb0ZnYmhQaUpXZDVTMnRXZnV2aEZMR3ZPbVNwb1p1d28Kc2x5QnNUOUNwOTdWVVRHbEQ3Ymh6allEcnVFQ1BicVk5NThkaGwwVUgrdHZvT0FIVVZDM0V6d05JcExUQ1BmTQptamVUZVkrKzRPdXRSQmkzTzVOZFJqL05QMWd2ZFZraEpCTGxKRmxoY2JHOXIwTE9JZkIzckdFNkloN1JpUmc4CjkvZzZhV1JOQW9HQWJSSG94d1B4MUVlRnRxVDlUdXowZWZUR3RwQTB0bkhDZTN2b2x1L201eEZ6N3BwS29HeUQKRkNPVm5jMmZ3LzQwYUFGTGdHYlFLMFBqTzFCbWZ3cjFvb09aT1hZYnExUXo3Q1cvN3A1OUFkR0VrWXFzdWZZcAp6OXlMd1dBUEdybm9jVjBVQXZ2SHcvbC9OK29NZEdpdmVTdDhRb3RHclgzdm9PTmVsWThCZDRNPQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=` - testKubeconfig = &k8s.Kubeconfig{ + testKubeconfig = api.Config{ APIVersion: kubeconfigAPIVersion, Kind: kubeconfigKind, - Clusters: []*k8s.KubeconfigClusterWithName{ + Clusters: []api.NamedCluster{ { Name: "test", - Cluster: k8s.KubeconfigCluster{ - CertificateAuthorityData: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01ETXdOREV4TVRVd01Gb1hEVE13TURNd05ERXhNVFV3TUZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdn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ERzMzRNS2kzWGtDZTIwST0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=", - Server: "https://test:6443", + Cluster: api.Cluster{ + CertificateAuthorityData: []byte(`-----BEGIN CERTIFICATE----- +MIICyDCCAbCgAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl +cm5ldGVzMB4XDTIwMDMwNDExMTUwMFoXDTMwMDMwNDExMTUwMFowFTETMBEGA1UE +AxMKa3ViZXJuZXRlczCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKu/ +LHYyMQ7EZdZZaZEAl2BZd2KVhO64tpgI7CTpnz4dLS6ivdPIRLlgFw0AWpr6APxk +yjx+Of2LqQTxNGCLQZ8Cv/1x+vRvS9arwVuHgZL/fdM19gj2Ec/eF7X8K0EZuSX1 +GoVbrANH72ocdxwoj6YsogdyRDzWnDc7joV9lfVc2QuP7G+aF8FAI6+1FiL9btQ7 +ywfaCZq83WJq1i9+j+4lEAChP5jVv/0KzuN2tnwuZWyc3zQlrGHmqld8X+cTBk0h +7xXI5wSuOO3/W0ipT9AWVf0hYZg5V9UiJN5ADFWzmyxT8m9Uetwm3aVRmgr+5N8a +F6GVMgvLAmyZ5mJhGDkCAwEAAaMjMCEwDgYDVR0PAQH/BAQDAgKkMA8GA1UdEwEB +/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAE/RLeOoms7NQx9H/62HroUQ2yLW +1yiiPUhPDQBDgZyOnuMcvJO8HT+pVw9eOk51y3TOMKbbpAhqsB7tJKATVqbygkG6 +sYD+Ky0rCHO7D139Et5xB6tkCA2qJUQ2AEoI27/gkvw+NVzKTSuRUAtAvWCgb6uY +dTkcihcRzhrWWSWNQ7s6E+tcsIKMbmBUOCl1Xz0DO3tlo9O/4LRA8otWP10xeKrk +UlQUK/Iqi/aS7W86n/Ie5tUo52QfNM2M1fEmQP6y8X1FVl8vp/Vqx11k3y/YHG/V +y7JHcXauRKI7bxgOugSep2d0lhYxJl65CPOCllawcu70Ds34MKi3XkCe20I= +-----END CERTIFICATE----- +`), + Server: "https://test:6443", }, }, }, - Contexts: []*k8s.KubeconfigContextWithName{ + Contexts: []api.NamedContext{ { Name: "test@test", - Context: k8s.KubeconfigContext{ - Cluster: "test", - User: "test", + Context: api.Context{ + Cluster: "test", + AuthInfo: "test", }, }, }, - Users: []*k8s.KubeconfigUserWithName{ + AuthInfos: []api.NamedAuthInfo{ { Name: "test", - User: k8s.KubeconfigUser{ + AuthInfo: api.AuthInfo{ Token: "qotGxuOfD74ajgWir18tMMPicxLIizKg3nt5PKHGbsbNDbGfqNojIdXI", Username: "test", Password: "test", - ClientCertificateData: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvekNDQWVlZ0F3SUJBZ0lJZERQak80Umphdzh3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TURBek1qTXdPRFEyTkRoYUZ3MHlNVEF6TWpNd09EUTJORGhhTUVFeApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1TWXdKQVlEVlFRREV4MXJkV0psTFdGd2FYTmxjblpsCmNpMXJkV0psYkdWMExXTnNhV1Z1ZERDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUIKQU54VjByQ0lBemNsdXIyV1VNb3NqOW1LQmlkclYzcnB5RmNwdnltMmtFVjZaOVo2TTBSRXpyTHo1c3BaWndCTwo1bHZrbEdzL2RJVndFK2pBd2tNWWNRRWlOaTQ2bHU4UFNSei9HVTFkek5mOEF2TXpnRWZER0xUY2x3eUs4di9kCklLenhTUnVOUFFseDZoTUw1bFpDeVBBZ3hqejNEdDZGWmUxUnVUdURWTUhnOWZIaHNwOFZTYnVCbWFYTTU2T0IKLzNZQXJLMXZOTlY0enRlQ3libFZnVUd3QUdKQ09zTlE0d0l4R0xSdjN5TVhtK3V3Y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RHbEQ3Ymh6allEcnVFQ1BicVk5NThkaGwwVUgrdHZvT0FIVVZDM0V6d05JcExUQ1BmTQptamVUZVkrKzRPdXRSQmkzTzVOZFJqL05QMWd2ZFZraEpCTGxKRmxoY2JHOXIwTE9JZkIzckdFNkloN1JpUmc4CjkvZzZhV1JOQW9HQWJSSG94d1B4MUVlRnRxVDlUdXowZWZUR3RwQTB0bkhDZTN2b2x1L201eEZ6N3BwS29HeUQKRkNPVm5jMmZ3LzQwYUFGTGdHYlFLMFBqTzFCbWZ3cjFvb09aT1hZYnExUXo3Q1cvN3A1OUFkR0VrWXFzdWZZcAp6OXlMd1dBUEdybm9jVjBVQXZ2SHcvbC9OK29NZEdpdmVTdDhRb3RHclgzdm9PTmVsWThCZDRNPQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo="), }, }, }, @@ -73,17 +91,22 @@ users: // testIfKubeconfigInFile checks if the given kubeconfig is in the given file // it test if the user, cluster and context of the kubeconfig file are in the given file -func testIfKubeconfigInFile(t *testing.T, filePath string, suffix string, kubeconfig *k8s.Kubeconfig) { +func testIfKubeconfigInFile(t *testing.T, filePath string, suffix string, kubeconfig api.Config) { kubeconfigBytes, err := ioutil.ReadFile(filePath) assert.Nil(t, err) - var existingKubeconfig k8s.Kubeconfig + var existingKubeconfig api.Config err = yaml.Unmarshal(kubeconfigBytes, &existingKubeconfig) assert.Nil(t, err) found := false for _, cluster := range existingKubeconfig.Clusters { if cluster.Name == kubeconfig.Clusters[0].Name+suffix { - assert.Equal(t, kubeconfig.Clusters[0].Cluster, cluster.Cluster) + //t.Log(string(cluster.Cluster.CertificateAuthorityData)) + // t.Log(string(kubeconfig.Clusters[0].Cluster.CertificateAuthorityData)) + // panic(string(cluster.Cluster.CertificateAuthorityData)) + //panic(string(kubeconfig.Clusters[0].Cluster.CertificateAuthorityData)) + assert.Equal(t, string(kubeconfig.Clusters[0].Cluster.CertificateAuthorityData), string(cluster.Cluster.CertificateAuthorityData)) + assert.Equal(t, kubeconfig.Clusters[0].Cluster.Server, cluster.Cluster.Server) found = true break } @@ -94,7 +117,7 @@ func testIfKubeconfigInFile(t *testing.T, filePath string, suffix string, kubeco for _, context := range existingKubeconfig.Contexts { if context.Name == kubeconfig.Contexts[0].Name+suffix { assert.Equal(t, kubeconfig.Contexts[0].Context.Cluster+suffix, context.Context.Cluster) - assert.Equal(t, kubeconfig.Contexts[0].Context.User+suffix, context.Context.User) + assert.Equal(t, kubeconfig.Contexts[0].Context.AuthInfo+suffix, context.Context.AuthInfo) assert.Equal(t, kubeconfig.Contexts[0].Context.Namespace, context.Context.Namespace) found = true break @@ -103,9 +126,9 @@ func testIfKubeconfigInFile(t *testing.T, filePath string, suffix string, kubeco assert.True(t, found, "context not found in kubeconfig for cluster with suffix %s", suffix) found = false - for _, user := range existingKubeconfig.Users { - if user.Name == kubeconfig.Users[0].Name+suffix { - assert.Equal(t, kubeconfig.Users[0].User, user.User) + for _, user := range existingKubeconfig.AuthInfos { + if user.Name == kubeconfig.AuthInfos[0].Name+suffix { + assert.Equal(t, kubeconfig.AuthInfos[0].AuthInfo.Username, user.AuthInfo.Username) found = true break } @@ -124,7 +147,7 @@ func Test_InstallKubeconfig(t *testing.T) { Check: core.TestCheckCombine( // no golden tests since it's os specific func(t *testing.T, ctx *core.CheckFuncCtx) { - testIfKubeconfigInFile(t, path.Join(os.TempDir(), "cli-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(*k8s.Kubeconfig)) + testIfKubeconfigInFile(t, path.Join(os.TempDir(), "cli-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(api.Config)) }, core.TestCheckExitCode(0), ), @@ -141,7 +164,7 @@ func Test_InstallKubeconfig(t *testing.T) { Check: core.TestCheckCombine( // no golden tests since it's os specific func(t *testing.T, ctx *core.CheckFuncCtx) { - testIfKubeconfigInFile(t, path.Join(os.TempDir(), "cli-merge-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(*k8s.Kubeconfig)) + testIfKubeconfigInFile(t, path.Join(os.TempDir(), "cli-merge-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(api.Config)) testIfKubeconfigInFile(t, path.Join(os.TempDir(), "cli-merge-test"), "", testKubeconfig) }, core.TestCheckExitCode(0), diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go b/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go index cefcad2bdb..6b275078ac 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go @@ -7,8 +7,8 @@ import ( "reflect" "strings" + "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" ) type k8sKubeconfigUninstallRequest struct { @@ -68,22 +68,22 @@ func k8sKubeconfigUninstallRun(ctx context.Context, argsI interface{}) (i interf return nil, err } - newClusters := []*k8s.KubeconfigClusterWithName{} + newClusters := []api.NamedCluster{} for _, cluster := range existingKubeconfig.Clusters { if !strings.HasSuffix(cluster.Name, request.ClusterID) { newClusters = append(newClusters, cluster) } } - newContexts := []*k8s.KubeconfigContextWithName{} + newContexts := []api.NamedContext{} for _, kubeconfigContext := range existingKubeconfig.Contexts { if !strings.HasSuffix(kubeconfigContext.Name, request.ClusterID) { newContexts = append(newContexts, kubeconfigContext) } } - newUsers := []*k8s.KubeconfigUserWithName{} - for _, user := range existingKubeconfig.Users { + newUsers := []api.NamedAuthInfo{} + for _, user := range existingKubeconfig.AuthInfos { if !strings.HasSuffix(user.Name, request.ClusterID) { newUsers = append(newUsers, user) } @@ -97,7 +97,7 @@ func k8sKubeconfigUninstallRun(ctx context.Context, argsI interface{}) (i interf // write the modification existingKubeconfig.Clusters = newClusters existingKubeconfig.Contexts = newContexts - existingKubeconfig.Users = newUsers + existingKubeconfig.AuthInfos = newUsers err = marshalAndWriteKubeconfig(existingKubeconfig, kubeconfigPath) if err != nil { diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall_test.go b/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall_test.go index a88023132c..2b7d92da5d 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall_test.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall_test.go @@ -7,14 +7,15 @@ 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" - "gopkg.in/yaml.v2" ) // testIfKubeconfigNotInFile checks if the given kubeconfig is not in the given file // it test if the user, cluster and context of the kubeconfig file are not in the given file -func testIfKubeconfigNotInFile(t *testing.T, filePath string, suffix string, kubeconfig *k8s.Kubeconfig) { +func testIfKubeconfigNotInFile(t *testing.T, filePath string, suffix string, kubeconfig api.Config) { kubeconfigBytes, err := ioutil.ReadFile(filePath) assert.Nil(t, err) var existingKubeconfig k8s.Kubeconfig @@ -41,7 +42,7 @@ func testIfKubeconfigNotInFile(t *testing.T, filePath string, suffix string, kub found = false for _, user := range existingKubeconfig.Users { - if user.Name == kubeconfig.Users[0].Name+suffix { + if user.Name == kubeconfig.AuthInfos[0].Name+suffix { found = true break } @@ -60,7 +61,7 @@ func Test_UninstallKubeconfig(t *testing.T) { Check: core.TestCheckCombine( // no golden tests since it's os specific func(t *testing.T, ctx *core.CheckFuncCtx) { - testIfKubeconfigNotInFile(t, path.Join(os.TempDir(), "cli-uninstall-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(*k8s.Kubeconfig)) + testIfKubeconfigNotInFile(t, path.Join(os.TempDir(), "cli-uninstall-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(api.Config)) }, core.TestCheckExitCode(0), ), @@ -93,7 +94,7 @@ func Test_UninstallKubeconfig(t *testing.T) { Check: core.TestCheckCombine( // no golden tests since it's os specific func(t *testing.T, ctx *core.CheckFuncCtx) { - testIfKubeconfigNotInFile(t, path.Join(os.TempDir(), "cli-uninstall-merge-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(*k8s.Kubeconfig)) + testIfKubeconfigNotInFile(t, path.Join(os.TempDir(), "cli-uninstall-merge-test"), "-"+ctx.Meta["Cluster"].(*k8s.Cluster).ID, ctx.Meta["Kubeconfig"].(api.Config)) testIfKubeconfigInFile(t, path.Join(os.TempDir(), "cli-uninstall-merge-test"), "", testKubeconfig) }, core.TestCheckExitCode(0), diff --git a/internal/namespaces/k8s/v1/helpers_test.go b/internal/namespaces/k8s/v1/helpers_test.go index 9aa9a5769e..5f31053fa3 100644 --- a/internal/namespaces/k8s/v1/helpers_test.go +++ b/internal/namespaces/k8s/v1/helpers_test.go @@ -5,6 +5,8 @@ import ( "io/ioutil" "strings" + "github.com/ghodss/yaml" + go_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" ) @@ -34,13 +36,21 @@ func createClusterAndWaitAndKubeconfig(metaKey string, kubeconfigMetaKey string, cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) - kubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ + apiKubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ Region: cluster.Region, ClusterID: cluster.ID, }) if err != nil { return err } + + var kubeconfig go_api.Config + + err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig) + if err != nil { + return err + } + ctx.Meta[kubeconfigMetaKey] = kubeconfig return nil } @@ -55,13 +65,21 @@ func createClusterAndWaitAndInstallKubeconfig(metaKey string, kubeconfigMetaKey cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) - kubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ + apiKubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ Region: cluster.Region, ClusterID: cluster.ID, }) if err != nil { return err } + + var kubeconfig go_api.Config + + err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig) + if err != nil { + return err + } + ctx.Meta[kubeconfigMetaKey] = kubeconfig cmd = fmt.Sprintf("scw k8s kubeconfig install %s", cluster.ID) _ = ctx.ExecuteCmd(strings.Split(cmd, " ")) @@ -78,13 +96,21 @@ func createClusterAndWaitAndKubeconfigAndPopulateFile(metaKey string, kubeconfig cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) - kubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ + apiKubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ Region: cluster.Region, ClusterID: cluster.ID, }) if err != nil { return err } + + var kubeconfig go_api.Config + + err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig) + if err != nil { + return err + } + ctx.Meta[kubeconfigMetaKey] = kubeconfig err = ioutil.WriteFile(file, content, 0644) return err @@ -100,13 +126,21 @@ func createClusterAndWaitAndKubeconfigAndPopulateFileAndInstall(metaKey string, cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) - kubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ + apiKubeconfig, err := api.GetClusterKubeConfig(&k8s.GetClusterKubeConfigRequest{ Region: cluster.Region, ClusterID: cluster.ID, }) if err != nil { return err } + + var kubeconfig go_api.Config + + err = yaml.Unmarshal(apiKubeconfig.GetRaw(), &kubeconfig) + if err != nil { + return err + } + ctx.Meta[kubeconfigMetaKey] = kubeconfig err = ioutil.WriteFile(file, content, 0644) if err != nil { diff --git a/internal/namespaces/k8s/v1/testdata/test-get-kubeconfig-simple.golden b/internal/namespaces/k8s/v1/testdata/test-get-kubeconfig-simple.golden index afb1f9f0f4..ee3020abec 100644 --- a/internal/namespaces/k8s/v1/testdata/test-get-kubeconfig-simple.golden +++ b/internal/namespaces/k8s/v1/testdata/test-get-kubeconfig-simple.golden @@ -2,15 +2,15 @@ 🟩🟩🟩 STDOUT️ 🟩🟩🟩️ apiVersion: v1 clusters: -- name: clitest - cluster: +- cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01ETXpNREUzTVRNMU5Wb1hEVE13TURNek1ERTNNVE0xTlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTjl6CmhRUmZPb3gxaDJBYjRYRDVLTitldktjdU9oYWI2UnkzUkEzZEhGYS9tSGJzUmdxL010SlltTytVMEw4c1dIdXIKWmpPRlZ2ZHViVFp0WFE5eWZPeWhMZkh6UllEMDdIOVY1VTFtOW9HUGRDRlo4UC9KTHo2MzhyZkpUbzd4bG15VgpyamZGMHpZMHBxM2ZCM2JUa3JaaTF6eGtoWTVNN2c3T0tYUUNHUitIZVNyd3pSNTNlTkljQ2lTY1NYZDN6K0FJCnlrdzd1OEF1Umg1ZGU2YU9vQ3lWQy9pYUFOaFZiRWZWY20waXpMYnRobFBZSWhybHJLb0Q4VVZnZDZ3c3JUUGMKRmc5V2pHeE4rK0ZDM2UvM3F5Nlk5ZmQySXZKNklMRURSS1lCMTgya2tLczdBWmJpMDZ5U3FGaTY4b3lTNmhIZAo5VXlMdHUyTTJPR1g1WGtmVitVQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFOMEVmdlVFWUVteExmd1l5Tk9ueWRlQWYxbTMKcENscFhKWFFGVXVWTDR2RUNZaEdmMTVpN0lZcnMyOUhscHFYT3dTUklIcUIyQTFrUU1Nd3hZSmQ5K3piam40MApLVDZLU1VvSGtPV1hYcFRrZnBpdXpLWHpRRnVmV2N2U1ZhTU15Yk1sY01FV0Uyd0hTeVl1cnVFSjFySGxKT0huCkxtd093bWwyS1ZxVi9yV09xWWtwaEtEdktmSnUrSkRGM2JzTE5KVjZmbnBxZXY4ZGFQS3VpR3dMU1M4ZE5VWkQKbDhBSkx4KzVnRGJNNU1CVlczczBTNEwxQjMrczQwK0NLMXJzajFRckhOdHoyVXZ2MGVQOEErR0lZOWVmZkMzcwpROHNjZkhNdnRNcUxwM2dEdSt5NFpGaDlhNnJUNm9vWVd3Zk94Ujd4cWR5QUd3ZUs5VmV4WW1qT01Qaz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= server: https://721f2b5f-0d18-444f-8b39-460423d45e1e.api.k8s.fr-par.scw.cloud:6443 + name: clitest contexts: -- name: admin@clitest - context: +- context: cluster: clitest user: clitest-admin + name: admin@clitest current-context: admin@clitest kind: Config preferences: {} @@ -18,5 +18,6 @@ users: - name: clitest-admin user: token: uRpZoTAhyyNA7xdeEIUUPqeHbkJm34042R0kepppoJTFkMnFDZEwn4j9 + 🟩🟩🟩 JSON STDOUT 🟩🟩🟩 -"apiVersion: v1\nclusters:\n- name: clitest\n cluster:\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVn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CVlczczBTNEwxQjMrczQwK0NLMXJzajFRckhOdHoyVXZ2MGVQOEErR0lZOWVmZkMzcwpROHNjZkhNdnRNcUxwM2dEdSt5NFpGaDlhNnJUNm9vWVd3Zk94Ujd4cWR5QUd3ZUs5VmV4WW1qT01Qaz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=\n server: https://721f2b5f-0d18-444f-8b39-460423d45e1e.api.k8s.fr-par.scw.cloud:6443\ncontexts:\n- name: admin@clitest\n context:\n cluster: clitest\n user: clitest-admin\ncurrent-context: admin@clitest\nkind: Config\npreferences: {}\nusers:\n- name: clitest-admin\n user:\n token: uRpZoTAhyyNA7xdeEIUUPqeHbkJm34042R0kepppoJTFkMnFDZEwn4j9" +"apiVersion: v1\nclusters:\n- cluster:\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01ETXpNREUzTVRNMU5Wb1hEVE13TURNek1ERTNNVE0xTlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTjl6CmhRUmZPb3gxaDJBYjRYRDVLTitldktjdU9oYWI2UnkzUkEzZEhGYS9tSGJzUmdxL010SlltTytVMEw4c1dIdXIKWmpPRlZ2ZHViVFp0WFE5eWZPeWhMZkh6UllEMDdIOVY1VTFtOW9HUGRDRlo4UC9KTHo2MzhyZkpUbzd4bG15VgpyamZGMHpZMHBxM2ZCM2JUa3JaaTF6eGtoWTVNN2c3T0tYUUNHUitIZVNyd3pSNTNlTkljQ2lTY1NYZDN6K0FJCnlrdzd1OEF1Umg1ZGU2YU9vQ3lWQy9pYUFOaFZiRWZWY20waXpMYnRobFBZSWhybHJLb0Q4VVZnZDZ3c3JUUGMKRmc5V2pHeE4rK0ZDM2UvM3F5Nlk5ZmQySXZKNklMRURSS1lCMTgya2tLczdBWmJpMDZ5U3FGaTY4b3lTNmhIZAo5VXlMdHUyTTJPR1g1WGtmVitVQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFOMEVmdlVFWUVteExmd1l5Tk9ueWRlQWYxbTMKcENscFhKWFFGVXVWTDR2RUNZaEdmMTVpN0lZcnMyOUhscHFYT3dTUklIcUIyQTFrUU1Nd3hZSmQ5K3piam40MApLVDZLU1VvSGtPV1hYcFRrZnBpdXpLWHpRRnVmV2N2U1ZhTU15Yk1sY01FV0Uyd0hTeVl1cnVFSjFySGxKT0huCkxtd093bWwyS1ZxVi9yV09xWWtwaEtEdktmSnUrSkRGM2JzTE5KVjZmbnBxZXY4ZGFQS3VpR3dMU1M4ZE5VWkQKbDhBSkx4KzVnRGJNNU1CVlczczBTNEwxQjMrczQwK0NLMXJzajFRckhOdHoyVXZ2MGVQOEErR0lZOWVmZkMzcwpROHNjZkhNdnRNcUxwM2dEdSt5NFpGaDlhNnJUNm9vWVd3Zk94Ujd4cWR5QUd3ZUs5VmV4WW1qT01Qaz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=\n server: https://721f2b5f-0d18-444f-8b39-460423d45e1e.api.k8s.fr-par.scw.cloud:6443\n name: clitest\ncontexts:\n- context:\n cluster: clitest\n user: clitest-admin\n name: admin@clitest\ncurrent-context: admin@clitest\nkind: Config\npreferences: {}\nusers:\n- name: clitest-admin\n user:\n token: uRpZoTAhyyNA7xdeEIUUPqeHbkJm34042R0kepppoJTFkMnFDZEwn4j9\n"