forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectivity.go
261 lines (221 loc) · 14.3 KB
/
connectivity.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cli
import (
"context"
"fmt"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/cilium/cilium/cilium-cli/api"
"github.com/cilium/cilium/cilium-cli/connectivity"
"github.com/cilium/cilium/cilium-cli/connectivity/check"
"github.com/cilium/cilium/cilium-cli/defaults"
"github.com/cilium/cilium/cilium-cli/sysdump"
"github.com/cilium/cilium/pkg/option"
)
func newCmdConnectivity(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
Use: "connectivity",
Short: "Connectivity troubleshooting",
Long: ``,
}
cmd.AddCommand(newCmdConnectivityTest(hooks))
cmd.AddCommand(newCmdConnectivityPerf(hooks))
return cmd
}
var params = check.Parameters{
ExternalDeploymentPort: 8080,
EchoServerHostPort: 4000,
JunitProperties: make(map[string]string),
NodeSelector: make(map[string]string),
Writer: os.Stdout,
SysdumpOptions: sysdump.Options{
LargeSysdumpAbortTimeout: sysdump.DefaultLargeSysdumpAbortTimeout,
LargeSysdumpThreshold: sysdump.DefaultLargeSysdumpThreshold,
Writer: os.Stdout,
},
}
var tests []string
func RunE(hooks api.Hooks) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, _ []string) error {
params.CiliumNamespace = namespace
for _, test := range tests {
if strings.HasPrefix(test, "!") {
rgx, err := regexp.Compile(strings.TrimPrefix(test, "!"))
if err != nil {
return fmt.Errorf("test filter: %w", err)
}
params.SkipTests = append(params.SkipTests, rgx)
} else {
rgx, err := regexp.Compile(test)
if err != nil {
return fmt.Errorf("test filter: %w", err)
}
params.RunTests = append(params.RunTests, rgx)
}
}
logger := check.NewConcurrentLogger(params.Writer, params.TestConcurrency)
connTests, err := newConnectivityTests(params, hooks, logger)
if err != nil {
return err
}
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
if params.Timeout > 0 {
timeoutCtx, cancelFunc := context.WithTimeoutCause(ctx, params.Timeout, fmt.Errorf("connectivity test suite timeout (%s) reached", params.Timeout))
defer cancelFunc()
ctx = timeoutCtx
}
go func() {
<-ctx.Done()
connTests[0].Logf("Cancellation request (%s) received, cancelling tests...", context.Cause(ctx))
}()
logger.Start(ctx)
defer logger.Stop()
return connectivity.Run(ctx, connTests, hooks)
}
}
func newCmdConnectivityTest(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Short: "Validate connectivity in cluster",
Long: ``,
RunE: RunE(hooks),
}
cmd.Flags().BoolVar(¶ms.SingleNode, "single-node", false, "Limit to tests able to run on a single node")
cmd.Flags().BoolVar(¶ms.PrintFlows, "print-flows", false, "Print flow logs for each test")
cmd.Flags().DurationVar(¶ms.PostTestSleepDuration, "post-test-sleep", 0, "Wait time after each test before next test starts")
cmd.Flags().BoolVar(¶ms.ForceDeploy, "force-deploy", false, "Force re-deploying test artifacts")
cmd.Flags().BoolVar(¶ms.Hubble, "hubble", true, "Automatically use Hubble for flow validation & troubleshooting")
cmd.Flags().StringVar(¶ms.HubbleServer, "hubble-server", "localhost:4245", "Address of the Hubble endpoint for flow validation")
cmd.Flags().StringVar(¶ms.AgentDaemonSetName, "agent-daemonset-name", defaults.AgentDaemonSetName, "Name of cilium agent daemonset")
cmd.Flags().StringVar(¶ms.AgentPodSelector, "agent-pod-selector", defaults.AgentPodSelector, "Label on cilium-agent pods to select with")
cmd.Flags().StringVar(¶ms.CiliumPodSelector, "cilium-pod-selector", defaults.CiliumPodSelector, "Label selector matching all cilium-related pods")
cmd.Flags().Var(¶ms.NamespaceAnnotations, "namespace-annotations", "Add annotations to the connectivity test namespace, e.g. '{\"foo\":\"bar\"}'")
cmd.Flags().MarkHidden("namespace-annotations")
cmd.Flags().MarkHidden("deployment-pod-annotations")
cmd.Flags().StringVar(¶ms.MultiCluster, "multi-cluster", "", "Test across clusters to given context")
cmd.Flags().StringSliceVar(&tests, "test", []string{}, "Run tests that match one of the given regular expressions, skip tests by starting the expression with '!', target Scenarios with e.g. '/pod-to-cidr'")
cmd.Flags().StringVar(¶ms.FlowValidation, "flow-validation", check.FlowValidationModeWarning, "Enable Hubble flow validation { disabled | warning | strict }")
cmd.Flags().BoolVar(¶ms.AllFlows, "all-flows", false, "Print all flows during flow validation")
cmd.Flags().StringVar(¶ms.AssumeCiliumVersion, "assume-cilium-version", "", "Assume Cilium version for connectivity tests")
cmd.Flags().BoolVarP(¶ms.Verbose, "verbose", "v", false, "Show informational messages and don't buffer any lines")
cmd.Flags().BoolVarP(¶ms.Timestamp, "timestamp", "t", false, "Show timestamp in messages")
cmd.Flags().BoolVarP(¶ms.PauseOnFail, "pause-on-fail", "p", false, "Pause execution on test failure")
cmd.Flags().StringVar(¶ms.ExternalTarget, "external-target", "one.one.one.one.", "Domain name to use as external target in connectivity tests")
cmd.Flags().StringVar(¶ms.ExternalTargetCANamespace, "external-target-ca-namespace", "", "Namespace of the CA secret for the external target. Used by client-egress-l7-tls test cases.")
cmd.Flags().StringVar(¶ms.ExternalTargetCAName, "external-target-ca-name", "cabundle", "Name of the CA secret for the external target. Used by client-egress-l7-tls test cases.")
cmd.Flags().StringVar(¶ms.ExternalCIDR, "external-cidr", "1.0.0.0/8", "CIDR to use as external target in connectivity tests")
cmd.Flags().StringVar(¶ms.ExternalIP, "external-ip", "1.1.1.1", "IP to use as external target in connectivity tests")
cmd.Flags().StringVar(¶ms.ExternalOtherIP, "external-other-ip", "1.0.0.1", "Other IP to use as external target in connectivity tests")
cmd.Flags().StringVar(¶ms.ServiceType, "service-type", "NodePort", "Type of Kubernetes Services created for connectivity tests")
cmd.Flags().StringSliceVar(¶ms.NodeCIDRs, "node-cidr", nil, "one or more CIDRs that cover all nodes in the cluster")
cmd.Flags().StringVar(¶ms.JunitFile, "junit-file", "", "Generate junit report and write to file")
cmd.Flags().Var(option.NewNamedMapOptions("junit-property", ¶ms.JunitProperties, nil), "junit-property", "Add key=value properties to the generated junit file")
cmd.Flags().BoolVar(¶ms.SkipIPCacheCheck, "skip-ip-cache-check", true, "Skip IPCache check")
cmd.Flags().MarkHidden("skip-ip-cache-check")
cmd.Flags().BoolVar(¶ms.IncludeUnsafeTests, "include-unsafe-tests", false, "Include tests which can modify cluster nodes state")
cmd.Flags().MarkHidden("include-unsafe-tests")
cmd.Flags().BoolVar(¶ms.K8sLocalHostTest, "k8s-localhost-test", false, "Include tests which test for policy enforcement for the k8s entity on its own host")
cmd.Flags().MarkHidden("k8s-localhost-test")
cmd.Flags().StringVar(¶ms.K8sVersion, "k8s-version", "", "Kubernetes server version in case auto-detection fails")
cmd.Flags().StringVar(¶ms.HelmChartDirectory, "chart-directory", "", "Helm chart directory")
cmd.Flags().StringVar(¶ms.HelmValuesSecretName, "helm-values-secret-name", defaults.HelmValuesSecretName, "Secret name to store the auto-generated helm values file. The namespace is the same as where Cilium will be installed")
cmd.Flags().StringVar(¶ms.CurlImage, "curl-image", defaults.ConnectivityCheckAlpineCurlImage, "Image path to use for curl")
cmd.Flags().StringVar(¶ms.JSONMockImage, "json-mock-image", defaults.ConnectivityCheckJSONMockImage, "Image path to use for json mock")
cmd.Flags().StringVar(¶ms.DNSTestServerImage, "dns-test-server-image", defaults.ConnectivityDNSTestServerImage, "Image path to use for CoreDNS")
cmd.Flags().StringVar(¶ms.TestConnDisruptImage, "test-conn-disrupt-image", defaults.ConnectivityTestConnDisruptImage, "Image path to use for connection disruption tests")
cmd.Flags().StringVar(¶ms.FRRImage, "frr-image", defaults.ConnectivityTestFRRImage, "Image path to use for FRR")
cmd.Flags().UintVar(¶ms.Retry, "retry", defaults.ConnectRetry, "Number of retries on connection failure to external targets")
cmd.Flags().DurationVar(¶ms.RetryDelay, "retry-delay", defaults.ConnectRetryDelay, "Delay between retries for external targets")
cmd.Flags().DurationVar(¶ms.ConnectTimeout, "connect-timeout", defaults.ConnectTimeout, "Maximum time to allow initiation of the connection to take")
cmd.Flags().DurationVar(¶ms.RequestTimeout, "request-timeout", defaults.RequestTimeout, "Maximum time to allow a request to take")
cmd.Flags().BoolVar(¶ms.CurlInsecure, "curl-insecure", false, "Pass --insecure to curl")
cmd.Flags().BoolVar(¶ms.CollectSysdumpOnFailure, "collect-sysdump-on-failure", false, "Collect sysdump after a test fails")
sysdump.InitSysdumpFlags(cmd, ¶ms.SysdumpOptions, "sysdump-", hooks)
cmd.Flags().BoolVar(¶ms.IncludeConnDisruptTest, "include-conn-disrupt-test", false, "Include conn disrupt test")
cmd.Flags().BoolVar(¶ms.ConnDisruptTestSetup, "conn-disrupt-test-setup", false, "Set up conn disrupt test dependencies")
cmd.Flags().StringVar(¶ms.ConnDisruptTestRestartsPath, "conn-disrupt-test-restarts-path", "/tmp/cilium-conn-disrupt-restarts", "Conn disrupt test temporary result file (used internally)")
cmd.Flags().StringVar(¶ms.ConnDisruptTestXfrmErrorsPath, "conn-disrupt-test-xfrm-errors-path", "/tmp/cilium-conn-disrupt-xfrm-errors", "Conn disrupt test temporary result file (used internally)")
cmd.Flags().DurationVar(¶ms.ConnDisruptDispatchInterval, "conn-disrupt-dispatch-interval", 0, "TCP packet dispatch interval")
cmd.Flags().StringSliceVar(¶ms.ExpectedDropReasons, "expected-drop-reasons", defaults.ExpectedDropReasons, "List of expected drop reasons")
cmd.Flags().MarkHidden("expected-drop-reasons")
cmd.Flags().StringSliceVar(¶ms.ExpectedXFRMErrors, "expected-xfrm-errors", defaults.ExpectedXFRMErrors, "List of expected XFRM errors")
cmd.Flags().MarkHidden("expected-xfrm-errors")
cmd.Flags().BoolVar(¶ms.FlushCT, "flush-ct", false, "Flush conntrack of Cilium on each node")
cmd.Flags().MarkHidden("flush-ct")
cmd.Flags().StringVar(¶ms.SecondaryNetworkIface, "secondary-network-iface", "", "Secondary network iface name (e.g., to test NodePort BPF on multiple networks)")
cmd.Flags().DurationVar(¶ms.Timeout, "timeout", defaults.ConnectivityTestSuiteTimeout, "Maximum time to allow the connectivity test suite to take")
cmd.Flags().IntVar(¶ms.TestConcurrency, "test-concurrency", 1, "Count of namespaces to perform the connectivity tests in parallel (value <= 0 will be treated as 1)")
hooks.AddConnectivityTestFlags(cmd.Flags())
registerCommonFlags(cmd.Flags())
return cmd
}
func newCmdConnectivityPerf(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
Use: "perf",
Short: "Test network performance",
Long: ``,
PreRun: func(_ *cobra.Command, _ []string) {
// This is a bit of hack that allows us to override default values
// of these parameters that are not visible in perf subcommand options
// as we can't have different defaults specified in test and perf subcommands
// and both of these commands share the same RunE for now.
params.Perf = true
params.ForceDeploy = true
},
RunE: RunE(hooks),
}
cmd.Flags().DurationVar(¶ms.PerfParameters.Duration, "duration", 10*time.Second, "Duration for the Performance test to run")
cmd.Flags().IntVar(¶ms.PerfParameters.MessageSize, "msg-size", 1024, "Size of message to use in UDP test")
cmd.Flags().BoolVar(¶ms.PerfParameters.CRR, "crr", false, "Run CRR test")
cmd.Flags().BoolVar(¶ms.PerfParameters.RR, "rr", true, "Run RR test")
cmd.Flags().BoolVar(¶ms.PerfParameters.UDP, "udp", false, "Run UDP tests")
cmd.Flags().BoolVar(¶ms.PerfParameters.Throughput, "throughput", true, "Run throughput test")
cmd.Flags().BoolVar(¶ms.PerfParameters.Mixed, "mixed", false, "Run pod-to-host and host-to-pod tests (only works if both host-net=true and pod-net=true)")
cmd.Flags().IntVar(¶ms.PerfParameters.Samples, "samples", 1, "Number of Performance samples to capture (how many times to run each test)")
cmd.Flags().BoolVar(¶ms.PerfParameters.HostNet, "host-net", true, "Test host network")
cmd.Flags().BoolVar(¶ms.PerfParameters.PodNet, "pod-net", true, "Test pod network")
cmd.Flags().StringVar(¶ms.PerfParameters.Image, "performance-image", defaults.ConnectivityPerformanceImage, "Image path to use for performance")
cmd.Flags().StringVar(¶ms.PerfParameters.ReportDir, "report-dir", "", "Directory to save perf results in json format")
registerCommonFlags(cmd.Flags())
return cmd
}
func registerCommonFlags(flags *pflag.FlagSet) {
flags.BoolVarP(¶ms.Debug, "debug", "d", false, "Show debug messages")
flags.Var(option.NewNamedMapOptions("node-selector", ¶ms.NodeSelector, nil), "node-selector", "Restrict connectivity pods to nodes matching this label")
flags.StringVar(¶ms.TestNamespace, "test-namespace", defaults.ConnectivityCheckNamespace, "Namespace to perform the connectivity in (always suffixed with a sequence number to be compliant with test-concurrency param, e.g.: cilium-test-1)")
flags.Var(¶ms.DeploymentAnnotations, "deployment-pod-annotations", "Add annotations to the connectivity pods, e.g. '{\"client\":{\"foo\":\"bar\"}}'")
}
func newConnectivityTests(
params check.Parameters,
hooks api.Hooks,
logger *check.ConcurrentLogger,
) ([]*check.ConnectivityTest, error) {
if params.TestConcurrency < 1 {
fmt.Printf("--test-concurrency parameter value is invalid [%d], using 1 instead\n", params.TestConcurrency)
params.TestConcurrency = 1
}
connTests := make([]*check.ConnectivityTest, 0, params.TestConcurrency)
for i := 0; i < params.TestConcurrency; i++ {
params := params
params.TestNamespace = fmt.Sprintf("%s-%d", params.TestNamespace, i+1)
params.TestNamespaceIndex = i
if params.ExternalTargetCANamespace == "" {
params.ExternalTargetCANamespace = params.TestNamespace
}
params.ExternalDeploymentPort += i
params.EchoServerHostPort += i
cc, err := check.NewConnectivityTest(k8sClient, params, hooks, logger)
if err != nil {
return nil, err
}
connTests = append(connTests, cc)
}
return connTests, nil
}