diff --git a/api/v1alpha1/types.go b/api/v1alpha1/types.go index a537b1a9..4bb1c7eb 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 @@ -74,6 +77,9 @@ type TLSProperties struct { // 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 c5c0804e..6610045b 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" ) @@ -43,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. @@ -337,6 +343,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..928468b3 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 @@ -6024,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 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/data/parameters.yaml b/pkg/operator/data/parameters.yaml index 1665b162..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: 876000h + duration: {{ .tls.certManager.duration | default "876000h" }} {{- if not .tls.certManager.issuer }} - apiVersion: cert-manager.io/v1 kind: Issuer diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 7cbdfc9e..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)