/
pg_chaos.go
89 lines (72 loc) · 2.8 KB
/
pg_chaos.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
// chaos is a utility package for injecting faults into running instances.
// Chaos Mesh takes awhile to apply the Chaos objects in which the backup could have already
// been completed. This can increase flakiness.
package chaos
import (
"context"
"fmt"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/anynines/a8s-deployment/test/framework/chaos/networkchaos"
"github.com/anynines/a8s-deployment/test/framework/chaos/podchaos"
"github.com/anynines/a8s-deployment/test/framework/postgresql"
)
type PgInjector struct {
Instance *postgresql.Postgresql
}
type PgChaosHelper interface {
StopReplicas(ctx context.Context, c runtimeClient.Client) (ChaosObject, error)
StopMaster(ctx context.Context, c runtimeClient.Client) (ChaosObject, error)
PartitionMaster(ctx, c runtimeClient.Client, t []string) (ChaosObject, error)
}
// StopReplicas applies PodChaos causing the PostgreSQL instance's replicas to fail.
func (pg PgInjector) StopReplicas(ctx context.Context, c runtimeClient.Client) (ChaosObject,
error) {
podChaos := podchaos.New(
pg.Instance.GetNamespace(),
podchaos.NewPodLabelSelector(pg.Instance.GetReplicaLabels(),
podchaos.WithSelectorMode("all"),
podchaos.WithSelectorNamespace([]string{pg.Instance.GetNamespace()}),
),
podchaos.WithName(fmt.Sprintf("replica-failure-%s", pg.Instance.GetName())),
podchaos.WithAction(podchaos.PodFailureAction),
)
if err := c.Create(ctx, podChaos.KubernetesObject()); err != nil {
return nil, err
}
return podChaos, nil
}
// StopMaster applies PodChaos causing the PostgreSQL instance's master to fail.
func (pg PgInjector) StopMaster(ctx context.Context, c runtimeClient.Client) (ChaosObject,
error) {
podChaos := podchaos.New(
pg.Instance.GetNamespace(),
podchaos.NewPodLabelSelector(
pg.Instance.GetMasterLabels(),
podchaos.WithSelectorMode("all"),
podchaos.WithSelectorNamespace([]string{pg.Instance.GetNamespace()}),
),
podchaos.WithName("master-failure"),
podchaos.WithAction(podchaos.PodFailureAction),
)
if err := c.Create(ctx, podChaos.KubernetesObject()); err != nil {
return nil, err
}
return podChaos, nil
}
// PartitionMaster applies NetworkChaos to isolate the master of the PostgreSQL instance.
func (pg PgInjector) PartitionMaster(ctx context.Context, c runtimeClient.Client, t []string) (
ChaosObject, error) {
nc := networkchaos.New(pg.Instance.GetNamespace(),
networkchaos.NewPodLabelSelector(pg.Instance.GetMasterLabels(),
networkchaos.WithSelectorMode("all"),
networkchaos.WithSelectorNamespace([]string{pg.Instance.GetNamespace()})),
networkchaos.WithName("partition-master"),
networkchaos.WithAction("partition"),
networkchaos.WithExternalTargets(t),
networkchaos.WithMode("all"),
)
if err := c.Create(ctx, nc.KubernetesObject()); err != nil {
return nil, err
}
return nc, nil
}