/
cluster_states.go
213 lines (195 loc) · 7.51 KB
/
cluster_states.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//
package model
const (
// ClusterStateStable is a cluster in a stable state and undergoing no changes.
ClusterStateStable = "stable"
// ClusterStateCreationRequested is a cluster in the process of being created.
ClusterStateCreationRequested = "creation-requested"
// ClusterStateCreationInProgress is a cluster that is being actively created.
ClusterStateCreationInProgress = "creation-in-progress"
// ClusterStateWaitingForNodes is a cluster that is waiting for nodes to be ready
ClusterStateWaitingForNodes = "waiting-for-nodes"
// ClusterStateProvisionInProgress is a cluster in the process of being provisioned.
ClusterStateProvisionInProgress = "provision-in-progress"
// ClusterStateCreationFailed is a cluster that failed creation.
ClusterStateCreationFailed = "creation-failed"
// ClusterStateProvisioningRequested is a cluster in the process of being
// provisioned with operators.
ClusterStateProvisioningRequested = "provisioning-requested"
// ClusterStateRefreshMetadata is a cluster that will have metadata refreshed.
ClusterStateRefreshMetadata = "refresh-metadata"
// ClusterStateProvisioningFailed is a cluster that failed provisioning.
ClusterStateProvisioningFailed = "provisioning-failed"
// ClusterStateUpgradeRequested is a cluster in the process of upgrading.
ClusterStateUpgradeRequested = "upgrade-requested"
// ClusterStateUpgradeFailed is a cluster that failed to upgrade.
ClusterStateUpgradeFailed = "upgrade-failed"
// ClusterStateResizeRequested is a cluster in the process of resizing.
ClusterStateResizeRequested = "resize-requested"
// ClusterStateResizeFailed is a cluster that failed to resize.
ClusterStateResizeFailed = "resize-failed"
// ClusterStateNodegroupsCreationRequested is a cluster in the process of creating nodegroups.
ClusterStateNodegroupsCreationRequested = "nodegroups-creation-requested"
// ClusterStateNodegroupsCreationFailed is a cluster that failed to create nodegroups.
ClusterStateNodegroupsCreationFailed = "nodegroups-creation-failed"
// ClusterStateNodegroupsDeletionRequested is a cluster in the process of deleting nodegroups.
ClusterStateNodegroupsDeletionRequested = "nodegroups-deletion-requested"
// ClusterStateNodegroupsDeletionFailed is a cluster that failed to delete nodegroups.
ClusterStateNodegroupsDeletionFailed = "nodegroups-deletion-failed"
// ClusterStateDeletionRequested is a cluster in the process of being deleted.
ClusterStateDeletionRequested = "deletion-requested"
// ClusterStateDeletionFailed is a cluster that failed deletion.
ClusterStateDeletionFailed = "deletion-failed"
// ClusterStateDeleted is a cluster that has been deleted
ClusterStateDeleted = "deleted"
)
// AllClusterStates is a list of all states a cluster can be in.
// Warning:
// When creating a new cluster state, it must be added to this list.
var AllClusterStates = []string{
ClusterStateStable,
ClusterStateRefreshMetadata,
ClusterStateCreationRequested,
ClusterStateCreationInProgress,
ClusterStateWaitingForNodes,
ClusterStateProvisionInProgress,
ClusterStateCreationFailed,
ClusterStateProvisioningRequested,
ClusterStateProvisioningFailed,
ClusterStateNodegroupsCreationRequested,
ClusterStateNodegroupsCreationFailed,
ClusterStateUpgradeRequested,
ClusterStateUpgradeFailed,
ClusterStateResizeRequested,
ClusterStateResizeFailed,
ClusterStateDeletionRequested,
ClusterStateDeletionFailed,
ClusterStateDeleted,
}
// AllClusterStatesPendingWork is a list of all cluster states that the supervisor
// will attempt to transition towards stable on the next "tick".
// Warning:
// When creating a new cluster state, it must be added to this list if the cloud
// cluster supervisor should perform some action on its next work cycle.
var AllClusterStatesPendingWork = []string{
ClusterStateCreationRequested,
ClusterStateCreationInProgress,
ClusterStateWaitingForNodes,
ClusterStateProvisionInProgress,
ClusterStateProvisioningRequested,
ClusterStateRefreshMetadata,
ClusterStateUpgradeRequested,
ClusterStateResizeRequested,
ClusterStateNodegroupsCreationRequested,
ClusterStateNodegroupsDeletionRequested,
ClusterStateDeletionRequested,
}
// ClusterStateWorkPriority is a map of states to their priority. Default priority is 0.
// States with higher priority will be processed first.
var ClusterStateWorkPriority = map[string]int{
ClusterStateCreationRequested: 4,
ClusterStateCreationInProgress: 3,
ClusterStateWaitingForNodes: 2,
ClusterStateProvisionInProgress: 1,
}
// AllClusterRequestStates is a list of all states that a cluster can be put in
// via the API.
// Warning:
// When creating a new cluster state, it must be added to this list if an API
// endpoint should put the cluster in this state.
var AllClusterRequestStates = []string{
ClusterStateCreationRequested,
ClusterStateProvisioningRequested,
ClusterStateUpgradeRequested,
ClusterStateResizeRequested,
ClusterStateNodegroupsCreationRequested,
ClusterStateDeletionRequested,
}
// ValidTransitionState returns whether a cluster can be transitioned into the
// new state or not based on its current state.
func (c *Cluster) ValidTransitionState(newState string) bool {
validStates, found := validClusterTransitions[newState]
if !found {
return false
}
return contains(validStates, c.State)
}
var (
validClusterTransitions = map[string][]string{
ClusterStateCreationRequested: {
ClusterStateCreationRequested,
ClusterStateCreationFailed,
},
ClusterStateProvisioningRequested: {
ClusterStateStable,
ClusterStateProvisioningFailed,
ClusterStateProvisioningRequested,
},
ClusterStateUpgradeRequested: {
ClusterStateStable,
ClusterStateUpgradeRequested,
ClusterStateUpgradeFailed,
},
ClusterStateResizeRequested: {
ClusterStateStable,
ClusterStateResizeRequested,
ClusterStateResizeFailed,
},
ClusterStateNodegroupsCreationRequested: {
ClusterStateStable,
ClusterStateNodegroupsCreationRequested,
ClusterStateNodegroupsCreationFailed,
},
ClusterStateNodegroupsDeletionRequested: {
ClusterStateStable,
ClusterStateNodegroupsDeletionRequested,
ClusterStateNodegroupsDeletionFailed,
},
ClusterStateDeletionRequested: {
ClusterStateStable,
ClusterStateCreationRequested,
ClusterStateCreationFailed,
ClusterStateProvisioningFailed,
ClusterStateUpgradeRequested,
ClusterStateUpgradeFailed,
ClusterStateDeletionRequested,
ClusterStateDeletionFailed,
},
}
)
// ClusterStateReport is a report of all cluster requests states.
type ClusterStateReport []StateReportEntry
// StateReportEntry is a report entry of a given request state.
type StateReportEntry struct {
RequestedState string
ValidStates StateList
InvalidStates StateList
}
// StateList is a list of states
type StateList []string
// Count provides the number of states in a StateList.
func (sl *StateList) Count() int {
return len(*sl)
}
// GetClusterRequestStateReport returns a ClusterStateReport based on the current
// model of cluster states.
func GetClusterRequestStateReport() ClusterStateReport {
report := ClusterStateReport{}
for _, requestState := range AllClusterRequestStates {
entry := StateReportEntry{
RequestedState: requestState,
}
for _, newState := range AllClusterStates {
c := Cluster{State: newState}
if c.ValidTransitionState(requestState) {
entry.ValidStates = append(entry.ValidStates, newState)
} else {
entry.InvalidStates = append(entry.InvalidStates, newState)
}
}
report = append(report, entry)
}
return report
}