From 77e1e43eb800bc0c06f2d632c112bf504321f6c0 Mon Sep 17 00:00:00 2001 From: Taekyu Date: Sat, 2 Apr 2022 01:15:54 +0900 Subject: [PATCH 1/4] feature. add workflowId to updateCluster(AppGroup)Status for tks-batch --- cmd/server/application_handler.go | 2 +- cmd/server/cluster_handler.go | 2 +- pkg/application/accessor.go | 4 ++-- pkg/application/model/application_group.go | 2 ++ pkg/cluster/accessor.go | 9 ++++++--- pkg/cluster/model/cluster.go | 2 ++ scripts/application_db.sql | 4 +++- scripts/cluster_db.sql | 4 +++- 8 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/server/application_handler.go b/cmd/server/application_handler.go index 6535c29..81f9485 100644 --- a/cmd/server/application_handler.go +++ b/cmd/server/application_handler.go @@ -154,7 +154,7 @@ func (*AppInfoServer) UpdateAppGroupStatus(ctx context.Context, in *pb.UpdateApp }, err } log.Info("UpdateAppGroupStatus request for app group ID: ", appGroupID) - if err = acc.UpdateAppGroupStatus(appGroupID, in.GetStatus()); err != nil { + if err = acc.UpdateAppGroupStatus(appGroupID, in.GetStatus(), in.GetStatusDesc(), in.GetWorkflowId()); err != nil { return &pb.SimpleResponse{ Code: pb.Code_INTERNAL, Error: &pb.Error{ diff --git a/cmd/server/cluster_handler.go b/cmd/server/cluster_handler.go index a903cd7..41b0b3b 100644 --- a/cmd/server/cluster_handler.go +++ b/cmd/server/cluster_handler.go @@ -224,7 +224,7 @@ func (s *ClusterInfoServer) UpdateClusterStatus(ctx context.Context, in *pb.Upda return &res, err } - err = clusterAccessor.UpdateStatus(clusterId, in.GetStatus()) + err = clusterAccessor.UpdateStatus(clusterId, in.GetStatus(), in.GetStatusDesc(), in.GetWorkflowId()) if err != nil { return &pb.SimpleResponse{ Code: pb.Code_INTERNAL, diff --git a/pkg/application/accessor.go b/pkg/application/accessor.go index 97b49c3..745e1b6 100644 --- a/pkg/application/accessor.go +++ b/pkg/application/accessor.go @@ -103,10 +103,10 @@ func (x *Accessor) GetAppGroup(appGroupID uuid.UUID) (*pb.AppGroup, error) { } // UpdateAppGroupStatus updates status of application group. -func (x *Accessor) UpdateAppGroupStatus(appGroupID uuid.UUID, status pb.AppGroupStatus) error { +func (x *Accessor) UpdateAppGroupStatus(appGroupID uuid.UUID, status pb.AppGroupStatus, statusDesc string, workflowId string) error { res := x.db.Model(&model.ApplicationGroup{}). Where("id = ?", appGroupID). - Update("status", status) + Updates(map[string]interface{}{"Status": status, "StatusDesc": statusDesc, "WorkflowId": workflowId}) if res.Error != nil { return res.Error diff --git a/pkg/application/model/application_group.go b/pkg/application/model/application_group.go index 71e5a64..0b1455d 100644 --- a/pkg/application/model/application_group.go +++ b/pkg/application/model/application_group.go @@ -13,7 +13,9 @@ type ApplicationGroup struct { ID uuid.UUID `gorm:"primarykey;type:uuid;default:uuid_generate_v4()"` Name string Type pb.AppGroupType + WorkflowId string Status pb.AppGroupStatus + StatusDesc string ClusterId uuid.UUID `gorm:"type:uuid;"` ExternalLabel string UpdatedAt time.Time diff --git a/pkg/cluster/accessor.go b/pkg/cluster/accessor.go index e50ce1e..a20c45c 100644 --- a/pkg/cluster/accessor.go +++ b/pkg/cluster/accessor.go @@ -79,6 +79,7 @@ func (x *ClusterAccessor) CreateClusterInfo(contractId uuid.UUID, cspId uuid.UUI CspID: cspId, Name: name, Status: pb.ClusterStatus_UNSPECIFIED, + StatusDesc: "", SshKeyName: conf.SshKeyName, Region: conf.Region, NumOfAz: conf.NumOfAz, @@ -97,10 +98,10 @@ func (x *ClusterAccessor) CreateClusterInfo(contractId uuid.UUID, cspId uuid.UUI } // UpdateStatus updates an status of cluster for Cluster. -func (x *ClusterAccessor) UpdateStatus(id uuid.UUID, status pb.ClusterStatus) error { +func (x *ClusterAccessor) UpdateStatus(id uuid.UUID, status pb.ClusterStatus, statusDesc string, workflowId string) error { res := x.db.Model(&model.Cluster{}). Where("ID = ?", id). - Update("Status", status) + Updates(map[string]interface{}{"Status": status, "StatusDesc": statusDesc, "WorkflowId": workflowId}) if res.Error != nil || res.RowsAffected == 0 { return fmt.Errorf("nothing updated in cluster with id %s", id.String()) @@ -123,8 +124,10 @@ func ConvertToPbCluster(cluster model.Cluster) *pb.Cluster { Id: cluster.ID.String(), Name: cluster.Name, CreatedAt: timestamppb.New(cluster.CreatedAt), - UpdatedAt: timestamppb.New(cluster.CreatedAt), + UpdatedAt: timestamppb.New(cluster.UpdatedAt), + WorkflowId: cluster.WorkflowId, Status: cluster.Status, + StatusDesc: cluster.StatusDesc, ContractId: cluster.ContractID.String(), CspId: cluster.CspID.String(), Kubeconfig: cluster.Kubeconfig, diff --git a/pkg/cluster/model/cluster.go b/pkg/cluster/model/cluster.go index cd18db0..090b0ea 100644 --- a/pkg/cluster/model/cluster.go +++ b/pkg/cluster/model/cluster.go @@ -14,7 +14,9 @@ type Cluster struct { Name string ContractID uuid.UUID CspID uuid.UUID + WorkflowId string Status pb.ClusterStatus + StatusDesc string SshKeyName string Region string NumOfAz int32 diff --git a/scripts/application_db.sql b/scripts/application_db.sql index a84232d..30b912d 100644 --- a/scripts/application_db.sql +++ b/scripts/application_db.sql @@ -5,7 +5,9 @@ CREATE TABLE application_groups name character varying(50) COLLATE pg_catalog."default", id uuid primary key, type bigint, - status bigint, + workflow_id character varying(100) COLLATE pg_catalog."default", + status integer, + status_desc character varying(10000) COLLATE pg_catalog."default", cluster_id uuid, external_label character varying(50) COLLATE pg_catalog."default", updated_at timestamp with time zone, diff --git a/scripts/cluster_db.sql b/scripts/cluster_db.sql index eeccb8a..474490d 100644 --- a/scripts/cluster_db.sql +++ b/scripts/cluster_db.sql @@ -5,7 +5,9 @@ CREATE TABLE clusters id uuid primary key, contract_id uuid, csp_id uuid, - status integer, + workflow_id character varying(100) COLLATE pg_catalog."default", + status bigint, + status_desc character varying(10000) COLLATE pg_catalog."default", ssh_key_name character varying(50) COLLATE pg_catalog."default", region character varying(50) COLLATE pg_catalog."default", num_of_az integer, From 1d03d9c8723875eced4e400d5dda7156310d2c0b Mon Sep 17 00:00:00 2001 From: Robert Choi Date: Mon, 4 Apr 2022 13:01:25 +0000 Subject: [PATCH 2/4] handle case that GetClusters returns empty list --- pkg/cluster/accessor.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/cluster/accessor.go b/pkg/cluster/accessor.go index a20c45c..7a67414 100644 --- a/pkg/cluster/accessor.go +++ b/pkg/cluster/accessor.go @@ -42,15 +42,20 @@ func (x *ClusterAccessor) GetClustersByContractID(contractId uuid.UUID) ([]*pb.C res := x.db.Find(&clusters, "contract_id = ?", contractId) - if res.RowsAffected == 0 || res.Error != nil { - return nil, fmt.Errorf("Could not find clusters with contractID: %s", contractId) + if res.Error != nil { + return nil, fmt.Errorf("Error while finding clusters with contractID: %s", contractId) } pbClusters := []*pb.Cluster{} + + // If no record is found, just return empty array. + if res.RowsAffected == 0 { + return pbClusters, nil + } + for _, cluster := range clusters { pbClusters = append(pbClusters, ConvertToPbCluster(cluster)) } - return pbClusters, nil } From 76eaf5427f6f73137a7566a060ebe26497841932 Mon Sep 17 00:00:00 2001 From: Robert Choi Date: Mon, 4 Apr 2022 13:01:25 +0000 Subject: [PATCH 3/4] handle case that GetClusters returns empty list --- .github/workflows/deploy-image-with-branch.yml | 1 + .github/workflows/deploy-image.yml | 2 +- .github/workflows/golangci-lint.yml | 1 + cmd/server/cluster_handler_test.go | 8 ++++---- pkg/application/accessor.go | 2 ++ pkg/application/accessor_test.go | 2 +- pkg/cluster/accessor.go | 11 ++++++++--- pkg/cluster/accessor_test.go | 2 +- 8 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.github/workflows/deploy-image-with-branch.yml b/.github/workflows/deploy-image-with-branch.yml index bfc0b07..a51167a 100644 --- a/.github/workflows/deploy-image-with-branch.yml +++ b/.github/workflows/deploy-image-with-branch.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - 'release**' jobs: build-and-pPush-image: runs-on: ubuntu-latest diff --git a/.github/workflows/deploy-image.yml b/.github/workflows/deploy-image.yml index 0407c9b..7b6f064 100644 --- a/.github/workflows/deploy-image.yml +++ b/.github/workflows/deploy-image.yml @@ -3,7 +3,7 @@ on: push: branches: - main - + - 'release**' jobs: build-and-push-image: runs-on: ubuntu-latest diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 1ccfe0b..e0e7a23 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -5,6 +5,7 @@ on: - v* branches: - main + - 'release**' pull_request: jobs: golangci: diff --git a/cmd/server/cluster_handler_test.go b/cmd/server/cluster_handler_test.go index 5263be9..a5839bc 100644 --- a/cmd/server/cluster_handler_test.go +++ b/cmd/server/cluster_handler_test.go @@ -208,8 +208,8 @@ func TestGetClusters(t *testing.T) { }, nil) }, checkResponse: func(req *pb.GetClustersRequest, res *pb.GetClustersResponse, err error) { - require.Error(t, err) - require.Equal(t, res.Code, pb.Code_NOT_FOUND) + require.NoError(t, err) + require.Equal(t, res.Code, pb.Code_OK_UNSPECIFIED) }, }, { @@ -272,8 +272,8 @@ func TestGetClusters(t *testing.T) { }, buildStubs: func(mockContractClient *mocktks.MockContractServiceClient) {}, checkResponse: func(req *pb.GetClustersRequest, res *pb.GetClustersResponse, err error) { - require.Error(t, err) - require.Equal(t, res.Code, pb.Code_NOT_FOUND) + require.NoError(t, err) + require.Equal(t, res.Code, pb.Code_OK_UNSPECIFIED) }, }, { diff --git a/pkg/application/accessor.go b/pkg/application/accessor.go index 745e1b6..673f117 100644 --- a/pkg/application/accessor.go +++ b/pkg/application/accessor.go @@ -193,7 +193,9 @@ func reflectToPbAppGroup(model model.ApplicationGroup) *pb.AppGroup { AppGroupId: model.ID.String(), AppGroupName: model.Name, Type: model.Type, + WorkflowId: model.WorkflowId, Status: model.Status, + StatusDesc: model.StatusDesc, ClusterId: model.ClusterId.String(), ExternalLabel: model.ExternalLabel, CreatedAt: timestamppb.New(model.CreatedAt), diff --git a/pkg/application/accessor_test.go b/pkg/application/accessor_test.go index 1df97c1..a4bf78d 100644 --- a/pkg/application/accessor_test.go +++ b/pkg/application/accessor_test.go @@ -129,7 +129,7 @@ func TestGetAppGroup(t *testing.T) { t.Logf("matching app group name: %s", appGroup.AppGroupName) } func TestUpdateAppGroupStatus(t *testing.T) { - if err := accessor.UpdateAppGroupStatus(appGroupID, pb.AppGroupStatus_APP_GROUP_RUNNING); err != nil { + if err := accessor.UpdateAppGroupStatus(appGroupID, pb.AppGroupStatus_APP_GROUP_RUNNING, "", ""); err != nil { t.Errorf("an error was unexpected while update application group: %s", err) } diff --git a/pkg/cluster/accessor.go b/pkg/cluster/accessor.go index a20c45c..7a67414 100644 --- a/pkg/cluster/accessor.go +++ b/pkg/cluster/accessor.go @@ -42,15 +42,20 @@ func (x *ClusterAccessor) GetClustersByContractID(contractId uuid.UUID) ([]*pb.C res := x.db.Find(&clusters, "contract_id = ?", contractId) - if res.RowsAffected == 0 || res.Error != nil { - return nil, fmt.Errorf("Could not find clusters with contractID: %s", contractId) + if res.Error != nil { + return nil, fmt.Errorf("Error while finding clusters with contractID: %s", contractId) } pbClusters := []*pb.Cluster{} + + // If no record is found, just return empty array. + if res.RowsAffected == 0 { + return pbClusters, nil + } + for _, cluster := range clusters { pbClusters = append(pbClusters, ConvertToPbCluster(cluster)) } - return pbClusters, nil } diff --git a/pkg/cluster/accessor_test.go b/pkg/cluster/accessor_test.go index c716fe7..3eaa9a5 100644 --- a/pkg/cluster/accessor_test.go +++ b/pkg/cluster/accessor_test.go @@ -118,7 +118,7 @@ func TestGetClustersByCspID(t *testing.T) { } func TestUpdateStatus(t *testing.T) { - err := clusterAccessor.UpdateStatus(clusterId, pb.ClusterStatus_INSTALLING) + err := clusterAccessor.UpdateStatus(clusterId, pb.ClusterStatus_INSTALLING, "", "") if err != nil { t.Errorf("An error occurred while updating cluster status. Err: %s", err) } From d82b21d670c6662e6729a47776fa4612efd7e759 Mon Sep 17 00:00:00 2001 From: Taekyu Date: Wed, 6 Apr 2022 13:39:52 +0900 Subject: [PATCH 4/4] update go.mod --- .github/workflows/golangci-lint.yml | 2 +- go.mod | 4 ++-- go.sum | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index e0e7a23..80e2f06 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -19,7 +19,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version - version: v1.29 + version: latest # Optional: working directory, useful for monorepos # working-directory: somedir diff --git a/go.mod b/go.mod index ef19b33..464994b 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,8 @@ require ( github.com/google/uuid v1.3.0 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/jackc/pgx/v4 v4.15.0 // indirect - github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457 - github.com/openinfradev/tks-proto v0.0.6-0.20220324075944-e471af2c8c49 + github.com/openinfradev/tks-common v0.0.0-20220406045249-8ce7aeb0cb72 + github.com/openinfradev/tks-proto v0.0.6-0.20220406043255-9fffe49c4625 github.com/stretchr/testify v1.7.0 golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 // indirect golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect diff --git a/go.sum b/go.sum index aa9b768..3459ee9 100644 --- a/go.sum +++ b/go.sum @@ -827,9 +827,11 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.m github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457 h1:yKhmkHl2qi2/pyNBST8zI8fpcjjcpTFzACBwtk2fjik= github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457/go.mod h1:3d+gW0PPeBzEUtVRoupTTXAFlfdJLyHy2Lzlw4rcnOk= +github.com/openinfradev/tks-common v0.0.0-20220406045249-8ce7aeb0cb72 h1:DzL7q4G4UA/z+oxPnsCuHFTcnJIdLP6LIkzUn4bZvJc= +github.com/openinfradev/tks-common v0.0.0-20220406045249-8ce7aeb0cb72/go.mod h1:3Mb3KGoozH5vT7zdIQb3CXYjqpIhUbgK/qhVJu3VMRY= github.com/openinfradev/tks-proto v0.0.6-0.20211015003551-ed8f9541f40d/go.mod h1:ul6kvgOXhNQvXEUmb92Wh5BmsuzknnTCb3wqmVNd/iI= -github.com/openinfradev/tks-proto v0.0.6-0.20220324075944-e471af2c8c49 h1:XYNl9fXGUH02/Laqht+ba2FmgOXrl7sUDHcAZHsSaUE= -github.com/openinfradev/tks-proto v0.0.6-0.20220324075944-e471af2c8c49/go.mod h1:3DrATRNCUJMW0oMLHT6D1teN4hX0vMGH4qOGnadakmo= +github.com/openinfradev/tks-proto v0.0.6-0.20220406043255-9fffe49c4625 h1:kvcIa/cU+sjsK4VhQHjtquS3mGC3UAmbNHL9Qe03vYA= +github.com/openinfradev/tks-proto v0.0.6-0.20220406043255-9fffe49c4625/go.mod h1:3DrATRNCUJMW0oMLHT6D1teN4hX0vMGH4qOGnadakmo= github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis= github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=