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

Commit

Permalink
Add --region to kube command
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <darren@acorn.io>
  • Loading branch information
ibuildthecloud committed Aug 11, 2023
1 parent 63b93a6 commit 14feb1e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
6 changes: 5 additions & 1 deletion pkg/cli/kube.go
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"

cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
"github.com/spf13/cobra"
)

Expand All @@ -26,6 +27,7 @@ acorn -j acorn kube k9s

type Kube struct {
client ClientFactory
Region string `usage:"Get access to the cluster supporting that specific region"`
}

func (s *Kube) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -37,7 +39,9 @@ func (s *Kube) Run(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

server, err := c.KubeProxyAddress(ctx)
server, err := c.KubeProxyAddress(ctx, &client.KubeProxyAddressOptions{
Region: s.Region,
})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/testdata/MockClient.go
Expand Up @@ -119,7 +119,7 @@ type MockClient struct {
EventItem *apiv1.Event
}

func (m *MockClient) KubeProxyAddress(ctx context.Context) (string, error) {
func (m *MockClient) KubeProxyAddress(ctx context.Context, opts *client.KubeProxyAddressOptions) (string, error) {
//TODO implement me
panic("implement me")
}
Expand Down
65 changes: 62 additions & 3 deletions pkg/client/client.go
@@ -1,7 +1,11 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
Expand All @@ -20,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -277,7 +282,7 @@ type Client interface {
GetProject() string
GetNamespace() string
GetClient() (kclient.WithWatch, error)
KubeProxyAddress(ctx context.Context) (string, error)
KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error)
}

type CredentialLookup func(ctx context.Context, serverAddress string) (*apiv1.RegistryAuth, bool, error)
Expand Down Expand Up @@ -346,6 +351,10 @@ type ContainerReplicaListOptions struct {
App string `json:"app,omitempty"`
}

type KubeProxyAddressOptions struct {
Region string `json:"region,omitempty"`
}

type EventStreamOptions struct {
Tail int `json:"tail,omitempty"`
Follow bool `json:"follow,omitempty"`
Expand Down Expand Up @@ -399,8 +408,58 @@ type DefaultClient struct {
Dialer *k8schannel.Dialer
}

func (c *DefaultClient) KubeProxyAddress(ctx context.Context) (string, error) {
handler, err := proxy.Handler(c.RESTConfig)
func (c *DefaultClient) getRESTConfig(ctx context.Context, opts *KubeProxyAddressOptions) (*rest.Config, error) {
if opts == nil || opts.Region == "" {
return c.RESTConfig, nil
}

client, err := rest.HTTPClientFor(c.RESTConfig)
if err != nil {
return nil, err
}

data, err := json.Marshal(map[string]any{
"kind": "AccessConfig",
"apiVersion": "account.manager.acorn.io/v1",
"regionName": opts.Region,
})
if err != nil {
return nil, err
}
// Could this be prettier
resp, err := client.Post(c.RESTConfig.Host+"/apis/account.manager.acorn.io/v1/accessconfigs",
"application/json", bytes.NewBuffer(data))
if err != nil {
return nil, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
_ = resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("invalid response looking up kubeconfig %d: %s", resp.StatusCode, body)
}

parsed := struct {
Config []byte
}{}
if err := json.Unmarshal(body, &parsed); err != nil {
return nil, err
}

return clientcmd.RESTConfigFromKubeConfig(parsed.Config)
}

func (c *DefaultClient) KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error) {
restConfig, err := c.getRESTConfig(ctx, opts)
if err != nil {
return "", err
}

handler, err := proxy.Handler(restConfig)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/deferred.go
Expand Up @@ -459,9 +459,9 @@ func (d *DeferredClient) GetClient() (client.WithWatch, error) {
return d.Client.GetClient()
}

func (d *DeferredClient) KubeProxyAddress(ctx context.Context) (string, error) {
func (d *DeferredClient) KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error) {
if err := d.create(); err != nil {
return "", err
}
return d.Client.KubeProxyAddress(ctx)
return d.Client.KubeProxyAddress(ctx, opts)
}
4 changes: 2 additions & 2 deletions pkg/client/multi.go
Expand Up @@ -656,10 +656,10 @@ func (m *MultiClient) GetClient() (kclient.WithWatch, error) {
return c.GetClient()
}

func (m *MultiClient) KubeProxyAddress(ctx context.Context) (string, error) {
func (m *MultiClient) KubeProxyAddress(ctx context.Context, opts *KubeProxyAddressOptions) (string, error) {
c, err := m.Factory.ForProject(context.Background(), m.Factory.DefaultProject())
if err != nil {
panic(err)
}
return c.KubeProxyAddress(ctx)
return c.KubeProxyAddress(ctx, opts)
}
8 changes: 4 additions & 4 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 14feb1e

Please sign in to comment.