/
action_network_loss.go
97 lines (87 loc) · 3.05 KB
/
action_network_loss.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
// 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"
)
func NewNetworkPackageLossContainerAction(r runc.Runc) action_kit_sdk.Action[NetworkActionState] {
return &networkAction{
optsProvider: packageLoss(r),
optsDecoder: packageLossDecode,
description: getNetworkPackageLossDescription(),
runc: r,
}
}
func getNetworkPackageLossDescription() action_kit_api.ActionDescription {
return action_kit_api.ActionDescription{
Id: fmt.Sprintf("%s.network_package_loss", BaseActionID),
Label: "Drop Outgoing Traffic",
Description: "Cause packet loss for outgoing network traffic (egress).",
Version: extbuild.GetSemverVersionStringOrUnknown(),
Icon: extutil.Ptr(lossIcon),
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: "networkLoss",
Label: "Network Loss",
Description: extutil.Ptr("How much of the traffic should be lost?"),
Type: action_kit_api.Percentage,
DefaultValue: extutil.Ptr("70"),
Required: extutil.Ptr(true),
Order: extutil.Ptr(1),
},
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 packageLoss(r runc.Runc) networkOptsProvider {
return func(ctx context.Context, sidecar network.SidecarOpts, request action_kit_api.PrepareActionRequestBody) (network.Opts, error) {
loss := extutil.ToUInt(request.Config["networkLoss"])
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.PackageLossOpts{
Filter: filter,
Loss: loss,
Interfaces: interfaces,
}, nil
}
}
func packageLossDecode(data json.RawMessage) (network.Opts, error) {
var opts network.PackageLossOpts
err := json.Unmarshal(data, &opts)
return &opts, err
}