/
silenced.go
180 lines (156 loc) · 5.12 KB
/
silenced.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
package v2
import (
"context"
"errors"
"fmt"
"net/url"
"path"
"sort"
"strconv"
"strings"
"time"
)
const (
// SilencedResource is the name of this resource type
SilencedResource = "silenced"
)
// StorePrefix returns the path prefix to this resource in the store
func (s *Silenced) StorePrefix() string {
return SilencedResource
}
// URIPath returns the path component of a silenced entry URI.
func (s *Silenced) URIPath() string {
if s.Name == "" {
s.Name, _ = SilencedName(s.Subscription, s.Check)
}
return path.Join(URLPrefix, "namespaces", url.PathEscape(s.Namespace), SilencedResource, url.PathEscape(s.Name))
}
// Validate returns an error if the CheckName and Subscription fields are not
// provided.
func (s *Silenced) Validate() error {
if (s.Subscription == "" && s.Check == "") || (s.Subscription == "*" && s.Check == "*") {
return errors.New("must provide check or subscription")
}
if s.Subscription != "" && s.Subscription != "*" {
if err := ValidateSubscriptionName(s.Subscription); err != nil {
return fmt.Errorf("Subscription %s", err)
}
}
if s.Check != "" && s.Check != "*" {
if err := ValidateName(s.Check); err != nil {
return fmt.Errorf("Check %s", err)
}
}
return nil
}
// StartSilence returns true if the current unix timestamp is less than the begin
// timestamp.
func (s *Silenced) StartSilence(currentTime int64) bool {
// if begin time is zero, it has not been set, so silencing can start.
if s.Begin == 0 {
return true
}
return currentTime > s.Begin
}
// Prepare prepares a silenced entry for storage
func (s *Silenced) Prepare(ctx context.Context) {
// Populate newSilence.Name with the subscription and checkName. Substitute a
// splat if one of the values does not exist. If both values are empty, the
// validator will return an error when attempting to update it in the store.
s.Name, _ = SilencedName(s.Subscription, s.Check)
// If begin timestamp was not already provided set it to the current time.
if s.Begin == 0 {
s.Begin = time.Now().Unix()
}
// Retrieve the subject of the JWT, which represents the logged on user, in
// order to set it as the creator of the silenced entry
if value := ctx.Value(ClaimsKey); value != nil {
claims, ok := value.(*Claims)
if ok {
s.Creator = claims.Subject
}
}
}
// NewSilenced creates a new Silenced entry.
func NewSilenced(meta ObjectMeta) *Silenced {
return &Silenced{ObjectMeta: meta}
}
// FixtureSilenced returns a testing fixutre for a Silenced event struct.
func FixtureSilenced(name string) *Silenced {
var check, subscription string
parts := strings.Split(name, ":")
if len(parts) == 2 {
check = parts[1]
subscription = parts[0]
} else if len(parts) == 3 {
check = parts[2]
subscription = strings.Join(parts[0:2], ":")
} else {
panic("invalid silenced name")
}
return &Silenced{
Check: check,
Subscription: subscription,
ObjectMeta: NewObjectMeta(name, "default"),
}
}
// SilencedName returns the canonical name for a silenced entry. It returns non-nil
// error if both subscription and check are empty strings.
func SilencedName(subscription, check string) (string, error) {
if subscription == "" && check == "" {
return "", errors.New("no subscription or check specified")
}
if subscription == "" {
subscription = "*"
}
if check == "" {
check = "*"
}
return fmt.Sprintf("%s:%s", subscription, check), nil
}
// SortSilencedByPredicate can be used to sort a given collection using a given
// predicate.
func SortSilencedByPredicate(es []*Silenced, fn func(a, b *Silenced) bool) sort.Interface {
return &silenceSorter{silences: es, byFn: fn}
}
// SortSilencedByName can be used to sort a given collection by their names.
func SortSilencedByName(es []*Silenced) sort.Interface {
return SortSilencedByPredicate(es, func(a, b *Silenced) bool { return a.Name < b.Name })
}
// SortSilencedByBegin can be used to sort a given collection by their begin
// timestamp.
func SortSilencedByBegin(es []*Silenced) sort.Interface {
return SortSilencedByPredicate(es, func(a, b *Silenced) bool { return a.Begin < b.Begin })
}
type silenceSorter struct {
silences []*Silenced
byFn func(a, b *Silenced) bool
}
// Len implements sort.Interface.
func (s *silenceSorter) Len() int {
return len(s.silences)
}
// Swap implements sort.Interface.
func (s *silenceSorter) Swap(i, j int) {
s.silences[i], s.silences[j] = s.silences[j], s.silences[i]
}
// Less implements sort.Interface.
func (s *silenceSorter) Less(i, j int) bool {
return s.byFn(s.silences[i], s.silences[j])
}
// SilencedFields returns a set of fields that represent that resource
func SilencedFields(r Resource) map[string]string {
resource := r.(*Silenced)
return map[string]string{
"silenced.name": resource.ObjectMeta.Name,
"silenced.namespace": resource.ObjectMeta.Namespace,
"silenced.check": resource.Check,
"silenced.creator": resource.Creator,
"silenced.expire_on_resolve": strconv.FormatBool(resource.ExpireOnResolve),
"silenced.subscription": resource.Subscription,
}
}
// SetNamespace sets the namespace of the resource.
func (s *Silenced) SetNamespace(namespace string) {
s.Namespace = namespace
}