From 7596e5f72228d05e2e4b72ddd28d8018be8a0149 Mon Sep 17 00:00:00 2001 From: n1Z3R Date: Wed, 18 Dec 2024 18:13:36 +0300 Subject: [PATCH 1/3] feat: add statuses for nodegroups --- pkg/v1/cluster/testing/requests_test.go | 1 + pkg/v1/nodegroup/schemas.go | 62 +++++++++++++++++++++++++ pkg/v1/nodegroup/testing/fixtures.go | 2 + 3 files changed, 65 insertions(+) diff --git a/pkg/v1/cluster/testing/requests_test.go b/pkg/v1/cluster/testing/requests_test.go index ff20704..e0b20f5 100644 --- a/pkg/v1/cluster/testing/requests_test.go +++ b/pkg/v1/cluster/testing/requests_test.go @@ -51,6 +51,7 @@ func TestGetCluster(t *testing.T) { if !reflect.DeepEqual(expectedGetClusterResponse, actual) { t.Fatalf("expected %#v, but got %#v", expectedGetClusterResponse, actual) } + t.Log(actual.Status) } func TestGetZonalCluster(t *testing.T) { diff --git a/pkg/v1/nodegroup/schemas.go b/pkg/v1/nodegroup/schemas.go index 06258d7..daa9545 100644 --- a/pkg/v1/nodegroup/schemas.go +++ b/pkg/v1/nodegroup/schemas.go @@ -1,11 +1,48 @@ package nodegroup import ( + "encoding/json" "time" "github.com/selectel/mks-go/pkg/v1/node" ) +// Status represents custom type for various nodegroup statuses. +type Status string + +const ( + StatusActive Status = "ACTIVE" + StatusPendingCreate Status = "PENDING_CREATE" + StatusPendingUpdate Status = "PENDING_UPDATE" + StatusPendingDelete Status = "PENDING_DELETE" + StatusPendingScaleUp Status = "PENDING_SCALE_UP" + StatusPendingScaleDown Status = "PENDING_SCALE_DOWN" + StatusPendingNodeReinstall Status = "PENDING_NODE_REINSTALL" + StatusUnknown Status = "UNKNOWN" +) + +func getSupportedStatuses() []Status { + return []Status{ + StatusActive, + StatusPendingCreate, + StatusPendingUpdate, + StatusPendingScaleUp, + StatusPendingScaleDown, + StatusPendingDelete, + StatusPendingNodeReinstall, + } +} + +func isStatusSupported(s Status) bool { + for _, v := range getSupportedStatuses() { + if s == v { + return true + } + } + + return false +} + // BaseView represents a base struct of unmarshalled nodegroup body from an API response. // //nolint:maligned @@ -19,6 +56,9 @@ type BaseView struct { // UpdatedAt is the timestamp in UTC timezone of when the nodegroup has been updated. UpdatedAt *time.Time `json:"updated_at"` + // Status represents the current status of the nodegroup. + Status Status `json:"-"` + // ClusterID contains cluster identifier. ClusterID string `json:"cluster_id"` @@ -83,6 +123,28 @@ type GetView struct { UserData string `json:"user_data"` } +func (result *GetView) UnmarshalJSON(b []byte) error { + type tmp GetView + var s struct { + tmp + Status Status `json:"status"` + } + if err := json.Unmarshal(b, &s); err != nil { + return err + } + + *result = GetView(s.tmp) + + // Check cluster status. + if isStatusSupported(s.Status) { + result.Status = s.Status + } else { + result.Status = StatusUnknown + } + + return nil +} + // TaintEffect represents an effect of the node's taint. type TaintEffect string diff --git a/pkg/v1/nodegroup/testing/fixtures.go b/pkg/v1/nodegroup/testing/fixtures.go index c7f9aad..b7f0e09 100644 --- a/pkg/v1/nodegroup/testing/fixtures.go +++ b/pkg/v1/nodegroup/testing/fixtures.go @@ -18,6 +18,7 @@ const testGetNodegroupResponseRaw = ` "flavor_id": "99b62670-9d78-43fd-8f55-d184a4800f8d", "id": "a376745a-fbcb-413d-b418-169d059d79ce", "local_volume": false, + "status": "ACTIVE", "nodes": [ { "created_at": "2020-02-19T15:41:45.948646Z", @@ -64,6 +65,7 @@ var expectedGetNodegroupResponse = &nodegroup.GetView{ ClusterID: "79265515-3700-49fa-af0e-7f547bce788a", FlavorID: "99b62670-9d78-43fd-8f55-d184a4800f8d", VolumeGB: 10, + Status: nodegroup.StatusActive, VolumeType: "basic.ru-1a", LocalVolume: false, AvailabilityZone: "ru-1a", From 609898879b51bd5cffab5cb8871990d3a5c9c176 Mon Sep 17 00:00:00 2001 From: n1Z3R Date: Wed, 18 Dec 2024 18:18:10 +0300 Subject: [PATCH 2/3] fix: small fixes --- pkg/v1/cluster/testing/requests_test.go | 1 - pkg/v1/nodegroup/schemas.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/v1/cluster/testing/requests_test.go b/pkg/v1/cluster/testing/requests_test.go index e0b20f5..ff20704 100644 --- a/pkg/v1/cluster/testing/requests_test.go +++ b/pkg/v1/cluster/testing/requests_test.go @@ -51,7 +51,6 @@ func TestGetCluster(t *testing.T) { if !reflect.DeepEqual(expectedGetClusterResponse, actual) { t.Fatalf("expected %#v, but got %#v", expectedGetClusterResponse, actual) } - t.Log(actual.Status) } func TestGetZonalCluster(t *testing.T) { diff --git a/pkg/v1/nodegroup/schemas.go b/pkg/v1/nodegroup/schemas.go index daa9545..582d306 100644 --- a/pkg/v1/nodegroup/schemas.go +++ b/pkg/v1/nodegroup/schemas.go @@ -135,7 +135,7 @@ func (result *GetView) UnmarshalJSON(b []byte) error { *result = GetView(s.tmp) - // Check cluster status. + // Check nodegroup status. if isStatusSupported(s.Status) { result.Status = s.Status } else { From 0f9eae3f1a01473db104707ad9749e76a47ec228 Mon Sep 17 00:00:00 2001 From: n1Z3R Date: Tue, 14 Jan 2025 16:08:49 +0300 Subject: [PATCH 3/3] fix: add status error --- pkg/v1/nodegroup/schemas.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/v1/nodegroup/schemas.go b/pkg/v1/nodegroup/schemas.go index 582d306..04b44e8 100644 --- a/pkg/v1/nodegroup/schemas.go +++ b/pkg/v1/nodegroup/schemas.go @@ -19,6 +19,7 @@ const ( StatusPendingScaleDown Status = "PENDING_SCALE_DOWN" StatusPendingNodeReinstall Status = "PENDING_NODE_REINSTALL" StatusUnknown Status = "UNKNOWN" + StatusError Status = "ERROR" ) func getSupportedStatuses() []Status { @@ -30,6 +31,7 @@ func getSupportedStatuses() []Status { StatusPendingScaleDown, StatusPendingDelete, StatusPendingNodeReinstall, + StatusError, } }