Skip to content

Commit

Permalink
feat: add LivenessEndpoint on gateway service
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ragot committed Dec 6, 2023
1 parent 45314de commit 068889d
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ spec:
broker:
nats:
replicas: 1
hostname: nats.default.svc.cluster.local
url: nats.nats.svc.cluster.local:4222
services:
gateway:
enableAuditPlugin: true
fallback: {{ .Values.gateway.fallback}}
livenessEndpoint: http://nats.default.svc.cluster.local:8222/healthz
control:
disabled: true
auth:
Expand Down
11 changes: 1 addition & 10 deletions components/operator/apis/stack/v1beta3/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,8 @@ type KafkaConfig struct {
}

type NatsConfig struct {
Hostname string `json:"hostname"`

// +kubebuilder:default:=4222
// +optional
Port int32 `json:"port"`

URL string `json:"url"`
// +kubebuilder:default:=3
// +optional
Replicas int `json:"replicas"`

// +kubebuilder:default:=8222
// +optional
MonitoringPort int32 `json:"monitoringPort,omitempty"`
}
3 changes: 3 additions & 0 deletions components/operator/apis/stack/v1beta3/gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ type GatewaySpec struct {

// +optional
EnableAuditPlugin *bool `json:"enableAuditPlugin,omitempty"`

// +optional
LivenessEndpoint *string `json:"livenessEndpoint,omitempty"`
}
5 changes: 1 addition & 4 deletions components/operator/internal/modules/broker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modules

import (
"strconv"
"strings"

"github.com/formancehq/operator/apis/stack/v1beta3"
Expand Down Expand Up @@ -35,12 +34,10 @@ func BrokerEnvVarsWithPrefix(broker v1beta3.Broker, serviceName string) Containe
)
}
} else {
port := strconv.FormatInt(int64(broker.Nats.Port), 10)

ret = ret.Append(
Env("BROKER", "nats"),
Env("PUBLISHER_NATS_ENABLED", "true"),
Env("PUBLISHER_NATS_URL", broker.Nats.Hostname+":"+port),
Env("PUBLISHER_NATS_URL", broker.Nats.URL),
Env("PUBLISHER_NATS_CLIENT_ID", serviceName),
)
}
Expand Down
17 changes: 4 additions & 13 deletions components/operator/internal/modules/gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func (g module) Versions() map[string]modules.Version {
ExposeHTTP: &modules.ExposeHTTP{
Path: "/",
},
Liveness: modules.LivenessDisable,
Topics: &modules.Topics{Name: "audit"},
Annotations: ctx.Configuration.Spec.Services.Gateway.Annotations.Service,
Liveness: modules.LivenessDisable,
LivenessEndpoint: ctx.Configuration.Spec.Services.Gateway.LivenessEndpoint,
Topics: &modules.Topics{Name: "audit"},
Annotations: ctx.Configuration.Spec.Services.Gateway.Annotations.Service,
Configs: func(resolveContext modules.ServiceInstallConfiguration) modules.Configs {
return modules.Configs{
"config": modules.Config{
Expand Down Expand Up @@ -84,16 +85,6 @@ func (g module) Versions() map[string]modules.Version {
},
}

if ctx.Configuration.Spec.Broker.Nats != nil {
livenessEndpoint := fmt.Sprintf(
"nats://%s:%d/%s",
ctx.Configuration.Spec.Broker.Nats.Hostname,
ctx.Configuration.Spec.Broker.Nats.MonitoringPort,
"healthz",
)

service.LivenessEndpoint = &livenessEndpoint
}
return modules.Services{service}
},
},
Expand Down
4 changes: 1 addition & 3 deletions components/operator/internal/modules/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package modules

import (
"fmt"
"strconv"
"strings"

stackv1beta3 "github.com/formancehq/operator/apis/stack/v1beta3"
Expand Down Expand Up @@ -67,9 +66,8 @@ func ElasticSearchEnvVars(stack *stackv1beta3.Stack, configuration *stackv1beta3
}
if configuration.Spec.Broker.Nats != nil {

port := strconv.FormatInt(int64(configuration.Spec.Broker.Nats.Port), 10)
ret = ret.Append(
Env("NATS_URL", configuration.Spec.Broker.Nats.Hostname+":"+port),
Env("NATS_URL", configuration.Spec.Broker.Nats.URL),
)
}
if configuration.Spec.Services.Search.ElasticSearchConfig.BasicAuth != nil {
Expand Down
4 changes: 1 addition & 3 deletions components/operator/internal/modules/search/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/fs"
"path/filepath"
"strconv"
"strings"

"golang.org/x/mod/semver"
Expand Down Expand Up @@ -461,9 +460,8 @@ func elasticSearchEnvVars(stack *stackv1beta3.Stack, configuration *stackv1beta3
}
}
if configuration.Spec.Broker.Nats != nil {
port := strconv.FormatInt(int64(configuration.Spec.Broker.Nats.Port), 10)
ret = ret.Append(
modules.Env("NATS_URL", configuration.Spec.Broker.Nats.Hostname+":"+port),
modules.Env("NATS_URL", configuration.Spec.Broker.Nats.URL),
)
}
if configuration.Spec.Services.Search.ElasticSearchConfig.BasicAuth != nil {
Expand Down
4 changes: 1 addition & 3 deletions components/operator/internal/modules/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"fmt"
"sort"
"strconv"
"strings"

"github.com/formancehq/stack/libs/go-libs/collectionutils"
Expand Down Expand Up @@ -418,8 +417,7 @@ func (r *serviceReconciler) configureNats() {
Retention: nats.InterestPolicy,
Replicas: r.Configuration.Spec.Broker.Nats.Replicas,
}
port := strconv.FormatInt(int64(r.Configuration.Spec.Broker.Nats.Port), 10)
nc, err := nats.Connect(r.Configuration.Spec.Broker.Nats.Hostname + ":" + port)
nc, err := nats.Connect(r.Configuration.Spec.Broker.Nats.URL)
if err != nil {
logging.Error(err)
}
Expand Down
5 changes: 1 addition & 4 deletions components/operator/internal/storage/nats/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nats

import (
"strconv"

"github.com/formancehq/operator/apis/stack/v1beta3"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
Expand All @@ -12,8 +10,7 @@ func NewClient(natsConfig *v1beta3.NatsConfig, clientId string) (*nats.Conn, err
options := []nats.Option{
nats.Name(clientId),
}
port := strconv.FormatInt(int64(natsConfig.Port), 10)
conn, err := nats.Connect(natsConfig.Hostname+":"+port, options...)
conn, err := nats.Connect(natsConfig.URL, options...)
if err != nil {
return nil, errors.Wrap(err, "cannot connect to nats-core")
}
Expand Down

0 comments on commit 068889d

Please sign in to comment.