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: removed fatal errors during onboarding #1518

Open
wants to merge 1 commit into
base: develop
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
17 changes: 10 additions & 7 deletions handlers/csv_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/uptrace/bun/dialect"
)

func (handler *ApiHandler) DownloadInventoryCSV(c *gin.Context) {
resources, err := handler.ctrl.ListResources(c)
if err != nil {
logrus.WithError(err).Error("Could not read from DB")
log.WithError(err).Error("Could not read from DB")
c.JSON(http.StatusInternalServerError, gin.H{"error": "cloud not read from DB"})
return
}
Expand All @@ -46,7 +45,7 @@ func (handler *ApiHandler) DownloadInventoryCSVForView(c *gin.Context) {
if len(view.Filters) == 0 {
err := handler.db.NewRaw("SELECT * FROM resources").Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Errorf("select failed")
log.WithError(err).Errorf("select failed")
}
respondWithCSVDownload(resources, c)
}
Expand Down Expand Up @@ -211,7 +210,7 @@ func (handler *ApiHandler) DownloadInventoryCSVForView(c *gin.Context) {

err = handler.db.NewRaw(query).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Errorf("scan failed")
log.WithError(err).Errorf("scan failed")
}
} else {
query := fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id", whereClause)
Expand Down Expand Up @@ -252,11 +251,15 @@ func respondWithCSVDownload(resources []models.Resource, c *gin.Context) {
for _, record := range resources {
tags, err := json.Marshal(record.Tags)
if err != nil {
log.Fatalf("Could not marshal tags")
log.Error("Could not marshal tags")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Something went wrong"})
return
}
metadata, err := json.Marshal(record.Metadata)
if err != nil {
log.Fatalf("Could not marshal metadata")
log.Error("Could not marshal metadata")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Something went wrong"})
return
}

row := []string{
Expand Down
12 changes: 6 additions & 6 deletions handlers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ func makeClientFromAccount(account models.Account) (*providers.ProviderClient, e
&clientcmd.ClientConfigLoadingRules{ExplicitPath: account.Credentials["path"]},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
log.Fatal(err)
return nil, err
}

k8sClient, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
log.Fatal(err)
return nil, err
}

client := providers.K8sClient{
Expand Down Expand Up @@ -293,7 +293,7 @@ func makeClientFromAccount(account models.Account) (*providers.ProviderClient, e
if account.Provider == "azure" {
creds, err := azidentity.NewClientSecretCredential(account.Credentials["tenantId"], account.Credentials["clientId"], account.Credentials["clientSecret"], &azidentity.ClientSecretCredentialOptions{})
if err != nil {
log.Fatal(err)
return nil, err
}

client := providers.AzureClient{
Expand Down Expand Up @@ -326,7 +326,7 @@ func makeClientFromAccount(account models.Account) (*providers.ProviderClient, e
t := digest.NewTransport(account.Credentials["publicApiKey"], account.Credentials["privateApiKey"])
tc, err := t.Client()
if err != nil {
log.Fatal(err.Error())
return nil, err
}

client := mdb.NewClient(tc)
Expand All @@ -339,12 +339,12 @@ func makeClientFromAccount(account models.Account) (*providers.ProviderClient, e
if account.Provider == "gcp" {
data, err := os.ReadFile(account.Credentials["accountKey"])
if err != nil {
log.Fatal(err)
return nil, err
}

creds, err := google.CredentialsFromJSON(context.Background(), data, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
log.Fatal(err)
return nil, err
}

return &providers.ProviderClient{
Expand Down
2 changes: 1 addition & 1 deletion internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func loadConfigFromBytes(b []byte) (*models.Config, error) {

err := toml.Unmarshal([]byte(b), &config)
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid config file")
}

return &config, nil
Expand Down
4 changes: 2 additions & 2 deletions providers/aws/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R
pricingResult := models.PricingResult{}
err := json.Unmarshal([]byte(pricingOutput.PriceList[0]), &pricingResult)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
log.Errorf("Failed to unmarshal JSON: %v", err)
}

for _, onDemand := range pricingResult.Terms.OnDemand {
for _, priceDimension := range onDemand.PriceDimensions {
hourlyCost, err = strconv.ParseFloat(priceDimension.PricePerUnit.USD, 64)
if err != nil {
log.Fatalf("Failed to parse hourly cost: %v", err)
log.Errorf("Failed to parse hourly cost: %v", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Program will still crash later because hourlycost won't have correct value

}
break
}
Expand Down
4 changes: 2 additions & 2 deletions providers/aws/elasticache/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ func Clusters(ctx context.Context, client ProviderClient) ([]Resource, error) {
pricingResult := models.PricingResult{}
err := json.Unmarshal([]byte(pricingOutput.PriceList[0]), &pricingResult)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
log.Errorf("Failed to unmarshal JSON: %v", err)
}

for _, onDemand := range pricingResult.Terms.OnDemand {
for _, priceDimension := range onDemand.PriceDimensions {
hourlyCost, err = strconv.ParseFloat(priceDimension.PricePerUnit.USD, 64)
if err != nil {
log.Fatalf("Failed to parse hourly cost: %v", err)
log.Errorf("Failed to parse hourly cost: %v", err)
}
break
}
Expand Down
4 changes: 2 additions & 2 deletions providers/aws/rds/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R
pricingResult := models.PricingResult{}
err := json.Unmarshal([]byte(pricingOutput.PriceList[0]), &pricingResult)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
log.Errorf("Failed to unmarshal JSON: %v", err)
}

for _, onDemand := range pricingResult.Terms.OnDemand {
for _, priceDimension := range onDemand.PriceDimensions {
hourlyCost, err = strconv.ParseFloat(priceDimension.PricePerUnit.USD, 64)
if err != nil {
log.Fatalf("Failed to parse hourly cost: %v", err)
log.Errorf("Failed to parse hourly cost: %v", err)
}
break
}
Expand Down
6 changes: 5 additions & 1 deletion utils/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"strings"

log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/migrations"
Expand Down Expand Up @@ -71,7 +72,10 @@ func SetupSchema(db *bun.DB, c *models.Config, accounts []models.Account) error
account.Status = "CONNECTED"
_, err = db.NewInsert().Model(&account).Exec(context.Background())
if err != nil {
log.Warnf("%s account cannot be inserted to database\n%v", account.Provider, err)
if strings.Contains(err.Error(), "failed: accounts.credentials (2067)") {
continue
}
log.Warnf("%s account cannot be inserted to database\n", account.Provider)
}
}

Expand Down
Loading