kubeclient icon indicating copy to clipboard operation
kubeclient copied to clipboard

Any call that use entity config should symbolize its keys

Open motymichaely opened this issue 9 years ago • 10 comments

Hey,

There's a potential issue with calls that use entity_config (like here). They all assume entity_config has symbolized keys.

I'd propose normalizing entity_config hash by symbolizing its keys:

def normalize_entity_config(entity_config)
  entity_config.to_hash.each_with_object({}) { |(k,v),o| o[k.to_sym] = v }
end

Any rejection for suggesting a pull request?

Thanks!

motymichaely avatar Sep 07 '16 09:09 motymichaely

cc @moolitayer

simon3z avatar Sep 07 '16 10:09 simon3z

RecursiveOpenStruct symbolizes keys for us: "Also, by default it will turn all hash keys into symbols internally:"

pod = {
  'metadata' => 
...
client.create_pod(pod) # will not work
client.create_pod(Kubeclient::Pod.new(pod)) # will not work
client.create_pod(Kubeclient::Pod.new(POD)) # will work

We should not re implement rails' deep_symbolize_keys

moolitayer avatar Sep 07 '16 12:09 moolitayer

@moolitayer Great. So I would suggest adding some info on this to the documentation.

For example:

namespace_str = <<-EOF
apiVersion: v1
kind: Namespace
metadata:
  name: "my-namespace"
  labels:
    name: "my-namespace"
EOF
client.create_namespace(YAML.load(namespace_yaml_str))

Produces this error:

NoMethodError: undefined method `[]' for nil:NilClass
    from /Users/moty/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/kubeclient-1.2.0/lib/kubeclient/common.rb:229:in `create_entity'
    from /Users/moty/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/kubeclient-1.2.0/lib/kubeclient/common.rb:116:in `block (2 levels) in define_entity_methods'

Based on your suggestion, this works:

client.create_namespace(Kubeclient::Namespace.new(YAML.load(namespace_str)))

motymichaely avatar Sep 07 '16 13:09 motymichaely

@motymichaely I'm good with a Readme change. I think the best we can do is to consistently advocate the usage of the generic Kubeclient::resource class post #185 throughout the Readme as well as all the tests. There are just too many ways being used in our example code now and that is confusing as well as the problem described in this issue.

In your example:

client.create_namespace(Kubeclient::Resource.new(YAML.load(namespace_str)))

@abonas @simon3z agree/disagree?

moolitayer avatar Sep 18 '16 17:09 moolitayer

@moolitayer :+1:

abonas avatar Sep 19 '16 02:09 abonas

@motymichaely interested in submitting a pr as per the above comment ?

moolitayer avatar Sep 19 '16 08:09 moolitayer

FYI Decided to go with .deep_symbolize_keys (rails) in our project... easier to debug and no more ObjectStruct bugs/overhead ...

grosser avatar Oct 04 '16 04:10 grosser

Now that we have as: :parsed_symbolized and as: :parsed (#306), kubeclient get_foo may give you data that isn't a Kubeclient::Resource, and in the latter case that you can't write back with create_foo/update_foo. Also, now that we use a single Kubeclient::Resource class everywhere, there's less value in forcing users to wrap everything in it...

=> I don't have bandwidth to work on this but would welcome a PR. @moolitayer are you still against?

cben avatar May 27 '18 14:05 cben

would prefer telling the user what they do wrong instead of doing work behind their back -> call .to_h on what they give in and check if first key is a symbol, otherwise ArgumentError

grosser avatar May 27 '18 16:05 grosser

hmm, good idea.

cben avatar May 27 '18 18:05 cben