Skip to content
Draft
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
32 changes: 0 additions & 32 deletions operator/charts/patroni-services/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,6 @@ K8s Platform envs
value: "https://kubernetes.default:443"
{{- end }}

{{/*
POSTGRES ADMIN env variables for DBaaS
*/}}
{{- define "postgres-dbaas.pgAdminEnvs" }}
- name: POSTGRES_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-credentials
key: password
- name: POSTGRES_ADMIN_USER
valueFrom:
secretKeyRef:
name: postgres-credentials
key: username
{{- end }}

{{/*
Aggregator Registration env variables for DBaaS
*/}}
{{- define "postgres-dbaas.aggregatorEnvsReg" }}
- name: DBAAS_AGGREGATOR_REGISTRATION_USERNAME
valueFrom:
secretKeyRef:
name: dbaas-aggregator-registration-credentials
key: username
- name: DBAAS_AGGREGATOR_REGISTRATION_PASSWORD
valueFrom:
secretKeyRef:
name: dbaas-aggregator-registration-credentials
key: password
{{- end }}

{{- define "find_image" -}}
{{- $image := .default -}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ spec:
configMap:
name: dbaas-postgres-adapter.extensions-config
defaultMode: 420
- name: dbaas-adapter-credentials
secret:
secretName: dbaas-adapter-credentials
- name: dbaas-adapter-registration-credentials
secret:
secretName: dbaas-adapter-registration-credentials
- name: postgres-credentials
secret:
secretName: postgres-credentials
{{- if not .Values.externalDataBase }}
{{- if and .Values.tls .Values.tls.enabled }}
- name: tls-cert
Expand Down Expand Up @@ -111,10 +120,8 @@ spec:
securityContext:
{{- include "restricted.globalContainerSecurityContext" . | nindent 12 }}
env:
{{- template "postgres-dbaas.pgAdminEnvs" . }}
- name: POSTGRES_DATABASE
value: {{ default "postgres" .Values.dbaas.dbName }}
{{- template "postgres-dbaas.aggregatorEnvsReg" . }}
- name: DBAAS_ADAPTER_ADDRESS
value: {{ default (printf "http://dbaas-postgres-adapter.%s:8080" .Release.Namespace) .Values.dbaas.adapter.address }}
- name: DBAAS_AGGREGATOR_REGISTRATION_ADDRESS
Expand All @@ -125,16 +132,6 @@ spec:
value: {{ include "dbaas.pgHostRO" . }}
- name: POSTGRES_PORT
value: {{ default "5432" .Values.dbaas.pgPort | quote }}
- name: DBAAS_ADAPTER_API_USER
valueFrom:
secretKeyRef:
name: dbaas-adapter-credentials
key: username
- name: DBAAS_ADAPTER_API_PASSWORD
valueFrom:
secretKeyRef:
name: dbaas-adapter-credentials
key: password
- name: DBAAS_AGGREGATOR_PHYSICAL_DATABASE_IDENTIFIER
value: {{ .Values.dbaas.aggregator.physicalDatabaseIdentifier | default (printf "%s:%s" .Release.Namespace "postgres")}}
- name: CLOUD_NAMESPACE
Expand Down Expand Up @@ -177,6 +174,15 @@ spec:
- name: tls-cert
mountPath: /certs/
{{- end }}
- name: dbaas-adapter-credentials
mountPath: /var/run/secrets/postgresql/dbaas-adapter-credentials
readOnly: true
- name: dbaas-adapter-registration-credentials
mountPath: /var/run/secrets/postgresql/dbaas-adapter-registration-credentials
readOnly: true
- name: postgres-credentials
mountPath: /var/run/secrets/postgresql/postgres-credentials
readOnly: true
{{- end }}
livenessProbe:
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ metadata:
name: logical-replication-controller-creds
data:
username: {{ default "replicator" .Values.replicationController.apiUser | b64enc }}
password: {{ default "paSsW0rdForReplicat!oN" .Values.replicationController.apiPassword | b64enc }}
password: {{ .Values.replicationController.apiPassword | b64enc }}
type: Opaque
{{ end }}
24 changes: 22 additions & 2 deletions operator/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ import (
"github.com/Netcracker/pgskipper-operator/pkg/util"
)

const (
secretsBasePath = "/var/run/secrets/postgresql/"

pgUserCredsPath = secretsBasePath + "postgres-credentials/"
)

var (
instance *PostgresClient
logger = util.GetLogger()
pgUser = flag.String("pg_user", getEnv("PG_ADMIN_USER", "postgres"), "Username of admin user in PostgreSQL, env: PG_ADMIN_USER")
pgPass = flag.String("pg_pass", getEnv("PG_ADMIN_PASSWORD", ""), "Password of admin user in PostgreSQL, env: PG_ADMIN_PASSWORD")
pgUser = flag.String("pg_user", ReadSecretFile(pgUserCredsPath+"username", "postgres"), "Username of admin user in PostgreSQL")
pgPass = flag.String("pg_pass", ReadSecretFile(pgUserCredsPath+"password", ""), "Password of admin user in PostgreSQL")
dbName = "postgres"
ssl = "off"
)
Expand Down Expand Up @@ -244,3 +250,17 @@ func getEnv(key, fallback string) string {
func EscapeString(str string) string {
return strings.ReplaceAll(str, "'", "''")
}

func ReadSecretFile(path, defaultVal string) string {
data, err := os.ReadFile(path)
if err != nil {
logger.Error(fmt.Sprintf("Failed to read secret file %s: %v", path, err))
return defaultVal
}
value := strings.TrimSpace(string(data))
if value == "" {
logger.Info(fmt.Sprintf("Secret file %s is empty, using default value", path))
return defaultVal
}
return value
}
99 changes: 45 additions & 54 deletions operator/pkg/deployment/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
)

var (
Expand All @@ -37,6 +38,7 @@ const (
MetricCollectorUserCredentials = "monitoring-credentials"
influxDbAdminCredentials = "influx-db-admin-credentials"
telegrafConfig = "telegraf-configmap"
PostgresUserCredentials = "postgres-credentials"
)

func NewMonitoringDeployment(metricCollector *netcrackerv1.MetricCollector, pgcluster string, serviceAccountName string) *appsv1.Deployment {
Expand Down Expand Up @@ -75,6 +77,33 @@ func NewMonitoringDeployment(metricCollector *netcrackerv1.MetricCollector, pgcl
},
},
},
{
Name: "monitoring-user-credentials",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: MetricCollectorUserCredentials,
DefaultMode: ptr.To[int32](0400),
},
},
},
{
Name: "influx-db-admin-credentials",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: influxDbAdminCredentials,
DefaultMode: ptr.To[int32](0400),
},
},
},
{
Name: "postgres-credentials",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "postgres-credentials",
DefaultMode: ptr.To[int32](0400),
},
},
},
},
InitContainers: []corev1.Container{},
Containers: []corev1.Container{
Expand All @@ -84,60 +113,6 @@ func NewMonitoringDeployment(metricCollector *netcrackerv1.MetricCollector, pgcl
Command: []string{},
Args: []string{},
Env: append([]corev1.EnvVar{
{
Name: "MONITORING_USER",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: MetricCollectorUserCredentials},
Key: "username",
},
},
},
{
Name: "MONITORING_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: MetricCollectorUserCredentials},
Key: "password",
},
},
},
{
Name: "PG_ROOT_USER",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: GetRootSecretName(pgcluster)},
Key: "username",
},
},
},
{
Name: "PG_ROOT_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: GetRootSecretName(pgcluster)},
Key: "password",
},
},
},
{
Name: "INFLUXDB_USER",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: influxDbAdminCredentials},
Key: "username",
},
},
},
{
Name: "INFLUXDB_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: influxDbAdminCredentials},
Key: "password",
},
},
},
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
Expand Down Expand Up @@ -197,6 +172,21 @@ func NewMonitoringDeployment(metricCollector *netcrackerv1.MetricCollector, pgcl
SubPath: "telegraf_temp.conf",
Name: "telegraf-config-volume",
},
{
MountPath: "/var/run/secrets/postgresql/monitoring-user-credentials",
Name: "monitoring-user-credentials",
ReadOnly: true,
},
{
MountPath: "/var/run/secrets/postgresql/influx-db-admin-credentials",
Name: "influx-db-admin-credentials",
ReadOnly: true,
},
{
MountPath: "/var/run/secrets/postgresql/postgres-credentials",
Name: "postgres-credentials",
ReadOnly: true,
},
},
Resources: *metricCollector.Resources,
LivenessProbe: &corev1.Probe{
Expand Down Expand Up @@ -232,6 +222,7 @@ func NewMonitoringDeployment(metricCollector *netcrackerv1.MetricCollector, pgcl
},
},
}

if metricCollector.PriorityClassName != "" {
deployment.Spec.Template.Spec.PriorityClassName = metricCollector.PriorityClassName
}
Expand Down
29 changes: 24 additions & 5 deletions operator/pkg/queryexporter/query_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)

const CMName = "query-exporter-config"
const (
CMName = "query-exporter-config"

secretsBasePath = "/var/run/secrets/postgresql/"

pgUserCredsPath = secretsBasePath + "postgres-credentials/"
)

var (
logger = util.GetLogger()
Expand Down Expand Up @@ -106,6 +112,14 @@ func getVolumes() []corev1.Volume {
},
},
},
{
Name: "postgresql-credentials",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "postgresql-credentials",
},
},
},
}
}

Expand All @@ -115,6 +129,10 @@ func getVolumeMounts() []corev1.VolumeMount {
MountPath: "/config",
Name: "config-volume",
},
{
MountPath: "/var/run/secrets/postgresql/",
Name: "postgresql-credentials",
},
}
}

Expand Down Expand Up @@ -152,13 +170,14 @@ func getEnvVariables(spec v1.QueryExporter) []corev1.EnvVar {
Name: "QUERY_EXPORTER_DISABLE_SELF_MONITOR",
Value: strconv.FormatBool(spec.SelfMonitorDisabled),
},
// todo: read credentials from secret
{
Name: "POSTGRES_USER",
ValueFrom: getSecretFieldEnv("username"),
Name: "POSTGRES_USER",
Value: util.ReadSecretFile(pgUserCredsPath+"username", "postgres"),
},
{
Name: "POSTGRES_PASSWORD",
ValueFrom: getSecretFieldEnv("password"),
Name: "POSTGRES_PASSWORD",
Value: util.ReadSecretFile(pgUserCredsPath+"password", ""),
},
{
Name: "EXCLUDED_QUERIES",
Expand Down
18 changes: 4 additions & 14 deletions operator/pkg/replicationcontroller/replication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,12 @@ func NewRCDeployment(cr v1.PatroniServices, sa, clusterName string, pgPort int)
Value: strconv.Itoa(pgPort),
},
{
Name: "POSTGRES_ADMIN_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "postgres-credentials"},
Key: "username",
},
},
Name: "POSTGRES_ADMIN_USER",
Value: util.ReadSecretFile(util.PgUserCredsPath+"username", "postgres"),
},
{
Name: "POSTGRES_ADMIN_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "postgres-credentials"},
Key: "password",
},
},
Name: "POSTGRES_ADMIN_PASSWORD",
Value: util.ReadSecretFile(util.PgUserCredsPath+"password", ""),
},
{
Name: "API_USER",
Expand Down
Loading
Loading