Skip to content

Commit

Permalink
ci(nats): fix entrypoint (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenn authored Jul 25, 2024
1 parent beb5758 commit 553134b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 101 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include tools/make/help.mk

.PHONY: all
all: check-generation lint test ## Run all the checks locally
.PHONY: check
check: check-generation lint test ## Run all the checks locally

.PHONY: check-generation
check-generation: ## Check files are generated locally
Expand Down
4 changes: 2 additions & 2 deletions build/ci/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ include ../../tools/make/help.mk

SRC_DIR := ../..

.PHONY: all
all: ## Run all the checks before commit on Dagger
.PHONY: check
check: ## Run all the checks before commit on Dagger
@dagger call check --src-dir $(SRC_DIR)

.PHONY: examples
Expand Down
111 changes: 52 additions & 59 deletions build/ci/dagger/brokers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ func bindBrokers(brokers map[string]*dagger.Service) func(r *dagger.Container) *
}
}

// brokers returns a map of containers for each broker as service.
func brokers() map[string]*dagger.Service {
// brokerServices returns a map of containers for each broker as service.
func brokerServices() map[string]*dagger.Service {
brokers := make(map[string]*dagger.Service)

// Kafka
brokers["kafka"] = brokerKafka()
brokers["kafka-tls"] = brokerKafkaSecure()
brokers["kafka-tls-basic-auth"] = brokerKafkaSecureBasicAuth()
brokers["kafka"] = brokerKafka().AsService()
brokers["kafka-tls"] = brokerKafkaSecure().AsService()
brokers["kafka-tls-basic-auth"] = brokerKafkaSecureBasicAuth().AsService()

// NATS
brokers["nats"] = brokerNATS()
brokers["nats-tls"] = brokerNATSSecure()
brokers["nats-tls-basic-auth"] = brokerNATSSecureBasicAuth()
brokers["nats"] = brokerNATS().AsService()
brokers["nats-tls"] = brokerNATSSecure().AsService()
brokers["nats-tls-basic-auth"] = brokerNATSSecureBasicAuth().AsService()

// NATS Jetstream
brokers["nats-jetstream"] = brokerNATSJetstream()
brokers["nats-jetstream-tls"] = brokerNATSJetstreamSecure()
brokers["nats-jetstream-tls-basic-auth"] = brokerNATSJetstreamSecureBasicAuth()
brokers["nats-jetstream"] = brokerNATSJetstream().AsService()
brokers["nats-jetstream-tls"] = brokerNATSJetstreamSecure().AsService()
brokers["nats-jetstream-tls-basic-auth"] = brokerNATSJetstreamSecureBasicAuth().AsService()

return brokers
}

// brokerKafka returns a service for the Kafka broker.
func brokerKafka() *dagger.Service {
// brokerKafka returns a container for the Kafka broker.
func brokerKafka() *dagger.Container {
return dag.Container().
// Set container image
From(kafkaImage).
Expand All @@ -67,14 +67,11 @@ func brokerKafka() *dagger.Service {

// Add exposed ports
WithExposedPort(9092).
WithExposedPort(9093).

// Return container as a service
AsService()
WithExposedPort(9093)
}

// brokerKafkaSecure returns a service for the Kafka broker secured with TLS.
func brokerKafkaSecure() *dagger.Service {
// brokerKafkaSecure returns a container for the Kafka broker secured with TLS.
func brokerKafkaSecure() *dagger.Container {
key, cert, cacert, err := testutil.GenerateSelfSignedCertificateWithCA("kafka-tls")
if err != nil {
panic(fmt.Errorf("failed to generate self signed certificate: %w", err))
Expand Down Expand Up @@ -110,14 +107,11 @@ func brokerKafkaSecure() *dagger.Service {
WithExposedPort(9093).

// Add server cert and key directory
WithDirectory("/bitnami/kafka/config/certs/", tlsDir).

// Return container as a service
AsService()
WithDirectory("/bitnami/kafka/config/certs/", tlsDir)
}

// brokerKafkaSecureBasicAuth returns a service for the Kafka broker secured with TLS and basic auth.
func brokerKafkaSecureBasicAuth() *dagger.Service {
// brokerKafkaSecureBasicAuth returns a container for the Kafka broker secured with TLS and basic auth.
func brokerKafkaSecureBasicAuth() *dagger.Container {
key, cert, cacert, err := testutil.GenerateSelfSignedCertificateWithCA("kafka-tls-basic-auth")
if err != nil {
panic(fmt.Errorf("failed to generate self signed certificate: %w", err))
Expand Down Expand Up @@ -160,25 +154,20 @@ func brokerKafkaSecureBasicAuth() *dagger.Service {
WithExposedPort(9093).

// Add server cert and key directory
WithDirectory("/bitnami/kafka/config/certs/", tlsDir).

// Return container as a service
AsService()
WithDirectory("/bitnami/kafka/config/certs/", tlsDir)
}

// brokerNATS returns a service for the NATS broker.
func brokerNATS() *dagger.Service {
// brokerNATS returns a container for the NATS broker.
func brokerNATS() *dagger.Container {
return dag.Container().
// Add base image
From(natsImage).
// Add exposed ports
WithExposedPort(4222).
// Return container as a service
AsService()
WithExposedPort(4222)
}

// brokerNATSSecure returns a service for the NATS broker secured with TLS.
func brokerNATSSecure() *dagger.Service {
// brokerNATSSecure returns a container for the NATS broker secured with TLS.
func brokerNATSSecure() *dagger.Container {
key, cert, err := testutil.GenerateSelfSignedCertificate("nats-tls")
if err != nil {
panic(fmt.Errorf("failed to generate self signed certificate: %w", err))
Expand All @@ -193,14 +182,13 @@ func brokerNATSSecure() *dagger.Service {
// Add server cert and key directory
WithDirectory("./tls", tlsDir).
// Start NATS with tls
WithExec([]string{"--tls", "--tlscert=/tls/server-cert.pem", "--tlskey=/tls/server-key.pem"}).
// Return container as a service
AsService()
WithoutEntrypoint().
WithExec([]string{"nats-server", "--tls", "--tlscert=/tls/server-cert.pem", "--tlskey=/tls/server-key.pem"})
}

// brokerNATSSecureBasicAuth returns a service for the NATS broker secured with TLS
// brokerNATSSecureBasicAuth returns a container for the NATS broker secured with TLS
// and basic auth user: user password: password.
func brokerNATSSecureBasicAuth() *dagger.Service {
func brokerNATSSecureBasicAuth() *dagger.Container {
key, cert, err := testutil.GenerateSelfSignedCertificate("nats-tls-basic-auth")
if err != nil {
panic(fmt.Errorf("failed to generate self signed certificate: %w", err))
Expand All @@ -215,31 +203,30 @@ func brokerNATSSecureBasicAuth() *dagger.Service {
// Add server cert and key directory
WithDirectory("./tls", tlsDir).
// Start NATS with tls and credentials
WithoutEntrypoint().
WithExec([]string{
"nats-server",
"--tls",
"--tlscert=/tls/server-cert.pem",
"--tlskey=/tls/server-key.pem",
"--user", "user",
"--pass", "password"}).
// Return container as a service
AsService()
"--pass", "password"})
}

// brokerNATSJetstream returns a service for the NATS broker.
func brokerNATSJetstream() *dagger.Service {
// brokerNATSJetstream returns a container for the NATS broker.
func brokerNATSJetstream() *dagger.Container {
return dag.Container().
// Add base image
From(natsImage).
// Add exposed ports
WithExposedPort(4222).
// Add command
WithExec([]string{"-js"}).
// Return container as a service
AsService()
WithoutEntrypoint().
WithExec([]string{"nats-server", "-js"})
}

// brokerNATSJetstreamSecure returns a service for the NATS broker secured with TLS.
func brokerNATSJetstreamSecure() *dagger.Service {
// brokerNATSJetstreamSecure returns a container for the NATS broker secured with TLS.
func brokerNATSJetstreamSecure() *dagger.Container {
key, cert, err := testutil.GenerateSelfSignedCertificate("nats-jetstream-tls-basic-auth")
if err != nil {
panic(fmt.Errorf("failed to generate self signed certificate: %w", err))
Expand All @@ -254,14 +241,13 @@ func brokerNATSJetstreamSecure() *dagger.Service {
// Add server cert and key directory
WithDirectory("./tls", tlsDir).
// Start NATS jetstream with tls
WithExec([]string{"-js", "--tls", "--tlscert=/tls/server-cert.pem", "--tlskey=/tls/server-key.pem"}).
// Return container as a service
AsService()
WithoutEntrypoint().
WithExec([]string{"nats-server", "-js", "--tls", "--tlscert=/tls/server-cert.pem", "--tlskey=/tls/server-key.pem"})
}

// brokerNATSJetstreamSecureBasicAuth returns a service for the NATS broker secured with TLS
// brokerNATSJetstreamSecureBasicAuth returns a container for the NATS broker secured with TLS
// and basic auth user: user password: password.
func brokerNATSJetstreamSecureBasicAuth() *dagger.Service {
func brokerNATSJetstreamSecureBasicAuth() *dagger.Container {
key, cert, err := testutil.GenerateSelfSignedCertificate("nats-jetstream-tls")
if err != nil {
panic(fmt.Errorf("failed to generate self signed certificate: %w", err))
Expand All @@ -276,7 +262,14 @@ func brokerNATSJetstreamSecureBasicAuth() *dagger.Service {
// Add server cert and key directory
WithDirectory("./tls", tlsDir).
// Start NATS jetstream with tls and credentials
WithExec([]string{"-js", "--tls", "--tlscert=/tls/server-cert.pem", "--tlskey=/tls/server-key.pem", "--user", "user", "--pass", "password"}). //nolint:lll
// Return container as a service
AsService()
WithoutEntrypoint().
WithExec([]string{
"nats-server",
"-js",
"--tls",
"--tlscert=/tls/server-cert.pem",
"--tlskey=/tls/server-key.pem",
"--user", "user",
"--pass", "password",
})
}
13 changes: 6 additions & 7 deletions build/ci/dagger/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package main
import (
"asyncapi-codegen/ci/dagger/internal/dagger"
"context"

"github.com/lerenn/asyncapi-codegen/pkg/utils"
)

const (
Expand All @@ -35,7 +33,7 @@ type AsyncapiCodegenCi struct {

func (ci *AsyncapiCodegenCi) cachedBrokers() map[string]*dagger.Service {
if ci.brokers == nil {
ci.brokers = brokers()
ci.brokers = brokerServices()
}
return ci.brokers
}
Expand Down Expand Up @@ -104,7 +102,6 @@ func (ci *AsyncapiCodegenCi) Examples(
}

// Get examples containers
containers := make(map[string]*dagger.Container, 0)
for _, p := range subdirs {
// Set app container
app := dag.Container().
Expand Down Expand Up @@ -133,11 +130,13 @@ func (ci *AsyncapiCodegenCi) Examples(
// Execute command
WithExec([]string{"go", "run", p + "/user"})

// Add user containers to containers
containers[p] = user
// Execute user container
stderr, err := user.Stderr(ctx)
if err != nil {
return stderr, err
}
}

executeContainers(ctx, utils.MapToList(containers)...)
return "", nil
}

Expand Down
31 changes: 0 additions & 31 deletions build/ci/dagger/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"asyncapi-codegen/ci/dagger/internal/dagger"
"context"
"sync"
)

func sourceCodeAndGoCache(dir *dagger.Directory) func(r *dagger.Container) *dagger.Container {
Expand Down Expand Up @@ -73,33 +72,3 @@ func isDir(ctx context.Context, parentDir *dagger.Directory, path string) (bool,
// We also assume that isNotDirErr and isNotFileErr are the same error
return false, isNotFileErr
}

func executeContainers(ctx context.Context, containers ...*dagger.Container) {
funcs := make([]func(context.Context) error, 0)
for _, c := range containers {
local := c
fn := func(ctx context.Context) error {
_, err := local.Stderr(ctx)
return err
}
funcs = append(funcs, fn)
}

execute(ctx, funcs...)
}

func execute(ctx context.Context, funcs ...func(context.Context) error) {
var wg sync.WaitGroup
for _, fn := range funcs {
go func(callback func(context.Context) error) {
if err := callback(ctx); err != nil {
panic(err)
}
wg.Done()
}(fn)

wg.Add(1)
}

wg.Wait()
}

0 comments on commit 553134b

Please sign in to comment.