-
Notifications
You must be signed in to change notification settings - Fork 149
/
dynamicconfig.go
91 lines (78 loc) · 2.14 KB
/
dynamicconfig.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
package daemon
import (
"context"
"fmt"
"github.com/go-playground/mold/v4/modifiers"
"github.com/go-playground/validator/v10"
corev1 "k8s.io/api/core/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
cfg *Config
)
func GetConfig() *Config {
return cfg
}
func SetConfig(c *Config) {
cfg = c
}
// ConfigFromConfigMap get eni-config form configmap if nodeName is not empty dynamic config is read
func ConfigFromConfigMap(ctx context.Context, client client.Client, nodeName string) (*Config, error) {
baseConfig, err := eniConfigFromConfigMap(ctx, client, "kube-system", "eni-config")
if err != nil {
return nil, err
}
if baseConfig == "" {
return nil, fmt.Errorf("terway configmap has no eni_conf filed")
}
topConfig := ""
if nodeName != "" {
cmName := nodeDynamicConfigName(ctx, client, nodeName)
if cmName != "" {
topConfig, err = eniConfigFromConfigMap(ctx, client, "kube-system", cmName)
if err != nil {
return nil, err
}
}
}
eniConf, err := MergeConfigAndUnmarshal([]byte(topConfig), []byte(baseConfig))
if err != nil {
return nil, fmt.Errorf("error parse terway configmap eni-config, %w", err)
}
mod := modifiers.New()
err = mod.Struct(ctx, eniConf)
if err != nil {
return nil, err
}
err = validator.New().Struct(eniConf)
if err != nil {
return nil, err
}
sgs := eniConf.GetSecurityGroups()
if len(sgs) > 5 {
return nil, fmt.Errorf("security groups should not be more than 5, current %d", len(sgs))
}
return eniConf, nil
}
func eniConfigFromConfigMap(ctx context.Context, client client.Client, namespace, name string) (string, error) {
cm := &corev1.ConfigMap{}
err := client.Get(ctx, k8stypes.NamespacedName{
Namespace: namespace,
Name: name,
}, cm)
if err != nil {
return "", fmt.Errorf("error get configmap %s/%s, %w", namespace, name, err)
}
return cm.Data["eni_conf"], nil
}
func nodeDynamicConfigName(ctx context.Context, client client.Client, nodeName string) string {
node := &corev1.Node{}
err := client.Get(ctx, k8stypes.NamespacedName{
Name: nodeName,
}, node)
if err != nil {
return ""
}
return node.Labels["terway-config"]
}