-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
configMapWriter.go
282 lines (239 loc) · 7.14 KB
/
configMapWriter.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
package fbcindex
import (
"bytes"
"compress/gzip"
"fmt"
"strings"
"text/template"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
yamlSeparator = "\n---\n"
gzipSuffixLength = 13
maxGZIPLength = maxConfigMapSize - gzipSuffixLength
ConfigMapEncodingAnnotationKey = "olm.contentEncoding"
ConfigMapEncodingAnnotationGzip = "gzip+base64"
)
type configMapWriter interface {
newConfigMap() *corev1.ConfigMap
writeYaml([]string) ([]*corev1.ConfigMap, error)
getFilePath() string
getCommandTemplate() *template.Template
}
type defaultCMWriter struct {
cmName string
namespace string
template *template.Template
}
func newDefaultWriter(name, namespace string) *defaultCMWriter {
t := template.Must(template.New("cmd").Parse(fbcCmdTemplate))
return &defaultCMWriter{
cmName: name,
namespace: namespace,
template: t,
}
}
// container entrypoint command for FBC type images.
const fbcCmdTemplate = `opm serve {{ .FBCIndexRootDir}} -p {{ .GRPCPort }}`
func (cmw defaultCMWriter) newConfigMap() *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: cmw.namespace,
},
Data: map[string]string{},
}
}
func (cmw defaultCMWriter) writeYaml(yamlDefs []string) ([]*corev1.ConfigMap, error) {
cm := cmw.newConfigMap()
cm.SetName(fmt.Sprintf("%s-partition-1", cmw.cmName))
configMaps := []*corev1.ConfigMap{cm}
partitionCount := 1
// for each chunk of yaml see if it can be added to the ConfigMap partition
for _, yamlDef := range yamlDefs {
yamlDef = strings.TrimSpace(yamlDef)
if len(strings.TrimSpace(yamlDef)) == 0 {
continue
}
// If the ConfigMap has data then lets attempt to add to it
if len(cm.Data) != 0 {
// Create a copy to use to verify that adding the data doesn't
// exceed the max ConfigMap size of 1 MiB.
tempCm := cm.DeepCopy()
tempCm.Data[defaultConfigMapKey] = tempCm.Data[defaultConfigMapKey] + yamlSeparator + yamlDef
// if it would be too large adding the data then partition it.
if tempCm.Size() >= maxConfigMapSize {
// Create a new ConfigMap
cm = cmw.newConfigMap()
// Set the ConfigMap name based on the partition it is
cm.SetName(fmt.Sprintf("%s-partition-%d", cmw.cmName, partitionCount+1))
// Since adding this data would have made the previous
// ConfigMap too large, add it to this new one.
// No chunk of YAML from the bundle should cause
// the ConfigMap size to exceed 1 MiB and if
// somehow it does then there is a problem with the
// YAML itself. We can't reasonably break it up smaller
// since it is a single object.
cm.Data[defaultConfigMapKey] = yamlDef
// Add the ConfigMap to the list of ConfigMaps
configMaps = append(configMaps, cm)
} else {
// if adding the data to the ConfigMap
// doesn't make the ConfigMap exceed the
// size limit then actually add it.
cm.Data = tempCm.Data
}
} else {
// If there is no data in the ConfigMap
// then this is the first pass. Since it is
// the first pass go ahead and add the data.
cm.Data[defaultConfigMapKey] = yamlDef
}
}
return configMaps, nil
}
func (cmw defaultCMWriter) getFilePath() string {
return fmt.Sprintf("%s.yaml", defaultConfigMapKey)
}
func (cmw defaultCMWriter) getCommandTemplate() *template.Template {
return cmw.template
}
type gzipCMWriter struct {
actualBuff *bytes.Buffer
helperBuff *bytes.Buffer
actualWriter *gzip.Writer
helperWriter *gzip.Writer
cmName string
namespace string
template *template.Template
}
func newGZIPWriter(name, namespace string) *gzipCMWriter {
actualBuff := &bytes.Buffer{}
helperBuff := &bytes.Buffer{}
t := template.Must(template.New("cmd").Parse(fbcGzipCmdTemplate))
return &gzipCMWriter{
actualBuff: actualBuff,
helperBuff: helperBuff,
actualWriter: gzip.NewWriter(actualBuff),
helperWriter: gzip.NewWriter(helperBuff),
cmName: name,
namespace: namespace,
template: t,
}
}
// container entrypoint command for gzipped FBC type images.
const fbcGzipCmdTemplate = `` +
`serverDir=/var/tmp/{{ .FBCIndexRootDir }}/;` +
`mkdir ${serverDir};` +
`for dir in {{ .FBCIndexRootDir }}/*configmap-partition-*;` +
`do targetDir="/var/tmp/${dir}";` +
`mkdir ${targetDir};` +
`for f in ${dir}/*gz; ` +
`do cat $f | gzip -d -c > "/var/tmp/${f%.*}";` +
`done;` +
`done;` +
`opm serve ${serverDir} -p {{ .GRPCPort }}`
func (cmw *gzipCMWriter) reset() {
cmw.actualBuff.Reset()
cmw.actualWriter.Reset(cmw.actualBuff)
cmw.helperBuff.Reset()
cmw.helperWriter.Reset(cmw.helperBuff)
}
func (cmw *gzipCMWriter) newConfigMap() *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: cmw.namespace,
Annotations: map[string]string{
ConfigMapEncodingAnnotationKey: ConfigMapEncodingAnnotationGzip,
},
},
BinaryData: map[string][]byte{},
}
}
func (cmw *gzipCMWriter) writeYaml(yamlDefs []string) ([]*corev1.ConfigMap, error) {
defer cmw.reset()
cm := cmw.newConfigMap()
cm.SetName(fmt.Sprintf("%s-partition-1", cmw.cmName))
configMaps := []*corev1.ConfigMap{cm}
partitionCount := 1
// for each chunk of yaml see if it can be added to the ConfigMap partition
for _, yamlDef := range yamlDefs {
yamlDef = strings.TrimSpace(yamlDef)
if len(yamlDef) == 0 {
continue
}
if cmw.actualBuff.Len() > 0 {
data := []byte(yamlSeparator + yamlDef)
_, err := cmw.helperWriter.Write(data)
if err != nil {
return nil, err
}
err = cmw.helperWriter.Flush()
if err != nil {
return nil, err
}
if cm.Size()+cmw.helperBuff.Len() > maxGZIPLength {
err = cmw.actualWriter.Close()
if err != nil {
return nil, err
}
err = cmw.actualWriter.Flush()
if err != nil {
return nil, err
}
cm.BinaryData[defaultConfigMapKey] = make([]byte, cmw.actualBuff.Len())
copy(cm.BinaryData[defaultConfigMapKey], cmw.actualBuff.Bytes())
cmw.reset()
partitionCount++
cm = cmw.newConfigMap()
cm.SetName(fmt.Sprintf("%s-partition-%d", cmw.cmName, partitionCount))
configMaps = append(configMaps, cm)
data = []byte(yamlDef)
_, err = cmw.helperWriter.Write(data)
if err != nil {
return nil, err
}
_, err = cmw.actualWriter.Write(data)
if err != nil {
return nil, err
}
} else {
_, err = cmw.actualWriter.Write(data)
if err != nil {
return nil, err
}
}
} else {
data := []byte(yamlDef)
_, err := cmw.helperWriter.Write(data)
if err != nil {
return nil, err
}
_, err = cmw.actualWriter.Write(data)
if err != nil {
return nil, err
}
}
}
// write the data of the last cm
err := cmw.actualWriter.Close()
if err != nil {
return nil, err
}
cm.BinaryData[defaultConfigMapKey] = cmw.actualBuff.Bytes()
return configMaps, nil
}
func (cmw *gzipCMWriter) getFilePath() string {
return fmt.Sprintf("%s.yaml.gz", defaultConfigMapKey)
}
func (cmw *gzipCMWriter) getCommandTemplate() *template.Template {
return cmw.template
}