-
Notifications
You must be signed in to change notification settings - Fork 156
/
kubeletdnat_controller.go
362 lines (313 loc) · 11.1 KB
/
kubeletdnat_controller.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 kubeletdnat
import (
"context"
"fmt"
"io"
"net"
"os/exec"
"sort"
"strconv"
"strings"
"github.com/go-test/deep"
"go.uber.org/zap"
"k8c.io/kubermatic/v2/pkg/provider"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/client-go/util/workqueue"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
ControllerName = "kubermatic_kubelet_dnat_controller"
)
// Reconciler updates iptable rules to match node addresses.
// Every node address gets a translation to the respective node-access (vpn) address.
type Reconciler struct {
ctrlruntimeclient.Client
nodeTranslationChainName string
nodeAccessNetwork net.IP
vpnInterface string
log *zap.SugaredLogger
}
// dnatRule stores address+port before translation (match) and
// provides address+port after translation.
type dnatRule struct {
originalTargetAddress string
originalTargetPort string
translatedAddress string
translatedPort string
}
// NewController creates a new controller for the specified data.
func Add(
mgr manager.Manager,
nodeTranslationChainName string,
nodeAccessNetwork net.IP,
log *zap.SugaredLogger,
vpnInterface string) error {
reconciler := &Reconciler{
Client: mgr.GetClient(),
nodeTranslationChainName: nodeTranslationChainName,
nodeAccessNetwork: nodeAccessNetwork,
vpnInterface: vpnInterface,
log: log,
}
ctrlOptions := controller.Options{
Reconciler: reconciler,
MaxConcurrentReconciles: 1,
}
c, err := controller.New(ControllerName, mgr, ctrlOptions)
if err != nil {
return err
}
return c.Watch(&source.Kind{Type: &corev1.Node{}}, &handler.Funcs{
CreateFunc: func(e event.CreateEvent, queue workqueue.RateLimitingInterface) { queue.Add(reconcile.Request{}) },
DeleteFunc: func(e event.DeleteEvent, queue workqueue.RateLimitingInterface) { queue.Add(reconcile.Request{}) },
GenericFunc: func(e event.GenericEvent, queue workqueue.RateLimitingInterface) { queue.Add(reconcile.Request{}) },
UpdateFunc: func(e event.UpdateEvent, queue workqueue.RateLimitingInterface) {
newNode, ok := e.ObjectNew.(*corev1.Node)
if !ok {
log.Warnf("Object from event was not a *corev1.Node. Instead got %T. Triggering a sync anyway", e.ObjectNew)
queue.Add(reconcile.Request{})
}
oldNode, ok := e.ObjectOld.(*corev1.Node)
if !ok {
log.Warnf("Object from event was not a *corev1.Node. Instead got %T. Triggering a sync anyway", e.ObjectOld)
queue.Add(reconcile.Request{})
}
// Only sync if nodes changed their addresses. Since Nodes get updated every 5 sec due to the HeartBeat
// it would otherwise cause a lot of useless syncs
if diff := deep.Equal(newNode.Status.Addresses, oldNode.Status.Addresses); diff != nil {
queue.Add(reconcile.Request{})
}
},
})
}
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
// Add a wrapping here so we can emit an event on error
err := r.syncDnatRules(ctx)
if err != nil {
r.log.Errorw("Failed reconciling", zap.Error(err))
}
return reconcile.Result{}, err
}
func (r *Reconciler) getDesiredRules(nodes []corev1.Node) []string {
rules := []string{}
for _, node := range nodes {
nodeRules, err := r.getRulesForNode(node)
if err != nil {
r.log.Errorw("could not generate rules for node, skipping", "node", node.Name, zap.Error(err))
continue
}
for _, rule := range nodeRules {
rules = append(rules, rule.RestoreLine(r.nodeTranslationChainName))
}
}
sort.Strings(rules)
return rules
}
// syncDnatRules will recreate the complete set of translation rules
// based on the list of nodes.
func (r *Reconciler) syncDnatRules(ctx context.Context) error {
// Get nodes from lister, make a copy.
nodeList := &corev1.NodeList{}
if err := r.List(ctx, nodeList); err != nil {
return fmt.Errorf("failed to list nodes: %v", err)
}
// Create the set of rules from all listed nodes.
desiredRules := r.getDesiredRules(nodeList.Items)
// Get the actual state (current iptable rules)
allActualRules, err := execSave()
if err != nil {
return fmt.Errorf("failed to read iptable rules: %v", err)
}
// filter out everything that's not relevant for us
actualRules, haveJump, haveMasquerade := r.filterDnatRules(allActualRules, r.nodeTranslationChainName)
if !equality.Semantic.DeepEqual(actualRules, desiredRules) || !haveJump || !haveMasquerade {
// Need to update chain in kernel.
r.log.Debugw("Updating iptables chain in kernel", "rules-count", len(desiredRules))
if err := r.applyDNATRules(desiredRules, haveJump, haveMasquerade); err != nil {
return fmt.Errorf("failed to apply iptable rules: %v", err)
}
}
return nil
}
// getNodeAddresses returns all relevant addresses of a node.
func getNodeAddresses(node corev1.Node) []string {
addressTypes := []corev1.NodeAddressType{corev1.NodeExternalIP, corev1.NodeInternalIP}
addresses := []string{}
for _, addressType := range addressTypes {
for _, address := range node.Status.Addresses {
if address.Type == addressType {
addresses = append(addresses, address.Address)
}
}
}
return addresses
}
func getInternalNodeAddress(node corev1.Node) (string, error) {
for _, address := range node.Status.Addresses {
if address.Type == corev1.NodeInternalIP {
return address.Address, nil
}
}
return "", fmt.Errorf("no internal address found; known addresses: %v", node.Status.Addresses)
}
// getRulesForNode determines the used kubelet address of a node
// and creates a dnatRule from it.
func (r *Reconciler) getRulesForNode(node corev1.Node) ([]*dnatRule, error) {
rules := []*dnatRule{}
port := int(node.Status.DaemonEndpoints.KubeletEndpoint.Port)
if port <= 0 {
port = provider.DefaultKubeletPort
}
internalIP, err := getInternalNodeAddress(node)
if err != nil {
return rules, fmt.Errorf("failed to get internal node address: %v", err)
}
octets := strings.Split(internalIP, ".")
for _, address := range getNodeAddresses(node) {
rule := &dnatRule{}
// Set matching part of the rule (original address).
rule.originalTargetAddress = address
rule.originalTargetPort = strconv.FormatInt(int64(port), 10)
// Set translation part of the rule (new destination)
// This implements the current node-access-network translations by
// changing the first two octets of the node-ip-address into the
// respective two octets of the node-access-network.
// The last two octets are the last two octets of the internal address
l := len(r.nodeAccessNetwork)
newAddress := fmt.Sprintf("%d.%d.%s.%s",
r.nodeAccessNetwork[l-4], r.nodeAccessNetwork[l-3],
octets[2], octets[3])
rule.translatedAddress = newAddress
rule.translatedPort = rule.originalTargetPort
rules = append(rules, rule)
}
return rules, nil
}
// applyRules creates a iptables-save file and pipes it to stdin of
// a iptables-restore process for atomically setting new rules.
// This function replaces a complete chain (removing all pre-existing rules).
func (r *Reconciler) applyDNATRules(rules []string, haveJump, haveMasquerade bool) error {
restore := []string{
"*nat",
fmt.Sprintf(":%s - [0:0]", r.nodeTranslationChainName)}
if !haveJump {
restore = append(restore,
fmt.Sprintf("-I OUTPUT -j %s", r.nodeTranslationChainName))
}
if !haveMasquerade {
restore = append(restore,
fmt.Sprintf("-I POSTROUTING -o %s -j MASQUERADE", r.vpnInterface))
}
restore = append(restore, rules...)
restore = append(restore, "COMMIT")
return execRestore(restore)
}
func execSave() ([]string, error) {
cmd := exec.Command("iptables-save", []string{"-t", "nat"}...)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to execute %q: %v. Output: \n%s", strings.Join(cmd.Args, " "), err, out)
}
return strings.Split(string(out), "\n"), err
}
func execRestore(rules []string) error {
cmd := exec.Command("iptables-restore", []string{"--noflush", "-v", "-T", "nat"}...)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
if _, err := io.WriteString(stdin, strings.Join(rules, "\n")+"\n"); err != nil {
return fmt.Errorf("failed to write to iptables-restore stdin: %v", err)
}
if err := stdin.Close(); err != nil {
return fmt.Errorf("failed to close iptables-restore stdin: %v", err)
}
out, err := cmd.CombinedOutput()
if err == nil {
return nil
}
if len(out) > 0 {
return fmt.Errorf("iptables-restore failed: %v (output: %s)", err, string(out))
}
return fmt.Errorf("iptables-restore failed: %v", err)
}
// GetMatchArgs returns iptables arguments to match for the
// rule's originalTargetAddress and Port.
func (rule *dnatRule) GetMatchArgs() []string {
return []string{
"-d", rule.originalTargetAddress + "/32",
"-p", "tcp",
"-m", "tcp",
"--dport", rule.originalTargetPort,
}
}
// GetTargetArgs returns iptables arguments to specify the
// rule's target after translation.
func (rule *dnatRule) GetTargetArgs() []string {
var target string
if len(rule.translatedAddress) > 0 {
target = rule.translatedAddress
}
target += ":"
if len(rule.translatedPort) > 0 {
target += rule.translatedPort
}
if len(target) == 0 {
return []string{}
}
return []string{
"-j", "DNAT",
"--to-destination", target,
}
}
// RestoreLine returns a line of `iptables-save`-file representing
// the rule.
func (rule *dnatRule) RestoreLine(chain string) string {
args := []string{"-A", chain}
args = append(args, rule.GetMatchArgs()...)
args = append(args, rule.GetTargetArgs()...)
return strings.Join(args, " ")
}
// filterDnatRules enumerates through all given rules and returns all
// rules matching the given chain. It also returns two booleans to
// indicate if the jump and the masquerade rule are present.
func (r *Reconciler) filterDnatRules(rules []string, chain string) ([]string, bool, bool) {
out := []string{}
haveJump := false
haveMasquerade := false
rulePrefix := fmt.Sprintf("-A %s ", chain)
jumpPattern := fmt.Sprintf("-A OUTPUT -j %s", chain)
masqPattern := fmt.Sprintf("-A POSTROUTING -o %s -j MASQUERADE", r.vpnInterface)
for _, rule := range rules {
if rule == jumpPattern {
haveJump = true
}
if rule == masqPattern {
haveMasquerade = true
}
if !strings.HasPrefix(rule, rulePrefix) {
continue
}
out = append(out, rule)
}
return out, haveJump, haveMasquerade
}