-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhostparams.go
254 lines (219 loc) · 7.61 KB
/
hostparams.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package hostparams
import (
"errors"
"fmt"
"hash/crc32"
"strconv"
"strings"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
v1 "github.com/infobloxopen/db-controller/api/v1"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/spf13/viper"
)
var (
defaultAuroraPostgresStr = "aurora-postgresql"
shapeDelimiter = "!"
INSTANCE_CLASS_INDEX = 0
STORAGE_TYPE_INDEX = 1
// DO NOT CHANGE THE DEFAULTS -
// They are used to determine the hash for RDS names. Any change will result in a new RDS instance being created for all applications!
// These values are purposely moved from the config file to the code to avoid accidental changes.
defaultShape = "db.t4g.medium"
defaultEngineVersion = "15.3"
defaultMajorVersion = "15"
defaultEngine = v1.Postgres
)
var (
ErrMaxStorageReduced = errors.New("reducing .spec.maxStorageGB value is not allowed (Also not spacifying maxStorageGB if specified earlier is not allowed.)")
ErrMaxStorageLesser = errors.New(".spec.maxStorageGB should always be greater than spec.minStorageGB")
ErrEngineVersionNotSpecified = errors.New(".spec.dbVersion is a mandatory field and cannot be empty")
)
type HostParams struct {
// FIXME: this should be DatabaseType, not string
Type string
Shape string
MinStorageGB int
MaxStorageGB int64
DBVersion string
MasterUsername string
InstanceClass string
StorageType string
SkipFinalSnapshotBeforeDeletion bool
PubliclyAccessible bool
EnableIAMDatabaseAuthentication bool
DeletionPolicy xpv1.DeletionPolicy
Port int64
isDefaultEngine bool
isDefaultShape bool
isDefaultInstanceClass bool
isDefaultStorage bool
IsDefaultVersion bool
}
func (p *HostParams) String() string {
return fmt.Sprintf("%s-%s-%s", p.Type, p.InstanceClass, p.DBVersion)
}
func (p *HostParams) Hash() string {
crc32q := crc32.MakeTable(0xD5828281)
return fmt.Sprintf("%08x", crc32.Checksum([]byte(p.String()), crc32q))
}
func (p *HostParams) HasShapeChanged(activeShape string) bool {
if p.isDefaultShape {
// request is for a "" shape
// default request should not trigger an upgrade
return false
}
return activeShape != p.Shape
}
func (p *HostParams) HasInstanceClassChanged(activeInstanceClass string) bool {
if p.isDefaultInstanceClass {
// request is for a "" shape
// default request should not trigger an upgrade
return false
}
return activeInstanceClass != p.InstanceClass
}
func (p *HostParams) HasStorageChanged(activeStorage int) bool {
// storage is not applicable to aurora postgres
if p.Type == defaultAuroraPostgresStr {
return false
}
if p.isDefaultStorage {
return false
}
return activeStorage != p.MinStorageGB
}
func (p *HostParams) HasEngineChanged(activeEngine string) bool {
if p.isDefaultEngine {
return false
}
return activeEngine != p.Type
}
func (p *HostParams) HasVersionChanged(activeVersion string) bool {
if p.IsDefaultVersion {
return false
}
return strings.Split(activeVersion, ".")[0] != strings.Split(p.DBVersion, ".")[0]
}
func (p *HostParams) IsUpgradeRequested(active *HostParams) bool {
return p.HasEngineChanged(active.Type) ||
p.HasInstanceClassChanged(active.InstanceClass) ||
p.HasVersionChanged(active.DBVersion)
}
func New(config *viper.Viper, dbClaim *v1.DatabaseClaim) (*HostParams, error) {
var (
err error
port string
iport int
)
hostParams := HostParams{}
hostParams.DeletionPolicy = xpv1.DeletionPolicy(
cases.Title(language.English, cases.Compact).String(string(dbClaim.Spec.DeletionPolicy)))
hostParams.Type = string(dbClaim.Spec.Type)
hostParams.DBVersion = dbClaim.Spec.DBVersion
hostParams.Shape = dbClaim.Spec.Shape
hostParams.MinStorageGB = dbClaim.Spec.MinStorageGB
hostParams.MaxStorageGB = dbClaim.Spec.MaxStorageGB
port = config.GetString("defaultMasterPort")
iport, err = strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("invalid master port")
}
hostParams.Port = int64(iport)
if hostParams.MasterUsername == "" {
hostParams.MasterUsername = config.GetString("defaultMasterUsername")
}
if hostParams.DBVersion == "" {
if dbClaim.Status.ActiveDB.DBVersion != "" {
//for an existing (Status.ActiveDB NOT empty) it picks up the Status DBVersion, so no update is triggered.
hostParams.DBVersion = dbClaim.Status.ActiveDB.DBVersion
} else {
//for a new Claim (Status.ActiveDB empty) it assumes 15 only
hostParams.IsDefaultVersion = true
hostParams.DBVersion = defaultMajorVersion
}
}
if hostParams.Shape == "" {
hostParams.isDefaultShape = true
hostParams.isDefaultInstanceClass = true
hostParams.Shape = defaultShape
}
if hostParams.Type == "" {
hostParams.isDefaultEngine = true
hostParams.Type = string(defaultEngine)
}
if hostParams.MinStorageGB == 0 {
hostParams.isDefaultStorage = true
hostParams.MinStorageGB = config.GetInt("defaultMinStorageGB")
}
hostParams.SkipFinalSnapshotBeforeDeletion = config.GetBool("defaultSkipFinalSnapshotBeforeDeletion")
hostParams.PubliclyAccessible = config.GetBool("defaultPubliclyAccessible")
if hostParams.DeletionPolicy == "" {
if config.GetString("defaultDeletionPolicy") == "delete" {
hostParams.DeletionPolicy = xpv1.DeletionDelete
} else {
hostParams.DeletionPolicy = xpv1.DeletionOrphan
}
}
// TODO - Enable IAM auth based on authSource config
hostParams.EnableIAMDatabaseAuthentication = false
hostParams.InstanceClass = getInstanceClass(hostParams.Shape)
hostParams.StorageType, err = getStorageType(config, hostParams.Type, hostParams.Shape)
if err != nil {
return &HostParams{}, err
}
if hostParams.Type == string(v1.Postgres) {
if hostParams.MaxStorageGB == 0 {
if dbClaim.Status.ActiveDB.MaxStorageGB != 0 {
return &HostParams{}, ErrMaxStorageReduced
}
} else if hostParams.MaxStorageGB < dbClaim.Status.ActiveDB.MaxStorageGB {
return &HostParams{}, ErrMaxStorageReduced
} else if hostParams.MaxStorageGB <= int64(hostParams.MinStorageGB) {
return &HostParams{}, ErrMaxStorageLesser
}
}
return &hostParams, nil
}
// Retrieves the current scenario, wha engine, version, instance, etc. actually is deployed
func GetActiveHostParams(dbClaim *v1.DatabaseClaim) *HostParams {
hostParams := HostParams{}
hostParams.Type = string(dbClaim.Status.ActiveDB.Type)
hostParams.DBVersion = dbClaim.Status.ActiveDB.DBVersion
hostParams.Shape = dbClaim.Status.ActiveDB.Shape
hostParams.InstanceClass = getInstanceClass(hostParams.Shape)
hostParams.MinStorageGB = dbClaim.Status.ActiveDB.MinStorageGB
return &hostParams
}
func getInstanceClass(shape string) string {
shapeParts := strings.Split(shape, shapeDelimiter)
return shapeParts[INSTANCE_CLASS_INDEX]
}
func getStorageType(config *viper.Viper, engine string, shape string) (string, error) {
storageType := ""
if engine == defaultAuroraPostgresStr {
shapeParts := strings.Split(shape, shapeDelimiter)
if len(shapeParts) > STORAGE_TYPE_INDEX {
switch strings.ToLower(shapeParts[STORAGE_TYPE_INDEX]) {
case "io1":
storageType = "aurora-iopt1"
case "io-optimized":
storageType = "aurora-iopt1"
case "standard":
storageType = "aurora"
case "aurora":
storageType = "aurora"
case "":
storageType = "aurora"
default:
return "", fmt.Errorf("invalid shape")
}
} else {
storageType = "aurora"
}
} else {
// TODO - add support for custom storage type for postgres
storageType = config.GetString("storageType")
}
return storageType, nil
}