Cannot set projectNamingStrategy in JCasC in helm chart

I’m using the role-strategy plugin, configure it with the help of the jenkins-configuration-as-code plugin and deploy from the official Helm chart v4.1.14. When using the role-strategy plugin, it is recommended to also set the projectNamingStrategy to roleBased.

When configuring it in JCasC though, I get the following error:

2022-08-08 13:46:39.788+0000 [id=93]    WARNING h.i.i.InstallUncaughtExceptionHandler#handleException: Caught unhandled exception with ID 41b013aa-e67b-4cc4-a9df-2f08b59387ae
io.jenkins.plugins.casc.ConfiguratorException: Found incompatible configuration elements YamlSource: /var/jenkins_home/casc_configs/role-strategy.yaml  in /var/jenkins_home/casc_configs/role-strategy.yaml, line 4,
 column 5:
        roleBased:
        ^
        at io.jenkins.plugins.casc.yaml.ErrorOnConflictMergeStrategy.merge(ErrorOnConflictMergeStrategy.java:21)
        at io.jenkins.plugins.casc.yaml.ErrorOnConflictMergeStrategy.merge(ErrorOnConflictMergeStrategy.java:46)
        at io.jenkins.plugins.casc.yaml.ErrorOnConflictMergeStrategy.merge(ErrorOnConflictMergeStrategy.java:46)
        at io.jenkins.plugins.casc.yaml.YamlUtils.merge(YamlUtils.java:47)
Caused: io.jenkins.plugins.casc.ConfiguratorException: Failed to read YamlSource: /var/jenkins_home/casc_configs/role-strategy.yaml
        at io.jenkins.plugins.casc.yaml.YamlUtils.merge(YamlUtils.java:51)
        at io.jenkins.plugins.casc.yaml.YamlUtils.loadFrom(YamlUtils.java:102)
        at io.jenkins.plugins.casc.ConfigurationAsCode.configureWith(ConfigurationAsCode.java:637)
...

The file /var/jenkins_home/casc_configs/role-strategy.yaml looks like this:

jenkins:
  projectNamingStrategy:
    roleBased:
      forceExistingJobs: true
  authorizationStrategy:
    roleBased:
      roles:
        global:
...

The issue seems to be, that the key is already set in templates/jcasc-config.yaml (it’s actually in the _helpers.tpl but used in the template) of the Helm chart. If I manually remove that line, applying the configs works. Strangely not even setting the same config projectNamingStrategy: "standard" works. It produces the same error.

Is there any known way of getting around this issue? Or is this a bug in the Helm chart or in the JCasC Yaml merging?

Does roleBased make sense for projectNamingStrategy? looking at JSON Schema incorrectly nests some fields · Issue #1636 · jenkinsci/configuration-as-code-plugin · GitHub (unrelated bug) it should be a string? which explains why its complainined about the nested roleBased inside projectNamingStrategy

I got the configuration by setting it in the UI and then looking at the JCasC Yaml export. So maybe this is an issue with the role-strategy plugin that is setting incompatible values there?

This is what CasC prints when viewing the configuration so it should be ok.

  projectNamingStrategy:
    roleBased:
      forceExistingJobs: false

PS: forceExisting: true doesn’t really makes sense for rolebased

I believe code wise, the export is based on @DataBoundSetter and @DataBoundGetters, which may actually not return the same value. The exporter is kinda a hinting tool, not a exact 1:1 output of what you need.

At least that’s my understanding.

I think the issue is more with the Yaml merging in JCasC.

The Helm chart renders a file jcasc-default-config.yaml with all default settings done in the values of the chart and mounts it to /var/jenkins_home/casc_configs/. This file contains projectNamingStrategy: "standard".

Additionally I have this extra file, that configures it as shown above.

If I (manually) remove the line from the default config, my config i successfully read and configures the strategy as defined. The issue seems to be merging Yaml files with String and Object values.

Unfortunately the Chart does not allow to overwrite that setting in the default config directly :confused:

Also I’m not entirely sure what the actual fix would be: Fix the merging? I guess this is done by another library and not JCasC itself, right? Or making the strategy configurable in the Chart? Or skip it in the default config, if it is set to empty?

I created a PR in the Helm chart repo: Make projectNamingStrategy in default config configurable by maxnitze · Pull Request #681 · jenkinsci/helm-charts · GitHub

1 Like