-
Notifications
You must be signed in to change notification settings - Fork 444
/
settings.go
55 lines (46 loc) · 1.06 KB
/
settings.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
package settingsutil
import (
"context"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
)
type settingsKeyStruct struct{}
var settingsKey = settingsKeyStruct{}
func WithSettings(ctx context.Context, settings *v1.Settings) context.Context {
return context.WithValue(ctx, settingsKey, settings)
}
func FromContext(ctx context.Context) *v1.Settings {
if ctx == nil {
return nil
}
settings := MaybeFromContext(ctx)
if settings != nil {
return settings
}
// we should always have settings when this method is called.
panic("no settings on context")
}
func MaybeFromContext(ctx context.Context) *v1.Settings {
if ctx == nil {
return nil
}
if settings, ok := ctx.Value(settingsKey).(*v1.Settings); ok {
return settings
}
return nil
}
func IsAllNamespacesFromSettings(s *v1.Settings) bool {
if s == nil {
return false
}
return IsAllNamespaces(s.GetWatchNamespaces())
}
func IsAllNamespaces(watchNs []string) bool {
switch {
case len(watchNs) == 0:
return true
case len(watchNs) == 1 && watchNs[0] == "":
return true
default:
return false
}
}