/
tunnelEndpointCreator-config.go
215 lines (192 loc) · 6.75 KB
/
tunnelEndpointCreator-config.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
package tunnelEndpointCreator
import (
"os"
"reflect"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/util/slice"
configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
crdclient "github.com/liqotech/liqo/pkg/crdClient"
liqonetutils "github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/utils"
)
func (tec *TunnelEndpointCreator) setNetParameters(config *configv1alpha1.ClusterConfig) {
podCIDR := config.Spec.LiqonetConfig.PodCIDR
serviceCIDR := config.Spec.LiqonetConfig.ServiceCIDR
if tec.PodCIDR != podCIDR {
if err := tec.IPManager.SetPodCIDR(podCIDR); err != nil {
klog.Error(err)
}
klog.Infof("PodCIDR set to %s", podCIDR)
tec.PodCIDR = podCIDR
}
if tec.ServiceCIDR != serviceCIDR {
if err := tec.IPManager.SetServiceCIDR(serviceCIDR); err != nil {
klog.Error(err)
}
klog.Infof("ServiceCIDR set to %s", serviceCIDR)
tec.ServiceCIDR = serviceCIDR
}
externalCIDR, err := tec.IPManager.GetExternalCIDR(liqonetutils.GetMask(podCIDR))
if err != nil {
klog.Error(err)
}
if tec.ExternalCIDR != externalCIDR {
klog.Infof("ExternalCIDR set to %s", externalCIDR)
tec.ExternalCIDR = externalCIDR
}
}
// Helper func that returns a true if the subnet slice passed as first parameter
// contains the subnet passed as second parameter. Otherwise it returns false.
func (tec *TunnelEndpointCreator) subnetSliceContains(subnetSlice []string, network string) bool {
return slice.ContainsString(subnetSlice, network, nil)
}
// Helper func that removes a subnet from the configuration file.
func (tec *TunnelEndpointCreator) removeReservedSubnet(network string) {
tec.ReservedSubnets = slice.RemoveString(tec.ReservedSubnets, network, nil)
}
// Helper func that adds a subnet from the configuration file.
func (tec *TunnelEndpointCreator) addReservedSubnet(network string) {
tec.ReservedSubnets = append(tec.ReservedSubnets, network)
}
// Helper func that removes a network pool from the configuration file.
func (tec *TunnelEndpointCreator) removeNetworkPool(network string) {
tec.AdditionalPools = slice.RemoveString(tec.AdditionalPools, network, nil)
}
// Helper func that adds a network pool from the configuration file.
func (tec *TunnelEndpointCreator) addNetworkPool(network string) {
tec.AdditionalPools = append(tec.AdditionalPools, network)
}
// getDifferences returns a boolean telling if the 2 slices received as parameters differs and eventually it returns
// a slice containing networks not present in the 1st slice and present in the 2nd and another one containing
// networks present in the 1st and not present in the 2nd.
func (tec *TunnelEndpointCreator) getDifferences(currentConfig, newConfig []string) (addedSubnets,
removedSubnets []string,
differs bool) {
//If the configuration is the same return
if reflect.DeepEqual(currentConfig, newConfig) {
return
}
addedSubnets = make([]string, 0)
removedSubnets = make([]string, 0)
differs = true
for _, network := range newConfig {
if contained := tec.subnetSliceContains(currentConfig, network); !contained {
addedSubnets = append(addedSubnets, network)
}
}
for _, network := range currentConfig {
if contained := tec.subnetSliceContains(newConfig, network); !contained {
removedSubnets = append(removedSubnets, network)
}
}
return
}
func (tec *TunnelEndpointCreator) updateReservedSubnets(reservedSubnets []string) error {
addedSubnets, removedSubnets, differs := tec.getDifferences(tec.ReservedSubnets, reservedSubnets)
if !differs {
return nil
}
//here we start to remove subnets from the reserved slice
if len(removedSubnets) > 0 {
for _, subnet := range removedSubnets {
//free subnet in ipam
klog.Infof("Freeing reserved subnet %s", subnet)
if err := tec.IPManager.FreeReservedSubnet(subnet); err != nil {
klog.Error(err)
}
//remove the subnet from the reserved ones
tec.removeReservedSubnet(subnet)
}
}
if len(addedSubnets) > 0 {
for _, subnet := range addedSubnets {
klog.Infof("Reserving subnet %s", subnet)
if err := tec.IPManager.AcquireReservedSubnet(subnet); err != nil {
klog.Error(err)
}
tec.addReservedSubnet(subnet)
}
}
return nil
}
func (tec *TunnelEndpointCreator) updatePools(additionalPools []string) error {
addedSubnets, removedSubnets, differs := tec.getDifferences(tec.AdditionalPools, additionalPools)
if !differs {
return nil
}
//here we start to remove network pools from ipam configuration
if len(removedSubnets) > 0 {
for _, subnet := range removedSubnets {
//remove network pool in ipam
klog.Infof("Removing network pool %s", subnet)
if err := tec.IPManager.RemoveNetworkPool(subnet); err != nil {
return err
}
//remove the subnet from the reserved ones
tec.removeNetworkPool(subnet)
}
}
if len(addedSubnets) > 0 {
for _, subnet := range addedSubnets {
klog.Infof("Adding network pool %s", subnet)
if err := tec.IPManager.AddNetworkPool(subnet); err != nil {
return err
}
tec.addNetworkPool(subnet)
}
}
return nil
}
func (tec *TunnelEndpointCreator) getReservedSubnets(config *configv1alpha1.ClusterConfig) []string {
reservedSubnets := make([]string, 0)
liqonetConfig := config.Spec.LiqonetConfig
// Cast CIDR to normal string and append
for _, cidr := range liqonetConfig.ReservedSubnets {
reservedSubnets = append(reservedSubnets, string(cidr))
}
return reservedSubnets
}
func (tec *TunnelEndpointCreator) getAdditionalPools(config *configv1alpha1.ClusterConfig) []string {
additionalPools := make([]string, 0)
liqonetConfig := config.Spec.LiqonetConfig
// Cast CIDR to normal string and append
for _, cidr := range liqonetConfig.AdditionalPools {
additionalPools = append(additionalPools, string(cidr))
}
return additionalPools
}
// WatchConfiguration start watcher of clusterconfig resource.
func (tec *TunnelEndpointCreator) WatchConfiguration(config *rest.Config, gv *schema.GroupVersion) {
config.ContentConfig.GroupVersion = gv
config.APIPath = "/apis"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
config.UserAgent = rest.DefaultKubernetesUserAgent()
CRDclient, err := crdclient.NewFromConfig(config)
if err != nil {
klog.Error(err, err.Error())
os.Exit(1)
}
go utils.WatchConfiguration(func(configuration *configv1alpha1.ClusterConfig) {
reservedSubnets := tec.getReservedSubnets(configuration)
additionalPools := tec.getAdditionalPools(configuration)
tec.setNetParameters(configuration)
err = tec.updateReservedSubnets(reservedSubnets)
if err != nil {
klog.Error(err)
return
}
err = tec.updatePools(additionalPools)
if err != nil {
klog.Error(err)
return
}
if !tec.cfgConfigured {
tec.WaitConfig.Done()
klog.Infof("called done on waitgroup")
tec.cfgConfigured = true
}
}, CRDclient, "")
}