-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_store.go
103 lines (86 loc) · 2.5 KB
/
check_store.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
package etcd
import (
"context"
"errors"
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/gogo/protobuf/proto"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
)
const (
checksPathPrefix = "checks"
)
var (
checkKeyBuilder = store.NewKeyBuilder(checksPathPrefix)
)
func getCheckConfigPath(check *types.CheckConfig) string {
return checkKeyBuilder.WithResource(check).Build(check.Name)
}
// GetCheckConfigsPath gets the path of the check config store.
func GetCheckConfigsPath(ctx context.Context, name string) string {
return checkKeyBuilder.WithContext(ctx).Build(name)
}
// DeleteCheckConfigByName deletes a CheckConfig by name.
func (s *Store) DeleteCheckConfigByName(ctx context.Context, name string) error {
if name == "" {
return errors.New("must specify name")
}
_, err := s.client.Delete(ctx, GetCheckConfigsPath(ctx, name))
return err
}
// GetCheckConfigs returns check configurations for an (optional) namespace.
func (s *Store) GetCheckConfigs(ctx context.Context, pred *store.SelectionPredicate) ([]*types.CheckConfig, error) {
checks := []*types.CheckConfig{}
err := List(ctx, s.client, GetCheckConfigsPath, &checks, pred)
return checks, err
}
// GetCheckConfigByName gets a CheckConfig by name.
func (s *Store) GetCheckConfigByName(ctx context.Context, name string) (*types.CheckConfig, error) {
if name == "" {
return nil, errors.New("must specify name")
}
resp, err := s.client.Get(ctx, GetCheckConfigsPath(ctx, name))
if err != nil {
return nil, err
}
if len(resp.Kvs) == 0 {
return nil, nil
}
checkBytes := resp.Kvs[0].Value
check := &types.CheckConfig{}
if err := unmarshal(checkBytes, check); err != nil {
return nil, err
}
if check.Labels == nil {
check.Labels = make(map[string]string)
}
if check.Annotations == nil {
check.Annotations = make(map[string]string)
}
return check, nil
}
// UpdateCheckConfig updates a CheckConfig.
func (s *Store) UpdateCheckConfig(ctx context.Context, check *types.CheckConfig) error {
if err := check.Validate(); err != nil {
return err
}
checkBytes, err := proto.Marshal(check)
if err != nil {
return err
}
cmp := clientv3.Compare(clientv3.Version(getNamespacePath(check.Namespace)), ">", 0)
req := clientv3.OpPut(getCheckConfigPath(check), string(checkBytes))
res, err := s.client.Txn(ctx).If(cmp).Then(req).Commit()
if err != nil {
return err
}
if !res.Succeeded {
return fmt.Errorf(
"could not create the check %s in namespace %s",
check.Name,
check.Namespace,
)
}
return nil
}