From a430f76a657ba45fb3c628b672e76cd5b64868ff Mon Sep 17 00:00:00 2001 From: Martin Briatka Date: Mon, 9 Nov 2026 13:44:22 +0100 Subject: [PATCH 1/4] Allow configuring additional annotations on the generated workload Add a spec.annotations (map[string]string) field. The operator merges its entries onto the metadata of every generated StatefulSet. The merge is done in the operator rather than in the chart templates, so it is independent of the vendored chart. The field is empty by default, so existing deployments are unaffected. --- api/v1alpha1/types.go | 3 + api/v1alpha1/zz_generated.deepcopy.go | 7 +++ crds/cache.cs.sap.com_valkeys.yaml | 6 ++ internal/transformer/annotation_generator.go | 64 ++++++++++++++++++++ pkg/operator/operator.go | 2 +- 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 internal/transformer/annotation_generator.go diff --git a/api/v1alpha1/types.go b/api/v1alpha1/types.go index a537b1a9..c8aba559 100644 --- a/api/v1alpha1/types.go +++ b/api/v1alpha1/types.go @@ -32,6 +32,9 @@ type ValkeySpec struct { Binding *BindingProperties `json:"binding,omitempty"` ExtraEnvVars []corev1.EnvVar `json:"extraEnvVars,omitempty"` ExtraFlags []string `json:"extraFlags,omitempty"` + // Additional annotations added to the generated workload metadata. + // +optional + Annotations map[string]string `json:"annotations,omitempty"` } // SentinelProperties models attributes of the sentinel sidecar diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index c5c0804e..9c038154 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -337,6 +337,13 @@ func (in *ValkeySpec) DeepCopyInto(out *ValkeySpec) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ValkeySpec. diff --git a/crds/cache.cs.sap.com_valkeys.yaml b/crds/cache.cs.sap.com_valkeys.yaml index 592b216f..29aaeb91 100644 --- a/crds/cache.cs.sap.com_valkeys.yaml +++ b/crds/cache.cs.sap.com_valkeys.yaml @@ -958,6 +958,12 @@ spec: x-kubernetes-list-type: atomic type: object type: object + annotations: + additionalProperties: + type: string + description: Additional annotations added to the generated workload + metadata. + type: object binding: description: BindingProperties models custom properties for the generated binding secret diff --git a/internal/transformer/annotation_generator.go b/internal/transformer/annotation_generator.go new file mode 100644 index 00000000..913c016b --- /dev/null +++ b/internal/transformer/annotation_generator.go @@ -0,0 +1,64 @@ +/* +SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and valkey-operator contributors +SPDX-License-Identifier: Apache-2.0 +*/ + +package transformer + +import ( + "context" + + "github.com/sap/component-operator-runtime/pkg/manifests" + componentoperatorruntimetypes "github.com/sap/component-operator-runtime/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// annotationGenerator wraps a generator and merges the component's spec.annotations +// onto the metadata of every generated StatefulSet. It is applied in the operator +// (not the vendored chart) so the behaviour is independent of the chart templates. +type annotationGenerator struct { + generator manifests.Generator +} + +func NewAnnotationGenerator(generator manifests.Generator) *annotationGenerator { + return &annotationGenerator{generator: generator} +} + +func (g *annotationGenerator) Generate(ctx context.Context, namespace string, name string, parameters componentoperatorruntimetypes.Unstructurable) ([]client.Object, error) { + objects, err := g.generator.Generate(ctx, namespace, name, parameters) + if err != nil { + return nil, err + } + annotations := annotationsFromParameters(parameters) + if len(annotations) == 0 { + return objects, nil + } + for i := 0; i < len(objects); i++ { + if statefulSet := asStatefulSet(objects[i]); statefulSet != nil { + merged := statefulSet.GetAnnotations() + if merged == nil { + merged = make(map[string]string, len(annotations)) + } + for key, value := range annotations { + merged[key] = value + } + statefulSet.SetAnnotations(merged) + objects[i] = asUnstructurable(statefulSet) + } + } + return objects, nil +} + +func annotationsFromParameters(parameters componentoperatorruntimetypes.Unstructurable) map[string]string { + raw, ok := parameters.ToUnstructured()["annotations"].(map[string]any) + if !ok { + return nil + } + annotations := make(map[string]string, len(raw)) + for key, value := range raw { + if s, ok := value.(string); ok { + annotations[key] = s + } + } + return annotations +} diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 7cbdfc9e..e7465100 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -122,7 +122,7 @@ func (o *Operator) Setup(mgr ctrl.Manager) error { if err := component.NewReconciler[*operatorv1alpha1.Valkey]( o.options.Name, - resourceGenerator, + transformer.NewAnnotationGenerator(resourceGenerator), component.ReconcilerOptions{}, ).WithPostReconcileHook( reconcileBinding, From a835176e031a3757e105205ce47c62573103d278 Mon Sep 17 00:00:00 2001 From: Martin Briatka Date: Thu, 12 Nov 2026 15:48:50 +0100 Subject: [PATCH 2/4] Allow configuring the certificate duration Add an optional spec.tls.duration field that is passed through to the generated cert-manager Certificate. When unset, the previous hard-coded default of 876000h (~100 years) is preserved, so existing Valkey resources are unaffected. Closes #65 --- api/v1alpha1/types.go | 3 +++ api/v1alpha1/zz_generated.deepcopy.go | 6 ++++++ crds/cache.cs.sap.com_valkeys.yaml | 4 ++++ pkg/operator/data/parameters.yaml | 2 +- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/api/v1alpha1/types.go b/api/v1alpha1/types.go index c8aba559..4b2a05d6 100644 --- a/api/v1alpha1/types.go +++ b/api/v1alpha1/types.go @@ -72,6 +72,9 @@ type MetricsPrometheusRuleProperties struct { type TLSProperties struct { Enabled bool `json:"enabled,omitempty"` CertManager *CertManagerProperties `json:"certManager,omitempty"` + // Duration of the issued certificate. Defaults to 876000h (~100 years) if not set. + // +optional + Duration *metav1.Duration `json:"duration,omitempty"` } // CertManagerProperties models cert-manager related attributes diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 9c038154..b8a10ac2 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -12,6 +12,7 @@ package v1alpha1 import ( monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ) @@ -217,6 +218,11 @@ func (in *TLSProperties) DeepCopyInto(out *TLSProperties) { *out = new(CertManagerProperties) (*in).DeepCopyInto(*out) } + if in.Duration != nil { + in, out := &in.Duration, &out.Duration + *out = new(metav1.Duration) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TLSProperties. diff --git a/crds/cache.cs.sap.com_valkeys.yaml b/crds/cache.cs.sap.com_valkeys.yaml index 29aaeb91..492a84cd 100644 --- a/crds/cache.cs.sap.com_valkeys.yaml +++ b/crds/cache.cs.sap.com_valkeys.yaml @@ -6042,6 +6042,10 @@ spec: type: string type: object type: object + duration: + description: Duration of the issued certificate. Defaults to 876000h + (~100 years) if not set. + type: string enabled: type: boolean type: object diff --git a/pkg/operator/data/parameters.yaml b/pkg/operator/data/parameters.yaml index 1665b162..999ea975 100644 --- a/pkg/operator/data/parameters.yaml +++ b/pkg/operator/data/parameters.yaml @@ -289,7 +289,7 @@ extraDeploy: {{- else }} name: {{ $fullname }}-selfsigned {{- end }} - duration: 876000h + duration: {{ .tls.duration | default "876000h" }} {{- if not .tls.certManager.issuer }} - apiVersion: cert-manager.io/v1 kind: Issuer From 642d3e2101fe77a152b1452f8b413eef7b2140c1 Mon Sep 17 00:00:00 2001 From: Martin Briatka Date: Wed, 18 Nov 2026 19:28:33 +0100 Subject: [PATCH 3/4] Merge the workload annotations in the object transformer Move the spec.annotations merge out of a dedicated generator wrapper and into the existing object transformer, alongside the StatefulSet topology handling it already performs. Object transformers do not receive the component parameters, so the transformer also implements ParameterTransformer to capture spec.annotations during the parameter phase (keyed by namespace/name) and applies them during the object phase. Behaviour is unchanged: the entries are still merged onto the metadata of every generated StatefulSet, independent of the vendored chart. --- internal/transformer/annotation_generator.go | 64 -------------------- internal/transformer/object_transformer.go | 63 ++++++++++++++++++- pkg/operator/operator.go | 3 +- 3 files changed, 63 insertions(+), 67 deletions(-) delete mode 100644 internal/transformer/annotation_generator.go diff --git a/internal/transformer/annotation_generator.go b/internal/transformer/annotation_generator.go deleted file mode 100644 index 913c016b..00000000 --- a/internal/transformer/annotation_generator.go +++ /dev/null @@ -1,64 +0,0 @@ -/* -SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and valkey-operator contributors -SPDX-License-Identifier: Apache-2.0 -*/ - -package transformer - -import ( - "context" - - "github.com/sap/component-operator-runtime/pkg/manifests" - componentoperatorruntimetypes "github.com/sap/component-operator-runtime/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -// annotationGenerator wraps a generator and merges the component's spec.annotations -// onto the metadata of every generated StatefulSet. It is applied in the operator -// (not the vendored chart) so the behaviour is independent of the chart templates. -type annotationGenerator struct { - generator manifests.Generator -} - -func NewAnnotationGenerator(generator manifests.Generator) *annotationGenerator { - return &annotationGenerator{generator: generator} -} - -func (g *annotationGenerator) Generate(ctx context.Context, namespace string, name string, parameters componentoperatorruntimetypes.Unstructurable) ([]client.Object, error) { - objects, err := g.generator.Generate(ctx, namespace, name, parameters) - if err != nil { - return nil, err - } - annotations := annotationsFromParameters(parameters) - if len(annotations) == 0 { - return objects, nil - } - for i := 0; i < len(objects); i++ { - if statefulSet := asStatefulSet(objects[i]); statefulSet != nil { - merged := statefulSet.GetAnnotations() - if merged == nil { - merged = make(map[string]string, len(annotations)) - } - for key, value := range annotations { - merged[key] = value - } - statefulSet.SetAnnotations(merged) - objects[i] = asUnstructurable(statefulSet) - } - } - return objects, nil -} - -func annotationsFromParameters(parameters componentoperatorruntimetypes.Unstructurable) map[string]string { - raw, ok := parameters.ToUnstructured()["annotations"].(map[string]any) - if !ok { - return nil - } - annotations := make(map[string]string, len(raw)) - for key, value := range raw { - if s, ok := value.(string); ok { - annotations[key] = s - } - } - return annotations -} diff --git a/internal/transformer/object_transformer.go b/internal/transformer/object_transformer.go index 996fc51a..d778c4b0 100644 --- a/internal/transformer/object_transformer.go +++ b/internal/transformer/object_transformer.go @@ -6,6 +6,10 @@ SPDX-License-Identifier: Apache-2.0 package transformer import ( + "sync" + + "github.com/sap/component-operator-runtime/pkg/manifests" + "github.com/sap/component-operator-runtime/pkg/types" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -14,13 +18,44 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -type objectTransformer struct{} +// objectTransformer post-processes the generated StatefulSets. It also implements +// ParameterTransformer to capture the component's spec.annotations during the +// parameter phase (object transformers do not receive the parameters), so they can +// be merged onto the StatefulSet metadata during the object phase. +type objectTransformer struct { + mutex sync.Mutex + annotations map[string]map[string]string +} + +var ( + _ manifests.ParameterTransformer = &objectTransformer{} + _ manifests.ObjectTransformer = &objectTransformer{} +) func NewObjectTransformer() *objectTransformer { - return &objectTransformer{} + return &objectTransformer{annotations: make(map[string]map[string]string)} +} + +func (t *objectTransformer) TransformParameters(namespace string, name string, parameters types.Unstructurable) (types.Unstructurable, error) { + annotations := annotationsFromParameters(parameters) + key := namespace + "/" + name + t.mutex.Lock() + if len(annotations) == 0 { + delete(t.annotations, key) + } else { + t.annotations[key] = annotations + } + t.mutex.Unlock() + return parameters, nil } func (t *objectTransformer) TransformObjects(namespace string, name string, objects []client.Object) ([]client.Object, error) { + key := namespace + "/" + name + t.mutex.Lock() + annotations := t.annotations[key] + delete(t.annotations, key) + t.mutex.Unlock() + for i := 0; i < len(objects); i++ { if statefulSet := asStatefulSet(objects[i]); statefulSet != nil { if len(statefulSet.Spec.Template.Spec.TopologySpreadConstraints) == 0 { @@ -41,6 +76,16 @@ func (t *objectTransformer) TransformObjects(namespace string, name string, obje constraint.MatchLabelKeys = []string{"controller-revision-hash"} } } + if len(annotations) > 0 { + merged := statefulSet.GetAnnotations() + if merged == nil { + merged = make(map[string]string, len(annotations)) + } + for k, v := range annotations { + merged[k] = v + } + statefulSet.SetAnnotations(merged) + } objects[i] = asUnstructurable(statefulSet) } } @@ -48,6 +93,20 @@ func (t *objectTransformer) TransformObjects(namespace string, name string, obje return objects, nil } +func annotationsFromParameters(parameters types.Unstructurable) map[string]string { + raw, ok := parameters.ToUnstructured()["annotations"].(map[string]any) + if !ok { + return nil + } + annotations := make(map[string]string, len(raw)) + for key, value := range raw { + if s, ok := value.(string); ok { + annotations[key] = s + } + } + return annotations +} + func asStatefulSet(object client.Object) *appsv1.StatefulSet { if statefulSet, ok := object.(*appsv1.StatefulSet); ok { return statefulSet diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index e7465100..a8182ec2 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -113,6 +113,7 @@ func (o *Operator) Setup(mgr ctrl.Manager) error { return errors.Wrap(err, "error initializing resource generator") } resourceGenerator. + WithParameterTransformer(objectTransformer). WithParameterTransformer(parameterTransformer). WithObjectTransformer(objectTransformer) @@ -122,7 +123,7 @@ func (o *Operator) Setup(mgr ctrl.Manager) error { if err := component.NewReconciler[*operatorv1alpha1.Valkey]( o.options.Name, - transformer.NewAnnotationGenerator(resourceGenerator), + resourceGenerator, component.ReconcilerOptions{}, ).WithPostReconcileHook( reconcileBinding, From 7dace255d020c60988b27d32ccc2afb13cbd2a03 Mon Sep 17 00:00:00 2001 From: Martin Briatka Date: Wed, 18 Nov 2026 19:28:38 +0100 Subject: [PATCH 4/4] Move the certificate duration under tls.certManager Relocate the optional duration field from spec.tls.duration to spec.tls.certManager.duration, so it sits with the other cert-manager specific settings. The field is only consumed when certManager is configured, so this is the more natural place for it. Behaviour and the 876000h default are unchanged. --- api/v1alpha1/types.go | 6 +++--- api/v1alpha1/zz_generated.deepcopy.go | 10 +++++----- crds/cache.cs.sap.com_valkeys.yaml | 8 ++++---- pkg/operator/data/parameters.yaml | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/api/v1alpha1/types.go b/api/v1alpha1/types.go index 4b2a05d6..4bb1c7eb 100644 --- a/api/v1alpha1/types.go +++ b/api/v1alpha1/types.go @@ -72,14 +72,14 @@ type MetricsPrometheusRuleProperties struct { type TLSProperties struct { Enabled bool `json:"enabled,omitempty"` CertManager *CertManagerProperties `json:"certManager,omitempty"` - // Duration of the issued certificate. Defaults to 876000h (~100 years) if not set. - // +optional - Duration *metav1.Duration `json:"duration,omitempty"` } // CertManagerProperties models cert-manager related attributes type CertManagerProperties struct { Issuer *ObjectReference `json:"issuer,omitempty"` + // Duration of the issued certificate. Defaults to 876000h (~100 years) if not set. + // +optional + Duration *metav1.Duration `json:"duration,omitempty"` } // ObjectReference models a reference to a Kubernetes object diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index b8a10ac2..6610045b 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -44,6 +44,11 @@ func (in *CertManagerProperties) DeepCopyInto(out *CertManagerProperties) { *out = new(ObjectReference) **out = **in } + if in.Duration != nil { + in, out := &in.Duration, &out.Duration + *out = new(metav1.Duration) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CertManagerProperties. @@ -218,11 +223,6 @@ func (in *TLSProperties) DeepCopyInto(out *TLSProperties) { *out = new(CertManagerProperties) (*in).DeepCopyInto(*out) } - if in.Duration != nil { - in, out := &in.Duration, &out.Duration - *out = new(metav1.Duration) - **out = **in - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TLSProperties. diff --git a/crds/cache.cs.sap.com_valkeys.yaml b/crds/cache.cs.sap.com_valkeys.yaml index 492a84cd..928468b3 100644 --- a/crds/cache.cs.sap.com_valkeys.yaml +++ b/crds/cache.cs.sap.com_valkeys.yaml @@ -6030,6 +6030,10 @@ spec: description: CertManagerProperties models cert-manager related attributes properties: + duration: + description: Duration of the issued certificate. Defaults + to 876000h (~100 years) if not set. + type: string issuer: description: ObjectReference models a reference to a Kubernetes object @@ -6042,10 +6046,6 @@ spec: type: string type: object type: object - duration: - description: Duration of the issued certificate. Defaults to 876000h - (~100 years) if not set. - type: string enabled: type: boolean type: object diff --git a/pkg/operator/data/parameters.yaml b/pkg/operator/data/parameters.yaml index 999ea975..8cfee603 100644 --- a/pkg/operator/data/parameters.yaml +++ b/pkg/operator/data/parameters.yaml @@ -289,7 +289,7 @@ extraDeploy: {{- else }} name: {{ $fullname }}-selfsigned {{- end }} - duration: {{ .tls.duration | default "876000h" }} + duration: {{ .tls.certManager.duration | default "876000h" }} {{- if not .tls.certManager.issuer }} - apiVersion: cert-manager.io/v1 kind: Issuer