From ec41b03f0af2e07cd484377f49f861471bcac332 Mon Sep 17 00:00:00 2001 From: shubhindia Date: Mon, 9 Oct 2023 14:27:59 +0530 Subject: [PATCH 1/5] add linter Signed-off-by: shubhindia --- .github/workflows/lint.yml | 19 ++++++++ go.mod | 2 +- handlers/feedback_handler.go | 2 +- ...413100000_set_default_tags_in_resources.go | 2 +- ..._create_custom_webhook_fields_in_alerts.go | 6 +-- .../20230619100000_add_new_relation_field.go | 4 +- .../20231014100000_add_new_status_field.go | 2 +- providers/aws/ec2/elastic_ips.go | 24 +++++----- providers/aws/elasticache/clusters.go | 2 +- providers/gcp/kms/keys.go | 2 +- utils/database.go | 4 +- utils/gcpcomputepricing/disk.go | 2 +- utils/gcpcomputepricing/machine.go | 44 +++++-------------- utils/gcpcomputepricing/snapshot.go | 9 +--- 14 files changed, 59 insertions(+), 65 deletions(-) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 000000000..e9e75a346 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,19 @@ +name: Lint +on: + push: + +jobs: + lint: + name: 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 5m + skip-cache: true \ No newline at end of file diff --git a/go.mod b/go.mod index 1275883b9..30fa0426d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/handlers/feedback_handler.go b/handlers/feedback_handler.go index 190de3c14..12ddd1f6a 100644 --- a/handlers/feedback_handler.go +++ b/handlers/feedback_handler.go @@ -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 { diff --git a/migrations/20230413100000_set_default_tags_in_resources.go b/migrations/20230413100000_set_default_tags_in_resources.go index be74b782b..0946ec806 100644 --- a/migrations/20230413100000_set_default_tags_in_resources.go +++ b/migrations/20230413100000_set_default_tags_in_resources.go @@ -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'"). diff --git a/migrations/20230602100000_create_custom_webhook_fields_in_alerts.go b/migrations/20230602100000_create_custom_webhook_fields_in_alerts.go index 63d39624e..f86798ab1 100644 --- a/migrations/20230602100000_create_custom_webhook_fields_in_alerts.go +++ b/migrations/20230602100000_create_custom_webhook_fields_in_alerts.go @@ -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; `) diff --git a/migrations/20230619100000_add_new_relation_field.go b/migrations/20230619100000_add_new_relation_field.go index 4b1e60688..ad7f9f4d7 100644 --- a/migrations/20230619100000_add_new_relation_field.go +++ b/migrations/20230619100000_add_new_relation_field.go @@ -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 { diff --git a/migrations/20231014100000_add_new_status_field.go b/migrations/20231014100000_add_new_status_field.go index 087b4c638..fd0ee46f8 100644 --- a/migrations/20231014100000_add_new_status_field.go +++ b/migrations/20231014100000_add_new_status_field.go @@ -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 diff --git a/providers/aws/ec2/elastic_ips.go b/providers/aws/ec2/elastic_ips.go index 5149596b8..afe353115 100644 --- a/providers/aws/ec2/elastic_ips.go +++ b/providers/aws/ec2/elastic_ips.go @@ -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 @@ -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{ { @@ -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)) @@ -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 { diff --git a/providers/aws/elasticache/clusters.go b/providers/aws/elasticache/clusters.go index 02d947661..de2be2361 100644 --- a/providers/aws/elasticache/clusters.go +++ b/providers/aws/elasticache/clusters.go @@ -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 diff --git a/providers/gcp/kms/keys.go b/providers/gcp/kms/keys.go index 662e2bfa2..4cd4d5276 100644 --- a/providers/gcp/kms/keys.go +++ b/providers/gcp/kms/keys.go @@ -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) { diff --git a/utils/database.go b/utils/database.go index e5324e8c8..74239fc6c 100644 --- a/utils/database.go +++ b/utils/database.go @@ -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 { diff --git a/utils/gcpcomputepricing/disk.go b/utils/gcpcomputepricing/disk.go index 589f6a403..9480bff03 100644 --- a/utils/gcpcomputepricing/disk.go +++ b/utils/gcpcomputepricing/disk.go @@ -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 diff --git a/utils/gcpcomputepricing/machine.go b/utils/gcpcomputepricing/machine.go index c6edd2461..e5d8b3936 100644 --- a/utils/gcpcomputepricing/machine.go +++ b/utils/gcpcomputepricing/machine.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 @@ -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 diff --git a/utils/gcpcomputepricing/snapshot.go b/utils/gcpcomputepricing/snapshot.go index 7fe5a8897..c7cfb3b2f 100644 --- a/utils/gcpcomputepricing/snapshot.go +++ b/utils/gcpcomputepricing/snapshot.go @@ -2,7 +2,6 @@ package gcpcomputepricing import ( "context" - "errors" "fmt" "time" @@ -47,9 +46,8 @@ func CalculateSnapshotCost(ctx context.Context, client providers.ProviderClient, } func typeSnapshotGetter(p *Pricing, opts Opts) (Subtype, error) { - var capacity Subtype // TODO: switch by snapshot type - capacity = p.Gcp.Compute.PersistentDisk.Snapshots.Storageregionalstandardsnapshotearlydeletion + capacity := p.Gcp.Compute.PersistentDisk.Snapshots.Storageregionalstandardsnapshotearlydeletion return capacity, nil } @@ -66,10 +64,7 @@ func getSnapshotMonthly(p *Pricing, opts Opts, tg func(*Pricing, Opts) (Subtype, 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) } normalizedSize := uint64(storageBytes) / 1024 / 1024 / 1024 From 0f23943d861d88c817b93a2fe8aa0a22d0a96a4c Mon Sep 17 00:00:00 2001 From: shubhindia Date: Mon, 9 Oct 2023 14:34:50 +0530 Subject: [PATCH 2/5] remove lint timeout Signed-off-by: shubhindia --- .github/workflows/lint.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e9e75a346..99169ab24 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,7 +13,4 @@ jobs: with: go-version-file: go.mod - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - args: --timeout 5m - skip-cache: true \ No newline at end of file + uses: golangci/golangci-lint-action@v3 \ No newline at end of file From 0aa93973eecdf18abc05db5f9723073d7e7c882a Mon Sep 17 00:00:00 2001 From: shubhindia Date: Mon, 9 Oct 2023 14:41:16 +0530 Subject: [PATCH 3/5] 30m timeout for lint Signed-off-by: shubhindia --- .github/workflows/lint.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 99169ab24..4579db448 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,4 +13,6 @@ jobs: with: go-version-file: go.mod - name: golangci-lint - uses: golangci/golangci-lint-action@v3 \ No newline at end of file + uses: golangci/golangci-lint-action@v3 + with: + args: --timeout=30m \ No newline at end of file From 5db0254d13168298dfe545da83eb0494b60206b0 Mon Sep 17 00:00:00 2001 From: Shubham Gopale Date: Mon, 9 Oct 2023 10:09:07 +0000 Subject: [PATCH 4/5] Update .github/workflows/lint.yml Co-authored-by: Azanul Haque <42029519+Azanul@users.noreply.github.com> --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4579db448..e17db886f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -4,7 +4,7 @@ on: jobs: lint: - name: Lint + name: golangci-lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 From 8d754f75f1d8768ea3862cf45dfb1a78cf3d074a Mon Sep 17 00:00:00 2001 From: shubhindia Date: Mon, 9 Oct 2023 15:42:06 +0530 Subject: [PATCH 5/5] add golangci-lint version and reduce the timeout to 10m Signed-off-by: shubhindia --- .github/workflows/lint.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e17db886f..dffddfc31 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -15,4 +15,5 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - args: --timeout=30m \ No newline at end of file + args: --timeout=10m + version: v1.54.2 \ No newline at end of file