-
Notifications
You must be signed in to change notification settings - Fork 11
/
condition.go
127 lines (100 loc) · 2.94 KB
/
condition.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package state
import (
"github.com/cosi-project/runtime/pkg/resource"
)
// ResourceConditionFunc checks some condition on the resource.
type ResourceConditionFunc func(resource.Resource) (bool, error)
// WatchForCondition describes condition WatchFor is waiting for.
type WatchForCondition struct {
// If set, match only if func returns true.
Condition ResourceConditionFunc
// If set, wait for resource phase to be one of the specified.
Phases []resource.Phase
// If set, watch only for specified event types.
EventTypes []EventType
// If true, wait for the finalizers to empty
FinalizersEmpty bool
}
// Matches checks whether event matches a condition.
func (condition *WatchForCondition) Matches(event Event) (bool, error) {
if condition.EventTypes != nil {
matched := false
for _, typ := range condition.EventTypes {
if typ == event.Type {
matched = true
break
}
}
if !matched {
return false, nil
}
}
// allow following checks rely on the resource being present
if event.Resource == nil {
return false, nil
}
if condition.Condition != nil {
matched, err := condition.Condition(event.Resource)
if err != nil {
return false, err
}
if !matched {
return false, nil
}
}
if condition.FinalizersEmpty {
if event.Type == Destroyed {
return false, nil
}
if !event.Resource.Metadata().Finalizers().Empty() {
return false, nil
}
}
if condition.Phases != nil {
matched := false
for _, phase := range condition.Phases {
if event.Resource.Metadata().Phase() == phase {
matched = true
break
}
}
if !matched {
return false, nil
}
}
// no conditions denied the event, consider it matching
return true, nil
}
// WatchForConditionFunc builds WatchForCondition.
type WatchForConditionFunc func(*WatchForCondition) error
// WithEventTypes watches for specified event types (one of).
func WithEventTypes(types ...EventType) WatchForConditionFunc {
return func(condition *WatchForCondition) error {
condition.EventTypes = append(condition.EventTypes, types...)
return nil
}
}
// WithCondition for specified condition on the resource.
func WithCondition(conditionFunc ResourceConditionFunc) WatchForConditionFunc {
return func(condition *WatchForCondition) error {
condition.Condition = conditionFunc
return nil
}
}
// WithFinalizerEmpty waits for the resource finalizers to be empty.
func WithFinalizerEmpty() WatchForConditionFunc {
return func(condition *WatchForCondition) error {
condition.FinalizersEmpty = true
return nil
}
}
// WithPhases watches for specified resource phases.
func WithPhases(phases ...resource.Phase) WatchForConditionFunc {
return func(condition *WatchForCondition) error {
condition.Phases = append(condition.Phases, phases...)
return nil
}
}