-
Notifications
You must be signed in to change notification settings - Fork 208
/
context_combiner.go
97 lines (80 loc) · 2.9 KB
/
context_combiner.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
package context
import (
"context"
"fmt"
"github.com/flant/shell-operator/pkg/hook/binding_context"
"github.com/flant/shell-operator/pkg/hook/controller"
"github.com/flant/shell-operator/pkg/hook/task_metadata"
"github.com/flant/shell-operator/pkg/hook/types"
shell_operator "github.com/flant/shell-operator/pkg/shell-operator"
"github.com/flant/shell-operator/pkg/task"
"github.com/flant/shell-operator/pkg/task/queue"
)
// TestTaskType is a type for tasks created by ContextCombiner.
const TestTaskType task.TaskType = "TestTask"
// TestQueueName is a name of the queue created by ContextCombiner.
const TestQueueName string = "test-queue"
// ContextCombiner is used to bring logic behind the combineBindingContextForHook method
// into tests. This method requires a ShellOperator instance with at least one queue
// with tasks.
type ContextCombiner struct {
op *shell_operator.ShellOperator
q *queue.TaskQueue
}
func NewContextCombiner() *ContextCombiner {
op := &shell_operator.ShellOperator{}
op.TaskQueues = queue.NewTaskQueueSet()
op.TaskQueues.WithContext(context.Background())
op.TaskQueues.NewNamedQueue(TestQueueName, nil)
return &ContextCombiner{
op: op,
q: op.TaskQueues.GetByName(TestQueueName),
}
}
func (c *ContextCombiner) AddBindingContext(bindingType types.BindingType, info controller.BindingExecutionInfo) {
t := task.NewTask(TestTaskType)
t.WithMetadata(task_metadata.HookMetadata{
Binding: info.Binding,
Group: info.Group,
BindingType: bindingType,
BindingContext: info.BindingContext,
AllowFailure: info.AllowFailure,
})
t.WithQueueName(TestQueueName)
c.q.AddLast(t)
}
// CombinedContext returns a combined context or a binding context
// from the first task.
func (c *ContextCombiner) Combined() []binding_context.BindingContext {
firstTask := c.q.GetFirst()
if firstTask == nil {
return []binding_context.BindingContext{}
}
taskMeta := firstTask.GetMetadata()
bc := taskMeta.(task_metadata.BindingContextAccessor).GetBindingContext()
combineResult := c.op.CombineBindingContextForHook(c.q, c.q.GetFirst(), nil)
if combineResult != nil {
bc = combineResult.BindingContexts
}
return bc
}
func (c *ContextCombiner) CombinedAndUpdated(hookCtrl controller.HookController) (GeneratedBindingContexts, error) {
bc := c.Combined()
bc = hookCtrl.UpdateSnapshots(bc)
return ConvertToGeneratedBindingContexts(bc)
}
func (c *ContextCombiner) QueueLen() int {
return c.q.Length()
}
func ConvertToGeneratedBindingContexts(bindingContexts []binding_context.BindingContext) (GeneratedBindingContexts, error) {
res := GeneratedBindingContexts{}
// Support only v1 binding contexts.
bcList := binding_context.ConvertBindingContextList("v1", bindingContexts)
data, err := bcList.Json()
if err != nil {
return res, fmt.Errorf("marshaling binding context error: %v", err)
}
res.BindingContexts = bindingContexts
res.Rendered = string(data)
return res, nil
}