-
Notifications
You must be signed in to change notification settings - Fork 587
/
dcs.go
324 lines (259 loc) · 10.7 KB
/
dcs.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package config
/*
Copyright 2020 - 2022 Crunchy Data Solutions, Ind.
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.
*/
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/util"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
)
const (
// PGHADCSConfigName represents the name of the DCS configuration stored in the
// "<clustername>-pgha-config" configMap, which is "<clustername>-dcs-config"
PGHADCSConfigName = "%s-dcs-config"
// DCSConfigMapName represents the name of the DCS configMap created for each cluster, which
// has the name "<clustername>-config"
dcsConfigMapName = "%s-config"
// dcsConfigAnnotation represents that name of the annotation used to store the cluster's DCS
// configuration
dcsConfigAnnotation = "config"
)
// DCS configures the DCS configuration settings for a specific PG cluster.
type DCS struct {
kubeclientset kubernetes.Interface
configMap *corev1.ConfigMap
configName string
clusterScope string
}
// DCSConfig represents the cluster-wide configuration that is stored in the Distributed
// Configuration Store (DCS).
type DCSConfig struct {
LoopWait int `json:"loop_wait,omitempty"`
TTL int `json:"ttl,omitempty"`
RetryTimeout int `json:"retry_timeout,omitempty"`
MaximumLagOnFailover int `json:"maximum_lag_on_failover,omitempty"`
MasterStartTimeout int `json:"master_start_timeout,omitempty"`
SynchronousMode bool `json:"synchronous_mode,omitempty"`
SynchronousModeStrict bool `json:"synchronous_mode_strict,omitempty"`
PostgreSQL *PostgresDCS `json:"postgresql,omitempty"`
StandbyCluster *StandbyDCS `json:"standby_cluster,omitempty"`
Slots map[string]SlotDCS `json:"slots,omitempty"`
}
// PostgresDCS represents the PostgreSQL settings that can be applied cluster-wide to a
// PostgreSQL cluster via the DCS.
type PostgresDCS struct {
UsePGRewind bool `json:"use_pg_rewind,omitempty"`
UseSlots bool `json:"use_slots,omitempty"`
RecoveryConf map[string]interface{} `json:"recovery_conf,omitempty"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
}
// StandbyDCS represents standby cluster settings that can be applied cluster-wide via the DCS.
type StandbyDCS struct {
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
PrimarySlotName map[string]interface{} `json:"primary_slot_name,omitempty"`
CreateReplicaMethods []string `json:"create_replica_methods,omitempty"`
RestoreCommand string `json:"restore_command,omitempty"`
ArchiveCleanupCommand string `json:"archive_cleanup_command,omitempty"`
RecoveryMinApplyDelay int `json:"recovery_min_apply_delay,omitempty"`
}
// SlotDCS represents slot settings that can be applied cluster-wide via the DCS.
type SlotDCS struct {
Type string `json:"type,omitempty"`
Database string `json:"database,omitempty"`
Plugin string `json:"plugin,omitempty"`
}
// NewDCS creates a new DCS config struct using the configMap provided. The DCSConfig will
// include a configMap that will be used to configure the DCS for a specific cluster.
func NewDCS(configMap *corev1.ConfigMap, kubeclientset kubernetes.Interface,
clusterScope string) *DCS {
clusterName := configMap.GetLabels()[config.LABEL_PG_CLUSTER]
return &DCS{
kubeclientset: kubeclientset,
configMap: configMap,
configName: fmt.Sprintf(PGHADCSConfigName, clusterName),
clusterScope: clusterScope,
}
}
// Sync attempts to apply all configuration in the the DCSConfig's configMap. If the DCS
// configuration is missing from the configMap, then and attempt is made to add it by refreshing
// the DCS configuration.
func (d *DCS) Sync() error {
clusterName := d.configMap.GetObjectMeta().GetLabels()[config.LABEL_PG_CLUSTER]
namespace := d.configMap.GetObjectMeta().GetNamespace()
log.Debugf("Cluster Config: syncing DCS config for cluster %s (namespace %s)", clusterName,
namespace)
if err := d.apply(); err != nil &&
errors.Is(err, ErrMissingClusterConfig) {
if err := d.refresh(); err != nil {
return err
}
} else if err != nil {
return err
}
log.Debugf("Cluster Config: finished syncing DCS config for cluster %s (namespace %s)",
clusterName, namespace)
return nil
}
// Update updates the contents of the DCS configuration stored within the configMap included
// in the DCS.
func (d *DCS) Update(dcsConfig *DCSConfig) error {
clusterName := d.configMap.GetObjectMeta().GetLabels()[config.LABEL_PG_CLUSTER]
namespace := d.configMap.GetObjectMeta().GetNamespace()
log.Debugf("Cluster Config: updating DCS config for cluster %s (namespace %s)", clusterName,
namespace)
content, err := yaml.Marshal(dcsConfig)
if err != nil {
return err
}
if err := patchConfigMapData(d.kubeclientset, d.configMap, d.configName, content); err != nil {
return err
}
log.Debugf("Cluster Config: successfully updated DCS config for cluster %s (namespace %s)",
clusterName, namespace)
return nil
}
// apply applies the DCS configuration stored in the ClusterConfig's configMap to the cluster's
// DCS. Specicially, it updates the cluster's DCS, i.e. the the "config" annotation of the
// "<clustername>-config" configMap, with the contents of the "<clustername-dcs-config>"
// configuration included in the DCS's configMap.
func (d *DCS) apply() error {
clusterName := d.configMap.GetLabels()[config.LABEL_PG_CLUSTER]
namespace := d.configMap.GetObjectMeta().GetNamespace()
log.Debugf("Cluster Config: applying DCS config to cluster %s in namespace %s", clusterName,
namespace)
// first grab the DCS config from the PGHA config map
dcsConfig, rawDCS, err := d.GetDCSConfig()
if err != nil {
return err
}
// next grab the current/live DCS from the "config" annotation of the Patroni configMap
clusterDCS, rawClusterDCS, err := d.getClusterDCSConfig()
if err != nil {
return err
}
// if the DCS contents are equal then no further action is needed
if reflect.DeepEqual(dcsConfig, clusterDCS) {
log.Debugf("Cluster Config: DCS config for cluster %s in namespace %s is up-to-date, "+
"nothing to apply", clusterName, namespace)
return nil
}
// ensure the current "pause" setting is not overridden if currently set for the cluster
if _, ok := rawClusterDCS["pause"]; ok {
rawDCS["pause"] = rawClusterDCS["pause"]
}
// proceed with updating the DCS with the contents of the configMap
dcsConfigJSON, err := json.Marshal(rawDCS)
if err != nil {
return err
}
if err := d.patchDCSAnnotation(string(dcsConfigJSON)); err != nil {
return err
}
log.Debugf("Cluster Config: successfully applied DCS to cluster %s in namespace %s",
clusterName, namespace)
return nil
}
// getClusterDCSConfig obtains the configuration that is currently stored in the cluster's DCS.
// Specifically, it obtains the configuration stored in the "config" annotation of the
// "<clustername>-config" configMap.
func (d *DCS) getClusterDCSConfig() (*DCSConfig, map[string]json.RawMessage, error) {
clusterDCS := &DCSConfig{}
namespace := d.configMap.GetObjectMeta().GetNamespace()
dcsCM, err := d.kubeclientset.CoreV1().ConfigMaps(namespace).
Get(fmt.Sprintf(dcsConfigMapName, d.clusterScope), metav1.GetOptions{})
if err != nil {
return nil, nil, err
}
config, ok := dcsCM.GetObjectMeta().GetAnnotations()[dcsConfigAnnotation]
if !ok {
return nil, nil, util.ErrMissingConfigAnnotation
}
if err := json.Unmarshal([]byte(config), clusterDCS); err != nil {
return nil, nil, err
}
var rawJSON map[string]json.RawMessage
if err := json.Unmarshal([]byte(config), &rawJSON); err != nil {
return nil, nil, err
}
return clusterDCS, rawJSON, nil
}
// GetDCSConfig returns the current DCS configuration included in the ClusterConfig's
// configMap, i.e. the contents of the "<clustername-dcs-config>" configuration unmarshalled
// into a DCSConfig struct.
func (d *DCS) GetDCSConfig() (*DCSConfig, map[string]json.RawMessage, error) {
dcsYAML, ok := d.configMap.Data[d.configName]
if !ok {
return nil, nil, ErrMissingClusterConfig
}
dcsConfig := &DCSConfig{}
if err := yaml.Unmarshal([]byte(dcsYAML), dcsConfig); err != nil {
return nil, nil, err
}
var rawJSON map[string]json.RawMessage
if err := yaml.Unmarshal([]byte(dcsYAML), &rawJSON); err != nil {
return nil, nil, err
}
return dcsConfig, rawJSON, nil
}
// patchDCSAnnotation patches the "config" annotation within the DCS configMap with the
// content provided.
func (d *DCS) patchDCSAnnotation(content string) error {
jsonOp := []util.JSONPatchOperation{{
Op: "replace",
Path: fmt.Sprintf("/metadata/annotations/%s", dcsConfigAnnotation),
Value: content,
}}
jsonOpBytes, err := json.Marshal(jsonOp)
if err != nil {
return err
}
if _, err := d.kubeclientset.CoreV1().ConfigMaps(d.configMap.GetNamespace()).Patch(
fmt.Sprintf(dcsConfigMapName, d.clusterScope), types.JSONPatchType,
jsonOpBytes); err != nil {
return err
}
return nil
}
// refresh updates the DCS configuration stored in the "<clustername>-pgha-config"
// configMap with the current DCS configuration for the cluster. Specifically, it is updated with
// the configuration stored in the "config" annotation of the "<clustername>-config" configMap.
func (d *DCS) refresh() error {
clusterName := d.configMap.Labels[config.LABEL_PG_CLUSTER]
namespace := d.configMap.GetObjectMeta().GetNamespace()
log.Debugf("Cluster Config: refreshing DCS config for cluster %s (namespace %s)", clusterName,
namespace)
clusterDCS, _, err := d.getClusterDCSConfig()
if err != nil {
return err
}
clusterDCSBytes, err := yaml.Marshal(clusterDCS)
if err != nil {
return err
}
if err := patchConfigMapData(d.kubeclientset, d.configMap, d.configName,
clusterDCSBytes); err != nil {
return err
}
log.Debugf("Cluster Config: successfully refreshed DCS config for cluster %s (namespace %s)",
clusterName, namespace)
return nil
}