-
Notifications
You must be signed in to change notification settings - Fork 2
/
action_stop.go
200 lines (175 loc) · 6.04 KB
/
action_stop.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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
package extcontainer
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/action-kit/go/action_kit_sdk"
"github.com/steadybit/extension-container/extcontainer/container/types"
"github.com/steadybit/extension-kit"
"github.com/steadybit/extension-kit/extbuild"
"github.com/steadybit/extension-kit/extutil"
"golang.org/x/sync/syncmap"
"time"
)
type stopAction struct {
client types.Client
completers syncmap.Map //map[uuid.UUID]*completer
}
type completer struct {
err <-chan error
cancel context.CancelFunc
}
type StopActionState struct {
ContainerId string
Graceful bool
ExecutionId uuid.UUID
}
// Make sure stopAction implements all required interfaces
var _ action_kit_sdk.Action[StopActionState] = (*stopAction)(nil)
var _ action_kit_sdk.ActionWithStatus[StopActionState] = (*stopAction)(nil)
var _ action_kit_sdk.ActionWithStop[StopActionState] = (*stopAction)(nil)
func NewStopContainerAction(client types.Client) action_kit_sdk.Action[StopActionState] {
return &stopAction{
client: client,
completers: syncmap.Map{},
}
}
func (a *stopAction) NewEmptyState() StopActionState {
return StopActionState{}
}
func (a *stopAction) Describe() action_kit_api.ActionDescription {
return action_kit_api.ActionDescription{
Id: fmt.Sprintf("%s.stop", BaseActionID),
Label: "Stop Container",
Description: "Stops or kills the Container",
Version: extbuild.GetSemverVersionStringOrUnknown(),
Icon: extutil.Ptr(stopIcon),
TargetSelection: &action_kit_api.TargetSelection{
TargetType: targetID,
SelectionTemplates: &targetSelectionTemplates,
},
Category: extutil.Ptr("state"),
Kind: action_kit_api.Attack,
TimeControl: action_kit_api.TimeControlInternal,
Parameters: []action_kit_api.ActionParameter{
{
Name: "graceful",
Label: "Graceful",
Description: extutil.Ptr("Stopped the container gracefully using SIGTERM or immediately killed using the SIGKILL signal?"),
Type: action_kit_api.Boolean,
DefaultValue: extutil.Ptr("true"),
Required: extutil.Ptr(true),
Order: extutil.Ptr(0),
},
},
Status: extutil.Ptr(action_kit_api.MutatingEndpointReferenceWithCallInterval{
CallInterval: extutil.Ptr("1s"),
}),
Stop: extutil.Ptr(action_kit_api.MutatingEndpointReference{}),
}
}
func (a *stopAction) Prepare(_ context.Context, state *StopActionState, request action_kit_api.PrepareActionRequestBody) (*action_kit_api.PrepareResult, error) {
containerId := request.Target.Attributes["container.id"]
if len(containerId) == 0 {
return nil, extension_kit.ToError("Target is missing the 'container.id' attribute.", nil)
}
state.ContainerId = containerId[0]
state.Graceful = extutil.ToBool(request.Config["graceful"])
state.ExecutionId = request.ExecutionId
return nil, nil
}
func (a *stopAction) Start(_ context.Context, state *StopActionState) (*action_kit_api.StartResult, error) {
err := a.stopContainer(state.ExecutionId, RemovePrefix(state.ContainerId), state.Graceful)
if err != nil {
return nil, extension_kit.ToError("Failed to stop container", err)
}
return &action_kit_api.StartResult{
Messages: extutil.Ptr([]action_kit_api.Message{
{
Level: extutil.Ptr(action_kit_api.Info),
Message: fmt.Sprintf("Stopping container %s (graceful=%t)", state.ContainerId, state.Graceful),
},
}),
}, nil
}
func (a *stopAction) Status(_ context.Context, state *StopActionState) (*action_kit_api.StatusResult, error) {
var messages []action_kit_api.Message
completed, err := a.isStopContainerCompleted(state.ExecutionId)
if err != nil {
messages = append(messages, action_kit_api.Message{
Level: extutil.Ptr(action_kit_api.Error),
Message: fmt.Sprintf("Failed to stop container %s: %s", state.ContainerId, err),
})
} else if completed {
messages = append(messages, action_kit_api.Message{
Level: extutil.Ptr(action_kit_api.Info),
Message: fmt.Sprintf("Container %s stopped", state.ContainerId),
})
}
return &action_kit_api.StatusResult{
Completed: completed,
Messages: &messages,
}, nil
}
func (a *stopAction) Stop(_ context.Context, state *StopActionState) (*action_kit_api.StopResult, error) {
messages := make([]action_kit_api.Message, 0)
stopped := a.cancelStopContainer(state.ExecutionId)
if stopped {
messages = append(messages, action_kit_api.Message{
Level: extutil.Ptr(action_kit_api.Info),
Message: fmt.Sprintf("Canceled stop container %s", state.ContainerId),
})
}
return &action_kit_api.StopResult{
Messages: &messages,
}, nil
}
func (a *stopAction) stopContainer(executionId uuid.UUID, containerId string, graceful bool) error {
//When the stop actions are graceful, it may take some time until the container is actually stopped.
//Therefore, we start the stop action, in a separate go routine, and return immediately.
//We save the cancel function and the error channel in a map, so that the status action can check if the stop is still completable and could also cancel if requested
errorChannel := make(chan error)
stopCtx, stopCancel := context.WithCancel(context.Background())
a.completers.Store(executionId, &completer{
err: errorChannel,
cancel: stopCancel,
})
go func() {
errorChannel <- a.client.Stop(stopCtx, containerId, graceful)
close(errorChannel)
}()
select {
case err := <-errorChannel:
if err != nil {
return err
}
case <-time.After(1 * time.Second):
break
}
return nil
}
func (a *stopAction) isStopContainerCompleted(executionId uuid.UUID) (bool, error) {
running, ok := a.completers.Load(executionId)
if !ok {
return true, nil
}
select {
case err := <-running.(*completer).err:
a.completers.Delete(executionId)
return true, err
default:
return false, nil
}
}
func (a *stopAction) cancelStopContainer(executionId uuid.UUID) bool {
running, ok := a.completers.Load(executionId)
if !ok {
return false
}
running.(*completer).cancel()
<-running.(*completer).err
return true
}