Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add -w to acorn kube to write kubeconfig to a file
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <[email protected]>
  • Loading branch information
ibuildthecloud committed Aug 11, 2023
1 parent 0f63ad9 commit f4a2b81
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/cli/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
func NewKubectl(c CommandContext) *cobra.Command {
cmd := cli.Command(&Kube{client: c.ClientFactory}, cobra.Command{
Use: "kube [flags]",
Args: cobra.MinimumNArgs(1),
Hidden: true,
SilenceUsage: true,
Short: "Run command with KUBECONFIG env set to a generated kubeconfig of the current project",
Expand All @@ -26,8 +25,9 @@ acorn -j acorn kube k9s
}

type Kube struct {
client ClientFactory
Region string `usage:"Get access to the cluster supporting that specific region"`
client ClientFactory
Region string `usage:"Get access to the cluster supporting that specific region"`
WriteFile string `usage:"Write kubeconfig to file" short:"w"`
}

func (s *Kube) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -36,6 +36,16 @@ func (s *Kube) Run(cmd *cobra.Command, args []string) error {
return err
}

if s.WriteFile != "" {
data, err := c.KubeConfig(cmd.Context(), &client.KubeProxyAddressOptions{
Region: s.Region,
})
if err != nil {
return err
}
return os.WriteFile(s.WriteFile, data, 0644)
}

ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

Expand Down Expand Up @@ -78,6 +88,10 @@ users:
return err
}

if len(args) == 0 {
args = []string{os.Getenv("SHELL")}
}

k := exec.Command(args[0], args[1:]...)
k.Env = append(os.Environ(), fmt.Sprintf("KUBECONFIG=%s", f.Name()))
k.Stdin = os.Stdin
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/testdata/MockClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ type MockClient struct {
EventItem *apiv1.Event
}

func (m *MockClient) KubeConfig(ctx context.Context, opts *client.KubeProxyAddressOptions) ([]byte, error) {
//TODO implement me
panic("implement me")
}

func (m *MockClient) KubeProxyAddress(ctx context.Context, opts *client.KubeProxyAddressOptions) (string, error) {
//TODO implement me
panic("implement me")
Expand Down
36 changes: 36 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -282,7 +283,9 @@ type Client interface {
GetProject() string
GetNamespace() string
GetClient() (kclient.WithWatch, error)

KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error)
KubeConfig(ctx context.Context, opts *KubeProxyAddressOptions) ([]byte, error)
}

type CredentialLookup func(ctx context.Context, serverAddress string) (*apiv1.RegistryAuth, bool, error)
Expand Down Expand Up @@ -408,6 +411,31 @@ type DefaultClient struct {
Dialer *k8schannel.Dialer
}

func generateKubeConfig(restConfig *rest.Config) ([]byte, error) {
config := &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
"acorn": {
Server: restConfig.Host,
CertificateAuthorityData: restConfig.TLSClientConfig.CAData,
},
},
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"auth": {
Token: restConfig.BearerToken,
},
},
Contexts: map[string]*clientcmdapi.Context{
"default": {
Cluster: "acorn",
AuthInfo: "auth",
},
},
CurrentContext: "default",
}

return clientcmd.Write(*config)
}

func (c *DefaultClient) getRESTConfig(ctx context.Context, opts *KubeProxyAddressOptions) (*rest.Config, error) {
if opts == nil || opts.Region == "" {
return c.RESTConfig, nil
Expand Down Expand Up @@ -453,6 +481,14 @@ func (c *DefaultClient) getRESTConfig(ctx context.Context, opts *KubeProxyAddres
return clientcmd.RESTConfigFromKubeConfig(parsed.Config)
}

func (c *DefaultClient) KubeConfig(ctx context.Context, opts *KubeProxyAddressOptions) ([]byte, error) {
restConfig, err := c.getRESTConfig(ctx, opts)
if err != nil {
return nil, err
}
return generateKubeConfig(restConfig)
}

func (c *DefaultClient) KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error) {
restConfig, err := c.getRESTConfig(ctx, opts)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/deferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ func (d *DeferredClient) GetClient() (client.WithWatch, error) {
return d.Client.GetClient()
}

func (d *DeferredClient) KubeConfig(ctx context.Context, opts *KubeProxyAddressOptions) ([]byte, error) {
if err := d.create(); err != nil {
return nil, err
}
return d.Client.KubeConfig(ctx, opts)
}

func (d *DeferredClient) KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error) {
if err := d.create(); err != nil {
return "", err
Expand Down
8 changes: 8 additions & 0 deletions pkg/client/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ func (m *MultiClient) GetClient() (kclient.WithWatch, error) {
return c.GetClient()
}

func (m *MultiClient) KubeConfig(ctx context.Context, opts *KubeProxyAddressOptions) ([]byte, error) {
c, err := m.Factory.ForProject(context.Background(), m.Factory.DefaultProject())
if err != nil {
panic(err)
}
return c.KubeConfig(ctx, opts)
}

func (m *MultiClient) KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error) {
c, err := m.Factory.ForProject(context.Background(), m.Factory.DefaultProject())
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/mocks/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f4a2b81

Please sign in to comment.