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

Merged
merged 17 commits into from
Jan 22, 2025
Merged
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
46 changes: 45 additions & 1 deletion cmd/deploy/service-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import (
"encoding/json"
"fmt"
"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 All @@ -24,6 +26,11 @@
},
}

const (
Yes = "y"

Check failure on line 30 in cmd/deploy/service-set.go

View workflow job for this annotation

GitHub Actions / lint

exported const Yes should have comment (or a comment on this block) or be unexported
No = "n"
nikhil-024 marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {

serviceSetDeployCmd.Flags().StringVar(&env, "env", "", "environment for deploying the service-set")
Expand Down Expand Up @@ -61,8 +68,45 @@
deployServiceSetRequest.Name = serviceSetName
}

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

services, errs := serviceClient.GetConflictingServices(&ctx, conflictingServicesRequest)
if errs != nil {
log.Fatal(fmt.Sprintf("Failed to list services with conflicting versions: %s", errs.Error()))
return
}
for _, service := range services.Services {

allowedInputsSlice := []string{Yes, No}
allowedInputs := make(map[string]struct{}, len(allowedInputsSlice))
for _, input := range allowedInputsSlice {
allowedInputs[input] = struct{}{}
}
message := fmt.Sprintf("Service: %s already deployed with different version : %s \n Do you want to deploy service with new version : %s ? (y/n)", service.Name, service.ExistingVersion, service.NewVersion)
inputHandler := ui.Input{}
val, err := inputHandler.AskWithConstraints(message, allowedInputs)

if err != nil {
log.Fatal(fmt.Sprintf("An error occurred while processing input: %s", err.Error()))
}

if val != Yes {
log.Info(fmt.Sprintf("Skipping service %s from deploy", service.Name))
for _, svc := range deployServiceSetRequest.Services {
if svc.ServiceName == service.Name {
svc.ForceFlag = false
}
}
}

}

err := serviceClient.DeployServiceSet(&ctx, &deployServiceSetRequest)
if err != nil {
log.Fatal("Failed to deploy service ", err)
log.Fatal("Failed to deploy service set. ", err)
surajgour-d11 marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion cmd/describe/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package describe
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"
"strings"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
"github.com/dream11/odin/pkg/util"
v1 "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1"
environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
Expand Down
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 @@ -51,6 +53,7 @@ func (e *Service) DeployService(ctx *context.Context, request *serviceProto.Depl
}

if response != nil {

message = util.GenerateResponseMessage(response.GetServiceResponse())
logFailedComponentMessagesOnce(response.GetServiceResponse())
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
Expand Down Expand Up @@ -79,6 +82,7 @@ func logFailedComponentMessagesOnce(response *serviceProto.ServiceResponse) {
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))
surajgour-d11 marked this conversation as resolved.
Show resolved Hide resolved
return err
}
client := serviceProto.NewServiceServiceClient(conn)
Expand All @@ -97,7 +101,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 @@ -107,17 +111,33 @@ 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", "Version", "Action", "Status", "Error"})
for _, serviceResponse := range response.GetServices() {
logFailedComponentMessagesOnce(serviceResponse.GetServiceResponse())
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.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)
surajgour-d11 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

Expand Down Expand Up @@ -318,8 +338,10 @@ 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,
ForceFlag: true,
})
}

Expand All @@ -345,3 +367,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))
surajgour-d11 marked this conversation as resolved.
Show resolved Hide resolved
}
return response, err
}
Binary file removed odin
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 @@ -11,6 +11,7 @@ import (

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

Expand All @@ -37,6 +38,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", "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
12 changes: 10 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;
surajgour-d11 marked this conversation as resolved.
Show resolved Hide resolved
}

message ServiceSet {
Expand Down Expand Up @@ -69,3 +70,10 @@ message Service {
repeated google.protobuf.Struct provisioning_config_files = 8;
repeated string versions = 9;
}

message ServiceVersionComparisonMetadata {
string name = 1;
string existing_version = 2;
string new_version = 3;
bool version_conflict = 4;
}
11 changes: 11 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 All @@ -38,6 +39,7 @@ message ServiceIdentifier {
string service_name = 1;
string service_version = 2;
string tags = 3;
bool force_flag = 4;
}

message DeployReleasedServiceRequest {
Expand Down Expand Up @@ -143,3 +145,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.ServiceVersionComparisonMetadata services = 1;
}
Loading
Loading