diff --git a/main.go b/main.go index c2629cd..708fb1f 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/base64" "flag" "fmt" "net" @@ -38,7 +37,11 @@ func main() { flag.Parse() kubeConfig := v1.NewConfig() - config, err := getSystemConfig(kubeConfig) + client, err := v1.NewClient(kubeConfig, nil) + if err != nil { + return + } + config, err := client.GetSystemConfig() if err != nil { log.Fatalf("Failed to get system config: %v", err) } @@ -54,32 +57,6 @@ func main() { startHTTPProxy() } -// TODO: Move this to v1.Client -func getSystemConfig(kubeConfig *v1.Config) (config map[string]string, err error) { - namespace := "onepanel" - client, err := v1.NewServerClient(kubeConfig) - if err != nil { - return - } - - configMap, err := client.GetConfigMap(namespace, "onepanel") - if err != nil { - return - } - config = configMap.Data - - secret, err := client.GetSecret(namespace, "onepanel") - if err != nil { - return - } - databaseUsername, _ := base64.StdEncoding.DecodeString(secret.Data["databaseUsername"]) - config["databaseUsername"] = string(databaseUsername) - databasePassword, _ := base64.StdEncoding.DecodeString(secret.Data["databaseUsername"]) - config["databasePassword"] = string(databasePassword) - - return -} - func startRPCServer(db *v1.DB, kubeConfig *v1.Config) { log.Printf("Starting RPC server on port %v", *rpcPort) lis, err := net.Listen("tcp", *rpcPort) diff --git a/pkg/client.go b/pkg/client.go index 23c56ba..7f74dd1 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -46,26 +46,14 @@ func NewConfig() (config *Config) { return } -func NewServerClient(config *Config) (client *Client, err error) { - kubeClient, err := kubernetes.NewForConfig(config) - if err != nil { - return - } - - argoClient, err := argoprojv1alpha1.NewForConfig(config) - if err != nil { - return - } - - return &Client{Interface: kubeClient, argoprojV1alpha1: argoClient}, nil -} - func NewClient(config *Config, db *sqlx.DB) (client *Client, err error) { - config.BearerTokenFile = "" - config.Username = "" - config.Password = "" - config.CertData = nil - config.CertFile = "" + if config.BearerToken != "" { + config.BearerTokenFile = "" + config.Username = "" + config.Password = "" + config.CertData = nil + config.CertFile = "" + } kubeClient, err := kubernetes.NewForConfig(config) if err != nil { @@ -80,7 +68,27 @@ func NewClient(config *Config, db *sqlx.DB) (client *Client, err error) { return &Client{Interface: kubeClient, argoprojV1alpha1: argoClient, DB: db}, nil } -func (c *Client) getNamespaceConfig(namespace string) (config map[string]string, err error) { +func (c *Client) GetSystemConfig() (config map[string]string, err error) { + namespace := "onepanel" + configMap, err := c.GetConfigMap(namespace, "onepanel") + if err != nil { + return + } + config = configMap.Data + + secret, err := c.GetSecret(namespace, "onepanel") + if err != nil { + return + } + databaseUsername, _ := base64.StdEncoding.DecodeString(secret.Data["databaseUsername"]) + config["databaseUsername"] = string(databaseUsername) + databasePassword, _ := base64.StdEncoding.DecodeString(secret.Data["databaseUsername"]) + config["databasePassword"] = string(databasePassword) + + return +} + +func (c *Client) GetNamespaceConfig(namespace string) (config map[string]string, err error) { configMap, err := c.GetConfigMap(namespace, "onepanel") if err != nil { log.WithFields(log.Fields{ @@ -107,7 +115,7 @@ func (c *Client) getNamespaceConfig(namespace string) (config map[string]string, return } -func (c *Client) getS3Client(namespace string, config map[string]string) (s3Client *s3.Client, err error) { +func (c *Client) GetS3Client(namespace string, config map[string]string) (s3Client *s3.Client, err error) { insecure, err := strconv.ParseBool(config[artifactRepositoryInSecureKey]) if err != nil { log.WithFields(log.Fields{ diff --git a/pkg/workflow_execution.go b/pkg/workflow_execution.go index 62f44ca..91aa68d 100644 --- a/pkg/workflow_execution.go +++ b/pkg/workflow_execution.go @@ -490,7 +490,7 @@ func (c *Client) GetWorkflowExecutionLogs(namespace, name, podName, containerNam ) if wf.Status.Nodes[podName].Completed() { - config, err = c.getNamespaceConfig(namespace) + config, err = c.GetNamespaceConfig(namespace) if err != nil { log.WithFields(log.Fields{ "Namespace": namespace, @@ -502,7 +502,7 @@ func (c *Client) GetWorkflowExecutionLogs(namespace, name, podName, containerNam return nil, util.NewUserError(codes.PermissionDenied, "Can't get configuration.") } - s3Client, err = c.getS3Client(namespace, config) + s3Client, err = c.GetS3Client(namespace, config) if err != nil { log.WithFields(log.Fields{ "Namespace": namespace, @@ -576,7 +576,7 @@ func (c *Client) GetWorkflowExecutionMetrics(namespace, name, podName string) (m config map[string]string ) - config, err = c.getNamespaceConfig(namespace) + config, err = c.GetNamespaceConfig(namespace) if err != nil { log.WithFields(log.Fields{ "Namespace": namespace, @@ -587,7 +587,7 @@ func (c *Client) GetWorkflowExecutionMetrics(namespace, name, podName string) (m return nil, util.NewUserError(codes.PermissionDenied, "Can't get configuration.") } - s3Client, err = c.getS3Client(namespace, config) + s3Client, err = c.GetS3Client(namespace, config) if err != nil { log.WithFields(log.Fields{ "Namespace": namespace,