-
Notifications
You must be signed in to change notification settings - Fork 2
/
action_network_delay.go
executable file
·114 lines (103 loc) · 3.52 KB
/
action_network_delay.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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
package extcontainer
import (
"context"
"encoding/json"
"fmt"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/action-kit/go/action_kit_commons/network"
"github.com/steadybit/action-kit/go/action_kit_commons/runc"
"github.com/steadybit/action-kit/go/action_kit_sdk"
"github.com/steadybit/extension-kit/extbuild"
"github.com/steadybit/extension-kit/extutil"
"time"
)
func NewNetworkDelayContainerAction(r runc.Runc) action_kit_sdk.Action[NetworkActionState] {
return &networkAction{
optsProvider: delay(r),
optsDecoder: delayDecode,
description: getNetworkDelayDescription(),
runc: r,
}
}
func getNetworkDelayDescription() action_kit_api.ActionDescription {
return action_kit_api.ActionDescription{
Id: fmt.Sprintf("%s.network_delay", BaseActionID),
Label: "Delay Traffic",
Description: "Inject latency into egress network traffic.",
Version: extbuild.GetSemverVersionStringOrUnknown(),
Icon: extutil.Ptr(delayIcon),
TargetSelection: &action_kit_api.TargetSelection{
TargetType: targetID,
SelectionTemplates: &targetSelectionTemplates,
},
Category: extutil.Ptr("network"),
Kind: action_kit_api.Attack,
TimeControl: action_kit_api.TimeControlExternal,
Parameters: append(
commonNetworkParameters,
action_kit_api.ActionParameter{
Name: "networkDelay",
Label: "Network Delay",
Description: extutil.Ptr("How much should the traffic be delayed?"),
Type: action_kit_api.Duration,
DefaultValue: extutil.Ptr("500ms"),
Required: extutil.Ptr(true),
Order: extutil.Ptr(1),
},
action_kit_api.ActionParameter{
Name: "networkDelayJitter",
Label: "Jitter",
Description: extutil.Ptr("Add random +/-30% jitter to network delay?"),
Type: action_kit_api.Boolean,
DefaultValue: extutil.Ptr("true"),
Required: extutil.Ptr(true),
Order: extutil.Ptr(2),
},
action_kit_api.ActionParameter{
Name: "networkInterface",
Label: "Network Interface",
Description: extutil.Ptr("Target Network Interface which should be affected. All if none specified."),
Type: action_kit_api.StringArray,
Required: extutil.Ptr(false),
Order: extutil.Ptr(104),
},
),
}
}
func delay(r runc.Runc) networkOptsProvider {
return func(ctx context.Context, sidecar network.SidecarOpts, request action_kit_api.PrepareActionRequestBody) (network.Opts, error) {
delay := time.Duration(extutil.ToInt64(request.Config["networkDelay"])) * time.Millisecond
hasJitter := extutil.ToBool(request.Config["networkDelayJitter"])
jitter := 0 * time.Millisecond
if hasJitter {
jitter = delay * 30 / 100
}
filter, err := mapToNetworkFilter(ctx, r, sidecar, request.Config, getRestrictedEndpoints(request))
if err != nil {
return nil, err
}
interfaces := extutil.ToStringArray(request.Config["networkInterface"])
if len(interfaces) == 0 {
interfaces, err = readNetworkInterfaces(ctx, r, sidecar)
if err != nil {
return nil, err
}
}
if len(interfaces) == 0 {
return nil, fmt.Errorf("no network interfaces specified")
}
return &network.DelayOpts{
Filter: filter,
Delay: delay,
Jitter: jitter,
Interfaces: interfaces,
}, nil
}
}
func delayDecode(data json.RawMessage) (network.Opts, error) {
var opts network.DelayOpts
err := json.Unmarshal(data, &opts)
return &opts, err
}