sous-chefs / consul

Development repository for the consul cookbook

Home Page:https://supermarket.chef.io/cookbooks/consul

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`services` definition throws "no implicit conversion of Symbol into Integer"

ikbenale opened this issue · comments

🗣️ Foreword

Thank for taking the time to fill this bug report fully. Without it we may not be able to fix the bug, and the issue may be closed without resolution.

👻 Brief Description

With a service definition like the following one:

consul_definition 'foo' do
  type 'services'
  parameters(
    [
      {
        id: "foo-master",
        name: "foo",
        tags: [
          'master'
        ],
      {
        id: "foo-replica",
        name: "foo",
        tags: [
          'replica'
        ]
      }
    ]
  )
end

We get following error:

           ================================================================================
           Error executing action `create` on resource 'consul_definition[consul]'
           ================================================================================
           
           TypeError
           ---------
           no implicit conversion of Symbol into Integer
           
           Cookbook Trace:
           ---------------
           /tmp/kitchen/cache/cookbooks/consul/libraries/consul_definition.rb:44:in `params_to_json'
           /tmp/kitchen/cache/cookbooks/consul/libraries/consul_definition.rb:63:in `block (3 levels) in <class:ConsulDefinition>'
           /tmp/kitchen/cache/cookbooks/consul/libraries/consul_definition.rb:62:in `block (2 levels) in <class:ConsulDefinition>'
           /tmp/kitchen/cache/cookbooks/poise/files/halite_gem/poise/helpers/subcontext_block.rb:54:in `instance_eval'
           /tmp/kitchen/cache/cookbooks/poise/files/halite_gem/poise/helpers/subcontext_block.rb:54:in `subcontext_block'
           /tmp/kitchen/cache/cookbooks/poise/files/halite_gem/poise/helpers/notifying_block.rb:67:in `notifying_block'
           /tmp/kitchen/cache/cookbooks/consul/libraries/consul_definition.rb:49:in `block in <class:ConsulDefinition>'

(full output at https://gitlab.com/gitlab-cookbooks/gitlab-patroni/-/jobs/698381424#L924)

🥞 Cookbook version

3.3.1

👩‍🍳 Chef-Infra Version

14.12.3

🎩 Platform details

Kitchen on Docker image ruby:2.6 on GitLab CI

Steps To Reproduce

Steps to reproduce the behavior:

  1. Have a consul_definition with services, as the one described above
  2. Run chef-client

🚓 Expected behavior

A consul definition file with the following contents is created:

{
  "services": [
    {
      "id": "foo-master",
      "name": "foo",
      "tags": [
        "master"
      ]
    },
    {
      "id": "foo-replica",
      "name": "foo",
      "tags": [
        "replica"
      ]
    }
  ]
}

➕ Additional context

Add any other context about the problem here. e.g. related issues or existing pull requests.

This used to work for us in previous versions. We've established that it breaks from v3.1.0 to v3.2.0, though we're not sure what change made it stop working https://github.com/sous-chefs/consul/compare/v3.1.0..v3.2.0.

We also tried with the latest version (v4.0.3) and we had the same issue. We can make our chef-client runs pass with the following patch, though we're not sure if it's optimal:

diff --git a/libraries/consul_definition.rb b/libraries/consul_definition.rb
index 2eefec6..bb406a7 100644
--- a/libraries/consul_definition.rb
+++ b/libraries/consul_definition.rb
@@ -37,7 +37,7 @@ module ConsulCookbook

       def params_to_json
         final_parameters = parameters
-        final_parameters = final_parameters.merge(name: name) if final_parameters[:name].nil?
+        final_parameters = final_parameters.merge(name: name) if %w(check service).include?(type) && final_parameters[:name].nil?
         JSON.pretty_generate(type => final_parameters)
       end

More details on this thread https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/9343#note_400279286 /cc @ahanselka

@eReGeBe could you please make a PR which includes this fix so we can see how this goes with tests?