From af51f074df8d8c7f1662e82a3c8a4db1daeb22cf Mon Sep 17 00:00:00 2001 From: Dale Peakall Date: Thu, 21 Mar 2024 17:56:57 +0000 Subject: [PATCH] feat: remove envoy from default docker compose --- README.md | 6 ++ config/manager/config.toml | 2 +- docker-compose-with-lb.yml | 149 ++++++++++++++++++++++++++++ docker-compose.yml | 52 ++-------- manager/adminui/server.go | 16 ++- manager/adminui/server_test.go | 10 +- manager/config/base_config.go | 8 +- manager/config/base_config_test.go | 8 +- manager/config/config.go | 16 +-- manager/config/config_test.go | 8 +- manager/config/settings_config.go | 8 +- manager/config/testdata/config.toml | 2 +- manager/server/api.go | 2 +- 13 files changed, 212 insertions(+), 75 deletions(-) create mode 100644 docker-compose-with-lb.yml diff --git a/README.md b/README.md index 6b6df59..1c3bb6f 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,12 @@ Charge stations can connect to the CSMS using: * `ws://localhost/ws/` * `wss://localhost/ws/` +If the charge station is also running in a Docker container then the charge +station docker container can connect to the `maeve-csms` network and the +charge station can connect to the CSMS using: +* `ws://gateway:9310/ws/` +* `wss://gateway:9311/ws/` + Charge stations can use either OCPP 1.6j or OCPP 2.0.1. For TLS, the charge station should use a certificate provisioned using the diff --git a/config/manager/config.toml b/config/manager/config.toml index 3fa49ec..6925bf4 100644 --- a/config/manager/config.toml +++ b/config/manager/config.toml @@ -7,7 +7,7 @@ mqtt.urls = ["mqtt://mqtt:1883"] [ocpi] addr = ":9411" -external_url = "http://lb:9411" +external_url = "http://manager:9411" country_code = "GB" party_id = "TWK" diff --git a/docker-compose-with-lb.yml b/docker-compose-with-lb.yml new file mode 100644 index 0000000..a2d88cd --- /dev/null +++ b/docker-compose-with-lb.yml @@ -0,0 +1,149 @@ +networks: + default: + name: maeve-csms + enable_ipv6: true + ipam: + config: + - subnet: 2001:db8:a::/64 + gateway: 2001:db8:a::1 + +services: + mqtt: + image: eclipse-mosquitto:2 + volumes: + - type: bind + source: ./config/mosquitto + target: /mosquitto/config + read_only: true + ports: + - "1883:1883" + - "9000:9000" + user: "10000:10000" + healthcheck: + test: ["CMD-SHELL", "timeout 5 mosquitto_sub -t '$$SYS/#' -C 1 | grep -v Error || exit 1"] + interval: 10s + timeout: 10s + retries: 3 + + lb: + image: envoyproxy/envoy:v1.26-latest + command: ["-c", "/config/envoy.yaml"] + volumes: + - type: bind + source: ./config/envoy + target: /config + read_only: true + depends_on: + manager: + condition: service_healthy + gateway: + condition: service_healthy + ports: + - "80:80" + - "443:443" + - "9410:9410" + - "9411:9411" + + gateway: + build: + context: gateway + depends_on: + mqtt: + condition: service_healthy + manager: + condition: service_healthy + command: + - "serve" + - "--ws-addr" + - ":9310" + - "--wss-addr" + - ":9311" + - "--status-addr" + - ":9312" + - "--tls-server-cert" + - "/certificates/csms.pem" + - "--tls-server-key" + - "/certificates/csms.key" + - "--tls-trust-cert" + - "/certificates/trust.pem" + - "--mqtt-addr" + - "mqtt://mqtt:1883" + - "--manager-api-addr" + - "http://manager:9410" + expose: + - "9310" + - "9311" + - "9312" + volumes: + - type: bind + source: ./config/certificates + target: /certificates + read_only: true + healthcheck: + test: ["CMD", "/usr/bin/curl", "-s", "--fail", "http://localhost:9312/health"] + interval: 10s + timeout: 10s + retries: 3 + user: "10000:10000" + + manager: + build: + context: manager + depends_on: + mqtt: + condition: service_healthy + environment: + ENVIRONMENT: dev + FIRESTORE_EMULATOR_HOST: firestore:8080 + command: + - "serve" + - "-c" + - "/config/config.toml" + volumes: + - type: bind + source: ./config/certificates + target: /certificates + read_only: true + - type: bind + source: ./config/manager + target: /config + read_only: true + expose: + - "9410" + - "9411" + healthcheck: + test: ["CMD", "/usr/bin/curl", "-s", "--fail", "http://localhost:9410/health"] + interval: 10s + timeout: 10s + retries: 3 + user: "10000:10000" + + firestore: + image: google/cloud-sdk + command: + - gcloud + - emulators + - firestore + - start + - --host-port=0.0.0.0:8080 + ports: + - "8080:8080" + + grafana: + image: grafana/grafana:7.5.7 + profiles: + - perf + ports: + - 3000:3000 + restart: unless-stopped + + prometheus: + image: prom/prometheus:v2.45.1 + profiles: + - perf + ports: + - "9090:9090" + volumes: + - ./prometheus:/etc/prometheus + command: + - '--config.file=/etc/prometheus/prometheus.yml' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a2d88cd..e4152f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,25 +25,6 @@ services: timeout: 10s retries: 3 - lb: - image: envoyproxy/envoy:v1.26-latest - command: ["-c", "/config/envoy.yaml"] - volumes: - - type: bind - source: ./config/envoy - target: /config - read_only: true - depends_on: - manager: - condition: service_healthy - gateway: - condition: service_healthy - ports: - - "80:80" - - "443:443" - - "9410:9410" - - "9411:9411" - gateway: build: context: gateway @@ -70,10 +51,10 @@ services: - "mqtt://mqtt:1883" - "--manager-api-addr" - "http://manager:9410" - expose: - - "9310" - - "9311" - - "9312" + ports: + - "80:9310" # charge station ws + - "443:9311" # charge station wss + - "9312:9312" # status volumes: - type: bind source: ./config/certificates @@ -108,9 +89,9 @@ services: source: ./config/manager target: /config read_only: true - expose: - - "9410" - - "9411" + ports: + - "9410:9410" # manager api + - "9411:9411" # status healthcheck: test: ["CMD", "/usr/bin/curl", "-s", "--fail", "http://localhost:9410/health"] interval: 10s @@ -128,22 +109,3 @@ services: - --host-port=0.0.0.0:8080 ports: - "8080:8080" - - grafana: - image: grafana/grafana:7.5.7 - profiles: - - perf - ports: - - 3000:3000 - restart: unless-stopped - - prometheus: - image: prom/prometheus:v2.45.1 - profiles: - - perf - ports: - - "9090:9090" - volumes: - - ./prometheus:/etc/prometheus - command: - - '--config.file=/etc/prometheus/prometheus.yml' \ No newline at end of file diff --git a/manager/adminui/server.go b/manager/adminui/server.go index bb045ff..e72bf65 100644 --- a/manager/adminui/server.go +++ b/manager/adminui/server.go @@ -29,7 +29,7 @@ var ( res embed.FS ) -func NewServer(externalAddr, orgName string, engine store.Engine, certificateProvider services.ChargeStationCertificateProvider) chi.Router { +func NewServer(host string, wsPort, wssPort int, orgName string, engine store.Engine, certificateProvider services.ChargeStationCertificateProvider) chi.Router { r := chi.NewRouter() templates := template.Must(template.ParseFS(res, "templates/*.gohtml")) @@ -86,10 +86,14 @@ func NewServer(externalAddr, orgName string, engine store.Engine, certificatePro _ = templates.ExecuteTemplate(w, "error.gohtml", nil) return } + optionalPort := "" + if wsPort != 80 { + optionalPort = fmt.Sprintf(":%d", wsPort) + } data = map[string]string{ "csid": csId, "auth": auth, - "url": fmt.Sprintf("ws://%s/ws/%s", externalAddr, csId), + "url": fmt.Sprintf("ws://%s%s/ws/%s", host, optionalPort, csId), "password": password, "invalidUsername": invalidUsername, } @@ -106,10 +110,14 @@ func NewServer(externalAddr, orgName string, engine store.Engine, certificatePro _ = templates.ExecuteTemplate(w, "error.gohtml", nil) return } + optionalPort := "" + if wsPort != 443 { + optionalPort = fmt.Sprintf(":%d", wssPort) + } data = map[string]string{ "csid": csId, "auth": auth, - "url": fmt.Sprintf("wss://%s/ws/%s", externalAddr, csId), + "url": fmt.Sprintf("wss://%s%s/ws/%s", host, optionalPort, csId), "password": password, "invalidUsername": invalidUsername, } @@ -129,7 +137,7 @@ func NewServer(externalAddr, orgName string, engine store.Engine, certificatePro data = map[string]string{ "csid": csId, "auth": auth, - "url": fmt.Sprintf("wss://%s/ws/%s", externalAddr, csId), + "url": fmt.Sprintf("wss://%s/ws/%s", host, csId), "clientCert": clientCert, "clientKey": clientKey, } diff --git a/manager/adminui/server_test.go b/manager/adminui/server_test.go index 63d6f96..1160298 100644 --- a/manager/adminui/server_test.go +++ b/manager/adminui/server_test.go @@ -33,7 +33,7 @@ func TestConnectWithUnsecuredAuth(t *testing.T) { CertificateReader: services.StringSource{Data: caCert}, PrivateKeyReader: services.StringSource{Data: caKey}, } - server := NewServer("localhost:9410", "Example", engine, certificateProvider) + server := NewServer("localhost", 80, 443, "Example", engine, certificateProvider) r := chi.NewRouter() r.Mount("/adminui", server) @@ -79,7 +79,7 @@ func TestConnectWithBasicAuth(t *testing.T) { CertificateReader: services.StringSource{Data: caCert}, PrivateKeyReader: services.StringSource{Data: caKey}, } - server := NewServer("localhost:9410", "Example", engine, certificateProvider) + server := NewServer("localhost", 80, 443, "Example", engine, certificateProvider) r := chi.NewRouter() r.Mount("/adminui", server) @@ -125,7 +125,7 @@ func TestConnectWithMTLS(t *testing.T) { CertificateReader: services.StringSource{Data: caCert}, PrivateKeyReader: services.StringSource{Data: caKey}, } - server := NewServer("localhost:9410", "Example", engine, certificateProvider) + server := NewServer("localhost", 80, 443, "Example", engine, certificateProvider) r := chi.NewRouter() r.Mount("/adminui", server) @@ -165,7 +165,7 @@ func TestConnectWithMTLS(t *testing.T) { func TestRegisterTokenWithShortUid(t *testing.T) { engine := inmemory.NewStore(clock.RealClock{}) - server := NewServer("localhost:9410", "Example", engine, nil) + server := NewServer("localhost", 80, 443, "Example", engine, nil) r := chi.NewRouter() r.Mount("/adminui", server) @@ -207,7 +207,7 @@ func TestRegisterTokenWithShortUid(t *testing.T) { func TestRegisterTokenWithLongUid(t *testing.T) { engine := inmemory.NewStore(clock.RealClock{}) - server := NewServer("localhost:9410", "Example", engine, nil) + server := NewServer("localhost", 80, 443, "Example", engine, nil) r := chi.NewRouter() r.Mount("/adminui", server) diff --git a/manager/config/base_config.go b/manager/config/base_config.go index f09843e..b63a427 100644 --- a/manager/config/base_config.go +++ b/manager/config/base_config.go @@ -29,9 +29,11 @@ type BaseConfig struct { // read from the TOML file will overlay this configuration. var DefaultConfig = BaseConfig{ Api: ApiSettingsConfig{ - Addr: "localhost:9410", - ExternalAddr: "localhost:9410", - OrgName: "Thoughtworks", + Addr: "localhost:9410", + Host: "localhost", + WsPort: 80, + WssPort: 443, + OrgName: "Thoughtworks", }, Transport: TransportConfig{ Type: "mqtt", diff --git a/manager/config/base_config_test.go b/manager/config/base_config_test.go index 8edd736..0cbb654 100644 --- a/manager/config/base_config_test.go +++ b/manager/config/base_config_test.go @@ -17,9 +17,11 @@ func TestParseConfig(t *testing.T) { want := &config.BaseConfig{ Api: config.ApiSettingsConfig{ - Addr: ":9410", - ExternalAddr: "https://example.com/", - OrgName: "Example", + Addr: ":9410", + Host: "example.com", + WsPort: 80, + WssPort: 443, + OrgName: "Example", }, Transport: config.TransportConfig{ Type: "mqtt", diff --git a/manager/config/config.go b/manager/config/config.go index 9f32584..3cce856 100644 --- a/manager/config/config.go +++ b/manager/config/config.go @@ -37,9 +37,11 @@ import ( ) type ApiSettings struct { - Addr string - ExternalAddr string - OrgName string + Addr string + Host string + WsPort int + WssPort int + OrgName string } type Config struct { @@ -71,9 +73,11 @@ func Configure(ctx context.Context, cfg *BaseConfig) (c *Config, err error) { c = &Config{ Api: ApiSettings{ - Addr: cfg.Api.Addr, - ExternalAddr: cfg.Api.ExternalAddr, - OrgName: cfg.Api.OrgName, + Addr: cfg.Api.Addr, + Host: cfg.Api.Host, + WsPort: cfg.Api.WsPort, + WssPort: cfg.Api.WssPort, + OrgName: cfg.Api.OrgName, }, } diff --git a/manager/config/config_test.go b/manager/config/config_test.go index fca691f..b83f6e3 100644 --- a/manager/config/config_test.go +++ b/manager/config/config_test.go @@ -20,9 +20,11 @@ func TestConfigure(t *testing.T) { require.NoError(t, err) wantApiSettings := config.ApiSettings{ - Addr: "localhost:9410", - ExternalAddr: "localhost:9410", - OrgName: "Thoughtworks", + Addr: "localhost:9410", + Host: "localhost", + WsPort: 80, + WssPort: 443, + OrgName: "Thoughtworks", } assert.Equal(t, wantApiSettings, settings.Api) diff --git a/manager/config/settings_config.go b/manager/config/settings_config.go index be71d40..6f12598 100644 --- a/manager/config/settings_config.go +++ b/manager/config/settings_config.go @@ -3,9 +3,11 @@ package config type ApiSettingsConfig struct { - Addr string `mapstructure:"addr" toml:"addr" validate:"required"` - ExternalAddr string `mapstructure:"external_addr,omitempty" toml:"external_addr,omitempty"` - OrgName string `mapstructure:"org_name,omitempty" toml:"org_name,omitempty"` + Addr string `mapstructure:"addr" toml:"addr" validate:"required"` + Host string `mapstructure:"host,omitempty" toml:"host,omitempty"` + WsPort int `mapstructure:"ws_port,omitempty" toml:"ws_port,omitempty"` + WssPort int `mapstructure:"wss_port,omitempty" toml:"wss_port,omitempty"` + OrgName string `mapstructure:"org_name,omitempty" toml:"org_name,omitempty"` } type OcppSettingsConfig struct { diff --git a/manager/config/testdata/config.toml b/manager/config/testdata/config.toml index d3564b4..a354f23 100644 --- a/manager/config/testdata/config.toml +++ b/manager/config/testdata/config.toml @@ -1,7 +1,7 @@ [api] addr = ":9410" org_name = "Example" -external_addr = "https://example.com/" +host = "example.com" [transport] type = "mqtt" diff --git a/manager/server/api.go b/manager/server/api.go index 713939b..6ae9547 100644 --- a/manager/server/api.go +++ b/manager/server/api.go @@ -50,7 +50,7 @@ func NewApiHandler(settings config.ApiSettings, engine store.Engine, ocpi ocpi.A r.Handle("/metrics", promhttp.Handler()) r.Get("/api/openapi.json", getApiSwaggerJson) r.With(logger).Mount("/api/v0", api.Handler(apiServer)) - r.With(logger).Mount("/adminui", adminui.NewServer(settings.ExternalAddr, settings.OrgName, engine, csCertProvider)) + r.With(logger).Mount("/adminui", adminui.NewServer(settings.Host, settings.WsPort, settings.WssPort, settings.OrgName, engine, csCertProvider)) return r }