Skip to content

PoC of new sidecars definition #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
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
33 changes: 18 additions & 15 deletions api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor

// skip if the configmap does not exist
if c != nil {
z.Spec.AdditionalVolumes = p.buildAdditionalVolumes(c)
z.Spec.Sidecars = p.buildSidecars(c)
_ = p.buildAdditionalVolumes(c) // TODO temporarily disable additional volumes as well
_ = p.buildSidecars(c) // TODO temporaily disabled sidecar definition in custom ressource
}

if p.HasSourceRanges() {
Expand Down Expand Up @@ -717,19 +717,22 @@ func (p *Postgres) buildSidecars(c *corev1.ConfigMap) []zalando.Sidecar {

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

// 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"
break
}
}
}
// if err := yaml.Unmarshal([]byte(c.Data["sidecars"]), &sidecars); err != nil {
// return nil
// }

// // 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"
// break
// }
// }
// }

// TODO only use envs here, leave the rest to the postgres-operator configmap?
// TODO also set PG_EXPORTER_CONSTANT_LABELS with partitionid and postgres cluster name

return sidecars
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/operatormanager/operatormanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ type OperatorManager struct {
log logr.Logger
meta.MetadataAccessor
*runtime.Scheme
options Options
options Options
globalSidecarsCM *corev1.ConfigMap
}

// New creates a new `OperatorManager`
Expand Down Expand Up @@ -415,6 +416,12 @@ func (m *OperatorManager) editConfigMap(cm *corev1.ConfigMap, namespace string,
cm.Data["replication_username"] = "standby"

cm.Data["enable_pod_antiaffinity"] = strconv.FormatBool(options.PodAntiaffinity)

if sidecarsCM, err := m.getSidecarsCM(); err == nil && sidecarsCM != nil {
cm.Data["sidecars"] = sidecarsCM.Data["sidecars"]
cm.Data["enable_sidecars"] = strconv.FormatBool(true)
}

}

// ensureCleanMetadata ensures obj has clean metadata
Expand Down Expand Up @@ -618,3 +625,22 @@ func (m *OperatorManager) UpdateAllOperators(ctx context.Context) error {
m.log.Info("Done updating postgres operators in managed namespaces")
return nil
}

func (m *OperatorManager) getSidecarsCM() (*corev1.ConfigMap, error) {
// return cached version
if m.globalSidecarsCM != nil {
return m.globalSidecarsCM, nil
}

// try to fetch the global sidecars configmap
cns := types.NamespacedName{
Namespace: m.options.PostgresletNamespace,
Name: m.options.SidecarsConfigMapName,
}
m.globalSidecarsCM = &corev1.ConfigMap{}
if err := m.Get(context.Background(), cns, m.globalSidecarsCM); err != nil {
// configmap with configuration does not exists, nothing we can do here...
return nil, fmt.Errorf("could not fetch config for sidecars")
}
return m.globalSidecarsCM, nil
}