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: "K8S_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},
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
4 changes: 4 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" }}"
{{- if .Values.metrics.enabled }}
- name: OPERATOR_SERVICE_URL
value: "http://shopware-operator.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.metrics.port | default 8080 }}"
{{- end }}
{{- if and (hasKey .Values "events") (hasKey .Values.events "nats") (.Values.events.nats.enable) }}
- name: NATS_ENABLE
value: "true"
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
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 }}
4 changes: 4 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ 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:
enabled: false
port: 8080
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"`
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