Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/service set update #272

Open
wants to merge 6 commits into
base: 2.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion cmd/deploy/service-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"

"github.com/dream11/odin/internal/ui"
"github.com/dream11/odin/pkg/config"
serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
Expand Down Expand Up @@ -61,8 +62,56 @@ func executeDeployServiceSet(cmd *cobra.Command) {
deployServiceSetRequest.Name = serviceSetName
}

conflictingServicesRequest := &serviceProto.GetConflictingServicesRequest{
EnvName: env,
Name: deployServiceSetRequest.Name,
}

services, errs := serviceClient.GetConflictingServices(&ctx, conflictingServicesRequest)
if errs != nil {
log.Fatal("Failed to list services with conflicting versions. ", errs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the err object printed?

return
}
var serviceNames []string
for _, service := range services.Services {

allowedInputsSlice := []string{"y", "n"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use constants for "y" or "n"

allowedInputs := make(map[string]struct{}, len(allowedInputsSlice))
for _, input := range allowedInputsSlice {
allowedInputs[input] = struct{}{}
}
message := "Service already deployed with different version.Do you want to deploy service " + service.Name + " ? (y/n)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use string formatter

inputHandler := ui.Input{}
val, err := inputHandler.AskWithConstraints(message, allowedInputs)

if err != nil {
log.Fatal(err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some error message here

}

if val != "y" {
log.Info("Skipping service ", service.Name, " from deploy")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use formatted strings

serviceNames = append(serviceNames, service.Name)
}
// Remove services from deployServiceSetRequest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this logic be part of the client? Do you think this should be handled by the deployer?

var updatedServices []*serviceProto.ServiceIdentifier
for _, svc := range deployServiceSetRequest.Services {
shouldRemove := false
for _, name := range serviceNames {
if svc.ServiceName == name {
shouldRemove = true
break
}
}
if !shouldRemove {
updatedServices = append(updatedServices, svc)
}
}
deployServiceSetRequest.Services = updatedServices

}

err := serviceClient.DeployServiceSet(&ctx, &deployServiceSetRequest)
if err != nil {
log.Fatal("Failed to deploy service ", err)
log.Fatal("Failed to deploy service set. ", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this err objected logged?

}
}
52 changes: 44 additions & 8 deletions internal/service/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/dream11/odin/pkg/util"
serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -49,7 +51,7 @@ func (e *Service) DeployService(ctx *context.Context, request *serviceProto.Depl
}

if response != nil {
message = util.GenerateResponseMessage(response.GetServiceResponse())
message = util.GenerateResponseMessage(response.ServiceResponse)
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
spinnerInstance.Start()
}
Expand All @@ -63,6 +65,7 @@ func (e *Service) DeployService(ctx *context.Context, request *serviceProto.Depl
func (e *Service) DeployServiceSet(ctx *context.Context, request *serviceProto.DeployServiceSetRequest) error {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is error log?

return err
}
client := serviceProto.NewServiceServiceClient(conn)
Expand All @@ -81,7 +84,7 @@ func (e *Service) DeployServiceSet(ctx *context.Context, request *serviceProto.D
var message string
for {
response, err := stream.Recv()
spinnerInstance.Stop()

if err != nil {
if errors.Is(err, context.Canceled) || err == io.EOF {
break
Expand All @@ -91,16 +94,34 @@ func (e *Service) DeployServiceSet(ctx *context.Context, request *serviceProto.D
}

if response != nil {
message = ""
spinnerInstance.Stop()
var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader([]string{"Service Name", "Service Version", "Service Tags", "Service Action", "Service Status", "Error"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
table.SetHeader([]string{"Service Name", "Service Version", "Service Tags", "Service Action", "Service Status", "Error"})
table.SetHeader([]string{"Service Name", "Version", "Tags", "Action", "Status", "Error"})

for _, serviceResponse := range response.GetServices() {
message += util.GenerateResponseMessage(serviceResponse.GetServiceResponse())
var errorMessage string
if serviceResponse.ServiceResponse.ServiceStatus.ServiceStatus == "FAILED" {
traceID := (*requestCtx).Value(constant.TraceIDKey)
errorMessage += fmt.Sprintf("[%s] TraceID: %s \n", serviceResponse.ServiceResponse.ServiceStatus.Error, traceID)
}
row := []string{
serviceResponse.ServiceIdentifier.ServiceName,
serviceResponse.ServiceIdentifier.ServiceVersion,
serviceResponse.ServiceIdentifier.Tags,
serviceResponse.ServiceResponse.ServiceStatus.ServiceAction,
serviceResponse.ServiceResponse.ServiceStatus.ServiceStatus,
errorMessage,
}
table.Append(row)
}

table.Render()
message = buf.String()
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
spinnerInstance.Start()
}
}

log.Info(message)
fmt.Println(message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why changed this?

return err
}

Expand Down Expand Up @@ -301,8 +322,9 @@ func (e *Service) ConvertToDeployServiceSetRequest(serviceSet *serviceDto.Servic
var services []*serviceProto.ServiceIdentifier
for _, service := range serviceSet.Services {
services = append(services, &serviceProto.ServiceIdentifier{
ServiceName: service.ServiceName,
ServiceVersion: service.ServiceVersion,
ServiceName: service.Name,
ServiceVersion: service.Version,
Tags: service.Tags,
})
}

Expand All @@ -328,3 +350,17 @@ func (e *Service) DescribeService(ctx *context.Context, request *serviceProto.De

return response, nil
}

// GetConflictingServices deploys service
func (e *Service) GetConflictingServices(ctx *context.Context, request *serviceProto.GetConflictingServicesRequest) (*serviceProto.GetConflictingServicesResponse, error) {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return &serviceProto.GetConflictingServicesResponse{}, err
}
client := serviceProto.NewServiceServiceClient(conn)
response, err := client.GetConflictingServices(*requestCtx, request)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this an error log?

}
return response, err
}
Binary file modified odin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this binary

Binary file not shown.
22 changes: 22 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

v1 "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
"github.com/google/uuid"
"github.com/olekukonko/tablewriter"
)

// SplitProviderAccount splits string into list of cloud provider accounts
Expand All @@ -35,6 +36,27 @@ func GenerateResponseMessage(response *v1.ServiceResponse) string {
return message
}

// GenerateServiceSetResponseMessage generate response message from ServiceSetResponse
func GenerateServiceSetResponseMessage(response *v1.DeployServiceSetServiceResponse) string {

message := fmt.Sprintf("\n Service %s %s %s %s", response.ServiceIdentifier.ServiceName, response.ServiceIdentifier.ServiceVersion, response.ServiceResponse.ServiceStatus.ServiceAction, response.ServiceResponse.ServiceStatus)
var tableData [][]string
row := []string{
response.ServiceIdentifier.ServiceName,
response.ServiceIdentifier.ServiceVersion,
response.ServiceResponse.ServiceStatus.ServiceAction,
response.ServiceResponse.ServiceStatus.ServiceStatus,
}
tableData = append(tableData, row)

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Service Name", "Service Version", "Service Action", "Service Status", "Error"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
table.SetHeader([]string{"Service Name", "Service Version", "Service Action", "Service Status", "Error"})
table.SetHeader([]string{"Service Name", "Version", "Action", "Status", "Error"})

table.AppendBulk(tableData)
table.Render()
return message

}

// FormatToHumanReadableDuration takes a date-time string representing the last deployment time, and returns a human-readable string representing the duration since the last deployment
func FormatToHumanReadableDuration(inputDateTime string) string {
// Check if the input is a Unix timestamp prefixed by "seconds:"
Expand Down
5 changes: 3 additions & 2 deletions proto/dream11/od/dto/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ message ServiceDefinition {
}

message ServiceIdentifier {
string service_name = 1;
string service_version = 2;
string name = 1;
string version = 2;
string tags = 3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tags is a string?

}

message ServiceSet {
Expand Down
10 changes: 10 additions & 0 deletions proto/dream11/od/service/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ service ServiceService {
rpc ListService(ListServiceRequest) returns (ListServiceResponse) {}
rpc DescribeService(DescribeServiceRequest) returns (DescribeServiceResponse) {}
rpc OperateComponentDiff(OperateComponentDiffRequest) returns (OperateComponentDiffResponse) {}
rpc GetConflictingServices(GetConflictingServicesRequest) returns (GetConflictingServicesResponse) {}
}

message DeployServiceRequest {
Expand Down Expand Up @@ -143,3 +144,12 @@ message OperateComponentDiffResponse {
google.protobuf.Struct old_values = 1;
google.protobuf.Struct new_values = 2;
}
message GetConflictingServicesRequest {
string env_name = 1;
string name = 2;
repeated ServiceIdentifier services = 3;
}

message GetConflictingServicesResponse {
repeated dto.v1.ServiceMetadata services = 1;
}
Loading
Loading