Skip to content

Conversation

LimKianAn
Copy link
Contributor

@LimKianAn LimKianAn commented Mar 5, 2021

@majst01 @eberlep
This branch shows the possibility of having the items in ConfigMap aligned with yaml manifests of target golang struct and then unmarshal them in the code so that we avoid setting the values manually. We might not need to merge this branch into main, but it serves as an example for further discussion.

@LimKianAn LimKianAn marked this pull request as draft March 5, 2021 20:35
@LimKianAn LimKianAn mentioned this pull request Mar 8, 2021
Base automatically changed from sidecars to main March 9, 2021 15:00
@LimKianAn LimKianAn marked this pull request as ready for review March 17, 2021 19:11
@LimKianAn
Copy link
Contributor Author

Ready for a merge if this direction is appreciated: Shift the logic of global sidecars configmap to helm yaml.

@LimKianAn LimKianAn requested a review from eberlep March 17, 2021 19:13
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 }}
per-namespace-sidecars-configmap-name: {{ .Values.sidecars.perNamespaceConfigmapName | quote }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe move this into the operatormanager

Copy link
Contributor Author

@LimKianAn LimKianAn Mar 18, 2021

Choose a reason for hiding this comment

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

We still need the variable here and here. By the linkage of per-namespace-sidecars-configmap-name go code and yaml share a single source of truth.

Copy link
Contributor Author

@LimKianAn LimKianAn Mar 18, 2021

Choose a reason for hiding this comment

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

This item is removed and instead derived dynamically in go code.


sccm := &v1.ConfigMap{}
if err := m.SetName(sccm, pg.SidecarsCMName); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe move this var to operatormanager.

Copy link
Contributor Author

@LimKianAn LimKianAn Mar 18, 2021

Choose a reason for hiding this comment

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

As above, when go code and yaml share a variable, use the one in the yaml.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As above, the name is dynamically derived.

Copy link
Collaborator

Choose a reason for hiding this comment

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

now it's getting even more complicated? we create that configmap ourselves, and then derive it's name from another config we also created?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Final decision: Use hard-coded const.

Protocol: v1.ProtocolTCP,
TargetPort: intstr.IntOrString{
Type: intstr.String,
StrVal: "exporter",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, maybe derive this value from the container definition? If someone changes the configmap an renames this port there, this service won't work anymore...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The configurable .Values.sidecars.exporter.containerPort in values.yaml is used now.

// Deal with dynamically assigned name
for i := range sidecars {
for j := range sidecars[i].Env {
if sidecars[i].Env[j].Name == "DATA_SOURCE_PASS" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this check should be more general: If there is a sidecars[i].Env[j].ValueFrom, we should then check if it is of the kind SecretKeyRef, and if so, we should proceed.

This implementation is just a workaround for one specific case.

@LimKianAn LimKianAn requested a review from eberlep March 18, 2021 20:31
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.

@@ -683,3 +703,19 @@ func (m *OperatorManager) UpdateAllOperators(ctx context.Context) error {
m.log.Info("Done updating postgres operators in managed namespaces")
return nil
}

func perNamespaceSidecarsConfigMapName(cm *v1.ConfigMap) string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should also talk about this, as already mentioned above. that looks too complicated to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As above, use hard-coded const instead.


sccm := &v1.ConfigMap{}
if err := m.SetName(sccm, pg.SidecarsCMName); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

now it's getting even more complicated? we create that configmap ourselves, and then derive it's name from another config we also created?

@LimKianAn
Copy link
Contributor Author

LimKianAn commented Mar 19, 2021

Tests:

  • 3 containers in DB pod are running. No errors in each log eventually.
  • Service postgres-exporter is created. One can create an in-cluster curl pod to pin the end-point and get html response.

Thus, merge this PR.

@LimKianAn LimKianAn merged commit b84c71d into main Mar 19, 2021
@LimKianAn LimKianAn deleted the sidecar-experimental branch March 19, 2021 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants