Skip to content

Commit

Permalink
feat(kubernetes): node group CRUD operations (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
peknur authored Jan 9, 2023
1 parent 782ff3b commit 37a13ee
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Added
- kubernetes: experimental support for node group CRUD operations

## [5.2.0]

### Added
Expand Down
69 changes: 69 additions & 0 deletions upcloud/request/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package request

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -104,3 +105,71 @@ type GetKubernetesVersionsRequest struct{}
func (r *GetKubernetesVersionsRequest) RequestURL() string {
return fmt.Sprintf("%s/versions", kubernetesClusterBasePath)
}

type GetKubernetesNodeGroupsRequest struct {
ClusterUUID string
}

func (r *GetKubernetesNodeGroupsRequest) RequestURL() string {
return fmt.Sprintf("%s/%s/node-groups", kubernetesClusterBasePath, r.ClusterUUID)
}

type GetKubernetesNodeGroupRequest struct {
ClusterUUID string
Name string
}

func (r *GetKubernetesNodeGroupRequest) RequestURL() string {
return fmt.Sprintf("%s/%s/node-groups/%s", kubernetesClusterBasePath, r.ClusterUUID, r.Name)
}

type KubernetesNodeGroup struct {
Count int `json:"count,omitempty"`
Labels []upcloud.Label `json:"labels,omitempty"`
Name string `json:"name,omitempty"`
Plan string `json:"plan,omitempty"`
SSHKeys []string `json:"ssh_keys,omitempty"`
Storage string `json:"storage,omitempty"`
KubeletArgs []upcloud.KubernetesKubeletArg `json:"kubelet_args,omitempty"`
Taints []upcloud.KubernetesTaint `json:"taints,omitempty"`
}

type CreateKubernetesNodeGroupRequest struct {
ClusterUUID string `json:"-"`
NodeGroup KubernetesNodeGroup
}

func (r *CreateKubernetesNodeGroupRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(r.NodeGroup)
}

func (r *CreateKubernetesNodeGroupRequest) RequestURL() string {
return fmt.Sprintf("%s/%s/node-groups", kubernetesClusterBasePath, r.ClusterUUID)
}

type ModifyKubernetesNodeGroup struct {
Count int `json:"count,omitempty"`
}

type ModifyKubernetesNodeGroupRequest struct {
ClusterUUID string `json:"-"`
Name string `json:"-"`
NodeGroup ModifyKubernetesNodeGroup
}

func (r *ModifyKubernetesNodeGroupRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(r.NodeGroup)
}

func (r *ModifyKubernetesNodeGroupRequest) RequestURL() string {
return fmt.Sprintf("%s/%s/node-groups/%s", kubernetesClusterBasePath, r.ClusterUUID, r.Name)
}

type DeleteKubernetesNodeGroupRequest struct {
ClusterUUID string
Name string
}

func (r *DeleteKubernetesNodeGroupRequest) RequestURL() string {
return fmt.Sprintf("%s/%s/node-groups/%s", kubernetesClusterBasePath, r.ClusterUUID, r.Name)
}
109 changes: 109 additions & 0 deletions upcloud/request/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,115 @@ func TestKubernetes(t *testing.T) {
})
}

func TestGetKubernetesNodeGroupsRequest(t *testing.T) {
t.Parallel()
r := GetKubernetesNodeGroupsRequest{ClusterUUID: "id"}
assert.Equal(t, fmt.Sprintf("%s/id/node-groups", kubernetesClusterBasePath), r.RequestURL())
}

func TestGetKubernetesNodeGroupRequest(t *testing.T) {
t.Parallel()
r := GetKubernetesNodeGroupRequest{ClusterUUID: "id", Name: "nid"}
assert.Equal(t, fmt.Sprintf("%s/id/node-groups/nid", kubernetesClusterBasePath), r.RequestURL())
}

func TestCreateKubernetesNodeGroupRequest(t *testing.T) {
t.Parallel()
const expectedJSON string = `
{
"count": 4,
"kubelet_args": [
{
"key": "log-flush-frequency",
"value": "5s"
}
],
"labels": [
{
"key": "environment",
"value": "development"
}
],
"name": "small",
"plan": "K8S-2xCPU-4GB",
"ssh_keys": [
"ssh-rsa AAAA.."
],
"storage": "01000000-0000-4000-8000-000160010100",
"taints": [
{
"effect": "NoSchedule",
"key": "environment",
"value": "development"
}
]
}
`
r := CreateKubernetesNodeGroupRequest{
ClusterUUID: "id",
NodeGroup: KubernetesNodeGroup{
Count: 4,
Labels: []upcloud.Label{
{
Key: "environment",
Value: "development",
},
},
Name: "small",
Plan: "K8S-2xCPU-4GB",
SSHKeys: []string{"ssh-rsa AAAA.."},
Storage: "01000000-0000-4000-8000-000160010100",
KubeletArgs: []upcloud.KubernetesKubeletArg{
{
Key: "log-flush-frequency",
Value: "5s",
},
},
Taints: []upcloud.KubernetesTaint{
{
Effect: "NoSchedule",
Key: "environment",
Value: "development",
},
},
},
}
assert.Equal(t, fmt.Sprintf("%s/id/node-groups", kubernetesClusterBasePath), r.RequestURL())
gotJS, err := json.Marshal(&r)
if !assert.NoError(t, err) {
return
}
assert.JSONEq(t, expectedJSON, string(gotJS))
}

func TestModifyKubernetesNodeGroupRequest(t *testing.T) {
t.Parallel()
const expectedJSON string = `
{
"count": 4
}
`
r := ModifyKubernetesNodeGroupRequest{
ClusterUUID: "id",
Name: "nid",
NodeGroup: ModifyKubernetesNodeGroup{
Count: 4,
},
}
assert.Equal(t, fmt.Sprintf("%s/id/node-groups/nid", kubernetesClusterBasePath), r.RequestURL())
gotJS, err := json.Marshal(&r)
if !assert.NoError(t, err) {
return
}
assert.JSONEq(t, expectedJSON, string(gotJS))
}

func TestDeleteKubernetesNodeGroupRequest(t *testing.T) {
t.Parallel()
r := DeleteKubernetesNodeGroupRequest{ClusterUUID: "id", Name: "nid"}
assert.Equal(t, fmt.Sprintf("%s/id/node-groups/nid", kubernetesClusterBasePath), r.RequestURL())
}

func exampleGetKubernetesClustersWithFiltersRequest() GetKubernetesClustersWithFiltersRequest {
return GetKubernetesClustersWithFiltersRequest{
Filters: []KubernetesFilter{
Expand Down
27 changes: 14 additions & 13 deletions upcloud/service/fixtures/create_kubernetes_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ interactions:
Content-Type:
- application/json
User-Agent:
- upcloud-go-api-ctx/4.9.0
url: https://api.upcloud.com/1.3/network/03e6deb5-5f39-4df5-8c8e-a74186aba000
- upcloud-go-api/5.2.0
url: https://api.upcloud.com/1.3/network/03e4970d-7791-4b80-a892-682ae0faf46b
method: GET
response:
body: |
Expand All @@ -28,19 +28,20 @@ interactions:
}
]
},
"labels" : [],
"name" : "upcloud-go-sdk-test",
"type" : "private",
"uuid" : "03e6deb5-5f39-4df5-8c8e-a74186aba000",
"uuid" : "03e4970d-7791-4b80-a892-682ae0faf46b",
"zone" : "de-fra1"
}
}
headers:
Content-Length:
- "461"
- "482"
Content-Type:
- application/json; charset=UTF-8
Date:
- Mon, 24 Oct 2022 12:17:08 GMT
- Tue, 03 Jan 2023 14:16:39 GMT
Server:
- Apache
Strict-Transport-Security:
Expand All @@ -49,15 +50,15 @@ interactions:
code: 200
duration: ""
- request:
body: '{"name":"go-sdk-test-ctx","network":"03e6deb5-5f39-4df5-8c8e-a74186aba000","network_cidr":"176.16.1.0/24","node_groups":[{"count":2,"labels":[{"key":"managedBy","value":"go-sdk"}],"name":"testgroup","plan":"K8S-2xCPU-4GB","ssh_keys":["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO3fnjc8UrsYDNU8365mL3lnOPQJg18V42Lt8U/8Sm+r testy_test"]}],"zone":"de-fra1"}'
body: '{"name":"go-sdk-test-ctx","network":"03e4970d-7791-4b80-a892-682ae0faf46b","network_cidr":"176.16.1.0/24","node_groups":[{"count":2,"labels":[{"key":"managedBy","value":"go-sdk"}],"name":"testgroup","plan":"K8S-2xCPU-4GB","ssh_keys":["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO3fnjc8UrsYDNU8365mL3lnOPQJg18V42Lt8U/8Sm+r testy_test"]}],"zone":"de-fra1"}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- upcloud-go-api-ctx/4.9.0
- upcloud-go-api/5.2.0
url: https://api.upcloud.com/1.3/kubernetes
method: POST
response:
Expand All @@ -66,7 +67,7 @@ interactions:
Content-Length:
- "0"
Date:
- Mon, 24 Oct 2022 12:17:08 GMT
- Tue, 03 Jan 2023 14:16:40 GMT
Location:
- /1.3/kubernetes/
Strict-Transport-Security:
Expand All @@ -75,7 +76,7 @@ interactions:
code: 307
duration: ""
- request:
body: '{"name":"go-sdk-test-ctx","network":"03e6deb5-5f39-4df5-8c8e-a74186aba000","network_cidr":"176.16.1.0/24","node_groups":[{"count":2,"labels":[{"key":"managedBy","value":"go-sdk"}],"name":"testgroup","plan":"K8S-2xCPU-4GB","ssh_keys":["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO3fnjc8UrsYDNU8365mL3lnOPQJg18V42Lt8U/8Sm+r testy_test"]}],"zone":"de-fra1"}'
body: '{"name":"go-sdk-test-ctx","network":"03e4970d-7791-4b80-a892-682ae0faf46b","network_cidr":"176.16.1.0/24","node_groups":[{"count":2,"labels":[{"key":"managedBy","value":"go-sdk"}],"name":"testgroup","plan":"K8S-2xCPU-4GB","ssh_keys":["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO3fnjc8UrsYDNU8365mL3lnOPQJg18V42Lt8U/8Sm+r testy_test"]}],"zone":"de-fra1"}'
form: {}
headers:
Accept:
Expand All @@ -85,22 +86,22 @@ interactions:
Referer:
- https://api.upcloud.com/1.3/kubernetes
User-Agent:
- upcloud-go-api-ctx/4.9.0
- upcloud-go-api/5.2.0
url: https://api.upcloud.com/1.3/kubernetes/
method: POST
response:
body: '{"name":"go-sdk-test-ctx","network":"03e6deb5-5f39-4df5-8c8e-a74186aba000","network_cidr":"176.16.1.0/24","node_groups":[{"count":2,"labels":[{"key":"managedBy","value":"go-sdk"}],"name":"testgroup","plan":"K8S-2xCPU-4GB","ssh_keys":["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO3fnjc8UrsYDNU8365mL3lnOPQJg18V42Lt8U/8Sm+r testy_test"]}],"state":"pending","uuid":"0d102e85-52e3-43cf-960b-9d46ec46ac4e","zone":"de-fra1"}'
body: '{"name":"go-sdk-test-ctx","network":"03e4970d-7791-4b80-a892-682ae0faf46b","network_cidr":"176.16.1.0/24","node_groups":[{"count":2,"labels":[{"key":"managedBy","value":"go-sdk"}],"name":"testgroup","plan":"K8S-2xCPU-4GB","ssh_keys":["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO3fnjc8UrsYDNU8365mL3lnOPQJg18V42Lt8U/8Sm+r testy_test"]}],"state":"pending","uuid":"0dca7a18-98e1-4e2d-aea5-ef5dd5fa450e","zone":"de-fra1"}'
headers:
Content-Length:
- "412"
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 24 Oct 2022 12:17:10 GMT
- Tue, 03 Jan 2023 14:16:41 GMT
Strict-Transport-Security:
- max-age=63072000
X-Request-Id:
- 238cd682-8012-4655-8e81-d2ed23d7f4dc
- 50caa53d-d788-4c2e-8d11-3adffe9f6f24
status: 201 Created
code: 201
duration: ""
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interactions:
Content-Type:
- application/json
User-Agent:
- upcloud-go-api-ctx/4.9.0
- upcloud-go-api/5.2.0
url: https://api.upcloud.com/1.3/network/
method: POST
response:
Expand All @@ -28,19 +28,20 @@ interactions:
}
]
},
"labels" : [],
"name" : "upcloud-go-sdk-test",
"type" : "private",
"uuid" : "03e6deb5-5f39-4df5-8c8e-a74186aba000",
"uuid" : "03e4970d-7791-4b80-a892-682ae0faf46b",
"zone" : "de-fra1"
}
}
headers:
Content-Length:
- "461"
- "482"
Content-Type:
- application/json; charset=UTF-8
Date:
- Mon, 24 Oct 2022 12:17:06 GMT
- Tue, 03 Jan 2023 14:16:39 GMT
Server:
- Apache
Strict-Transport-Security:
Expand Down
10 changes: 5 additions & 5 deletions upcloud/service/fixtures/delete_kubernetes_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ interactions:
Content-Type:
- application/json
User-Agent:
- upcloud-go-api-ctx/4.9.0
url: https://api.upcloud.com/1.3/kubernetes/0d102e85-52e3-43cf-960b-9d46ec46ac4e
- upcloud-go-api/5.2.0
url: https://api.upcloud.com/1.3/kubernetes/0dca7a18-98e1-4e2d-aea5-ef5dd5fa450e
method: DELETE
response:
body: '{"message":"deletion started for cluster 0d102e85-52e3-43cf-960b-9d46ec46ac4e"}'
body: '{"message":"deletion started for cluster 0dca7a18-98e1-4e2d-aea5-ef5dd5fa450e"}'
headers:
Content-Length:
- "79"
Content-Type:
- application/json; charset=utf-8
Date:
- Mon, 24 Oct 2022 12:20:27 GMT
- Tue, 03 Jan 2023 14:20:04 GMT
Strict-Transport-Security:
- max-age=63072000
X-Request-Id:
- e81a3e4b-20e6-4029-8709-a83e5185ceed
- b30177de-4573-4ca1-ae98-869d2bdab1af
status: 202 Accepted
code: 202
duration: ""
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ interactions:
Content-Type:
- application/json
User-Agent:
- upcloud-go-api-ctx/4.9.0
url: https://api.upcloud.com/1.3/network/03e6deb5-5f39-4df5-8c8e-a74186aba000
- upcloud-go-api/5.2.0
url: https://api.upcloud.com/1.3/network/03e4970d-7791-4b80-a892-682ae0faf46b
method: DELETE
response:
body: ""
headers:
Date:
- Mon, 24 Oct 2022 12:26:30 GMT
- Tue, 03 Jan 2023 14:25:10 GMT
Server:
- Apache
Strict-Transport-Security:
Expand Down
Loading

0 comments on commit 37a13ee

Please sign in to comment.