Skip to content

Commit ca185dc

Browse files
feat(server_group): strict anti-affinity (#235)
1 parent ee3f184 commit ca185dc

8 files changed

Lines changed: 383 additions & 339 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
### Added
9+
- ServerGroup `AntiAffinityPolicy` field to support strict, best-effort and off policies. This replaces `AntiAffinity`
10+
11+
### Removed
12+
- ServerGroup `AntiAffinity` boolean field in favor of `AntiAffinityPolicy` string enum field
13+
814
### Changed
915
- GetManagedDatabaseIndices method to return a slice of structs instead of pointers
1016

upcloud/error_codes.go

Lines changed: 179 additions & 178 deletions
Large diffs are not rendered by default.

upcloud/request/server_group.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func (s GetServerGroupRequest) RequestURL() string {
5050

5151
// CreateServerGroupRequest represents a request to create server group
5252
type CreateServerGroupRequest struct {
53-
Labels *upcloud.LabelSlice `json:"labels,omitempty"`
54-
Members upcloud.ServerUUIDSlice `json:"servers,omitempty"`
55-
AntiAffinity upcloud.Boolean `json:"anti_affinity,omitempty"`
56-
Title string `json:"title,omitempty"`
53+
Labels *upcloud.LabelSlice `json:"labels,omitempty"`
54+
Members upcloud.ServerUUIDSlice `json:"servers,omitempty"`
55+
AntiAffinityPolicy upcloud.ServerGroupAntiAffinityPolicy `json:"anti_affinity,omitempty"`
56+
Title string `json:"title,omitempty"`
5757
}
5858

5959
func (s CreateServerGroupRequest) RequestURL() string {
@@ -74,11 +74,11 @@ func (r CreateServerGroupRequest) MarshalJSON() ([]byte, error) {
7474

7575
// ModifyServerGroupRequest represents a request to modify server group
7676
type ModifyServerGroupRequest struct {
77-
Labels *upcloud.LabelSlice `json:"labels,omitempty"`
78-
Members *upcloud.ServerUUIDSlice `json:"servers,omitempty"`
79-
AntiAffinity upcloud.Boolean `json:"anti_affinity,omitempty"`
80-
Title string `json:"title,omitempty"`
81-
UUID string `json:"-"`
77+
Labels *upcloud.LabelSlice `json:"labels,omitempty"`
78+
Members *upcloud.ServerUUIDSlice `json:"servers,omitempty"`
79+
AntiAffinityPolicy upcloud.ServerGroupAntiAffinityPolicy `json:"anti_affinity,omitempty"`
80+
Title string `json:"title,omitempty"`
81+
UUID string `json:"-"`
8282
}
8383

8484
func (s ModifyServerGroupRequest) RequestURL() string {

upcloud/request/server_group_test.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestCreateServerGroupRequest(t *testing.T) {
9292
"server": ["x", "y"]
9393
},
9494
"title": "test",
95-
"anti_affinity": "yes"
95+
"anti_affinity": "strict"
9696
}
9797
}
9898
`
@@ -103,9 +103,9 @@ func TestCreateServerGroupRequest(t *testing.T) {
103103
Value: "upcloud-go-sdk-unit-test",
104104
},
105105
},
106-
Members: upcloud.ServerUUIDSlice{"x", "y"},
107-
Title: "test",
108-
AntiAffinity: upcloud.True,
106+
Members: upcloud.ServerUUIDSlice{"x", "y"},
107+
Title: "test",
108+
AntiAffinityPolicy: upcloud.ServerGroupAntiAffinityPolicyStrict,
109109
}
110110
actual, err = json.Marshal(&r)
111111
assert.NoError(t, err)
@@ -160,10 +160,10 @@ func TestModifyServerGroupRequest(t *testing.T) {
160160
}
161161
`
162162
r = ModifyServerGroupRequest{
163-
UUID: "id",
164-
Title: "test",
165-
Members: &upcloud.ServerUUIDSlice{"x"},
166-
AntiAffinity: upcloud.False,
163+
UUID: "id",
164+
Title: "test",
165+
Members: &upcloud.ServerUUIDSlice{"x"},
166+
AntiAffinityPolicy: upcloud.ServerGroupAntiAffinityPolicyOff,
167167
}
168168
actual, err = json.Marshal(&r)
169169
assert.NoError(t, err)
@@ -180,9 +180,30 @@ func TestModifyServerGroupRequest(t *testing.T) {
180180
}
181181
`
182182
r = ModifyServerGroupRequest{
183-
UUID: "id",
184-
Members: &upcloud.ServerUUIDSlice{"x"},
185-
AntiAffinity: upcloud.True,
183+
UUID: "id",
184+
Members: &upcloud.ServerUUIDSlice{"x"},
185+
AntiAffinityPolicy: upcloud.ServerGroupAntiAffinityPolicyBestEffort,
186+
}
187+
actual, err = json.Marshal(&r)
188+
assert.NoError(t, err)
189+
assert.JSONEq(t, expected, string(actual))
190+
191+
assert.Equal(t, "/server-group/id", r.RequestURL())
192+
193+
expected = `
194+
{
195+
"server_group": {
196+
"servers": {
197+
"server": ["x"]
198+
},
199+
"anti_affinity": "strict"
200+
}
201+
}
202+
`
203+
r = ModifyServerGroupRequest{
204+
UUID: "id",
205+
Members: &upcloud.ServerUUIDSlice{"x"},
206+
AntiAffinityPolicy: upcloud.ServerGroupAntiAffinityPolicyStrict,
186207
}
187208
actual, err = json.Marshal(&r)
188209
assert.NoError(t, err)

upcloud/server_group.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ const (
1010
ServerAntiAffinityStatusUnmet ServerAntiAffinityStatus = "unmet"
1111
)
1212

13+
// ServerGroupAntiAffinityPolicy represents the anti affinity setting for a server groups. Can be "strict", "yes" or "no"
14+
type ServerGroupAntiAffinityPolicy string
15+
16+
const (
17+
// ServerGroupAntiAffinityPolicyStrict doesn't allow servers in the same server group to be on the same host
18+
ServerGroupAntiAffinityPolicyStrict ServerGroupAntiAffinityPolicy = "strict"
19+
// ServerGroupAntiAffinityPolicyBestEffort tries to put servers on different hosts, but this is not guaranteed
20+
ServerGroupAntiAffinityPolicyBestEffort ServerGroupAntiAffinityPolicy = "yes"
21+
// ServerGroupAntiAffinityPolicyOff doesn't affect server host affinity i.e. the feature is turned off
22+
ServerGroupAntiAffinityPolicyOff ServerGroupAntiAffinityPolicy = "no"
23+
)
24+
1325
// ServerGroupMemberAntiAffinityStatus represents all the data related to an anti affinity status for a single member within the server group
1426
type ServerGroupMemberAntiAffinityStatus struct {
1527
ServerUUID string `json:"uuid"`
@@ -22,7 +34,7 @@ type ServerGroup struct {
2234
Members ServerUUIDSlice `json:"servers,omitempty"`
2335
Title string `json:"title,omitempty"`
2436
UUID string `json:"uuid,omitempty"`
25-
AntiAffinity Boolean `json:"anti_affinity,omitempty"`
37+
AntiAffinityPolicy ServerGroupAntiAffinityPolicy `json:"anti_affinity,omitempty"`
2638
AntiAffinityStatus []ServerGroupMemberAntiAffinityStatus `json:"anti_affinity_status,omitempty"`
2739
}
2840

upcloud/server_group_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestUnmarshalServerGroup(t *testing.T) {
3232
},
3333
"title" : "my group",
3434
"uuid" : "server_group_uuid",
35-
"anti_affinity": 1,
35+
"anti_affinity": "yes",
3636
"anti_affinity_status": [
3737
{
3838
"uuid": "x",
@@ -51,11 +51,11 @@ func TestUnmarshalServerGroup(t *testing.T) {
5151
assert.NoError(t, err)
5252

5353
expected := ServerGroup{
54-
Labels: LabelSlice{Label{Key: "managedBy", Value: "upcloud-go-sdk-unit-test"}, Label{Key: "env", Value: "test"}},
55-
Members: []string{"x", "y"},
56-
Title: "my group",
57-
UUID: "server_group_uuid",
58-
AntiAffinity: True,
54+
Labels: LabelSlice{Label{Key: "managedBy", Value: "upcloud-go-sdk-unit-test"}, Label{Key: "env", Value: "test"}},
55+
Members: []string{"x", "y"},
56+
Title: "my group",
57+
UUID: "server_group_uuid",
58+
AntiAffinityPolicy: ServerGroupAntiAffinityPolicyBestEffort,
5959
AntiAffinityStatus: []ServerGroupMemberAntiAffinityStatus{
6060
{
6161
ServerUUID: "x",
@@ -93,7 +93,7 @@ func TestUnmarshalServerGroups(t *testing.T) {
9393
},
9494
"title" : "my group 1",
9595
"uuid" : "id",
96-
"anti_affinity": 0
96+
"anti_affinity": "no"
9797
},
9898
{
9999
"labels" : {
@@ -117,7 +117,7 @@ func TestUnmarshalServerGroups(t *testing.T) {
117117
},
118118
"title" : "my group 2",
119119
"uuid" : "id",
120-
"anti_affinity": 1,
120+
"anti_affinity": "strict",
121121
"anti_affinity_status": [
122122
{
123123
"uuid": "a",
@@ -143,18 +143,18 @@ func TestUnmarshalServerGroups(t *testing.T) {
143143

144144
expected := ServerGroups{
145145
{
146-
Labels: LabelSlice{Label{Key: "managedBy", Value: "upcloud-go-sdk-unit-test"}},
147-
Members: []string{"x"},
148-
Title: "my group 1",
149-
UUID: "id",
150-
AntiAffinity: False,
146+
Labels: LabelSlice{Label{Key: "managedBy", Value: "upcloud-go-sdk-unit-test"}},
147+
Members: []string{"x"},
148+
Title: "my group 1",
149+
UUID: "id",
150+
AntiAffinityPolicy: ServerGroupAntiAffinityPolicyOff,
151151
},
152152
{
153-
Labels: LabelSlice{Label{Key: "managedBy", Value: "upcloud-go-sdk-unit-test"}, Label{Key: "isSecondTestCase", Value: "true"}},
154-
Members: []string{"a", "b", "c"},
155-
Title: "my group 2",
156-
UUID: "id",
157-
AntiAffinity: True,
153+
Labels: LabelSlice{Label{Key: "managedBy", Value: "upcloud-go-sdk-unit-test"}, Label{Key: "isSecondTestCase", Value: "true"}},
154+
Members: []string{"a", "b", "c"},
155+
Title: "my group 2",
156+
UUID: "id",
157+
AntiAffinityPolicy: ServerGroupAntiAffinityPolicyStrict,
158158
AntiAffinityStatus: []ServerGroupMemberAntiAffinityStatus{
159159
{
160160
ServerUUID: "a",

0 commit comments

Comments
 (0)