/
export_k8s_configmap_util.go
64 lines (50 loc) · 1.2 KB
/
export_k8s_configmap_util.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
package pkg
import (
"context"
"os"
"path/filepath"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
)
func (initp *InitParams) ExportK8sConfigmap(ctx context.Context, namespace, cmName, toDir string) error {
configMap := &corev1.ConfigMap{}
if err := initp.k8sClient.Get(ctx,
types.NamespacedName{Name: cmName, Namespace: namespace}, configMap); err != nil {
return err
}
if err := os.MkdirAll(toDir, 0755); err != nil { //nolint:gocritic // file permission
return err
}
for key, value := range configMap.Data {
f, err := os.Create(filepath.Join(toDir, key))
if err != nil {
return err
}
if _, err = f.WriteString(value); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
}
for key, value := range configMap.BinaryData {
f, err := os.Create(filepath.Join(toDir, key))
if err != nil {
return err
}
if _, err = f.Write(value); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
}
initp.logger.Info("Created and populated config map directory", "cm-name", cmName, "dir", toDir)
return nil
}