mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-10-05 13:46:51 +08:00
v1.Client cleanup
This commit is contained in:
33
main.go
33
main.go
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@@ -38,7 +37,11 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
kubeConfig := v1.NewConfig()
|
kubeConfig := v1.NewConfig()
|
||||||
config, err := getSystemConfig(kubeConfig)
|
client, err := v1.NewClient(kubeConfig, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
config, err := client.GetSystemConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to get system config: %v", err)
|
log.Fatalf("Failed to get system config: %v", err)
|
||||||
}
|
}
|
||||||
@@ -54,32 +57,6 @@ func main() {
|
|||||||
startHTTPProxy()
|
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) {
|
func startRPCServer(db *v1.DB, kubeConfig *v1.Config) {
|
||||||
log.Printf("Starting RPC server on port %v", *rpcPort)
|
log.Printf("Starting RPC server on port %v", *rpcPort)
|
||||||
lis, err := net.Listen("tcp", *rpcPort)
|
lis, err := net.Listen("tcp", *rpcPort)
|
||||||
|
@@ -46,26 +46,14 @@ func NewConfig() (config *Config) {
|
|||||||
return
|
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) {
|
func NewClient(config *Config, db *sqlx.DB) (client *Client, err error) {
|
||||||
config.BearerTokenFile = ""
|
if config.BearerToken != "" {
|
||||||
config.Username = ""
|
config.BearerTokenFile = ""
|
||||||
config.Password = ""
|
config.Username = ""
|
||||||
config.CertData = nil
|
config.Password = ""
|
||||||
config.CertFile = ""
|
config.CertData = nil
|
||||||
|
config.CertFile = ""
|
||||||
|
}
|
||||||
|
|
||||||
kubeClient, err := kubernetes.NewForConfig(config)
|
kubeClient, err := kubernetes.NewForConfig(config)
|
||||||
if err != nil {
|
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
|
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")
|
configMap, err := c.GetConfigMap(namespace, "onepanel")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
@@ -107,7 +115,7 @@ func (c *Client) getNamespaceConfig(namespace string) (config map[string]string,
|
|||||||
return
|
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])
|
insecure, err := strconv.ParseBool(config[artifactRepositoryInSecureKey])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
|
@@ -490,7 +490,7 @@ func (c *Client) GetWorkflowExecutionLogs(namespace, name, podName, containerNam
|
|||||||
)
|
)
|
||||||
|
|
||||||
if wf.Status.Nodes[podName].Completed() {
|
if wf.Status.Nodes[podName].Completed() {
|
||||||
config, err = c.getNamespaceConfig(namespace)
|
config, err = c.GetNamespaceConfig(namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"Namespace": namespace,
|
"Namespace": namespace,
|
||||||
@@ -502,7 +502,7 @@ func (c *Client) GetWorkflowExecutionLogs(namespace, name, podName, containerNam
|
|||||||
return nil, util.NewUserError(codes.PermissionDenied, "Can't get configuration.")
|
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 {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"Namespace": namespace,
|
"Namespace": namespace,
|
||||||
@@ -576,7 +576,7 @@ func (c *Client) GetWorkflowExecutionMetrics(namespace, name, podName string) (m
|
|||||||
config map[string]string
|
config map[string]string
|
||||||
)
|
)
|
||||||
|
|
||||||
config, err = c.getNamespaceConfig(namespace)
|
config, err = c.GetNamespaceConfig(namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"Namespace": namespace,
|
"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.")
|
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 {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"Namespace": namespace,
|
"Namespace": namespace,
|
||||||
|
Reference in New Issue
Block a user