Skip to content

Commit

Permalink
Merge branch '2.x.x' into fix/cli-fail-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
umanggoyald11 authored Jan 28, 2025
2 parents 012f586 + 7a51646 commit ef4ce20
Show file tree
Hide file tree
Showing 13 changed files with 890 additions and 343 deletions.
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 @@ package deploy

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 @@ var serviceSetDeployCmd = &cobra.Command{
},
}

const (
Yes = "y"
No = "n"
)

func init() {

serviceSetDeployCmd.Flags().StringVar(&env, "env", "", "environment for deploying the service-set")
Expand Down Expand Up @@ -61,8 +68,45 @@ func executeDeployServiceSet(cmd *cobra.Command) {
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)
}
}
143 changes: 98 additions & 45 deletions cmd/describe/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package describe
import (
"encoding/json"
"fmt"
"strings"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
"github.com/dream11/odin/pkg/table"
"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"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,66 +66,117 @@ func writeOutput(response *environment.DescribeEnvironmentResponse, format strin

switch format {
case constant.TEXT:
writeAsTextEnvResponse(response)
printEnvInfo(response)
case constant.JSON:
writeAsJSONEnvResponse(response)
default:
log.Fatal("Unknown output format: ", format)
}
}

func writeAsTextEnvResponse(response *environment.DescribeEnvironmentResponse) {

tableHeaders := []string{"Name",
"state",
"autoDeletionTime",
"cloudProviderAccounts",
"createdBy",
"updatedBy",
"createdAt",
"updatedAt",
"services"}
var tableData [][]interface{}
func printEnvInfo(response *environment.DescribeEnvironmentResponse) {
env := response.Environment
var accountInfoList []string

// Extracting necessary fields
name := env.GetName()
envType := env.GetProvisioningType()
state := env.GetStatus()
autoDeletionTime := env.AutoDeletionTime.AsTime().String()
cloudProviderAccounts := []string{}
providerAccountCluster := map[string][]string{}
for _, accountInfo := range env.AccountInformation {
accountInfoList = append(accountInfoList, accountInfo.ProviderAccountName)
}
accountInfoListJSON, err := json.Marshal(accountInfoList)
if err != nil {
log.Fatal("Failed to marshal account info list: ", err)
cloudProviderAccounts = append(cloudProviderAccounts, accountInfo.ProviderAccountName)
providerAccountCluster[accountInfo.ProviderAccountName] = getClusterNames(accountInfo)
}

var servicesSummary []map[string]interface{}
createdBy := env.GetCreatedBy()
updatedBy := env.GetUpdatedBy()
createdAt := env.CreatedAt.AsTime().String()
updatedAt := env.UpdatedAt.AsTime().String()
services := []string{}
for _, svc := range env.Services {
serviceMap := map[string]interface{}{
"name": svc.Name,
"version": svc.Version,
"status": svc.Status,
}
if len(svc.Components) > 0 {
serviceMap["components"] = svc.Components
if serviceName == "" {
services = append(services, fmt.Sprintf(" - name: %s\n version: %s\n status: %s", *svc.Name, *svc.Version, svc.GetStatus()))
} else {
if serviceName == *svc.Name {
var customServicesOp = []string{}
customServicesOp = append(customServicesOp, fmt.Sprintf(" - name: %s\n version: %s\n status: %s\n", *svc.Name, *svc.Version, svc.GetStatus()))
customServicesOp = append(customServicesOp, " components: \n")
componentBytes, err := json.MarshalIndent(svc.Components, "", " ")
if err != nil {
log.Fatal("Failed to marshal services summary: ", err)
}
var formatedComponentData = string(componentBytes)
formatedComponentData, _ = util.ConvertJSONToYAML(formatedComponentData)
lines := strings.Split(formatedComponentData, "\n")

for i, line := range lines {
lines[i] = "\t" + line
}
formatedComponentData = strings.Join(lines, "\n")
// Add two tabs before each line in the string
customServicesOp = append(customServicesOp, formatedComponentData)

services = append(services, strings.Join(customServicesOp, ""))
}

}
servicesSummary = append(servicesSummary, serviceMap)
}
servicesSummaryJSON, err := json.Marshal(servicesSummary)
if err != nil {
log.Fatal("Failed to marshal services summary: ", err)

// Formatting and printing the information
fmt.Printf("Describing Env: %s\n\n", name)
fmt.Printf("name: %s\n", name)
fmt.Printf("envType: %s\n", envType)
fmt.Printf("state: %s\n", state)
fmt.Printf("autoDeletionTime: \"%s\"\n", autoDeletionTime)
fmt.Printf("cloudProviderAccounts:\n")
for _, account := range cloudProviderAccounts {
fmt.Printf(" - %s\n", account)
}
fmt.Printf("cluster:\n")
for account, clusters := range providerAccountCluster {
fmt.Printf(" - %s\n", account)
for _, cluster := range clusters {
fmt.Printf(" - %s\n", cluster)
}

tableData = append(tableData, []interface{}{
env.GetName(),
env.GetStatus(),
env.AutoDeletionTime.AsTime().String(),
string(accountInfoListJSON),
env.GetCreatedBy(),
env.GetUpdatedBy(),
env.CreatedAt.AsTime().String(),
env.UpdatedAt.AsTime().String(),
string(servicesSummaryJSON),
})
}
fmt.Printf("createdBy: %s\n", createdBy)
fmt.Printf("updatedBy: %s\n", updatedBy)
fmt.Printf("createdAt: \"%s\"\n", createdAt)
fmt.Printf("updatedAt: \"%s\"\n", updatedAt)
fmt.Printf("services:\n%s\n", strings.Join(services, "\n"))
}

table.Write(tableHeaders, tableData)
func findValueByKey(val interface{}, key string) string {
switch v := val.(type) {
case map[string]interface{}: // If it's a map, check for the key
if value, ok := v[key]; ok {
return fmt.Sprintf("%v", value)
}
// Recurse through nested maps or slices
for _, subVal := range v {
return findValueByKey(subVal, key)
}
case []interface{}: // If it's a slice, recurse for each element
for _, item := range v {
return findValueByKey(item, key)
}
}
return "" // Key not found
}

func getClusterNames(information *v1.AccountInformation) []string {
clusterNames := []string{}
for _, service := range information.ServiceAccountsSnapshot.Account.Services {
if service.Category == "KUBERNETES" {
for key, val := range service.GetData().AsMap() {
if key == "clusters" {
clusterNames = append(clusterNames, findValueByKey(val, "name"))
}
}
}
}
return clusterNames
}

func writeAsJSONEnvResponse(response *environment.DescribeEnvironmentResponse) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/uuid v1.6.0
github.com/mitchellh/cli v1.1.5
github.com/olekukonko/tablewriter v0.0.5
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 1 addition & 1 deletion internal/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func grpcClient(ctx *context.Context) (*grpc.ClientConn, *context.Context, error) {
appConfig := config.GetConfig()
traceID := util.GenerateTraceID()
log.Debugf("Generated trace ID: %s", traceID)
log.Infof("Generated trace ID: %s", traceID)

if appConfig.BackendAddress == "" {
log.Fatal("Cannot create grpc client: Backend address is empty in config! Run `odin configure` to set backend address")
Expand Down
3 changes: 2 additions & 1 deletion internal/service/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (e *Component) OperateComponent(ctx *context.Context, request *serviceProto
}
if response != nil {
message = util.GenerateResponseMessageComponentSpecific(response.GetServiceResponse(), []string{request.GetComponentName()})
logFailedComponentMessagesOnceForComponents(response.GetServiceResponse(), []string{request.GetComponentName()})
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
spinnerInstance.Start()
}
Expand Down Expand Up @@ -89,7 +90,7 @@ func (e *Component) DescribeComponentType(ctx *context.Context, request *compone
}

// CompareOperationChanges compares the operation changes
func (c *Component) CompareOperationChanges(ctx *context.Context, request *serviceProto.OperateComponentDiffRequest) (*serviceProto.OperateComponentDiffResponse, error) {
func (e *Component) CompareOperationChanges(ctx *context.Context, request *serviceProto.OperateComponentDiffRequest) (*serviceProto.OperateComponentDiffResponse, error) {

conn, requestCtx, err := grpcClient(ctx)
if err != nil {
Expand Down
Loading

0 comments on commit ef4ce20

Please sign in to comment.