-
Notifications
You must be signed in to change notification settings - Fork 175
/
seeds.go
76 lines (64 loc) · 2.06 KB
/
seeds.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
package seeds
import (
"context"
"errors"
"github.com/sensu/sensu-go/backend/store"
storev2 "github.com/sensu/sensu-go/backend/store/v2"
)
type Config struct {
// AdminUsername is the username of the cluster admin.
AdminUsername string
// AdminPassword is the password of the cluster admin.
AdminPassword string
// AdminAPIKey is the API key of the cluster admin. Can be used instead of
// AdminUsername and AdminPassword.
AdminAPIKey string
}
var ErrAlreadyInitialized = errors.New("sensu-backend already initialized")
func seedCluster(config Config) storev2.InitializeFunc {
logger := logger.WithField("component", "backend.seeds")
logger.Info("seeding store with initial data")
return func(ctx context.Context, str storev2.Interface) error {
nsStore := str.GetNamespaceStore()
_, err := nsStore.Get(ctx, "default")
if err == nil {
return ErrAlreadyInitialized
}
if _, ok := err.(*store.ErrNotFound); !ok {
return err
}
if err := setupNamespaces(ctx, nsStore, config); err != nil {
return err
}
if err := setupUsers(ctx, str, config); err != nil {
return err
}
if err := setupAPIKeys(ctx, str, config); err != nil {
return err
}
if err := setupClusterRoles(ctx, str, config); err != nil {
return err
}
if err := setupClusterRoleBindings(ctx, str, config); err != nil {
return err
}
return nil
}
}
// SeedCluster seeds the cluster according to the provided config.
func SeedCluster(ctx context.Context, s storev2.Interface, config Config) (fErr error) {
return s.GetConfigStore().Initialize(ctx, seedCluster(config))
}
// SeedInitialDataWithContext is like SeedInitialData except it takes an existing
// context.
func SeedInitialDataWithContext(ctx context.Context, s storev2.Interface) (err error) {
config := Config{
AdminUsername: "admin",
AdminPassword: "P@ssw0rd!",
}
return SeedCluster(ctx, s, config)
}
func createResource[R storev2.Resource[T], T any](ctx context.Context, s storev2.Interface, resource R) error {
resourceStore := storev2.Of[R](s)
return resourceStore.CreateIfNotExists(ctx, resource)
}