-
Notifications
You must be signed in to change notification settings - Fork 54
/
config.go
146 lines (115 loc) · 4.04 KB
/
config.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package config
import (
"errors"
"fmt"
"time"
"code.cloudfoundry.org/korifi/tools"
"go.uber.org/zap/zapcore"
"k8s.io/client-go/rest"
)
const (
defaultExternalProtocol = "https"
OrgRole RoleLevel = "org"
SpaceRole RoleLevel = "space"
)
type (
APIConfig struct {
InternalPort int `yaml:"internalPort"`
IdleTimeout int `yaml:"idleTimeout"`
ReadTimeout int `yaml:"readTimeout"`
ReadHeaderTimeout int `yaml:"readHeaderTimeout"`
WriteTimeout int `yaml:"writeTimeout"`
ExternalFQDN string `yaml:"externalFQDN"`
ExternalPort int `yaml:"externalPort"`
ServerURL string
RootNamespace string `yaml:"rootNamespace"`
BuilderName string `yaml:"builderName"`
ContainerRepositoryPrefix string `yaml:"containerRepositoryPrefix"`
ContainerRegistryType string `yaml:"containerRegistryType"`
PackageRegistrySecretName string `yaml:"packageRegistrySecretName"`
DefaultDomainName string `yaml:"defaultDomainName"`
UserCertificateExpirationWarningDuration string `yaml:"userCertificateExpirationWarningDuration"`
DefaultLifecycleConfig DefaultLifecycleConfig `yaml:"defaultLifecycleConfig"`
RoleMappings map[string]Role `yaml:"roleMappings"`
AuthProxyHost string `yaml:"authProxyHost"`
AuthProxyCACert string `yaml:"authProxyCACert"`
LogLevel zapcore.Level `yaml:"logLevel"`
}
RoleLevel string
Role struct {
Name string `yaml:"name"`
Level RoleLevel `yaml:"level"`
Propagate bool `yaml:"propagate"`
}
// DefaultLifecycleConfig contains default values of the Lifecycle block of CFApps and Builds created by the Shim
DefaultLifecycleConfig struct {
Type string `yaml:"type"`
Stack string `yaml:"stack"`
StagingMemoryMB int `yaml:"stagingMemoryMB"`
StagingDiskMB int `yaml:"stagingDiskMB"`
}
)
func LoadFromPath(path string) (*APIConfig, error) {
var config APIConfig
err := tools.LoadConfigInto(&config, path)
if err != nil {
return nil, err
}
err = config.validate()
if err != nil {
return nil, err
}
config.ServerURL, err = config.composeServerURL()
if err != nil {
return nil, err
}
return &config, nil
}
func GetLogLevelFromPath(path string) (zapcore.Level, error) {
cfg, err := LoadFromPath(path)
if err != nil {
return zapcore.InfoLevel, err
}
return cfg.LogLevel, nil
}
func (c *APIConfig) validate() error {
if c.ExternalFQDN == "" {
return errors.New("ExternalFQDN not specified")
}
if c.AuthProxyHost != "" && c.AuthProxyCACert == "" {
return errors.New("AuthProxyHost requires a value for AuthProxyCACert")
}
if c.AuthProxyCACert != "" && c.AuthProxyHost == "" {
return errors.New("AuthProxyCACert requires a value for AuthProxyHost")
}
if c.UserCertificateExpirationWarningDuration != "" {
if _, err := time.ParseDuration(c.UserCertificateExpirationWarningDuration); err != nil {
return errors.New(`Invalid duration format for userCertificateExpirationWarningDuration. Use a format like "48h"`)
}
}
if c.BuilderName == "" {
return errors.New("BuilderName must have a value")
}
return nil
}
func (c *APIConfig) GetUserCertificateDuration() time.Duration {
if c.UserCertificateExpirationWarningDuration == "" {
return time.Hour * 24 * 7
}
d, _ := time.ParseDuration(c.UserCertificateExpirationWarningDuration)
return d
}
func (c *APIConfig) composeServerURL() (string, error) {
toReturn := defaultExternalProtocol + "://" + c.ExternalFQDN
if c.ExternalPort != 0 {
toReturn += ":" + fmt.Sprint(c.ExternalPort)
}
return toReturn, nil
}
func (c *APIConfig) GenerateK8sClientConfig(k8sClientConfig *rest.Config) *rest.Config {
if c.AuthProxyHost != "" && c.AuthProxyCACert != "" {
k8sClientConfig.Host = c.AuthProxyHost
k8sClientConfig.CAData = []byte(c.AuthProxyCACert)
}
return k8sClientConfig
}