Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Azanul authored Oct 9, 2023
2 parents 94881a1 + 9c54d99 commit b23d397
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 72 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lint
on:
push:

jobs:
lint:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v4
with:
go-version-file: go.mod
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --timeout=10m
version: v1.54.2
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ require (
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230330154414-c0448cd141ea
google.golang.org/genproto v0.0.0-20230330154414-c0448cd141ea // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion handlers/feedback_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (handler *ApiHandler) NewFeedbackHandler(c *gin.Context) {
]
}`, description, email)

payloadField.Write([]byte(payloadJSON))
_, _ = payloadField.Write([]byte(payloadJSON))

imagePart, err := writer.CreateFormFile("files[0]", filepath.Base("temp-image"))
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/api/v1/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -3013,7 +3012,7 @@ func RestoreAsset(dir, name string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -46,7 +45,7 @@ func loadConfigFromFile(path string) (*Config, error) {
return nil, fmt.Errorf("no such file %s", filename)
}

yamlFile, err := ioutil.ReadFile(filename)
yamlFile, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -436,7 +435,7 @@ func Load(configPath string, telemetry bool, analytics utils.Analytics, db *bun.

accounts = append(accounts, cloudAccount)

data, err := ioutil.ReadFile(account.ServiceAccountKeyPath)
data, err := os.ReadFile(account.ServiceAccountKeyPath)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion migrations/20230413100000_set_default_tags_in_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func init() {
// Set default value for tags in resources table, because sometimes it was 'null'
// The Resources model was updated to set the default value to []string{}, but all older instances
// of Komiser didn't have that default value set, so we need to update the database
db.NewUpdate().
_, _ = db.NewUpdate().
Table("resources").
Set("tags = ?", []string{}).
Where("tags = 'null'").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func init() {
`)

if err != nil {
db.ExecContext(ctx, `
_, _ = db.ExecContext(ctx, `
ALTER TABLE alerts
ADD COLUMN is_slack BOOLEAN DEFAULT 1;
`)

db.ExecContext(ctx, `
_, _ = db.ExecContext(ctx, `
ALTER TABLE alerts
ADD COLUMN endpoint TEXT;
`)

db.ExecContext(ctx, `
_, _ = db.ExecContext(ctx, `
ALTER TABLE alerts
ADD COLUMN secret TEXT;
`)
Expand Down
4 changes: 2 additions & 2 deletions migrations/20230619100000_add_new_relation_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func init() {
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
// adding new column relation for migration
if db.Dialect().Name() == dialect.SQLite {
db.ExecContext(ctx, "ALTER TABLE resources ADD COLUMN relations TEXT DEFAULT '[]';")
_, _ = db.ExecContext(ctx, "ALTER TABLE resources ADD COLUMN relations TEXT DEFAULT '[]';")
} else {
db.ExecContext(ctx, "ALTER TABLE resources ADD COLUMN relations JSONB DEFAULT '[]'::jsonb;")
_, _ = db.ExecContext(ctx, "ALTER TABLE resources ADD COLUMN relations JSONB DEFAULT '[]'::jsonb;")
}
return nil
}, func(ctx context.Context, db *bun.DB) error {
Expand Down
2 changes: 1 addition & 1 deletion migrations/20231014100000_add_new_status_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func init() {
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
// adding new column relation for migration
db.ExecContext(ctx, "ALTER TABLE accounts ADD COLUMN status TEXT;")
_, _ = db.ExecContext(ctx, "ALTER TABLE accounts ADD COLUMN status TEXT;")
return nil
}, func(ctx context.Context, db *bun.DB) error {
// No rollback needed
Expand Down
24 changes: 12 additions & 12 deletions providers/aws/ec2/elastic_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ElasticIps(ctx context.Context, client ProviderClient) ([]Resource, error)
stsClient := sts.NewFromConfig(*client.AWSClient)
stsOutput, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return resources, err
return resources, err
}

accountId := stsOutput.Account
Expand All @@ -45,9 +45,9 @@ func ElasticIps(ctx context.Context, client ProviderClient) ([]Resource, error)
Value: *tag.Value,
})
}

cost := 0.0

resourceConfig, err := configClient.BatchGetResourceConfig(ctx, &configservice.BatchGetResourceConfigInput{
ResourceKeys: []types.ResourceKey{
{
Expand Down Expand Up @@ -86,7 +86,6 @@ func ElasticIps(ctx context.Context, client ProviderClient) ([]Resource, error)

}
}


resourceArn := fmt.Sprintf("arn:aws:ec2:%s:%s:elastic-ip/%s", client.AWSClient.Region, *accountId, *elasticIps.AllocationId)
relations := getEIPRelations(&elasticIps, fmt.Sprintf("arn:aws:ec2:%s:%s", client.AWSClient.Region, *accountId))
Expand All @@ -101,33 +100,34 @@ func ElasticIps(ctx context.Context, client ProviderClient) ([]Resource, error)
FetchedAt: time.Now(),
Tags: tags,
Link: fmt.Sprintf("https:/%s.console.aws.amazon.com/ec2/home?region=%s#ElasticIpDetails:AllocationId=%s", client.AWSClient.Region, client.AWSClient.Region, *elasticIps.AllocationId),
Relations: relations,
Relations: relations,
})
}

log.WithFields(log.Fields{
"provider": "AWS",
"account": client.Name,
"region": client.AWSClient.Region,
"service": "Elastic IP",
"resources": len(resources),
}).Info("Fetched resources")
}).Info("Fetched resources")

// nolint:staticcheck // SA4004 ignore this
return resources, nil
}
}


func getEIPRelations(ip *etype.Address, resourceArn string) (rel []models.Link) {

if ip.InstanceId != nil {
id := fmt.Sprintf("%s:instance/%s", resourceArn, *ip.InstanceId)
rel = append(rel, models.Link{
ResourceID: id,
Type: "EC2",
Relation: "USES",
Type: "EC2",
Relation: "USES",
})
}
return
return
}

func hoursSince(t time.Time) float64 {
Expand Down
2 changes: 1 addition & 1 deletion providers/aws/elasticache/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Clusters(ctx context.Context, client ProviderClient) ([]Resource, error) {
MaxResults: aws.Int32(1),
})
if err != nil {
log.Warnf("Couldn't fetch pricing information for %s", cluster)
log.Warnf("Couldn't fetch pricing information for %s", *cluster.ARN)
}

hourlyCost := 0.0
Expand Down
4 changes: 2 additions & 2 deletions providers/gcp/compute/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package compute
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/tailwarden/komiser/providers"
Expand All @@ -13,7 +13,7 @@ import (
func TestInstances(t *testing.T) {
t.Skip("Only for local development because it is using a Google Cloud connection")
// Replace the empty string with a SA or credentials file location
data, err := ioutil.ReadFile("")
data, err := os.ReadFile("")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion providers/gcp/kms/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"time"

kms "cloud.google.com/go/kms/apiv1"
kmspb "cloud.google.com/go/kms/apiv1/kmspb"
"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)

func Keys(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
Expand Down
4 changes: 3 additions & 1 deletion utils/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func SetupSchema(db *bun.DB, c *models.Config, accounts []models.Account) error
func doMigrations(db *bun.DB, ctx context.Context) error {
migrator := migrate.NewMigrator(db, migrations.Migrations)

migrator.Init(ctx)
if err := migrator.Init(ctx); err != nil {
return err
}

group, err := migrator.Migrate(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils/gcpcomputepricing/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func getDiskMonthly(p *Pricing, opts Opts, tg typeDiskGetter) (uint64, error) {
capacityPricePerRegion = region.Prices[0].Nanos
}
} else {
return 0, errors.New(fmt.Sprintf("capacity price not found for %q region", opts.Region))
return 0, fmt.Errorf("capacity price not found for %q region", opts.Region)
}

var sum uint64 = 0
Expand Down
44 changes: 11 additions & 33 deletions utils/gcpcomputepricing/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ func typeGetterE2(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.Commitmente2CPU3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.Commitmente2RAM3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -161,9 +159,7 @@ func typeGetterC3(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.C3.Commitmentc3CPU3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.C3.Commitmentc3RAM3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -185,9 +181,7 @@ func typeGetterN2(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.Commitmentn2CPU3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.Commitmentn2RAM3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -209,9 +203,7 @@ func typeGetterN2D(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.Commitmentn2Dcpu3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.Commitmentn2Dram3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -227,9 +219,7 @@ func typeGetterT2A(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsPreemptible.CoresPerCore.T2A.Vmimagepreemptiblet2Astandardcore
memory = p.Gcp.Compute.GCE.VmsPreemptible.MemoryPerGb.T2A.Vmimagepreemptiblet2Astandardram
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -251,9 +241,7 @@ func typeGetterT2D(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.Commitmentt2Dcpu3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.Commitmentt2Dram3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -269,9 +257,7 @@ func typeGetterN1(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsPreemptible.CoresPerCore.Vmimagepreemptiblen1Standardcore
memory = p.Gcp.Compute.GCE.VmsPreemptible.MemoryPerGb.Vmimagepreemptiblen1Standardram
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -293,9 +279,7 @@ func typeGetterC2D(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.Commitmentc2Dcpu3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.Commitmentc2Dram3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -317,9 +301,7 @@ func typeGetterM3(p *Pricing, opts Opts) (Subtype, Subtype, error) {
core = p.Gcp.Compute.GCE.VmsCommit3Year.CoresPerCore.M3.Commitmentm3CPU3Yv1
memory = p.Gcp.Compute.GCE.VmsCommit3Year.MemoryPerGb.M3.Commitmentm3RAM3Yv1
default:
return Subtype{}, Subtype{}, errors.New(
fmt.Sprintf("commitment %q not supported", opts.Commitment),
)
return Subtype{}, Subtype{}, fmt.Errorf("commitment %q not supported", opts.Commitment)
}
return core, memory, nil
}
Expand All @@ -336,9 +318,7 @@ func getHourly(p *Pricing, opts Opts, tg typeMachineGetter) (uint64, error) {
corePricePerRegion = region.Prices[0].Nanos
}
} else {
return 0, errors.New(
fmt.Sprintf("core price not found for %q region", opts.Region),
)
return 0, fmt.Errorf("core price not found for %q region", opts.Region)
}

var memoryPricePerRegion uint64 = 0
Expand All @@ -347,9 +327,7 @@ func getHourly(p *Pricing, opts Opts, tg typeMachineGetter) (uint64, error) {
memoryPricePerRegion = region.Prices[0].Nanos
}
} else {
return 0, errors.New(
fmt.Sprintf("memory not found for %q region", opts.Region),
)
return 0, fmt.Errorf("memory not found for %q region", opts.Region)
}

var sum uint64 = 0
Expand Down
Loading

0 comments on commit b23d397

Please sign in to comment.