This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
/
p2_balancer.go
166 lines (139 loc) · 4.32 KB
/
p2_balancer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package audit
import (
"encoding/json"
"github.com/square/p2/pkg/util"
pcfields "github.com/square/p2/pkg/pc/fields"
"github.com/square/p2/pkg/types"
)
type P2BalancerErrorMessage string
type PodTransferID string
const (
// P2BalancerInProgressEvent denotes the start of a P2 Balancer pod move
P2BalancerInProgressEvent EventType = "P2_BALANCER_START"
// P2BalancerCompletionEvent denotes the successful completion of a
// pod move
P2BalancerCompletionEvent EventType = "P2_BALANCER_COMPLETION"
// P2BalancerFailureEvent denotes a pod move failure due
// to unrecoverable errors
P2BalancerFailureEvent EventType = "P2_BALANCER_FAILURE"
)
type CommonP2BalancerDetails struct {
// PodTransferID is a uuid that will be the same for all audit log records
// associated with the same pod transfer. It can be used to match up
// "start" events with "failure" or "completion" events
PodTransferID PodTransferID `json:"pod_transfer_id"`
// PodID denotes the pod ID of the pod cluster that the RC belongs to
PodID types.PodID `json:"pod_id"`
// AvailabilityZone is the availability zone of the pod cluster that
// the RC belongs to
AvailabilityZone pcfields.AvailabilityZone `json:"availability_zone"`
// ClusterName is the name of the pod cluster that the RC belongs to
ClusterName pcfields.ClusterName `json:"cluster_name"`
// OldNode denotes the node that is above healthy threshold
// and needs a pod moved off of it
OldNode types.NodeName `json:"old_node"`
// NewNode denotes the node that has resource capacity for a pod to be moved onto it
NewNode types.NodeName `json:"new_node"`
}
type P2BalancerInProgressDetails struct {
CommonP2BalancerDetails
}
type P2BalancerCompletionDetails struct {
CommonP2BalancerDetails
}
type P2BalancerFailureDetails struct {
CommonP2BalancerDetails
// This will be set to the error message at the point of failure
ErrorMessage P2BalancerErrorMessage `json:"error_message"`
}
func NewP2BalancerInProgressDetails(
podTransferID PodTransferID,
podID types.PodID,
availabilityZone pcfields.AvailabilityZone,
clusterName pcfields.ClusterName,
oldNode types.NodeName,
newNode types.NodeName,
) (json.RawMessage, error) {
details := P2BalancerInProgressDetails{
CommonP2BalancerDetails: commonP2BalancerDetails(
podTransferID,
podID,
availabilityZone,
clusterName,
oldNode,
newNode,
),
}
jsonBytes, err := json.Marshal(details)
if err != nil {
return nil, util.Errorf("could not marshal p2 balancer pod move in progress details as JSON: %s", err)
}
return json.RawMessage(jsonBytes), nil
}
func NewP2BalancerCompletionDetails(
podTransferID PodTransferID,
podID types.PodID,
availabilityZone pcfields.AvailabilityZone,
clusterName pcfields.ClusterName,
oldNode types.NodeName,
newNode types.NodeName,
) (json.RawMessage, error) {
details := P2BalancerCompletionDetails{
CommonP2BalancerDetails: commonP2BalancerDetails(
podTransferID,
podID,
availabilityZone,
clusterName,
oldNode,
newNode,
),
}
jsonBytes, err := json.Marshal(details)
if err != nil {
return nil, util.Errorf("could not marshal p2 balancer pod move completion details as JSON: %s", err)
}
return json.RawMessage(jsonBytes), nil
}
func NewP2BalancerFailureDetails(
podTransferID PodTransferID,
podID types.PodID,
availabilityZone pcfields.AvailabilityZone,
clusterName pcfields.ClusterName,
oldNode types.NodeName,
newNode types.NodeName,
errorMessage P2BalancerErrorMessage,
) (json.RawMessage, error) {
details := P2BalancerFailureDetails{
CommonP2BalancerDetails: commonP2BalancerDetails(
podTransferID,
podID,
availabilityZone,
clusterName,
oldNode,
newNode,
),
ErrorMessage: errorMessage,
}
jsonBytes, err := json.Marshal(details)
if err != nil {
return nil, util.Errorf("could not marshal p2 balancer pod move failure details as JSON: %s", err)
}
return json.RawMessage(jsonBytes), nil
}
func commonP2BalancerDetails(
podTransferID PodTransferID,
podID types.PodID,
availabilityZone pcfields.AvailabilityZone,
clusterName pcfields.ClusterName,
oldNode types.NodeName,
newNode types.NodeName,
) CommonP2BalancerDetails {
return CommonP2BalancerDetails{
PodTransferID: podTransferID,
PodID: podID,
AvailabilityZone: availabilityZone,
ClusterName: clusterName,
OldNode: oldNode,
NewNode: newNode,
}
}