-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
81 lines (70 loc) · 2.67 KB
/
db.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
package whalewall
import (
"context"
"fmt"
"github.com/capnspacehook/whalewall/database"
)
// TODO: use 'go run' when https://github.com/golang/go/issues/33468 is fixed
// or use 'go tool' instead if https://github.com/golang/go/issues/48429 is implemented
//go:generate go install github.com/sqlc-dev/sqlc/cmd/[email protected]
//go:generate sqlc generate
func (r *RuleManager) containerExists(ctx context.Context, db database.Querier, id string) (bool, error) {
exists, err := db.ContainerExists(ctx, id)
if err != nil {
return false, err
}
return exists == 1, nil
}
func (r *RuleManager) addContainer(ctx context.Context, tx database.TX, id, name, service string, addrs map[string][]byte, estContainers map[string]struct{}) error {
for _, addr := range addrs {
err := tx.AddContainerAddr(ctx, addr, id)
if err != nil {
return fmt.Errorf("error adding container addr to database: %w", err)
}
}
// add names the container may have been referred to in user rules
// so when creating rules that specify this container it can be found
aliases := containerAliases(name, service)
for _, alias := range aliases {
err := tx.AddContainerAlias(ctx, id, alias)
if err != nil {
return fmt.Errorf("error adding container alias to database: %w", err)
}
}
// keep track if rules were put into other container's chains so
// they can be cleaned up when this container is stopped
for estContainer := range estContainers {
err := tx.AddEstContainer(ctx, id, estContainer)
if err != nil {
return fmt.Errorf("error adding established container to database: %w", err)
}
}
return tx.Commit()
}
func containerAliases(name, service string) []string {
aliases := []string{"/" + name}
if service != "" && service != name {
aliases = append(aliases, service)
aliases = append(aliases, "/"+service)
}
return aliases
}
func (r *RuleManager) deleteContainer(ctx context.Context, tx database.TX, id string) error {
if err := tx.DeleteContainerAddrs(ctx, id); err != nil {
return fmt.Errorf("error deleting container addrs in database: %w", err)
}
if err := tx.DeleteContainerAliases(ctx, id); err != nil {
return fmt.Errorf("error deleting container aliases in database: %w", err)
}
if err := tx.DeleteEstContainers(ctx, id, id); err != nil {
return fmt.Errorf("error deleting established container in database: %w", err)
}
// delete waiting container rules that this container created
if err := tx.DeleteWaitingContainerRules(ctx, id); err != nil {
return fmt.Errorf("error deleting waiting container rules in database: %w", err)
}
if err := tx.DeleteContainer(ctx, id); err != nil {
return fmt.Errorf("error deleting container in database: %w", err)
}
return tx.Commit()
}