Skip to content

Commit

Permalink
feat: add plugin v2
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jan 12, 2024
1 parent 2aeacaf commit 0f3446a
Show file tree
Hide file tree
Showing 123 changed files with 18,294 additions and 470 deletions.
32 changes: 16 additions & 16 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/log"
Expand Down Expand Up @@ -72,7 +72,7 @@ type ConnectCmd struct {
kubeClientConfig clientcmd.ClientConfig
kubeClient *kubernetes.Clientset
restConfig *rest.Config
rawConfig api.Config
rawConfig clientcmdapi.Config

portForwarding bool
interruptChan chan struct{}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (cmd *ConnectCmd) connectOss(ctx context.Context, vCluster *find.VCluster,
return cmd.writeKubeConfig(kubeConfig, vCluster.Name)
}

func (cmd *ConnectCmd) writeKubeConfig(kubeConfig *api.Config, vClusterName string) error {
func (cmd *ConnectCmd) writeKubeConfig(kubeConfig *clientcmdapi.Config, vClusterName string) error {
// write kube config to buffer
out, err := clientcmd.Write(*kubeConfig)
if err != nil {
Expand All @@ -279,12 +279,12 @@ func (cmd *ConnectCmd) writeKubeConfig(kubeConfig *api.Config, vClusterName stri

// write kube config to file
if cmd.UpdateCurrent {
var clusterConfig *api.Cluster
var clusterConfig *clientcmdapi.Cluster
for _, c := range kubeConfig.Clusters {
clusterConfig = c
}

var authConfig *api.AuthInfo
var authConfig *clientcmdapi.AuthInfo
for _, a := range kubeConfig.AuthInfos {
authConfig = a
}
Expand Down Expand Up @@ -404,7 +404,7 @@ func (cmd *ConnectCmd) prepare(ctx context.Context, vCluster *find.VCluster) err
return nil
}

func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient proclient.Client, vCluster *pro.VirtualClusterInstanceProject) (*api.Config, error) {
func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient proclient.Client, vCluster *pro.VirtualClusterInstanceProject) (*clientcmdapi.Config, error) {
contextOptions, err := use.CreateVirtualClusterInstanceOptions(ctx, proClient, "", vCluster.Project.Name, vCluster.VirtualCluster, false, false, cmd.Log)
if err != nil {
return nil, fmt.Errorf("prepare vCluster kube config: %w", err)
Expand Down Expand Up @@ -463,7 +463,7 @@ func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient p

// set service account token
for k := range kubeConfig.AuthInfos {
kubeConfig.AuthInfos[k] = &api.AuthInfo{
kubeConfig.AuthInfos[k] = &clientcmdapi.AuthInfo{
Token: token,
Extensions: make(map[string]runtime.Object),
ImpersonateUserExtra: make(map[string][]string),
Expand All @@ -474,7 +474,7 @@ func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient p
return kubeConfig, nil
}

func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName string, command []string) (*api.Config, error) {
func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName string, command []string) (*clientcmdapi.Config, error) {
var err error
podName := cmd.PodName
if podName == "" {
Expand Down Expand Up @@ -587,7 +587,7 @@ func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName s

// set service account token
for k := range kubeConfig.AuthInfos {
kubeConfig.AuthInfos[k] = &api.AuthInfo{
kubeConfig.AuthInfos[k] = &clientcmdapi.AuthInfo{
Token: token,
Extensions: make(map[string]runtime.Object),
ImpersonateUserExtra: make(map[string][]string),
Expand All @@ -598,7 +598,7 @@ func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName s
return kubeConfig, nil
}

func (cmd *ConnectCmd) setServerIfExposed(ctx context.Context, vClusterName string, vClusterConfig *api.Config) error {
func (cmd *ConnectCmd) setServerIfExposed(ctx context.Context, vClusterName string, vClusterConfig *clientcmdapi.Config) error {
printedWaiting := false
err := wait.PollUntilContextTimeout(ctx, time.Second*2, time.Minute*5, true, func(ctx context.Context) (done bool, err error) {
// first check for load balancer service, look for the other service if it's not there
Expand Down Expand Up @@ -665,7 +665,7 @@ func (cmd *ConnectCmd) setServerIfExposed(ctx context.Context, vClusterName stri
// exchangeContextName switches the context name specified in the remote kubeconfig with
// the context name specified by the user. It cannot correctly handle kubeconfigs with multiple entries
// for clusters, authInfos, contexts, but ideally this is pointed at a secret created by us.
func (cmd *ConnectCmd) exchangeContextName(kubeConfig *api.Config, vclusterName string) error {
func (cmd *ConnectCmd) exchangeContextName(kubeConfig *clientcmdapi.Config, vclusterName string) error {
if cmd.KubeConfigContextName == "" {
if vclusterName != "" {
cmd.KubeConfigContextName = find.VClusterContextName(vclusterName, cmd.Namespace, cmd.rawConfig.CurrentContext)
Expand Down Expand Up @@ -712,7 +712,7 @@ func (cmd *ConnectCmd) exchangeContextName(kubeConfig *api.Config, vclusterName
return nil
}

func (cmd *ConnectCmd) executeCommand(vKubeConfig api.Config, command []string) error {
func (cmd *ConnectCmd) executeCommand(vKubeConfig clientcmdapi.Config, command []string) error {
// convert to local kube config
out, err := clientcmd.Write(vKubeConfig)
if err != nil {
Expand Down Expand Up @@ -774,7 +774,7 @@ func (cmd *ConnectCmd) executeCommand(vKubeConfig api.Config, command []string)
}
}

func (cmd *ConnectCmd) getLocalVClusterConfig(vKubeConfig api.Config) api.Config {
func (cmd *ConnectCmd) getLocalVClusterConfig(vKubeConfig clientcmdapi.Config) clientcmdapi.Config {
// wait until we can access the virtual cluster
vKubeConfig = *vKubeConfig.DeepCopy()
for k := range vKubeConfig.Clusters {
Expand All @@ -783,7 +783,7 @@ func (cmd *ConnectCmd) getLocalVClusterConfig(vKubeConfig api.Config) api.Config
return vKubeConfig
}

func (cmd *ConnectCmd) getLocalVClusterClient(vKubeConfig api.Config) (kubernetes.Interface, error) {
func (cmd *ConnectCmd) getLocalVClusterClient(vKubeConfig clientcmdapi.Config) (kubernetes.Interface, error) {
vRestConfig, err := clientcmd.NewDefaultClientConfig(cmd.getLocalVClusterConfig(vKubeConfig), &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.Wrap(err, "create virtual rest config")
Expand All @@ -797,7 +797,7 @@ func (cmd *ConnectCmd) getLocalVClusterClient(vKubeConfig api.Config) (kubernete
return vKubeClient, nil
}

func (cmd *ConnectCmd) waitForVCluster(ctx context.Context, vKubeConfig api.Config, errorChan chan error) error {
func (cmd *ConnectCmd) waitForVCluster(ctx context.Context, vKubeConfig clientcmdapi.Config, errorChan chan error) error {
vKubeClient, err := cmd.getLocalVClusterClient(vKubeConfig)
if err != nil {
return err
Expand All @@ -820,7 +820,7 @@ func (cmd *ConnectCmd) waitForVCluster(ctx context.Context, vKubeConfig api.Conf
return nil
}

func (cmd *ConnectCmd) createServiceAccountToken(ctx context.Context, vKubeConfig api.Config) (string, error) {
func (cmd *ConnectCmd) createServiceAccountToken(ctx context.Context, vKubeConfig clientcmdapi.Config) (string, error) {
vKubeClient, err := cmd.getLocalVClusterClient(vKubeConfig)
if err != nil {
return "", err
Expand Down
30 changes: 15 additions & 15 deletions cmd/vclusterctl/cmd/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"gotest.tools/v3/assert"
"k8s.io/client-go/tools/clientcmd/api"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

const (
Expand All @@ -15,16 +15,16 @@ const (
func TestExchangeContextName(t *testing.T) {
vclusterName := "vcluster-name"
namespace := "default"
defaultContext := &api.Config{
Clusters: map[string]*api.Cluster{
defaultContext := &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
defaultContextName: {Server: "foo"},
},
AuthInfos: map[string]*api.AuthInfo{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
defaultContextName: {
Token: "foo",
},
},
Contexts: map[string]*api.Context{
Contexts: map[string]*clientcmdapi.Context{
defaultContextName: {
Cluster: defaultContextName,
AuthInfo: defaultContextName,
Expand All @@ -35,23 +35,23 @@ func TestExchangeContextName(t *testing.T) {
testTable := []struct {
desc string
newContextName string
config *api.Config
expectedConfig *api.Config
config *clientcmdapi.Config
expectedConfig *clientcmdapi.Config
}{
{
desc: "KubeConfigContextName specified",
newContextName: "new-context",
config: defaultContext.DeepCopy(),
expectedConfig: &api.Config{
Clusters: map[string]*api.Cluster{
expectedConfig: &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
"new-context": {Server: "foo"},
},
AuthInfos: map[string]*api.AuthInfo{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"new-context": {
Token: "foo",
},
},
Contexts: map[string]*api.Context{
Contexts: map[string]*clientcmdapi.Context{
"new-context": {
Cluster: "new-context",
AuthInfo: "new-context",
Expand All @@ -64,16 +64,16 @@ func TestExchangeContextName(t *testing.T) {
desc: "KubeConfigContextName same as default",
newContextName: defaultContextName,
config: defaultContext.DeepCopy(),
expectedConfig: &api.Config{
Clusters: map[string]*api.Cluster{
expectedConfig: &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
defaultContextName: {Server: "foo"},
},
AuthInfos: map[string]*api.AuthInfo{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
defaultContextName: {
Token: "foo",
},
},
Contexts: map[string]*api.Context{
Contexts: map[string]*clientcmdapi.Context{
defaultContextName: {
Cluster: defaultContextName,
AuthInfo: defaultContextName,
Expand Down
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

const VirtualClusterSelector = "app=vcluster"
Expand All @@ -45,12 +45,12 @@ const (
StatusUnknown Status = "Unknown"
)

func SwitchContext(kubeConfig *api.Config, otherContext string) error {
func SwitchContext(kubeConfig *clientcmdapi.Config, otherContext string) error {
kubeConfig.CurrentContext = otherContext
return clientcmd.ModifyConfig(clientcmd.NewDefaultClientConfigLoadingRules(), *kubeConfig, false)
}

func CurrentContext() (string, *api.Config, error) {
func CurrentContext() (string, *clientcmdapi.Config, error) {
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
return "", nil, err
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/go-openapi/loads v0.21.2
github.com/google/go-github/v53 v53.2.1-0.20230815134205-bb00f570d301
github.com/gorilla/websocket v1.5.1
github.com/hashicorp/go-plugin v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/loft-sh/agentapi/v3 v3.4.0-beta.11
Expand Down Expand Up @@ -75,16 +76,21 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/cel-go v0.16.1 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect
github.com/loft-sh/admin-apis v0.0.0-20231102064836-7a6574be5f16 // indirect
github.com/loft-sh/apiserver v0.0.0-20240110162222-7923cde3a92d // indirect
github.com/loft-sh/jspolicy v0.2.2 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/otiai10/copy v1.11.0 // indirect
Expand Down
Loading

0 comments on commit 0f3446a

Please sign in to comment.