Skip to content

Commit

Permalink
Merge branch 'main' into support-for-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
TrayserCassa authored Jan 8, 2025
2 parents 1e727b7 + 7bb4762 commit ff36d67
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ build: manifests generate ## Build manager binary.

.PHONY: run
run: manifests generate zap-pretty ## Run a controller from your host.
go run ./cmd/manager.go --namespace ${NAMESPACE} --disable-checks --debug 2>&1 | $(ZAP_PRETTY) --all
go run ./cmd/manager.go --namespace ${NAMESPACE} --disable-checks --debug --log-structured 2>&1 | $(ZAP_PRETTY) --all

# If you wish to build the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
Expand Down
4 changes: 4 additions & 0 deletions api/v1/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ type DatabaseSpec struct {
// +kubebuilder:default=shopware
Name string `json:"name"`

// +kubebuilder:validation:MinLength=1
// +kubebuilder:default=PREFERRED
SSLMode string `json:"sslMode,omitempty"`

PasswordSecretRef SecretRef `json:"passwordSecretRef"`
}

Expand Down
49 changes: 26 additions & 23 deletions cmd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"os"
"time"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand All @@ -33,10 +32,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"
"github.com/go-logr/zapr"
"go.uber.org/zap"

shopv1 "github.com/shopware/shopware-operator/api/v1"
"github.com/shopware/shopware-operator/internal/controller"
Expand All @@ -59,42 +57,41 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var debug bool
var logStructured bool
var disableChecks bool
var probeAddr string
var namespace string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&namespace, "namespace", "default", "The namespace in which the operator is running in")
flag.BoolVar(&debug, "debug", false, "Set's the logger to debug with more logging output")
flag.BoolVar(&logStructured, "log-structured", false, "Set's the logger to output with human logs")
flag.BoolVar(&disableChecks, "disable-checks", false,
"Disable the s3 connection check and the database connection check")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)

flag.Parse()

var logger logr.Logger
if debug {
logger = zap.New()
logger.Info("Use development logger")
var cfg zap.Config

if logStructured {
cfg = zap.NewProductionConfig()
} else {
logger = zap.New(zap.UseFlagOptions(&opts), func(o *zap.Options) {
o.EncoderConfigOptions = append(o.EncoderConfigOptions,
func(c *zapcore.EncoderConfig) {
c.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime)
}, func(c *zapcore.EncoderConfig) {
c.EncodeLevel = func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + level.CapitalString() + "]")
}
})
},
)
cfg = zap.NewDevelopmentConfig()
}

if debug {
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
}

zlogger, err := cfg.Build()
if err != nil {
setupLog.Error(err, "setup zap logger")
return
}
logger := zapr.NewLogger(zlogger)
ctrl.SetLogger(logger)

// Overwrite namespace when env is set, which is always set running in a cluster
Expand Down Expand Up @@ -154,6 +151,12 @@ func main() {
}
//+kubebuilder:scaffold:builder

defer func() {
if err := recover(); err != nil {
zlogger.Fatal("Panic occurred", zap.Any("error", err))
}
}()

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ spec:
containers:
- args:
- --leader-elect
{{- if .Values.logStructured }}
- --log-structured
{{- end }}
{{- if .Values.debug }}
- --debug
{{- end }}
command:
- /manager
env:
Expand Down
2 changes: 1 addition & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ labels: {}
podAnnotations: {}

logStructured: false
logLevel: "INFO"
debug: false
18 changes: 16 additions & 2 deletions internal/k8s/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func GetWatchNamespace() (string, error) {
Expand Down Expand Up @@ -354,8 +355,21 @@ func EnsureObjectWithHash(
obj.SetAnnotations(annotations)
}

if oldObject.GetAnnotations()["shopware.com/last-config-hash"] != hash ||
!objectMetaEqual(obj, oldObject) {
hashChanged := oldObject.GetAnnotations()["shopware.com/last-config-hash"] != hash
objectMetaChanged := !objectMetaEqual(obj, oldObject)

if hashChanged || objectMetaChanged {
if hashChanged {
log.FromContext(ctx).Info("Object last-config-hash has changed", "old", oldObject.GetAnnotations()["shopware.com/last-config-hash"], "new", hash)
} else {
log.FromContext(ctx).Info(
"Object meta has changed",
"oldAnnotations", oldObject.GetAnnotations(),
"newAnnotations", obj.GetAnnotations(),
"oldLabels", oldObject.GetLabels(),
"newLabels", obj.GetLabels(),
)
}

obj.SetResourceVersion(oldObject.GetResourceVersion())
switch object := obj.(type) {
Expand Down
3 changes: 2 additions & 1 deletion internal/util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
func GenerateDatabaseURLForShopware(db *v1.DatabaseSpec, dbHost string, p []byte) []byte {
urlP := url.QueryEscape(string(p))
plain := fmt.Sprintf(
"mysql://%s:%s@%s:%d/%s?serverVersion=%s",
"mysql://%s:%s@%s:%d/%s?serverVersion=%s&sslMode=%s",
db.User,
urlP,
dbHost,
db.Port,
db.Name,
db.Version,
db.SSLMode,
)
return []byte(plain)
}
Expand Down

0 comments on commit ff36d67

Please sign in to comment.