Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions api/v1/store_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
corev1 "k8s.io/api/core/v1"
)

// operatorServiceURL is set once at operator startup and injected into every
// store container as K8S_OPERATOR_URL so the Shopware consumer knows how to
// reach the operator.
var operatorServiceURL string

// SetOperatorServiceURL stores the operator's service URL globally so it can
// be included in every store's environment without modifying the Store CRD.
func SetOperatorServiceURL(url string) {
operatorServiceURL = url
}

// TODO: If building more than one instance print a warning for the cache to use
// redis
func (s *Store) getAppCache() []corev1.EnvVar {
Expand Down Expand Up @@ -509,6 +520,13 @@ func (s *Store) GetEnv() []corev1.EnvVar {
},
}

if operatorServiceURL != "" {
c = append(c, corev1.EnvVar{
Name: "SHOPWARE_OPERATOR_URL",
Value: operatorServiceURL,
})
}

if s.Spec.ShopConfiguration.UsageDataConsent == "revoked" {
c = append(c, corev1.EnvVar{
Name: "SHOPWARE_USAGE_DATA_CONSENT",
Expand Down
7 changes: 5 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/go-logr/zapr"
shopv1 "github.com/shopware/shopware-operator/api/v1"
Expand Down Expand Up @@ -68,6 +69,8 @@ func main() {
os.Exit(1)
}

shopv1.SetOperatorServiceURL(cfg.OperatorServiceURL)

logger := logging.NewLogger(cfg.LogLevel, cfg.LogFormat).
With(zapz.String("service", "shopware-operator")).
With(zapz.String("operator_version", version)).
Expand All @@ -84,8 +87,8 @@ func main() {
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
// Metrics: metricsserver.Options{BindAddress: cfg.MetricsAddr},
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: cfg.MetricsAddr, SecureServing: false},
HealthProbeBindAddress: cfg.ProbeAddr,
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.22.0 // indirect
github.com/prometheus/client_golang v1.22.0
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
9 changes: 9 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ spec:
value: "{{ .Values.logFormat | default "json" }}"
- name: DISABLE_CHECKS
value: "{{ .Values.disableChecks | default "false" }}"
Comment thread
TrayserCassa marked this conversation as resolved.
{{- if .Values.metrics.enabled }}
- name: OPERATOR_SERVICE_URL
value: "{{ .Values.metrics.shopwareOperatorUrl | default (printf "http://shopware-operator.%s.svc.cluster.local:%d" .Release.Namespace (.Values.metrics.port | default 8080 | int)) }}"
{{- end }}
{{- if and (hasKey .Values "events") (hasKey .Values.events "nats") (.Values.events.nats.enable) }}
- name: NATS_ENABLE
value: "true"
Expand Down Expand Up @@ -106,6 +110,11 @@ spec:
initialDelaySeconds: 15
periodSeconds: 20
name: operator
{{- if .Values.metrics.enabled }}
ports:
- containerPort: {{ .Values.metrics.port | default 8080 }}
name: http-metrics
{{- end }}
readinessProbe:
httpGet:
path: /readyz
Expand Down
29 changes: 29 additions & 0 deletions helm/templates/metrics-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{- if not .Values.crds.installOnly }}
{{- if .Values.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
name: shopware-operator
Comment thread
TrayserCassa marked this conversation as resolved.
namespace: {{ .Release.Namespace }}
labels:
app.kubernetes.io/component: metrics
app.kubernetes.io/created-by: shopware-operator
app.kubernetes.io/instance: shopware-operator
app.kubernetes.io/managed-by: shopware-operator
app.kubernetes.io/name: service
app.kubernetes.io/part-of: shopware-operator
control-plane: shopware-operator
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
type: ClusterIP
selector:
control-plane: shopware-operator
ports:
- name: http-metrics
port: {{ .Values.metrics.port | default 8080 }}
targetPort: 8080
protocol: TCP
{{- end }}
{{- end }}
35 changes: 35 additions & 0 deletions helm/templates/service-monitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{- if not .Values.crds.installOnly }}
{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: shopware-operator
namespace: {{ .Release.Namespace }}
labels:
app.kubernetes.io/component: metrics
app.kubernetes.io/created-by: shopware-operator
app.kubernetes.io/instance: shopware-operator
app.kubernetes.io/managed-by: shopware-operator
app.kubernetes.io/name: servicemonitor
app.kubernetes.io/part-of: shopware-operator
control-plane: shopware-operator
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.metrics.serviceMonitor.additionalLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
endpoints:
- port: http-metrics
path: /metrics
interval: {{ .Values.metrics.serviceMonitor.interval }}
scrapeTimeout: {{ .Values.metrics.serviceMonitor.scrapeTimeout }}
selector:
matchLabels:
control-plane: shopware-operator
namespaceSelector:
matchNames:
- {{ .Release.Namespace }}
{{- end }}
{{- end }}
25 changes: 25 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,28 @@ logFormat: json
# Disable check for s3/database/fastly and Opensearch checks. Useful if network access is not given for one of the services.
# This is a global level. You can also control this per store.
disableChecks: false

metrics:
# When enabled, a Service named 'shopware-operator' is created, the metrics
# endpoint is exposed, and K8S_OPERATOR_URL is injected into every store
# container so the Shopware consumer can reach the operator.
enabled: false
port: 8080
# shopwareOperatorUrl overrides the K8S_OPERATOR_URL injected into store
# containers (admin, storefront, worker). When left empty the URL is
# auto-constructed as:
# http://shopware-operator.<namespace>.svc.cluster.local:<port>
shopwareOperatorUrl: ""
# serviceMonitor: configure a Prometheus Operator ServiceMonitor to scrape
# the operator metrics. Requires the Prometheus Operator CRDs to be installed.
serviceMonitor:
enabled: false
# Interval at which Prometheus scrapes the metrics endpoint.
interval: 30s
# Timeout for each scrape request.
scrapeTimeout: 10s
# Additional labels added to the ServiceMonitor, e.g. to match a Prometheus
# Operator release label selector:
# additionalLabels:
# release: kube-prometheus-stack
additionalLabels: {}
6 changes: 5 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ type StoreConfig struct {
NatsHandler NatsHandler `env:",prefix=NATS_"`

// Metrics and health probe configuration
MetricsAddr string `env:"METRICS_BIND_ADDRESS, default=0"`
MetricsAddr string `env:"METRICS_BIND_ADDRESS, default=:8080"`
Comment thread
TrayserCassa marked this conversation as resolved.
ProbeAddr string `env:"HEALTH_PROBE_BIND_ADDRESS, default=:8081"`

// OperatorServiceURL is exposed to store containers as K8S_OPERATOR_URL so
// the Shopware consumer knows how to reach the operator service.
OperatorServiceURL string `env:"OPERATOR_SERVICE_URL"`

EnableLeaderElection bool `env:"LEADER_ELECT, default=true"`
DisableChecks bool `env:"DISABLE_CHECKS, default=false"`
Namespace string `env:"NAMESPACE, default=default"`
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/store_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/shopware/shopware-operator/internal/job"
"github.com/shopware/shopware-operator/internal/k8s"
"github.com/shopware/shopware-operator/internal/logging"
"github.com/shopware/shopware-operator/internal/metrics"
"github.com/shopware/shopware-operator/internal/pdb"
"github.com/shopware/shopware-operator/internal/secret"
"github.com/shopware/shopware-operator/internal/service"
Expand Down Expand Up @@ -160,6 +161,7 @@ func (r *StoreReconciler) Reconcile(
// }

if !store.DeletionTimestamp.IsZero() {
metrics.RemoveStoreMetrics(store)
return shortRequeue, nil
}

Expand Down
12 changes: 12 additions & 0 deletions internal/controller/store_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
v1 "github.com/shopware/shopware-operator/api/v1"
"github.com/shopware/shopware-operator/internal/cronjob"
"github.com/shopware/shopware-operator/internal/deployment"
"github.com/shopware/shopware-operator/internal/job"
"github.com/shopware/shopware-operator/internal/k8s"
"github.com/shopware/shopware-operator/internal/logging"
"github.com/shopware/shopware-operator/internal/metrics"
"github.com/shopware/shopware-operator/internal/util"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -117,6 +119,16 @@ func (r *StoreReconciler) reconcileCRStatus(

logging.FromContext(ctx).Infow("Update store status", zap.Any("status", store.Status))
r.SendEvent(ctx, *store, "Update store status")
metrics.UpdateStoreMetrics(store)

scheduledCronJob, err := cronjob.GetScheduledCronJob(ctx, r.Client, *store)
if err != nil {
if !k8serrors.IsNotFound(err) {
logging.FromContext(ctx).Warnw("failed to get scheduled task cronjob for metrics", zap.Error(err))
}
scheduledCronJob = nil
}
metrics.UpdateScheduledTaskMetrics(store, scheduledCronJob)

return writeStoreStatus(ctx, r.Client, types.NamespacedName{
Namespace: store.Namespace,
Expand Down
Loading
Loading