Просмотр исходного кода

temp agent restructuring commit

Alexander Belanger 5 лет назад
Родитель
Сommit
de4e813451

+ 1 - 1
internal/forms/user.go

@@ -65,7 +65,7 @@ type UpdateUserForm struct {
 	WriteUserForm
 	ID              uint     `form:"required"`
 	RawKubeConfig   string   `json:"rawKubeConfig,omitempty"`
-	AllowedContexts []string `json:"allowedContexts,omitempty"`
+	AllowedContexts []string `json:"allowedContexts"`
 }
 
 // ToUser converts an UpdateUserForm to models.User by parsing the kubeconfig

+ 0 - 50
internal/helm/action_config.go

@@ -1,50 +0,0 @@
-package helm
-
-import (
-	"github.com/porter-dev/porter/internal/logger"
-
-	"helm.sh/helm/v3/pkg/action"
-	"helm.sh/helm/v3/pkg/kube"
-	"k8s.io/cli-runtime/pkg/genericclioptions"
-	"k8s.io/client-go/kubernetes"
-	"k8s.io/client-go/rest"
-)
-
-// NewActionConfig creates an action.Configuration, which can then be used to create Helm 3 actions.
-// Among other things, the action.Configuration controls which namespace the command is run against.
-func NewActionConfig(
-	l *logger.Logger,
-	newStorageDriver NewStorageDriver,
-	config *rest.Config,
-	clientset *kubernetes.Clientset,
-	namespace string,
-) (*action.Configuration, error) {
-	actionConfig := &action.Configuration{}
-	store := newStorageDriver(l, namespace, clientset)
-	restClientGetter := NewConfigFlagsFromCluster(namespace, config)
-	actionConfig.RESTClientGetter = restClientGetter
-	actionConfig.KubeClient = kube.New(restClientGetter)
-	actionConfig.Releases = store
-	actionConfig.Log = l.Printf
-	return actionConfig, nil
-}
-
-// NewConfigFlagsFromCluster returns ConfigFlags with default values set from within cluster.
-func NewConfigFlagsFromCluster(namespace string, clusterConfig *rest.Config) genericclioptions.RESTClientGetter {
-	impersonateGroup := []string{}
-
-	// CertFile and KeyFile must be nil for the BearerToken to be used for authentication and authorization instead of the pod's service account.
-	return &genericclioptions.ConfigFlags{
-		Insecure:         &clusterConfig.TLSClientConfig.Insecure,
-		Timeout:          stringptr("0"),
-		Namespace:        stringptr(namespace),
-		APIServer:        stringptr(clusterConfig.Host),
-		CAFile:           stringptr(clusterConfig.CAFile),
-		BearerToken:      stringptr(clusterConfig.BearerToken),
-		ImpersonateGroup: &impersonateGroup,
-	}
-}
-
-func stringptr(val string) *string {
-	return &val
-}

+ 8 - 14
internal/helm/agent.go

@@ -7,6 +7,7 @@ import (
 	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/logger"
 	"helm.sh/helm/v3/pkg/action"
+	"helm.sh/helm/v3/pkg/kube"
 	"helm.sh/helm/v3/pkg/release"
 	"helm.sh/helm/v3/pkg/storage"
 
@@ -68,20 +69,13 @@ func (h *Form) ToAgent(
 		return nil, err
 	}
 
-	// use k8s agent to create a new helm agent
-	actionConfig, err := NewActionConfig(
-		l,
-		StorageMap[h.Storage],
-		k8sAgent.ClientConfig,
-		k8sAgent.Clientset,
-		h.Namespace,
-	)
-
-	if err != nil {
-		return nil, err
-	}
-
-	return &Agent{actionConfig}, nil
+	// use k8s agent to create Helm agent
+	return &Agent{&action.Configuration{
+		RESTClientGetter: k8sAgent.RESTClientGetter,
+		KubeClient:       kube.New(k8sAgent.RESTClientGetter),
+		Releases:         StorageMap[h.Storage](l, h.Namespace, k8sAgent.Clientset),
+		Log:              l.Printf,
+	}}, nil
 }
 
 // ListReleases lists releases based on a ListFilter

+ 4 - 2
internal/helm/driver.go

@@ -8,8 +8,10 @@ package helm
 // - memory
 // - postgres
 //
-// This file implements first-class support for each driver type, and integrates with the
-// logger.
+// This file implements first-class support for the first three driver types,
+// and integrates with the logger.
+//
+// TODO -- include support for SQL storage...
 
 import (
 	"github.com/porter-dev/porter/internal/logger"

+ 6 - 4
internal/kubernetes/agent.go

@@ -5,15 +5,15 @@ import (
 
 	v1 "k8s.io/api/core/v1"
 	v1Machinery "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/cli-runtime/pkg/genericclioptions"
 	"k8s.io/client-go/kubernetes"
-	"k8s.io/client-go/rest"
 )
 
 // Agent is a Kubernetes agent for performing operations that interact with the
 // api server
 type Agent struct {
-	ClientConfig *rest.Config
-	Clientset    *kubernetes.Clientset
+	RESTClientGetter genericclioptions.RESTClientGetter
+	Clientset        *kubernetes.Clientset
 }
 
 // Form represents the options for connecting to a cluster and
@@ -22,6 +22,7 @@ type Form struct {
 	KubeConfig      []byte
 	AllowedContexts []string
 	Context         string `json:"context" form:"required"`
+	ConfigFlags     *genericclioptions.ConfigFlags
 }
 
 // ToAgent uses the Form to generate an agent
@@ -43,13 +44,14 @@ func (h *Form) ToAgent() (*Agent, error) {
 		return nil, err
 	}
 
+	restClientGetter := NewRESTClientGetterFromClientConfig(clientConf)
 	clientset, err := kubernetes.NewForConfig(clientConf)
 
 	if err != nil {
 		return nil, err
 	}
 
-	return &Agent{clientConf, clientset}, nil
+	return &Agent{restClientGetter, clientset}, nil
 }
 
 // ListNamespaces simply lists namespaces

+ 23 - 0
internal/kubernetes/helpers.go

@@ -0,0 +1,23 @@
+package kubernetes
+
+import (
+	"k8s.io/cli-runtime/pkg/genericclioptions"
+	"k8s.io/client-go/rest"
+)
+
+// NewRESTClientGetterFromClientConfig returns a RESTClientGetter using
+// default values set from the *rest.Config
+func NewRESTClientGetterFromClientConfig(conf *rest.Config) genericclioptions.RESTClientGetter {
+	cfs := genericclioptions.NewConfigFlags(false)
+
+	cfs.Insecure = &conf.Insecure
+	cfs.APIServer = stringptr(conf.Host)
+	cfs.CAFile = stringptr(conf.CAFile)
+	cfs.BearerToken = stringptr(conf.BearerToken)
+
+	return cfs
+}
+
+func stringptr(val string) *string {
+	return &val
+}