Skip to content

Commit

Permalink
add deployments clusters get-credentials command (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm authored Oct 28, 2023
1 parent 83f9c72 commit 7cb22f6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
63 changes: 63 additions & 0 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 7cb22f6

Please sign in to comment.