|
1 | 1 | package v1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "github.com/onepanelio/core/pkg/util/ptr" |
| 7 | + log "github.com/sirupsen/logrus" |
4 | 8 | corev1 "k8s.io/api/core/v1" |
| 9 | + "sigs.k8s.io/yaml" |
5 | 10 | "strings" |
6 | 11 | ) |
7 | 12 |
|
| 13 | +// SystemConfig is configuration loaded from kubernetes config and secrets that includes information about the |
| 14 | +// database, server, etc. |
| 15 | +type SystemConfig map[string]string |
| 16 | + |
| 17 | +// NodePoolOption extends ParameterOption to support resourceRequirements |
| 18 | +type NodePoolOption struct { |
| 19 | + ParameterOption |
| 20 | + Resources corev1.ResourceRequirements |
| 21 | +} |
| 22 | + |
| 23 | +// NewSystemConfig creates a System config by getting the required data from a ConfigMap and Secret |
| 24 | +func NewSystemConfig(configMap *ConfigMap, secret *Secret) (config SystemConfig, err error) { |
| 25 | + config = configMap.Data |
| 26 | + |
| 27 | + databaseUsername, err := base64.StdEncoding.DecodeString(secret.Data["databaseUsername"]) |
| 28 | + if err != nil { |
| 29 | + return |
| 30 | + } |
| 31 | + config["databaseUsername"] = string(databaseUsername) |
| 32 | + |
| 33 | + databasePassword, err := base64.StdEncoding.DecodeString(secret.Data["databasePassword"]) |
| 34 | + if err != nil { |
| 35 | + return |
| 36 | + } |
| 37 | + config["databasePassword"] = string(databasePassword) |
| 38 | + |
| 39 | + return |
| 40 | +} |
| 41 | + |
| 42 | +// GetValue returns the value in the underlying map if it exists, otherwise nil is returned |
| 43 | +// If the value does not exist, it is also logged. |
| 44 | +func (s SystemConfig) GetValue(name string) *string { |
| 45 | + value, ok := s[name] |
| 46 | + if !ok { |
| 47 | + log.WithFields(log.Fields{ |
| 48 | + "Method": "SystemConfig.GetValue", |
| 49 | + "Name": name, |
| 50 | + "Error": "does not exist", |
| 51 | + }) |
| 52 | + |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + return &value |
| 57 | +} |
| 58 | + |
| 59 | +// Domain gets the ONEPANEL_DOMAIN value, or nil. |
| 60 | +func (s SystemConfig) Domain() *string { |
| 61 | + return s.GetValue("ONEPANEL_DOMAIN") |
| 62 | +} |
| 63 | + |
| 64 | +// APIURL gets the ONEPANEL_API_URL, or nil. |
| 65 | +func (s SystemConfig) APIURL() *string { |
| 66 | + return s.GetValue("ONEPANEL_API_URL") |
| 67 | +} |
| 68 | + |
| 69 | +// APIProtocol returns either http:// or https:// or nil. |
| 70 | +// It is based on the ONEPANEL_API_URL config value and checks if it has https or http |
| 71 | +func (s SystemConfig) APIProtocol() *string { |
| 72 | + url := s.APIURL() |
| 73 | + if url == nil { |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + if strings.HasPrefix(*url, "https://") { |
| 78 | + return ptr.String("https://") |
| 79 | + } |
| 80 | + |
| 81 | + return ptr.String("http://") |
| 82 | +} |
| 83 | + |
| 84 | +// FQDN gets the ONEPANEL_FQDN value or nil. |
| 85 | +func (s SystemConfig) FQDN() *string { |
| 86 | + return s.GetValue("ONEPANEL_FQDN") |
| 87 | +} |
| 88 | + |
| 89 | +// NodePoolLabel gets the applicationNodePoolLabel from the config or returns nil. |
| 90 | +func (s SystemConfig) NodePoolLabel() (label *string) { |
| 91 | + return s.GetValue("applicationNodePoolLabel") |
| 92 | +} |
| 93 | + |
| 94 | +// NodePoolOptions loads and parses the applicationNodePoolOptions from the config. |
| 95 | +// If there is no data, an error is returned. |
| 96 | +func (s SystemConfig) NodePoolOptions() (options []*NodePoolOption, err error) { |
| 97 | + data := s.GetValue("applicationNodePoolOptions") |
| 98 | + if data == nil { |
| 99 | + return nil, fmt.Errorf("no nodePoolOptions in config") |
| 100 | + } |
| 101 | + |
| 102 | + if err = yaml.Unmarshal([]byte(*data), &options); err != nil { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + return |
| 107 | +} |
| 108 | + |
| 109 | +// NodePoolOptionByValue returns the nodePoolOption based on a given value |
| 110 | +func (s SystemConfig) NodePoolOptionByValue(value string) (option *NodePoolOption, err error) { |
| 111 | + options, err := s.NodePoolOptions() |
| 112 | + if err != nil { |
| 113 | + return |
| 114 | + } |
| 115 | + for _, opt := range options { |
| 116 | + if opt.Value == value { |
| 117 | + option = opt |
| 118 | + return |
| 119 | + } |
| 120 | + } |
| 121 | + return |
| 122 | +} |
| 123 | + |
| 124 | +// DatabaseDriverName gets the databaseDriverName value, or nil. |
| 125 | +func (s SystemConfig) DatabaseDriverName() *string { |
| 126 | + return s.GetValue("databaseDriverName") |
| 127 | +} |
| 128 | + |
| 129 | +// DatabaseConnection returns system config information to connect to a database |
| 130 | +func (s SystemConfig) DatabaseConnection() (driverName, dataSourceName string) { |
| 131 | + dataSourceName = fmt.Sprintf("host=%v user=%v password=%v dbname=%v sslmode=disable", |
| 132 | + s["databaseHost"], s["databaseUsername"], s["databasePassword"], s["databaseName"]) |
| 133 | + |
| 134 | + driverName = *s.DatabaseDriverName() |
| 135 | + |
| 136 | + return |
| 137 | +} |
| 138 | + |
8 | 139 | type ArtifactRepositoryS3Config struct { |
9 | 140 | KeyFormat string |
10 | 141 | Bucket string |
|
0 commit comments