From 7cb22f6b95cac243b8eaa0c5e3c99b3e86e42113 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Sat, 28 Oct 2023 04:11:55 +0200 Subject: [PATCH] add deployments clusters get-credentials command (#467) --- cmd/plural/cd.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 6 ++--- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/cmd/plural/cd.go b/cmd/plural/cd.go index 5720c0dd..ced48c41 100644 --- a/cmd/plural/cd.go +++ b/cmd/plural/cd.go @@ -219,6 +219,12 @@ func (p *Plural) cdClusterCommands() []cli.Command { cli.StringFlag{Name: "kubeconf-context", Usage: "the kubeconfig context you want to use. If not specified, the current one will be used"}, }, }, + { + Name: "get-credentials", + Action: latestVersion(requireArgs(p.handleGetClusterCredentials, []string{"CLUSTER_ID"})), + Usage: "updates kubeconfig file with appropriate credentials to point to specified cluster", + ArgsUsage: "CLUSTER_ID", + }, } } @@ -694,7 +700,64 @@ func (p *Plural) handleUpdateCluster(c *cli.Context) error { } return []string{cl.ID, cl.Name, handle, *cl.Version, provider}, nil }) +} + +func (p *Plural) handleGetClusterCredentials(c *cli.Context) error { + if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { + return err + } + + cluster, err := p.ConsoleClient.GetCluster(getIdAndName(c.Args().Get(0))) + if err != nil { + return err + } + if cluster == nil { + return fmt.Errorf("cluster is nil") + } + + return buildKubeconfig(cluster) +} + +func buildKubeconfig(cluster *gqlclient.ClusterFragment) error { + configAccess := clientcmd.NewDefaultPathOptions() + config, err := configAccess.GetStartingConfig() + if err != nil { + return fmt.Errorf("cannot read kubeconfig: %v", err) + } + if config == nil { + config = &clientcmdapi.Config{} + } + + // TODO: Set CertificateAuthority. + configCluster := clientcmdapi.NewCluster() + configCluster.Server = *cluster.KasURL + if config.Clusters == nil { + config.Clusters = make(map[string]*clientcmdapi.Cluster) + } + config.Clusters[cluster.Name] = configCluster + + configAuthInfo := clientcmdapi.NewAuthInfo() + configAuthInfo.Token = fmt.Sprintf("plrl:%s:%s", cluster.ID, consoleToken) + if config.AuthInfos == nil { + config.AuthInfos = make(map[string]*clientcmdapi.AuthInfo) + } + config.AuthInfos[cluster.Name] = configAuthInfo + + configContext := clientcmdapi.NewContext() + configContext.Cluster = cluster.Name + configContext.AuthInfo = cluster.Name + if config.Contexts == nil { + config.Contexts = make(map[string]*clientcmdapi.Context) + } + config.Contexts[cluster.Name] = configContext + + config.CurrentContext = cluster.Name + + if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil { + return err + } + fmt.Printf("set your kubectl context to %s\n", cluster.Name) return nil } diff --git a/go.mod b/go.mod index 1f5d69d9..efe0480c 100644 --- a/go.mod +++ b/go.mod @@ -53,7 +53,7 @@ require ( github.com/packethost/packngo v0.29.0 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 github.com/pluralsh/cluster-api-migration v0.2.15 - github.com/pluralsh/console-client-go v0.0.20 + github.com/pluralsh/console-client-go v0.0.23 github.com/pluralsh/gqlclient v1.10.0 github.com/pluralsh/plural-operator v0.5.5 github.com/pluralsh/polly v0.1.1 diff --git a/go.sum b/go.sum index b1263be5..e0afe6de 100644 --- a/go.sum +++ b/go.sum @@ -1419,10 +1419,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pluralsh/cluster-api-migration v0.2.15 h1:TIfusD+wnhZTGmwNfIlKlKJOT2dE3rUaZawDJw98GjY= github.com/pluralsh/cluster-api-migration v0.2.15/go.mod h1:J6lEvC/70KouikX16mE331cxc3y3sBwtmfHGwZqu06w= -github.com/pluralsh/console-client-go v0.0.18 h1:GhfThwExfyQbU+NjMLdxH0KPuVJjdsI5NdK0cdTcakA= -github.com/pluralsh/console-client-go v0.0.18/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU= -github.com/pluralsh/console-client-go v0.0.20 h1:1VrFYctcVGQ9K0q1SlThJtxX03vP2rAZHoChFUOY2JM= -github.com/pluralsh/console-client-go v0.0.20/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU= +github.com/pluralsh/console-client-go v0.0.23 h1:BH+uZDeSo6P4xqtc6hdZnEYST0SZs5AEQJwGHxlrYco= +github.com/pluralsh/console-client-go v0.0.23/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU= github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E= github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s= github.com/pluralsh/gqlclient v1.10.0 h1:ccYB+A0JbPYkEeVzdfajd29l65N6x/buSKPMMxM8OIA=