-
Notifications
You must be signed in to change notification settings - Fork 1
/
directive_handler.go
163 lines (135 loc) · 4.42 KB
/
directive_handler.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
package agent
import (
"context"
"fmt"
"time"
agentapi "github.com/datawire/ambassador-agent/pkg/api/agent"
"github.com/datawire/dlib/dlog"
)
type DirectiveHandler interface {
HandleDirective(context.Context, *Agent, *agentapi.Directive)
}
type BasicDirectiveHandler struct {
DefaultMinReportPeriod time.Duration
rolloutsGetterFactory rolloutsGetterFactory
secretsGetterFactory secretsGetterFactory
}
func (dh *BasicDirectiveHandler) HandleDirective(ctx context.Context, a *Agent, directive *agentapi.Directive) {
if directive == nil {
dlog.Warn(ctx, "Received empty directive, ignoring.")
return
}
ctx = dlog.WithField(ctx, "directive", directive.ID)
dlog.Debug(ctx, "Directive received")
if directive.StopReporting {
// The Director wants us to stop reporting
a.StopReporting(ctx)
}
if directive.MinReportPeriod != nil {
// The Director wants to adjust the minimum time we wait between reports
protoDur := directive.MinReportPeriod
// Note: This conversion ignores potential overflow. In practice this
// shouldn't be a problem, as the server will be constructing this
// durationpb.Duration from a valid time.Duration.
dur := time.Duration(protoDur.Seconds)*time.Second + time.Duration(protoDur.Nanos)*time.Nanosecond
dur = MaxDuration(dur, dh.DefaultMinReportPeriod) // respect configured minimum
a.SetMinReportPeriod(ctx, dur)
}
for _, command := range directive.Commands {
if command.Message != "" {
dlog.Info(ctx, command.Message)
}
if command.RolloutCommand != nil {
dh.handleRolloutCommand(ctx, command.RolloutCommand, a)
} else if command.SecretSyncCommand != nil {
dh.handleSecretSyncCommand(ctx, command.SecretSyncCommand, a)
}
}
a.SetLastDirectiveID(ctx, directive.ID)
}
func (dh *BasicDirectiveHandler) handleSecretSyncCommand(
ctx context.Context, cmdSchema *agentapi.SecretSyncCommand, a *Agent,
) {
if dh.secretsGetterFactory == nil {
dlog.Warn(ctx, "Received secret sync command but does not know how to talk to kube API")
return
}
var (
name = cmdSchema.GetName()
namespace = cmdSchema.GetNamespace()
action = int32(cmdSchema.GetAction())
commandID = cmdSchema.GetCommandId()
secret = cmdSchema.GetSecret()
)
if name == "" {
dlog.Warn(ctx, "Secret sync command received without a secret name")
return
}
if namespace == "" {
dlog.Warn(ctx, "Secret sync command received without a secret namespace")
return
}
if commandID == "" {
dlog.Warn(ctx, "Secret sync command received without a command ID")
return
}
cmd := &secretSyncCommand{
name: name,
namespace: namespace,
action: secretSyncAction(agentapi.SecretSyncCommand_Action_name[action]),
secret: secret,
}
err := cmd.RunWithClientFactory(ctx, dh.secretsGetterFactory)
if err != nil {
dlog.Errorf(ctx, "error running secret sync command %s: %s", cmd, err)
}
dh.reportCommandResult(ctx, commandID, cmd, err, a)
}
func (dh *BasicDirectiveHandler) handleRolloutCommand(
ctx context.Context, cmdSchema *agentapi.RolloutCommand, a *Agent,
) {
if dh.rolloutsGetterFactory == nil {
dlog.Warn(ctx, "Received rollout command but does not know how to talk to Argo Rollouts")
return
}
rolloutName := cmdSchema.GetName()
namespace := cmdSchema.GetNamespace()
action := int32(cmdSchema.GetAction())
commandID := cmdSchema.GetCommandId()
if rolloutName == "" {
dlog.Warn(ctx, "Rollout command received without a rollout name")
return
}
if namespace == "" {
dlog.Warn(ctx, "Rollout command received without a namespace")
return
}
if commandID == "" {
dlog.Warn(ctx, "Rollout command received without a command ID")
return
}
cmd := &rolloutCommand{
rolloutName: rolloutName,
namespace: namespace,
action: rolloutAction(agentapi.RolloutCommand_Action_name[action]),
}
err := cmd.RunWithClientFactory(ctx, dh.rolloutsGetterFactory)
if err != nil {
dlog.Errorf(ctx, "error running rollout command %s: %s", cmd, err)
}
dh.reportCommandResult(ctx, commandID, cmd, err, a)
}
func (dh *BasicDirectiveHandler) reportCommandResult(
ctx context.Context, commandID string, cmd fmt.Stringer, cmdError error, a *Agent,
) {
result := &agentapi.CommandResult{CommandId: commandID, Success: true}
if cmdError != nil {
result.Success = false
result.Message = cmdError.Error()
}
apiKey := a.ambassadorAPIKey
err := a.comm.ReportCommandResult(ctx, result, apiKey)
if err != nil {
dlog.Errorf(ctx, "error reporting result of command %s: %s", cmd, err)
}
}