-
Notifications
You must be signed in to change notification settings - Fork 74
/
k8s_reactor.go
284 lines (234 loc) 路 6.81 KB
/
k8s_reactor.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
//go:build linux
// +build linux
package sidecar
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
gosync "sync"
"time"
"github.com/testground/sdk-go/runtime"
"github.com/testground/sdk-go/sync"
"github.com/testground/testground/pkg/docker"
"github.com/testground/testground/pkg/logging"
"github.com/containernetworking/cni/libcni"
"github.com/hashicorp/go-multierror"
lru "github.com/hashicorp/golang-lru"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
const (
controlNetworkIfname = "eth0"
dataNetworkIfname = "net1"
)
var (
kubeDnsClusterIP = net.IPv4(10, 32, 0, 0)
)
type K8sReactor struct {
gosync.Mutex
client sync.Client
manager *docker.Manager
allowedServices []AllowedService
runidsCache *lru.Cache
}
func NewK8sReactor() (Reactor, error) {
docker, err := docker.NewManager()
if err != nil {
return nil, err
}
client, err := sync.NewGenericClient(context.Background(), logging.S())
if err != nil {
return nil, err
}
cache, _ := lru.New(32)
r := &K8sReactor{
client: client,
manager: docker,
runidsCache: cache,
}
r.ResolveServices("constructor")
return r, nil
}
func (d *K8sReactor) ResolveServices(runid string) {
d.Lock()
defer d.Unlock()
if _, ok := d.runidsCache.Get(runid); ok {
return
}
wantedServices := []struct {
name string
host string
}{
{
"sync-service",
os.Getenv(EnvSyncServiceHost),
},
{
"influxdb",
os.Getenv(EnvInfluxdbHost),
},
}
var resolvedServices []AllowedService
for _, s := range wantedServices {
if s.host == "" {
continue
}
ip, err := net.ResolveIPAddr("ip4", s.host)
if err != nil {
logging.S().Warnw("failed to resolve host", "service", s.name, "host", s.host, "err", err.Error())
continue
}
resolvedServices = append(resolvedServices, AllowedService{s.name, ip.IP})
}
d.runidsCache.Add(runid, struct{}{})
d.allowedServices = resolvedServices
}
func (d *K8sReactor) Handle(ctx context.Context, handler InstanceHandler) error {
return d.manager.Watch(ctx, func(cctx context.Context, container *docker.ContainerRef) error {
logging.S().Debugw("got container", "container", container.ID)
inst, err := d.manageContainer(cctx, container)
if err != nil {
return fmt.Errorf("failed to initialise the container: %w", err)
}
if inst == nil {
logging.S().Debugw("ignoring container", "container", container.ID)
return nil
}
logging.S().Debugw("managing container", "container", container.ID)
err = handler(cctx, inst)
if err != nil {
return fmt.Errorf("container worker failed: %w", err)
}
return nil
})
}
func (d *K8sReactor) Close() error {
var err *multierror.Error
err = multierror.Append(err, d.manager.Close())
err = multierror.Append(err, d.client.Close())
return err.ErrorOrNil()
}
func (d *K8sReactor) manageContainer(ctx context.Context, container *docker.ContainerRef) (inst *Instance, err error) {
// Get the state/config of the cluster
info, err := container.Inspect(ctx)
if err != nil {
return nil, fmt.Errorf("inspect failed: %w", err)
}
if !info.State.Running {
return nil, fmt.Errorf("container is not running: %s", container.ID)
}
// Construct the runtime environment
params, err := runtime.ParseRunParams(info.Config.Env)
if err != nil {
return nil, fmt.Errorf("failed to parse run environment: %w", err)
}
if !params.TestSidecar {
return nil, nil
}
podName, ok := info.Config.Labels["io.kubernetes.pod.name"]
if !ok {
return nil, fmt.Errorf("couldn't get pod name from container labels for: %s", container.ID)
}
// Resolve allowed services, so that we update network routes
d.ResolveServices(params.TestRun)
err = waitForPodRunningPhase(ctx, podName)
if err != nil {
return nil, err
}
// Remove the TestOutputsPath. We can't store anything from the sidecar.
params.TestOutputsPath = ""
runenv := runtime.NewRunEnv(*params)
//////////////////
// NETWORKING //
//////////////////
// Initialise CNI config
cninet := libcni.NewCNIConfig(filepath.SplitList("/host/opt/cni/bin"), nil)
// Get a netlink handle.
nshandle, err := netns.GetFromPid(info.State.Pid)
if err != nil {
return nil, fmt.Errorf("failed to lookup the net namespace: %s", err)
}
defer nshandle.Close()
netlinkHandle, err := netlink.NewHandleAt(nshandle)
if err != nil {
return nil, fmt.Errorf("failed to get handle to network namespace: %w", err)
}
defer func() {
if err != nil {
netlinkHandle.Delete()
}
}()
// Finally, construct the network manager.
network := &K8sNetwork{
netnsPath: fmt.Sprintf("/proc/%d/ns/net", info.State.Pid),
cninet: cninet,
container: container,
subnet: runenv.TestSubnet.String(),
nl: netlinkHandle,
activeLinks: make(map[string]*k8sLink),
externalRouting: map[string]*route{},
}
// Remove all routes but redis and the data subnet
// We've found a control network (or some other network).
controlLink, err := netlinkHandle.LinkByName(controlNetworkIfname)
if err != nil {
return nil, fmt.Errorf("failed to get link by name %s: %w", controlNetworkIfname, err)
}
controlLinkRoutes, err := netlinkHandle.RouteList(controlLink, netlink.FAMILY_ALL)
if err != nil {
return nil, fmt.Errorf("failed to list routes for control link %s", controlLink.Attrs().Name)
}
// Adding DNS route
for _, route := range controlLinkRoutes {
if route.Dst == nil && route.Src == nil {
// if default route, get the gw and add a route for DNS
dnsRoute := route
dnsRoute.Src = nil
dnsRoute.Dst = &net.IPNet{
IP: kubeDnsClusterIP,
Mask: net.CIDRMask(32, 32),
}
logging.S().Debugw("adding dns route", "container", container.ID)
if err := netlinkHandle.RouteAdd(&dnsRoute); err != nil {
return nil, fmt.Errorf("failed to add dns route to pod: %v", err)
}
}
}
return NewInstance(d.client, runenv, info.Config.Hostname, network)
}
func waitForPodRunningPhase(ctx context.Context, podName string) error {
k8scfg, err := clientcmd.BuildConfigFromFlags("", "")
if err != nil {
return fmt.Errorf("error in wait for pod running phase: %v", err)
}
k8sClientset, err := kubernetes.NewForConfig(k8scfg)
if err != nil {
return fmt.Errorf("error in wait for pod running phase: %v", err)
}
var phase string
for {
select {
case <-ctx.Done():
return fmt.Errorf("wait for pod context (pod name: %s) erred with: %w", podName, ctx.Err())
default:
if phase == "Running" {
return nil
}
pod, err := k8sClientset.CoreV1().Pods("default").Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("error in wait for pod running phase: %v", err)
}
phase = string(pod.Status.Phase)
time.Sleep(1 * time.Second)
}
}
}
type AllowedService struct {
Name string
IP net.IP
}