Skip to content

Commit

Permalink
Support template parameters in configmap/secret customData
Browse files Browse the repository at this point in the history
Template parameter expansion has only been supported in template
files themselves, and not in any other sources supplied in the
customData. This patch extends support for expanding parameters
in all customData strings.

The intent is to support template parameters in customServiceConfig
data. A common use case is when a service's config parameter needs
to be set to a value that can be templated, e.g. the service's
own password.

Consider the glance service when it's configured to use cinder
for a backend. The final configuration needs to specify the
'cinder_store_password', which currently requires two things.

1. The cloud admin will need to create a secret containing the
   setting, and reference it in the customServiceConfigSecrets.
2. The cloud admin will need to track down the actual value
   (i.e. glance's password) in order to put it in the secret.

An alternative approach would allow the cloud admin to use a
template parameter, whereby glance's CR could reference the
password like this:

    glance:
      template:
        customServiceConfig: |
          [cinder_backend]
          cinder_store_password = {{ .ServicePassword }}

The only restriction is the service's controller must support the
parameter, meaning it must include it in its templateParameters.
If any error occurs when expanding a customData string, an INFO
message logged and the original string is retained (no expansion).
For example, if a CR references a parameter that isn't supported,
the log message will state:

Skipped customData expansion due to: template: tmp:x:y: \
  executing "tmp" at <.BadParam>: map has no entry for key "BadParam"
  • Loading branch information
ASBishop committed Oct 18, 2023
1 parent e8a0540 commit eb060f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion modules/common/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ func createOrPatchConfigMap(
// Note: this can overwrite data rendered from GetTemplateData() if key is same
if len(cm.CustomData) > 0 {
for k, v := range cm.CustomData {
configMap.Data[k] = v
v_expanded, err := util.ExecuteTemplateData(v, cm.ConfigOptions)
if err == nil {
configMap.Data[k] = v_expanded
} else {
h.GetLogger().Info(fmt.Sprintf("Skipped customData expansion due to: %s", err))
configMap.Data[k] = v
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion modules/common/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ func createOrUpdateSecret(
// Note: this can overwrite data rendered from GetTemplateData() if key is same
if len(st.CustomData) > 0 {
for k, v := range st.CustomData {
dataString[k] = v
v_expanded, err := util.ExecuteTemplateData(v, st.ConfigOptions)
if err == nil {
dataString[k] = v_expanded
} else {
h.GetLogger().Info(fmt.Sprintf("Skipped customData expansion due to: %s", err))
dataString[k] = v
}
}
}

Expand Down

0 comments on commit eb060f9

Please sign in to comment.