Skip to content
Merged
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
181 changes: 39 additions & 142 deletions api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package v1
import (
"fmt"
"reflect"
"strconv"

"regexp"

Expand All @@ -23,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand All @@ -43,16 +43,6 @@ const (
ManagedByLabelValue string = "postgreslet"
// PostgresFinalizerName Name of the finalizer to use
PostgresFinalizerName string = "postgres.finalizers.database.fits.cloud"
// SidecarsCMName Namem of the ConfigMap containing the config for the sidecars
SidecarsCMName string = "postgres-sidecars-configmap"
// SidecarsCMFluentBitConfKey Name of the key containing the fluent-bit.conf config file
SidecarsCMFluentBitConfKey string = "fluent-bit.conf"
// FluentBitSidecarName Defines the name of the fluent-bit sidecar
FluentBitSidecarName string = "postgres-fluentbit"
// SidecarsCMExporterQueriesKey Name of the key containing the queries.yaml config file
SidecarsCMExporterQueriesKey string = "queries.yaml"
// ExporterSidecarName Defines the name of the postgres exporter sidecar
ExporterSidecarName string = "postgres-exporter"
// CreatedByAnnotationKey is used to store who in person created this database
CreatedByAnnotationKey string = "postgres.database.fits.cloud/created-by"
// BackupConfigLabelName if set to true, this secret stores the backupConfig
Expand All @@ -61,6 +51,13 @@ const (
BackupConfigKey = "config"
)

var (
ZalandoPostgresqlTypeMeta = metav1.TypeMeta{
APIVersion: "acid.zalan.do/v1",
Kind: "postgresql",
}
)

// BackupConfig defines all properties to configure backup of a database.
// This config is stored in the data section under the key BackupConfigKey as json payload.
type BackupConfig struct {
Expand Down Expand Up @@ -93,71 +90,6 @@ type BackupConfig struct {
S3EncryptionKey *string `json:"s3encryptionkey,omitempty"`
}

var (
ZalandoPostgresqlTypeMeta = metav1.TypeMeta{
APIVersion: "acid.zalan.do/v1",
Kind: "postgresql",
}

additionalVolumes = []zalando.AdditionalVolume{
{
Name: "empty",
MountPath: "/opt/empty",
TargetContainers: []string{
"all",
},
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "postgres-exporter-configmap",
MountPath: "/metrics",
TargetContainers: []string{
ExporterSidecarName,
},
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: SidecarsCMName,
},
Items: []corev1.KeyToPath{
{
Key: SidecarsCMExporterQueriesKey,
Path: "queries.yaml",
},
},
},
},
},
{
Name: "postgres-fluentbit-configmap",
MountPath: "/fluent-bit/etc",
TargetContainers: []string{
FluentBitSidecarName,
},
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: SidecarsCMName,
},
Items: []corev1.KeyToPath{
{
Key: SidecarsCMFluentBitConfKey,
Path: "fluent-bit.conf",
},
},
},
},
},
}

ExporterSidecarPortName intstr.IntOrString = intstr.IntOrString{
Type: intstr.String,
StrVal: "exporter",
}
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Tenant",type=string,JSONPath=`.spec.tenant`
Expand Down Expand Up @@ -486,7 +418,7 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor

// skip if the configmap does not exist
if c != nil {
z.Spec.AdditionalVolumes = additionalVolumes
z.Spec.AdditionalVolumes = p.buildAdditionalVolumes(c)
z.Spec.Sidecars = p.buildSidecars(c)
}

Expand Down Expand Up @@ -575,77 +507,42 @@ func init() {
SchemeBuilder.Register(&Postgres{}, &PostgresList{})
}

func (p *Postgres) buildAdditionalVolumes(c *corev1.ConfigMap) []zalando.AdditionalVolume {
if c == nil {
// abort if the global configmap is not there
return nil
}

// Unmarshal yaml-string of additional volumes
volumes := []zalando.AdditionalVolume{}
if err := yaml.Unmarshal([]byte(c.Data["additional-volumes"]), &volumes); err != nil {
return nil
}

return volumes
}

func (p *Postgres) buildSidecars(c *corev1.ConfigMap) []zalando.Sidecar {
if c == nil {
// abort if the global configmap is not there
return nil
}

exporterContainerPort, error := strconv.ParseInt(c.Data["postgres-exporter-container-port"], 10, 32)
if error != nil {
// todo log error
exporterContainerPort = 9187
// Unmarshal yaml-string of exporter
sidecars := []zalando.Sidecar{}
if err := yaml.Unmarshal([]byte(c.Data["sidecars"]), &sidecars); err != nil {
return nil
}
return []zalando.Sidecar{
{
Name: ExporterSidecarName,
DockerImage: c.Data["postgres-exporter-image"],
Ports: []corev1.ContainerPort{
{
Name: ExporterSidecarPortName.StrVal,
ContainerPort: int32(exporterContainerPort),
Protocol: corev1.ProtocolTCP,
},
},
Resources: zalando.Resources{
ResourceLimits: zalando.ResourceDescription{
CPU: c.Data["postgres-exporter-limits-cpu"],
Memory: c.Data["postgres-exporter-limits-memory"],
},
ResourceRequests: zalando.ResourceDescription{
CPU: c.Data["postgres-exporter-requests-cpu"],
Memory: c.Data["postgres-exporter-requests-memory"],
},
},
Env: []corev1.EnvVar{
{
Name: "DATA_SOURCE_URI",
Value: "127.0.0.1:5432/postgres?sslmode=disable",
},
{
Name: "DATA_SOURCE_USER",
Value: "postgres",
},
{
Name: "DATA_SOURCE_PASS",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "postgres." + p.ToPeripheralResourceName() + ".credentials",
},
Key: "password",
},
},
},
{
Name: "PG_EXPORTER_EXTEND_QUERY_PATH",
Value: "/metrics/queries.yaml",
},
},
},
{
Name: FluentBitSidecarName,
DockerImage: c.Data["postgres-fluentbit-image"],
Resources: zalando.Resources{
ResourceLimits: zalando.ResourceDescription{
CPU: c.Data["postgres-fluentbit-limits-cpu"],
Memory: c.Data["postgres-fluentbit-limits-memory"],
},
ResourceRequests: zalando.ResourceDescription{
CPU: c.Data["postgres-fluentbit-requests-cpu"],
Memory: c.Data["postgres-fluentbit-requests-memory"],
},
},
},

// Deal with dynamically assigned name
for i := range sidecars {
for j := range sidecars[i].Env {
if sidecars[i].Env[j].ValueFrom != nil && sidecars[i].Env[j].ValueFrom.SecretKeyRef != nil {
sidecars[i].Env[j].ValueFrom.SecretKeyRef.Name = "postgres." + p.ToPeripheralResourceName() + ".credentials"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still hardcoded for this usecase. I would prefer if this was completely generic. The name of the Secret should move back into the YAML/ConfigMap (the operator already supports the use of variables, in this case that string would look something like postgres.{{ main.teamid }}-{{ main.name }}.credentials).

And while you're at it, you could also add ConfigMapKeyRef, FieldRef and ResourceFieldRef (https://pkg.go.dev/k8s.io/api@v0.20.4/core/v1?utm_source=gopls#EnvVarSource).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we talk about it: remove that complete logic and only use postgres.{{ main.teamid }}-{{ main.name }}.credentials in the ConfigMap

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, my bad: Try {cluster}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! The current setting in the ConfigMap is as follows: secret_name_template: '{username}.{cluster}.credentials'

We can't use this feature in this case.

break
}
}
}

return sidecars
}
76 changes: 63 additions & 13 deletions charts/postgreslet/templates/configmap-sidecars.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,70 @@
apiVersion: v1
data:
postgres-exporter-image: {{ .Values.sidecars.exporter.image | quote }}
postgres-exporter-container-port: {{ .Values.sidecars.exporter.containerPort | quote }}
postgres-exporter-service-port: {{ .Values.sidecars.exporter.servicePort | quote }}
postgres-exporter-requests-cpu: {{ .Values.sidecars.exporter.resources.requests.cpu | quote }}
postgres-exporter-requests-memory: {{ .Values.sidecars.exporter.resources.requests.memory | quote }}
postgres-exporter-limits-cpu: {{ .Values.sidecars.exporter.resources.limits.cpu | quote }}
postgres-exporter-limits-memory: {{ .Values.sidecars.exporter.resources.limits.memory | quote }}
postgres-fluentbit-image: {{ .Values.sidecars.fluentbit.image | quote }}
postgres-fluentbit-requests-cpu: {{ .Values.sidecars.fluentbit.resources.requests.cpu | quote }}
postgres-fluentbit-requests-memory: {{ .Values.sidecars.fluentbit.resources.requests.memory | quote }}
postgres-fluentbit-limits-cpu: {{ .Values.sidecars.fluentbit.resources.limits.cpu | quote }}
postgres-fluentbit-limits-memory: {{ .Values.sidecars.fluentbit.resources.limits.memory | quote }}
postgres-exporter-service-target-port: {{ .Values.sidecars.exporter.containerPort | quote }}
queries.yaml: {{ b64enc .Values.sidecars.exporter.queries }}
fluent-bit.conf: {{ b64enc .Values.sidecars.fluentbit.conf }}

sidecars: |
- name: postgres-exporter
image: {{ .Values.sidecars.exporter.image }}
ports:
- name: exporter
containerPort: {{ .Values.sidecars.exporter.containerPort }}
protocol: TCP
resources:
requests:
cpu: {{ .Values.sidecars.exporter.resources.requests.cpu }}
memory: {{ .Values.sidecars.exporter.resources.requests.memory }}
limits:
cpu: {{ .Values.sidecars.exporter.resources.limits.cpu }}
memory: {{ .Values.sidecars.exporter.resources.limits.memory }}
env:
- name: DATA_SOURCE_URI
value: 127.0.0.1:5432/postgres?sslmode=disable
- name: DATA_SOURCE_USER
value: postgres
- name: DATA_SOURCE_PASS
valueFrom:
secretKeyRef:
key: password
- name: PG_EXPORTER_EXTEND_QUERY_PATH
value: /metrics/queries.yaml
- name: postgres-fluentbit
image: {{ .Values.sidecars.fluentbit.image }}
resources:
requests:
cpu: {{ .Values.sidecars.fluentbit.resources.requests.cpu }}
memory: {{ .Values.sidecars.fluentbit.resources.requests.memory }}
limits:
cpu: {{ .Values.sidecars.fluentbit.resources.limits.cpu }}
memory: {{ .Values.sidecars.fluentbit.resources.limits.memory }}
additional-volumes: |
- name: empty
mountPath: /opt/empty
targetContainers:
- all
volumeSource:
emptyDir: {}
- name: postgres-exporter-configmap
mountPath: /metrics
targetContainers:
- postgres-exporter
volumeSource:
configMap:
name: postgres-sidecars-configmap
items:
- key: queries.yaml
path: queries.yaml
- name: postgres-fluentbit-configmap
mountPath: /fluent-bit/etc
targetContainers:
- postgres-fluentbit
volumeSource:
configMap:
name: postgres-sidecars-configmap
items:
- key: fluent-bit.conf
path: fluent-bit.conf
kind: ConfigMap
metadata:
name: {{ include "postgreslet.fullname" . }}-postgres-sidecars
Expand All @@ -24,4 +74,4 @@ metadata:
{{- toYaml . | nindent 4 }}
{{- end }}
labels:
{{- include "postgreslet.labels" . | nindent 4 }}
{{- include "postgreslet.labels" . | nindent 4 }}
Loading