From d314502bf2cd6cb6055805e84056381adb956161 Mon Sep 17 00:00:00 2001 From: Sekiranda Hamza Date: Wed, 30 Mar 2022 12:06:57 +0300 Subject: [PATCH 1/4] Draft commit. Signed-off-by: Sekiranda Hamza sekirandahamza@gmail.com Signed-off-by: Sekiranda Hamza --- go.mod | 1 + go.sum | 6 ++--- utils/kubernetes/kubeconfig.go | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 5c487d08..7f08b436 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ replace ( ) require ( + github.com/aws/aws-sdk-go v1.34.9 github.com/evanphx/json-patch v4.12.0+incompatible // indirect github.com/go-git/go-git/v5 v5.4.2 github.com/go-logr/logr v0.4.0 diff --git a/go.sum b/go.sum index 240a3a85..fa4bb016 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,7 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY= github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0= +github.com/aws/aws-sdk-go v1.34.9 h1:cUGBW9CVdi0mS7K1hDzxIqTpfeWhpoQiguq81M1tjK0= github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= @@ -596,6 +597,7 @@ github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc= github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= github.com/jmoiron/sqlx v1.3.1 h1:aLN7YINNZ7cYOPK3QC83dbM6KT0NMqVMw961TqrejlE= github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= @@ -751,12 +753,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296 h1:vU9tpM3apjYlLLeY23zRWJ9Zktr5jp+mloR942LEOpY= github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= -github.com/nats-io/nats-server/v2 v2.7.3 h1:P0NgsnbTxrPMMPZ1/rLXWjS5bbPpRMCcPwlMd4nBDK4= -github.com/nats-io/nats-server/v2 v2.7.3/go.mod h1:eJUrA5gm0ch6sJTEv85xmXIgQWsB0OyjkTsKXvlHbYc= github.com/nats-io/nats-server/v2 v2.7.4 h1:c+BZJ3rGzUKCBIM4IXO8uNT2u1vajGbD1kPA6wqCEaM= github.com/nats-io/nats-server/v2 v2.7.4/go.mod h1:1vZ2Nijh8tcyNe8BDVyTviCd9NYzRbubQYiEHsvOQWc= -github.com/nats-io/nats.go v1.13.1-0.20220121202836-972a071d373d h1:GRSmEJutHkdoxKsRypP575IIdoXe7Bm6yHQF6GcDBnA= -github.com/nats-io/nats.go v1.13.1-0.20220121202836-972a071d373d/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d h1:zJf4l8Kp67RIZhoVeniSLZs69SHNgjLHz0aNsqPPlx8= github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8= diff --git a/utils/kubernetes/kubeconfig.go b/utils/kubernetes/kubeconfig.go index 8422e922..b445df51 100644 --- a/utils/kubernetes/kubeconfig.go +++ b/utils/kubernetes/kubeconfig.go @@ -1,9 +1,16 @@ package kubernetes import ( + "log" "os" "path/filepath" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/eks" + "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + "github.com/layer5io/meshkit/models" "github.com/layer5io/meshkit/utils" "github.com/pkg/errors" @@ -38,3 +45,39 @@ func (c *Client) GetCurrentContext() (string, error) { return config.CurrentContext, nil } + +func WriteEKSConfig(clusterName, region, namespace string) error { + sess := session.Must(session.NewSession(&aws.Config{ + Region: aws.String(region), + })) + eksSvc := eks.New(sess) + + input := &eks.DescribeClusterInput{ + Name: aws.String(clusterName), + } + result, err := eksSvc.DescribeCluster(input) + if err != nil { + log.Fatalf("Error calling DescribeCluster: %v", err) + } + + cname := *result.Cluster.Arn + endpt := *result.Cluster.Endpoint + clusters := make(map[string]*clientcmdapi.Cluster) + clusters[clusterName] = &clientcmdapi.Cluster{ + Server: endpt, + CertificateAuthorityData: []byte(*result.Cluster.CertificateAuthority.Data), + } + contexts := make(map[string]*clientcmdapi.Context) + contexts[cname] = &clientcmdapi.Context{ + Cluster: cname, + } + // Can be switched for k8s.io/client-go/rest.Config + clientConfig := clientcmdapi.Config{ + Kind: "Config", + APIVersion: "v1", + Clusters: clusters, + Contexts: contexts, + CurrentContext: cname, + } + clientcmd.WriteToFile(clientConfig, namespace+utils.ConfigPath) +} From 465515e586c79af25c48fade47ae6bcbe2bb5327 Mon Sep 17 00:00:00 2001 From: Sekiranda Hamza Date: Thu, 31 Mar 2022 03:34:26 +0300 Subject: [PATCH 2/4] Draft commit1. Signed-off-by: Sekiranda Hamza sekirandahamza@gmail.com Signed-off-by: Sekiranda Hamza --- utils/kubernetes/kubeconfig.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/utils/kubernetes/kubeconfig.go b/utils/kubernetes/kubeconfig.go index b445df51..9935480f 100644 --- a/utils/kubernetes/kubeconfig.go +++ b/utils/kubernetes/kubeconfig.go @@ -46,7 +46,7 @@ func (c *Client) GetCurrentContext() (string, error) { return config.CurrentContext, nil } -func WriteEKSConfig(clusterName, region, namespace string) error { +func WriteEKSConfig(clusterName, region, configPath string) error { sess := session.Must(session.NewSession(&aws.Config{ Region: aws.String(region), })) @@ -71,7 +71,7 @@ func WriteEKSConfig(clusterName, region, namespace string) error { contexts[cname] = &clientcmdapi.Context{ Cluster: cname, } - // Can be switched for k8s.io/client-go/rest.Config + clientConfig := clientcmdapi.Config{ Kind: "Config", APIVersion: "v1", @@ -79,5 +79,8 @@ func WriteEKSConfig(clusterName, region, namespace string) error { Contexts: contexts, CurrentContext: cname, } - clientcmd.WriteToFile(clientConfig, namespace+utils.ConfigPath) + if err := clientcmd.WriteToFile(clientConfig, configPath); err != nil { + return err + } + return nil } From 6ef9cc93ab7674b38b33d1604eb8cae021120875 Mon Sep 17 00:00:00 2001 From: Sekiranda Hamza Date: Sun, 3 Apr 2022 22:24:47 +0300 Subject: [PATCH 3/4] Added warning message. Signed-off-by: Sekiranda Hamza sekirandahamza@gmail.com Signed-off-by: Sekiranda Hamza --- utils/kubernetes/kubeconfig.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/utils/kubernetes/kubeconfig.go b/utils/kubernetes/kubeconfig.go index 9935480f..996b37dd 100644 --- a/utils/kubernetes/kubeconfig.go +++ b/utils/kubernetes/kubeconfig.go @@ -1,6 +1,7 @@ package kubernetes import ( + "io/ioutil" "log" "os" "path/filepath" @@ -79,8 +80,20 @@ func WriteEKSConfig(clusterName, region, configPath string) error { Contexts: contexts, CurrentContext: cname, } + + prevConfigbytes, err := ioutil.ReadFile(configPath) + if err != nil { + return err + } + tmpArch := "tmp/kube/config" + err = os.WriteFile(tmpArch, prevConfigbytes, 0644) + if err != nil { + return err + } + log.Printf("Warning: Overwriting previous config, archived config at %s", tmpArch) if err := clientcmd.WriteToFile(clientConfig, configPath); err != nil { return err } + // O_CREATE O_TRUNC return nil } From 739ea5abb2bc0f575ffbb6af510e5fdf63365ba9 Mon Sep 17 00:00:00 2001 From: Sekiranda Hamza Date: Tue, 12 Apr 2022 14:35:02 +0300 Subject: [PATCH 4/4] Added time to filename. Signed-off-by: Sekiranda Hamza sekirandahamza@gmail.com Signed-off-by: Sekiranda Hamza --- utils/kubernetes/kubeconfig.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/utils/kubernetes/kubeconfig.go b/utils/kubernetes/kubeconfig.go index 996b37dd..c188fcd0 100644 --- a/utils/kubernetes/kubeconfig.go +++ b/utils/kubernetes/kubeconfig.go @@ -1,10 +1,11 @@ package kubernetes import ( + "fmt" "io/ioutil" - "log" "os" "path/filepath" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" @@ -15,6 +16,7 @@ import ( "github.com/layer5io/meshkit/models" "github.com/layer5io/meshkit/utils" "github.com/pkg/errors" + logger "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) @@ -58,7 +60,7 @@ func WriteEKSConfig(clusterName, region, configPath string) error { } result, err := eksSvc.DescribeCluster(input) if err != nil { - log.Fatalf("Error calling DescribeCluster: %v", err) + return errors.New(fmt.Sprintf("Error calling DescribeCluster: %v", err)) } cname := *result.Cluster.Arn @@ -70,7 +72,8 @@ func WriteEKSConfig(clusterName, region, configPath string) error { } contexts := make(map[string]*clientcmdapi.Context) contexts[cname] = &clientcmdapi.Context{ - Cluster: cname, + Cluster: cname, + AuthInfo: cname, } clientConfig := clientcmdapi.Config{ @@ -85,12 +88,18 @@ func WriteEKSConfig(clusterName, region, configPath string) error { if err != nil { return err } - tmpArch := "tmp/kube/config" + t := time.Now().Local().Format(time.RFC3339) + // Need for a common project wide directory to dump these archived config + err = os.MkdirAll("/tmp/meshery/", os.ModePerm) + if err != nil { + return err + } + tmpArch := "tmp/meshery/config" + t + ".yaml" err = os.WriteFile(tmpArch, prevConfigbytes, 0644) if err != nil { return err } - log.Printf("Warning: Overwriting previous config, archived config at %s", tmpArch) + logger.WithFields(logger.Fields{"archived": tmpArch}).Warn("Overwriting previous config, archived config at %s", tmpArch) if err := clientcmd.WriteToFile(clientConfig, configPath); err != nil { return err }