-
Notifications
You must be signed in to change notification settings - Fork 69
/
config_server.go
93 lines (78 loc) · 2.34 KB
/
config_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package server
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/empty"
api "github.com/onepanelio/core/api/gen"
v1 "github.com/onepanelio/core/pkg"
"github.com/onepanelio/core/server/auth"
)
// ConfigServer contains actions for system configuration related items
type ConfigServer struct {
api.UnimplementedConfigServiceServer
}
// NewConfigServer creates a new ConfigServer
func NewConfigServer() *ConfigServer {
return &ConfigServer{}
}
func getArtifactRepositoryBucket(client *v1.Client, namespace string) (string, error) {
if namespace == "" {
return "", nil
}
namespaceConfig, err := client.GetNamespaceConfig(namespace)
if err != nil {
return "", err
}
if namespaceConfig.ArtifactRepository.S3 != nil {
return namespaceConfig.ArtifactRepository.S3.Bucket, nil
}
return "", fmt.Errorf("unknown artifact repository")
}
// GetConfig returns the system configuration options
func (c *ConfigServer) GetConfig(ctx context.Context, req *empty.Empty) (*api.GetConfigResponse, error) {
client := getClient(ctx)
allowed, err := auth.IsAuthorized(client, "", "list", "", "namespaces", "")
if err != nil || !allowed {
return nil, err
}
sysConfig, err := client.GetSystemConfig()
if err != nil {
return nil, err
}
nodePool := &api.NodePool{
Label: *sysConfig.GetValue("applicationNodePoolLabel"),
Options: make([]*api.NodePoolOption, 0),
}
nodePoolOptions, err := sysConfig.NodePoolOptions()
if err != nil {
return nil, err
}
type ConfigServer struct{}
for _, option := range nodePoolOptions {
nodePool.Options = append(nodePool.Options, &api.NodePoolOption{
Name: option.Name,
Value: option.Value,
})
}
return &api.GetConfigResponse{
ApiUrl: sysConfig["ONEPANEL_API_URL"],
Domain: sysConfig["ONEPANEL_DOMAIN"],
Fqdn: sysConfig["ONEPANEL_FQDN"],
NodePool: nodePool,
}, err
}
// GetNamespaceConfig returns the namespace configuration
func (c *ConfigServer) GetNamespaceConfig(ctx context.Context, req *api.GetNamespaceConfigRequest) (*api.GetNamespaceConfigResponse, error) {
client := getClient(ctx)
allowed, err := auth.IsAuthorized(client, "", "get", "", "namespaces", "")
if err != nil || !allowed {
return nil, err
}
bucket, err := getArtifactRepositoryBucket(client, req.Namespace)
if err != nil {
return nil, err
}
return &api.GetNamespaceConfigResponse{
Bucket: bucket,
}, err
}