Skip to content

Commit b518c7d

Browse files
authored
[shell-operator] feat/compaction and queue refactor (#789)
Signed-off-by: Timur Tuktamyshev <timur.tuktamyshev@flant.com>
1 parent 6c86d67 commit b518c7d

File tree

14 files changed

+2518
-229
lines changed

14 files changed

+2518
-229
lines changed

pkg/hook/task_metadata/task_metadata.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type BindingContextAccessor interface {
2626
GetBindingContext() []bindingcontext.BindingContext
2727
}
2828

29+
type BindingContextSetter interface {
30+
SetBindingContext([]bindingcontext.BindingContext) interface{}
31+
}
32+
33+
type MonitorIDSetter interface {
34+
SetMonitorIDs([]string) interface{}
35+
}
36+
2937
type MonitorIDAccessor interface {
3038
GetMonitorIDs() []string
3139
}
@@ -74,6 +82,12 @@ func (m HookMetadata) GetBindingContext() []bindingcontext.BindingContext {
7482
return m.BindingContext
7583
}
7684

85+
func (m HookMetadata) SetBindingContext(context []bindingcontext.BindingContext) interface{} {
86+
m.BindingContext = context
87+
88+
return m
89+
}
90+
7791
func (m HookMetadata) GetAllowFailure() bool {
7892
return m.AllowFailure
7993
}
@@ -82,6 +96,11 @@ func (m HookMetadata) GetMonitorIDs() []string {
8296
return m.MonitorIDs
8397
}
8498

99+
func (m HookMetadata) SetMonitorIDs(monitorIDs []string) interface{} {
100+
m.MonitorIDs = monitorIDs
101+
return m
102+
}
103+
85104
func (m *HookMetadata) WithHookName(name string) *HookMetadata {
86105
m.HookName = name
87106
return m
Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package task_metadata
22

33
import (
4-
"fmt"
5-
"strings"
64
"testing"
75

86
. "github.com/onsi/gomega"
9-
"github.com/stretchr/testify/assert"
107

118
bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
129
htypes "github.com/flant/shell-operator/pkg/hook/types"
13-
"github.com/flant/shell-operator/pkg/metric"
1410
"github.com/flant/shell-operator/pkg/task"
15-
"github.com/flant/shell-operator/pkg/task/queue"
1611
)
1712

1813
func Test_HookMetadata_Access(t *testing.T) {
@@ -38,74 +33,3 @@ func Test_HookMetadata_Access(t *testing.T) {
3833
g.Expect(hm.BindingContext[0].Binding).Should(Equal("each_1_min"))
3934
g.Expect(hm.BindingContext[1].Binding).Should(Equal("each_5_min"))
4035
}
41-
42-
func Test_HookMetadata_QueueDump_Task_Description(t *testing.T) {
43-
g := NewWithT(t)
44-
45-
logLabels := map[string]string{
46-
"hook": "hook1.sh",
47-
}
48-
49-
metricStorage := metric.NewStorageMock(t)
50-
metricStorage.HistogramObserveMock.Set(func(metric string, value float64, labels map[string]string, buckets []float64) {
51-
assert.Equal(t, metric, "{PREFIX}tasks_queue_action_duration_seconds")
52-
assert.NotZero(t, value)
53-
assert.Equal(t, map[string]string{
54-
"queue_action": "AddLast",
55-
"queue_name": "",
56-
}, labels)
57-
assert.Nil(t, buckets)
58-
})
59-
60-
q := queue.NewTasksQueue().WithMetricStorage(metricStorage)
61-
62-
q.AddLast(task.NewTask(EnableKubernetesBindings).
63-
WithMetadata(HookMetadata{
64-
HookName: "hook1.sh",
65-
Binding: string(EnableKubernetesBindings),
66-
}))
67-
68-
q.AddLast(task.NewTask(HookRun).
69-
WithMetadata(HookMetadata{
70-
HookName: "hook1.sh",
71-
BindingType: htypes.OnKubernetesEvent,
72-
Binding: "monitor_pods",
73-
}).
74-
WithLogLabels(logLabels).
75-
WithQueueName("main"))
76-
77-
q.AddLast(task.NewTask(HookRun).
78-
WithMetadata(HookMetadata{
79-
HookName: "hook1.sh",
80-
BindingType: htypes.Schedule,
81-
AllowFailure: true,
82-
Binding: "every 1 sec",
83-
Group: "monitor_pods",
84-
}).
85-
WithLogLabels(logLabels).
86-
WithQueueName("main"))
87-
88-
queueDump := taskQueueToText(q)
89-
90-
g.Expect(queueDump).Should(ContainSubstring("hook1.sh"), "Queue dump should reveal a hook name.")
91-
g.Expect(queueDump).Should(ContainSubstring("EnableKubernetesBindings"), "Queue dump should reveal EnableKubernetesBindings.")
92-
g.Expect(queueDump).Should(ContainSubstring(":kubernetes:"), "Queue dump should show kubernetes binding.")
93-
g.Expect(queueDump).Should(ContainSubstring(":schedule:"), "Queue dump should show schedule binding.")
94-
g.Expect(queueDump).Should(ContainSubstring("group=monitor_pods"), "Queue dump should show group name.")
95-
}
96-
97-
func taskQueueToText(q *queue.TaskQueue) string {
98-
var buf strings.Builder
99-
buf.WriteString(fmt.Sprintf("Queue '%s': length %d, status: '%s'\n", q.Name, q.Length(), q.Status))
100-
buf.WriteString("\n")
101-
102-
index := 1
103-
q.Iterate(func(task task.Task) {
104-
buf.WriteString(fmt.Sprintf("%2d. ", index))
105-
buf.WriteString(task.GetDescription())
106-
buf.WriteString("\n")
107-
index++
108-
})
109-
110-
return buf.String()
111-
}

0 commit comments

Comments
 (0)