kontena / k8s-client

Ruby Kubernetes API client
Apache License 2.0
76 stars 26 forks source link

Recursive compact does not seem to be recursive #136

Closed kke closed 5 years ago

kke commented 5 years ago
    # Recursive compact for Hash/Array
    #
    # @param hash_or_array [Hash,Array]
    # @return [Hash,Array]
    def self.recursive_compact(hash_or_array)
      p = proc do |*args|
        v = args.last
        v.delete_if(&p) if v.respond_to?(:delete_if) && !v.is_a?(Array)
        v.nil? || v.respond_to?(:empty?) && (v.empty? && (v.is_a?(Hash) || v.is_a?(Array)))
      end

      hash_or_array.delete_if(&p)
    end

It's weird and I don't see how it would go deeper than one level.

kke commented 5 years ago

I don't understand the "k8s-style":

  describe '#recursive_compact' do
    it 'compacts hashes recursively with k8s style' do
      hash = YAML.load('
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: web-allow-external
spec:
  podSelector:
    matchLabels:
      app: web
      foo: ""
      bar: {}
      baz: []
  ingress:
  - from: []
')
      expect(described_class.recursive_compact(hash)).to eq({
        "apiVersion"=>"networking.k8s.io/v1",
        "kind"=>"NetworkPolicy",
        "metadata"=>{"name"=>"web-allow-external"},
        "spec"=>{
          "podSelector"=>{
            "matchLabels"=>{
              "app"=>"web",
              "foo"=>""
            }
          },
          "ingress"=>[{
            "from"=>[]
          }]
        }
      })
    end
  end

Why did spec.podSelector.bar/baz get dropped but spec.ingress.*.from was not? Where is this style documented?

kke commented 5 years ago

I don't think it actually makes much difference. It's only used for storing the last-applied-configuration for stacks.

Looking at kube docs:

image

There's an empty hash in there.

kke commented 5 years ago

Does not matter