-
Notifications
You must be signed in to change notification settings - Fork 80
/
configmap_helper.go
206 lines (178 loc) · 6.47 KB
/
configmap_helper.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
/*
* configmap_helper.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021 Apple Inc. and the FoundationDB project authors
*
* 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 internal
import (
"encoding/json"
"fmt"
"strings"
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// ClusterFileKey defines the key name in the ConfigMap
ClusterFileKey = "cluster-file"
)
// GetConfigMap builds a config map for a cluster's dynamic config
func GetConfigMap(cluster *fdbv1beta2.FoundationDBCluster) (*corev1.ConfigMap, error) {
data := make(map[string]string)
connectionString := cluster.Status.ConnectionString
data[ClusterFileKey] = connectionString
data["running-version"] = cluster.Status.RunningVersion
var caFile strings.Builder
for _, ca := range cluster.Spec.TrustedCAs {
if caFile.Len() > 0 {
caFile.WriteString("\n")
}
caFile.WriteString(ca)
}
if caFile.Len() > 0 {
data["ca-file"] = caFile.String()
}
desiredCountStruct, err := cluster.GetProcessCountsWithDefaults()
if err != nil {
return nil, err
}
desiredCounts := desiredCountStruct.Map()
imageTypes := make(map[FDBImageType]fdbv1beta2.None, len(cluster.Status.ImageTypes))
for _, imageType := range cluster.Status.ImageTypes {
imageTypes[FDBImageType(imageType)] = fdbv1beta2.None{}
}
for processClass, count := range desiredCounts {
if count == 0 {
continue
}
serversPerPodSlice := []int{1}
if processClass == fdbv1beta2.ProcessClassStorage {
// If the status field is not initialized we fallback to only the specified count
// in the cluster spec. This should only happen in the initial phase of a new cluster.
if len(cluster.Status.StorageServersPerDisk) == 0 {
serversPerPodSlice = []int{cluster.GetDesiredServersPerPod(processClass)}
} else {
serversPerPodSlice = cluster.Status.StorageServersPerDisk
}
}
if processClass.SupportsMultipleLogServers() {
// If the status field is not initialized we fallback to only the specified count
// in the cluster spec. This should only happen in the initial phase of a new cluster.
if len(cluster.Status.LogServersPerDisk) == 0 {
serversPerPodSlice = []int{cluster.GetDesiredServersPerPod(processClass)}
} else {
serversPerPodSlice = cluster.Status.LogServersPerDisk
}
}
if _, useUnifiedImage := imageTypes[FDBImageTypeUnified]; useUnifiedImage {
for _, serversPerPod := range serversPerPodSlice {
filename, jsonData, err := getDataForMonitorConf(cluster, FDBImageTypeUnified, processClass, serversPerPod)
if err != nil {
return nil, err
}
data[filename] = string(jsonData)
}
}
if _, useSplitImage := imageTypes[FDBImageTypeSplit]; useSplitImage {
for _, serversPerPod := range serversPerPodSlice {
err := setMonitorConfForFilename(cluster, data, GetConfigMapMonitorConfEntry(processClass, FDBImageTypeSplit, serversPerPod), connectionString, processClass, serversPerPod)
if err != nil {
return nil, err
}
}
}
}
if cluster.Spec.ConfigMap != nil {
for k, v := range cluster.Spec.ConfigMap.Data {
data[k] = v
}
}
metadata := getConfigMapMetadata(cluster)
metadata.OwnerReferences = BuildOwnerReference(cluster.TypeMeta, cluster.ObjectMeta)
return &corev1.ConfigMap{
ObjectMeta: metadata,
Data: data,
}, nil
}
func getConfigMapMetadata(cluster *fdbv1beta2.FoundationDBCluster) metav1.ObjectMeta {
var metadata metav1.ObjectMeta
if cluster.Spec.ConfigMap != nil {
metadata = GetObjectMetadata(cluster, &cluster.Spec.ConfigMap.ObjectMeta, "", "")
} else {
metadata = GetObjectMetadata(cluster, nil, "", "")
}
if metadata.Name == "" {
metadata.Name = fmt.Sprintf("%s-config", cluster.Name)
} else {
metadata.Name = fmt.Sprintf("%s-%s", cluster.Name, metadata.Name)
}
return metadata
}
func getDataForMonitorConf(cluster *fdbv1beta2.FoundationDBCluster, imageType FDBImageType, pClass fdbv1beta2.ProcessClass, serversPerPod int) (string, []byte, error) {
config := GetMonitorProcessConfiguration(cluster, pClass, serversPerPod, imageType, nil)
jsonData, err := json.Marshal(config)
if err != nil {
return "", nil, err
}
filename := GetConfigMapMonitorConfEntry(pClass, imageType, serversPerPod)
return filename, jsonData, nil
}
func setMonitorConfForFilename(cluster *fdbv1beta2.FoundationDBCluster, data map[string]string, filename string, connectionString string, processClass fdbv1beta2.ProcessClass, serversPerPod int) error {
if connectionString == "" {
data[filename] = ""
} else {
conf, err := GetMonitorConf(cluster, processClass, nil, serversPerPod)
if err != nil {
return err
}
data[filename] = conf
}
return nil
}
// GetConfigMapMonitorConfEntry returns the specific key for the monitor conf in the ConfigMap
func GetConfigMapMonitorConfEntry(pClass fdbv1beta2.ProcessClass, imageType FDBImageType, serversPerPod int) string {
if imageType == FDBImageTypeUnified {
if serversPerPod > 1 {
return fmt.Sprintf("fdbmonitor-conf-%s-json-multiple", pClass)
}
return fmt.Sprintf("fdbmonitor-conf-%s-json", pClass)
}
if serversPerPod > 1 {
return fmt.Sprintf("fdbmonitor-conf-%s-density-%d", pClass, serversPerPod)
}
return fmt.Sprintf("fdbmonitor-conf-%s", pClass)
}
// GetDynamicConfHash gets a hash of the data from the config map holding the
// cluster's dynamic conf.
//
// This will omit keys that we do not expect the Pods to reference e.g. for storage Pods only include the storage config.
func GetDynamicConfHash(configMap *corev1.ConfigMap, pClass fdbv1beta2.ProcessClass, imageType FDBImageType, serversPerPod int) (string, error) {
fields := []string{
ClusterFileKey,
GetConfigMapMonitorConfEntry(pClass, imageType, serversPerPod),
"running-version",
"ca-file",
"sidecar-conf",
}
var data = make(map[string]string, len(fields))
for _, field := range fields {
if val, ok := configMap.Data[field]; ok {
data[field] = val
}
}
return GetJSONHash(data)
}