Skip to content

chore: remove some infrastructure tests (moved to dedicated repository) #596

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

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pre-commit:

BUILD ./test/*+pre-commit
BUILD ./tools/*+pre-commit
BUILD ./deployments/pulumi/*+pre-commit

openapi:
FROM node:20-alpine
Expand Down
11 changes: 0 additions & 11 deletions deployments/pulumi/Earthfile

This file was deleted.

41 changes: 41 additions & 0 deletions deployments/pulumi/ledger/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
VERSION 0.8
PROJECT FormanceHQ/ledger

IMPORT github.com/formancehq/earthly:tags/v0.19.0 AS core

FROM core+base-image

CACHE --sharing=shared --id go-mod-cache /go/pkg/mod
CACHE --sharing=shared --id go-cache /root/.cache/go-build
CACHE --sharing=shared --id golangci-cache /root/.cache/golangci-lint

sources:
FROM core+builder-image
WORKDIR /src
COPY *.go go.* Pulumi.yaml .
COPY --dir pkg .
SAVE ARTIFACT /src

tidy:
FROM +sources
CACHE --id go-mod-cache /go/pkg/mod
CACHE --id go-cache /root/.cache/go-build
RUN go mod tidy

SAVE ARTIFACT go.mod AS LOCAL go.mod
SAVE ARTIFACT go.sum AS LOCAL go.sum

lint:
FROM +tidy
CACHE --id go-mod-cache /go/pkg/mod
CACHE --id go-cache /root/.cache/go-build
CACHE --id golangci-cache /root/.cache/golangci-lint

RUN golangci-lint run --fix --build-tags it --timeout 5m

SAVE ARTIFACT main.go AS LOCAL main.go
SAVE ARTIFACT pkg AS LOCAL pkg

pre-commit:
BUILD +tidy
BUILD +lint
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider enhancing pre-commit checks

The pre-commit target could benefit from additional validation steps:

  1. Running tests (if applicable)
  2. Validating Pulumi configuration
  3. Security scanning for infrastructure code
 pre-commit:
     BUILD +tidy
     BUILD +lint
+    # Add test target if applicable
+    BUILD +test
+    # Validate Pulumi configuration
+    RUN pulumi preview --stack dev --non-interactive

Committable suggestion skipped: line range outside the PR's diff.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/formancehq/ledger/deployments/pulumi
module github.com/formancehq/ledger/deployments/pulumi/ledger

go 1.22

Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions deployments/pulumi/ledger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"errors"
"fmt"
"github.com/formancehq/ledger/deployments/pulumi/ledger/pkg"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
conf := config.New(ctx, "")
postgresURI := conf.Require("postgres.uri")

namespace, err := conf.Try("namespace")
if err != nil {
namespace = "default"
}

version, err := conf.Try("version")
if err != nil {
version = "latest"
}

timeout, err := conf.TryInt("timeout")
if err != nil {
if errors.Is(err, config.ErrMissingVar) {
timeout = 60
} else {
return fmt.Errorf("error reading timeout: %w", err)
}
}

debug, _ := conf.TryBool("debug")
imagePullPolicy, _ := conf.Try("image.pullPolicy")

replicaCount, _ := conf.TryInt("replicaCount")
experimentalFeatures, _ := conf.TryBool("experimentalFeatures")
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling consistency for optional configurations.

The error handling is inconsistent across optional configurations. Some errors are silently ignored which could lead to unexpected behavior.

Consider validating and logging when using default values:

-		debug, _ := conf.TryBool("debug")
-		imagePullPolicy, _ := conf.Try("image.pullPolicy")
-		replicaCount, _ := conf.TryInt("replicaCount")
-		experimentalFeatures, _ := conf.TryBool("experimentalFeatures")
+		debug, err := conf.TryBool("debug")
+		if err != nil && !errors.Is(err, config.ErrMissingVar) {
+			return fmt.Errorf("invalid debug configuration: %w", err)
+		}
+
+		imagePullPolicy, err := conf.Try("image.pullPolicy")
+		if err != nil && !errors.Is(err, config.ErrMissingVar) {
+			return fmt.Errorf("invalid imagePullPolicy configuration: %w", err)
+		}
+
+		replicaCount, err := conf.TryInt("replicaCount")
+		if err != nil && !errors.Is(err, config.ErrMissingVar) {
+			return fmt.Errorf("invalid replicaCount configuration: %w", err)
+		}
+		if replicaCount < 0 {
+			return fmt.Errorf("replicaCount must be non-negative")
+		}
+
+		experimentalFeatures, err := conf.TryBool("experimentalFeatures")
+		if err != nil && !errors.Is(err, config.ErrMissingVar) {
+			return fmt.Errorf("invalid experimentalFeatures configuration: %w", err)
+		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
debug, _ := conf.TryBool("debug")
imagePullPolicy, _ := conf.Try("image.pullPolicy")
replicaCount, _ := conf.TryInt("replicaCount")
experimentalFeatures, _ := conf.TryBool("experimentalFeatures")
debug, err := conf.TryBool("debug")
if err != nil && !errors.Is(err, config.ErrMissingVar) {
return fmt.Errorf("invalid debug configuration: %w", err)
}
imagePullPolicy, err := conf.Try("image.pullPolicy")
if err != nil && !errors.Is(err, config.ErrMissingVar) {
return fmt.Errorf("invalid imagePullPolicy configuration: %w", err)
}
replicaCount, err := conf.TryInt("replicaCount")
if err != nil && !errors.Is(err, config.ErrMissingVar) {
return fmt.Errorf("invalid replicaCount configuration: %w", err)
}
if replicaCount < 0 {
return fmt.Errorf("replicaCount must be non-negative")
}
experimentalFeatures, err := conf.TryBool("experimentalFeatures")
if err != nil && !errors.Is(err, config.ErrMissingVar) {
return fmt.Errorf("invalid experimentalFeatures configuration: %w", err)
}


_, err = pulumi_ledger.NewLedgerComponent(ctx, "ledger", &pulumi_ledger.LedgerComponentArgs{
Namespace: pulumi.String(namespace),
Timeout: pulumi.Int(timeout),
Tag: pulumi.String(version),
ImagePullPolicy: pulumi.String(imagePullPolicy),
PostgresURI: pulumi.String(postgresURI),
Debug: pulumi.Bool(debug),
ReplicaCount: pulumi.Int(replicaCount),
ExperimentalFeatures: pulumi.Bool(experimentalFeatures),
})

return err
})
}
79 changes: 79 additions & 0 deletions deployments/pulumi/ledger/pkg/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package pulumi_ledger

import (
"fmt"
"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

type LedgerComponent struct {
pulumi.ResourceState

ServiceName pulumi.StringPtrOutput
ServiceNamespace pulumi.StringPtrOutput
ServicePort pulumi.IntPtrOutput
ServiceInternalURL pulumi.StringOutput
}

type LedgerComponentArgs struct {
Namespace pulumi.StringInput
Timeout pulumi.IntInput
Tag pulumi.StringInput
ImagePullPolicy pulumi.StringInput
PostgresURI pulumi.StringInput
Debug pulumi.BoolInput
ReplicaCount pulumi.IntInput
ExperimentalFeatures pulumi.BoolInput
}

func NewLedgerComponent(ctx *pulumi.Context, name string, args *LedgerComponentArgs, opts ...pulumi.ResourceOption) (*LedgerComponent, error) {
cmp := &LedgerComponent{}
err := ctx.RegisterComponentResource("Formance:Ledger:Deployment", name, cmp, opts...)
if err != nil {
return nil, err
}

rel, err := helm.NewRelease(ctx, "ledger", &helm.ReleaseArgs{
Chart: pulumi.String("../../../helm"),
Namespace: args.Namespace,
CreateNamespace: pulumi.BoolPtr(true),
Timeout: args.Timeout,
Values: pulumi.Map(map[string]pulumi.Input{
"image": pulumi.Map{
"repository": pulumi.String("ghcr.io/formancehq/ledger"),
"tag": args.Tag,
"pullPolicy": args.ImagePullPolicy,
},
"postgres": pulumi.Map{
"uri": args.PostgresURI,
},
"debug": args.Debug,
"replicaCount": args.ReplicaCount,
"experimentalFeatures": args.ExperimentalFeatures,
}),
}, pulumi.Parent(cmp))
if err != nil {
return nil, fmt.Errorf("installing release: %w", err)
}

cmp.ServiceName = rel.Status.Name()
cmp.ServiceNamespace = rel.Status.Namespace()
cmp.ServicePort = pulumi.IntPtr(8080).ToIntPtrOutput()
cmp.ServiceInternalURL = pulumi.Sprintf(
"http://%s.%s.svc.cluster.local:%d",
cmp.ServiceName.Elem(),
cmp.ServiceNamespace.Elem(),
cmp.ServicePort.Elem(),
)

if err := ctx.RegisterResourceOutputs(cmp, pulumi.Map{
"service-name": cmp.ServiceName,
"service-namespace": cmp.ServiceNamespace,
"service-port": cmp.ServicePort,
"service-internal-url": cmp.ServiceInternalURL,
}); err != nil {
return nil, fmt.Errorf("registering resource outputs: %w", err)
}

return cmp, nil
}
73 changes: 0 additions & 73 deletions deployments/pulumi/main.go

This file was deleted.

41 changes: 41 additions & 0 deletions deployments/pulumi/postgres/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
VERSION 0.8
PROJECT FormanceHQ/ledger

IMPORT github.com/formancehq/earthly:tags/v0.19.0 AS core

FROM core+base-image

CACHE --sharing=shared --id go-mod-cache /go/pkg/mod
CACHE --sharing=shared --id go-cache /root/.cache/go-build
CACHE --sharing=shared --id golangci-cache /root/.cache/golangci-lint

sources:
FROM core+builder-image
WORKDIR /src
COPY *.go go.* Pulumi.yaml .
COPY --dir pkg .
SAVE ARTIFACT /src

tidy:
FROM +sources
CACHE --id go-mod-cache /go/pkg/mod
CACHE --id go-cache /root/.cache/go-build
RUN go mod tidy

SAVE ARTIFACT go.mod AS LOCAL go.mod
SAVE ARTIFACT go.sum AS LOCAL go.sum

lint:
FROM +tidy
CACHE --id go-mod-cache /go/pkg/mod
CACHE --id go-cache /root/.cache/go-build
CACHE --id golangci-cache /root/.cache/golangci-lint

RUN golangci-lint run --fix --build-tags it --timeout 5m

SAVE ARTIFACT main.go AS LOCAL main.go
SAVE ARTIFACT pkg AS LOCAL pkg

pre-commit:
BUILD +tidy
BUILD +lint
7 changes: 7 additions & 0 deletions deployments/pulumi/postgres/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: postgres
description: A minimal Kubernetes Go Pulumi program
runtime: go
config:
pulumi:tags:
value:
pulumi:template: kubernetes-go
Loading
Loading