forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology.go
221 lines (198 loc) · 7.63 KB
/
topology.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
// Copyright (c) 2017-2018 Tigera, Inc. All rights reserved.
//
// 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 infrastructure
import (
"context"
"fmt"
"time"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"regexp"
"github.com/projectcalico/felix/fv/containers"
api "github.com/projectcalico/libcalico-go/lib/apis/v3"
client "github.com/projectcalico/libcalico-go/lib/clientv3"
"github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/options"
)
type TopologyOptions struct {
FelixLogSeverity string
EnableIPv6 bool
ExtraEnvVars map[string]string
ExtraVolumes map[string]string
WithTypha bool
WithFelixTyphaTLS bool
TyphaLogSeverity string
IPIPEnabled bool
IPIPRoutesEnabled bool
InitialFelixConfiguration *api.FelixConfiguration
}
func DefaultTopologyOptions() TopologyOptions {
return TopologyOptions{
FelixLogSeverity: "info",
EnableIPv6: true,
ExtraEnvVars: map[string]string{},
ExtraVolumes: map[string]string{},
WithTypha: false,
WithFelixTyphaTLS: false,
TyphaLogSeverity: "info",
IPIPEnabled: true,
IPIPRoutesEnabled: true,
}
}
// StartSingleNodeEtcdTopology starts an etcd container and a single Felix container; it initialises
// the datastore and installs a Node resource for the Felix node.
func StartSingleNodeEtcdTopology(options TopologyOptions) (felix *Felix, etcd *containers.Container, calicoClient client.Interface) {
felixes, etcd, calicoClient := StartNNodeEtcdTopology(1, options)
felix = felixes[0]
return
}
// StartNNodeEtcdTopology starts an etcd container and a set of Felix hosts. If n > 1, sets
// up IPIP, otherwise this is skipped.
//
// - Configures an IPAM pool for 10.65.0.0/16 (so that Felix programs the all-IPAM blocks IP set)
// but (for simplicity) we don't actually use IPAM to assign IPs.
// - Configures routes between the hosts, giving each host 10.65.x.0/24, where x is the
// index in the returned array. When creating workloads, use IPs from the relevant block.
// - Configures the Tunnel IP for each host as 10.65.x.1.
func StartNNodeEtcdTopology(n int, opts TopologyOptions) (felixes []*Felix, etcd *containers.Container, client client.Interface) {
log.Infof("Starting a %d-node etcd topology.", n)
eds, err := GetEtcdDatastoreInfra()
Expect(err).ToNot(HaveOccurred())
etcd = eds.etcdContainer
felixes, client = StartNNodeTopology(n, opts, eds)
return
}
// StartSingleNodeEtcdTopology starts an etcd container and a single Felix container; it initialises
// the datastore and installs a Node resource for the Felix node.
func StartSingleNodeTopology(options TopologyOptions, infra DatastoreInfra) (felix *Felix, calicoClient client.Interface) {
felixes, calicoClient := StartNNodeTopology(1, options, infra)
felix = felixes[0]
return
}
// StartNNodeEtcdTopology starts an etcd container and a set of Felix hosts. If n > 1, sets
// up IPIP, otherwise this is skipped.
//
// - Configures an IPAM pool for 10.65.0.0/16 (so that Felix programs the all-IPAM blocks IP set)
// but (for simplicity) we don't actually use IPAM to assign IPs.
// - Configures routes between the hosts, giving each host 10.65.x.0/24, where x is the
// index in the returned array. When creating workloads, use IPs from the relevant block.
// - Configures the Tunnel IP for each host as 10.65.x.1.
func StartNNodeTopology(n int, opts TopologyOptions, infra DatastoreInfra) (felixes []*Felix, client client.Interface) {
log.Infof("Starting a %d-node topology.", n)
success := false
var err error
defer func() {
if !success {
log.WithError(err).Error("Failed to start topology, tearing down containers")
for _, felix := range felixes {
felix.Stop()
}
infra.Stop()
}
}()
// Get client.
client = infra.GetCalicoClient()
mustInitDatastore(client)
// If asked to, pre-create a felix configuration. We do this before enabling IPIP because IPIP set-up can
// create/update a FelixConfiguration as a side-effect.
if opts.InitialFelixConfiguration != nil {
log.WithField("config", opts.InitialFelixConfiguration).Info(
"Installing initial FelixConfiguration")
Eventually(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err = client.FelixConfigurations().Create(ctx, opts.InitialFelixConfiguration, options.SetOptions{})
if _, ok := err.(errors.ErrorResourceAlreadyExists); ok {
// Try to delete the unexpected config, then, if there's still time in the Eventually loop,
// we'll try to recreate
_, _ = client.FelixConfigurations().Delete(ctx, "default", options.DeleteOptions{})
}
return err
}, "10s").ShouldNot(HaveOccurred())
}
if n > 1 {
Eventually(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ipPool := api.NewIPPool()
ipPool.Name = "test-pool"
ipPool.Spec.CIDR = "10.65.0.0/16"
if opts.IPIPEnabled {
ipPool.Spec.IPIPMode = api.IPIPModeAlways
} else {
ipPool.Spec.IPIPMode = api.IPIPModeNever
}
_, err = client.IPPools().Create(ctx, ipPool, options.SetOptions{})
return err
}).ShouldNot(HaveOccurred())
}
if opts.WithTypha {
typha := RunTypha(infra, opts)
opts.ExtraEnvVars["FELIX_TYPHAADDR"] = typha.IP + ":5473"
}
for i := 0; i < n; i++ {
// Then start Felix and create a node for it.
felix := RunFelix(infra, opts)
if opts.IPIPEnabled {
infra.SetExpectedIPIPTunnelAddr(felix, i, bool(n > 1))
}
var w chan struct{}
if felix.ExpectedIPIPTunnelAddr != "" {
// If felix has an IPIP tunnel address defined, Felix may restart after loading its config.
// Handle that here by monitoring the log and waiting for the correct tunnel IP to show up
// before we return.
w = felix.WatchStdoutFor(regexp.MustCompile(
`"IpInIpTunnelAddr":"` + regexp.QuoteMeta(felix.ExpectedIPIPTunnelAddr) + `"`))
}
infra.AddNode(felix, i, bool(n > 1))
if w != nil {
// Wait for any Felix restart...
log.Info("Wait for Felix to restart")
Eventually(w, "10s").Should(BeClosed())
}
felixes = append(felixes, felix)
}
// Set up routes between the hosts, note: we're not using IPAM here but we set up similar
// CIDR-based routes.
for i, iFelix := range felixes {
for j, jFelix := range felixes {
if i == j {
continue
}
jBlock := fmt.Sprintf("10.65.%d.0/24", j)
if opts.IPIPEnabled && opts.IPIPRoutesEnabled {
err := iFelix.ExecMayFail("ip", "route", "add", jBlock, "via", jFelix.IP, "dev", "tunl0", "onlink")
Expect(err).ToNot(HaveOccurred())
} else {
err := iFelix.ExecMayFail("ip", "route", "add", jBlock, "via", jFelix.IP, "dev", "eth0")
Expect(err).ToNot(HaveOccurred())
}
}
}
success = true
return
}
func mustInitDatastore(client client.Interface) {
Eventually(func() error {
log.Info("Initializing the datastore...")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err := client.EnsureInitialized(
ctx,
"v3.0.0-test",
"felix-fv",
)
log.WithError(err).Info("EnsureInitialized result")
return err
}).ShouldNot(HaveOccurred(), "mustInitDatastore failed")
}