-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathconfiguration_deprecated.go
More file actions
298 lines (266 loc) · 10.4 KB
/
configuration_deprecated.go
File metadata and controls
298 lines (266 loc) · 10.4 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2019 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package configuration
import (
"fmt"
"path"
"github.com/haproxytech/dataplaneapi/storage"
"github.com/haproxytech/dataplaneapi/storagetype"
)
func (c *Configuration) migrateDeprecatedFields() ([]string, error) {
if c.DeprecatedBootstrapKey.Load() != "" {
c.Cluster.BootstrapKey.Store(c.DeprecatedBootstrapKey.Load())
}
deprecationInfoMsg := make([]string, 0)
// Users
deprMsgUsers, err := c.migrateUsers()
deprecationInfoMsg = append(deprecationInfoMsg, deprMsgUsers...)
if err != nil {
return deprecationInfoMsg, err
}
// Cluster
deprMsgCluster, err := c.migrateCluster()
deprecationInfoMsg = append(deprecationInfoMsg, deprMsgCluster...)
if err != nil {
return deprecationInfoMsg, err
}
// Service Discovery Consuls
deprMsgConsuls, err := c.migrateSDConsuls()
deprecationInfoMsg = append(deprecationInfoMsg, deprMsgConsuls...)
if err != nil {
return deprecationInfoMsg, err
}
// Service Discovery AWS Regions
deprMsgAwsRegions, err := c.migrateSDAwsRegions()
deprecationInfoMsg = append(deprecationInfoMsg, deprMsgAwsRegions...)
if err != nil {
return deprecationInfoMsg, err
}
// Remove deprecated fields after migration
c.storage.Get().emptyDeprecatedSections()
return deprecationInfoMsg, nil
}
func (c *Configuration) migrateUsers() ([]string, error) {
deprecationInfoMsg := make([]string, 0)
dapiCfgStorage := c.storage.Get()
// Users
dapiStorageUsers := c.clusterModeStorage.GetUsers()
clusterModeStoragePath := path.Join(c.HAProxy.DataplaneStorageDir, storage.ClusterModeDataFileName)
usersToMigrate := make([]storagetype.User, 0)
if dapiCfgStorage.Dataplaneapi != nil {
for _, singleModeUser := range dapiCfgStorage.Dataplaneapi.Users {
found := false
// Only migrate cluster users
if !singleModeUser.IsClusterUser() {
continue
}
var muser storagetype.User
for _, muser = range dapiStorageUsers {
if muser.Name == singleModeUser.Name {
found = true
break
}
}
// Already migrated
if found {
msg := fmt.Sprintf("[CFG DEPRECATED] [SKIP] [User] [%s]: already migrated. Old location [%s] New location [%s]. Use only new location",
singleModeUser.Name,
c.HAProxy.DataplaneConfig,
clusterModeStoragePath)
// Logging is not done here as at startup, the logger is not yet initialized
// so it's done later on
deprecationInfoMsg = append(deprecationInfoMsg, msg)
} else {
// If not already migrated, then migrate it
msg := fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [User] [%s]: migrating. Old location [%s] New location [%s]. Use only new location",
singleModeUser.Name,
c.HAProxy.DataplaneConfig,
clusterModeStoragePath)
// Logging is not done here as at startup, the logger is not yet initialized
// so it's done later on
deprecationInfoMsg = append(deprecationInfoMsg, msg)
usersToMigrate = append(usersToMigrate, singleModeUser)
}
}
}
if err := c.clusterModeStorage.AddUsersAndStore(usersToMigrate); err != nil {
return deprecationInfoMsg, err
}
return deprecationInfoMsg, nil
}
func (c *Configuration) migrateCluster() ([]string, error) {
deprecationInfoMsg := make([]string, 0)
dapiCfgStorage := c.storage.Get()
dapiStorageCluster := c.clusterModeStorage.GetCluster()
clusterStoragePath := path.Join(c.HAProxy.DataplaneStorageDir, storage.ClusterModeDataFileName)
var alreadyMigrated bool
// Status
dapiCfgStatus := dapiCfgStorage.DeprecatedStatus
dapiStorageStatus := c.clusterModeStorage.GetStatus()
if dapiCfgStatus != nil {
alreadyMigrated = dapiStorageStatus != nil && *dapiStorageStatus != ""
if alreadyMigrated {
deprecationInfoMsg = append(deprecationInfoMsg,
fmt.Sprintf("[CFG DEPRECATED] [SKIP] [Status]: already migrated. Old location [%s] New location [%s]. Use only new location",
c.HAProxy.DataplaneConfig,
clusterStoragePath))
} else {
deprecationInfoMsg = append(deprecationInfoMsg,
fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [Status]: migrating. Old location [%s] New location [%s]. Use only new location",
c.HAProxy.DataplaneConfig,
clusterStoragePath))
// not already migrated
c.Status.Store(*dapiCfgStatus)
c.clusterModeStorage.SetStatusAndStore(dapiCfgStatus)
}
}
// Nothing in dapi configuration file
dapiCfgCluster := dapiCfgStorage.DeprecatedCluster
if dapiCfgCluster == nil {
return deprecationInfoMsg, nil
}
// Comparing on Bootstrap Key
alreadyMigrated = isAlreadyMigrated(dapiCfgCluster, dapiStorageCluster)
if alreadyMigrated {
// If we have a cluster in dapi configuration file (deprecated)
// We do not try to check if there was any change between dapi config file and storage, we skip
deprecationInfoMsg = append(deprecationInfoMsg,
fmt.Sprintf("[CFG DEPRECATED] [SKIP] [Cluster] [%s]: already migrated. Old location [%s] New location [%s]. Use only new location",
dapiStorageCluster.LogDisplayName(),
c.HAProxy.DataplaneConfig,
clusterStoragePath))
} else {
// If not already migrated, then migrate it
deprecationInfoMsg = append(deprecationInfoMsg,
fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [Cluster] [%s]: migrating. Old location [%s] New location [%s]. Use only new location",
dapiCfgCluster.LogDisplayName(),
c.HAProxy.DataplaneConfig,
clusterStoragePath))
if err := c.clusterModeStorage.SetClusterAndStore(dapiCfgCluster); err != nil {
return deprecationInfoMsg, err
}
// Then udpate cluster in configuration too
c.copyClusterToConfiguration(dapiCfgCluster)
}
return deprecationInfoMsg, nil
}
func isAlreadyMigrated(dapiCfgCluster *storagetype.Cluster, dapiStorageCluster *storagetype.Cluster) bool {
if dapiStorageCluster == nil || dapiCfgCluster == nil {
return false
}
if dapiStorageCluster.BootstrapKey != nil && dapiCfgCluster.BootstrapKey != nil && *dapiStorageCluster.BootstrapKey == *dapiCfgCluster.BootstrapKey {
return true
}
return false
}
func (c *Configuration) migrateSDConsuls() ([]string, error) {
deprecationInfoMsg := make([]string, 0)
dapiCfgStorage := c.storage.Get()
dapiStorageConsuls := c.sdConsulStorage.GetConsuls()
consulsStoragePath := path.Join(c.HAProxy.DataplaneStorageDir, storage.ConsulFileName)
consulsToMigrate := make(storagetype.Consuls, 0)
if dapiCfgStorage.DeprecatedServiceDiscovery == nil || dapiCfgStorage.DeprecatedServiceDiscovery.Consuls == nil {
return deprecationInfoMsg, nil
}
for _, cfgConsul := range *dapiCfgStorage.DeprecatedServiceDiscovery.Consuls {
found := false
// Check done on ID
for _, dapiStorageConsul := range dapiStorageConsuls {
if dapiStorageConsul.ID != nil && cfgConsul.ID != nil &&
*dapiStorageConsul.ID == *cfgConsul.ID {
found = true
break
}
}
consulID := ""
if cfgConsul.ID != nil {
consulID = *cfgConsul.ID
}
// Already migrated
if found {
msg := fmt.Sprintf("[CFG DEPRECATED] [SKIP] [Consul] [%s]: already migrated. Old location [%s] New location [%s]. Use only new location",
consulID,
c.HAProxy.DataplaneConfig,
consulsStoragePath)
// Logging is not done here as at startup, the logger is not yet initialized
// so it's done later on
deprecationInfoMsg = append(deprecationInfoMsg, msg)
} else {
// If not already migrated, then migrate it
msg := fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [Consul] [%s]: migrating. Old location [%s] New location [%s]. Use only new location",
consulID,
c.HAProxy.DataplaneConfig,
consulsStoragePath)
// Logging is not done here as at startup, the logger is not yet initialized
// so it's done later on
deprecationInfoMsg = append(deprecationInfoMsg, msg)
consulsToMigrate = append(consulsToMigrate, cfgConsul)
}
}
if err := c.sdConsulStorage.AddConsulsAndStore(consulsToMigrate); err != nil {
return deprecationInfoMsg, err
}
c.ServiceDiscovery.Consuls = append(c.ServiceDiscovery.Consuls, consulsToMigrate...)
return deprecationInfoMsg, nil
}
func (c *Configuration) migrateSDAwsRegions() ([]string, error) {
deprecationInfoMsg := make([]string, 0)
dapiCfgStorage := c.storage.Get()
dapiStorageAwsRegions := c.sdAWSRegionStorage.GetAWSRegions()
awsRegionsStoragePath := path.Join(c.HAProxy.DataplaneStorageDir, storage.AWSRegionFileName)
awsRegionssToMigrate := make(storagetype.AWSRegions, 0)
if dapiCfgStorage.DeprecatedServiceDiscovery == nil || dapiCfgStorage.DeprecatedServiceDiscovery.AWSRegions == nil {
return deprecationInfoMsg, nil
}
for _, cfgAwsRegion := range *dapiCfgStorage.DeprecatedServiceDiscovery.AWSRegions {
found := false
for _, dapiStorageAwsRegion := range dapiStorageAwsRegions {
if dapiStorageAwsRegion.Name != nil && cfgAwsRegion.Name != nil &&
*dapiStorageAwsRegion.Name == *cfgAwsRegion.Name {
found = true
break
}
}
awsRegionName := ""
if cfgAwsRegion.Name != nil {
awsRegionName = *cfgAwsRegion.Name
}
// Already migrated
if found {
msg := fmt.Sprintf("[CFG DEPRECATED] [SKIP] [AWS Region] [%s]: already migrated. Old location [%s] New location [%s]. Use only new location",
awsRegionName,
c.HAProxy.DataplaneConfig,
awsRegionsStoragePath)
// Logging is not done here as at startup, the logger is not yet initialized
// so it's done later on
deprecationInfoMsg = append(deprecationInfoMsg, msg)
} else {
// If not already migrated, then migrate it
msg := fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [AWS Region] [%s]: migrating. Old location [%s] New location [%s]. Use only new location",
awsRegionName,
c.HAProxy.DataplaneConfig,
awsRegionsStoragePath)
// Logging is not done here as at startup, the logger is not yet initialized
// so it's done later on
deprecationInfoMsg = append(deprecationInfoMsg, msg)
awsRegionssToMigrate = append(awsRegionssToMigrate, cfgAwsRegion)
}
}
if err := c.sdAWSRegionStorage.AddAWSRegionsAndStore(awsRegionssToMigrate); err != nil {
return deprecationInfoMsg, err
}
c.ServiceDiscovery.AWSRegions = append(c.ServiceDiscovery.AWSRegions, awsRegionssToMigrate...)
return deprecationInfoMsg, nil
}