Class: Kube::Cluster::Middleware::SetNamespace

Inherits:
Kube::Cluster::Middleware show all
Defined in:
lib/kube/cluster/middleware/set_namespace.rb

Overview

Sets metadata.namespace on all namespace-scoped resources, skipping HelmCharts and resources that already have a non-default namespace set.

use SetNamespace, 'authelia'

Constant Summary

Constants inherited from Kube::Cluster::Middleware

DEFAULT_FILTER

Instance Method Summary collapse

Methods inherited from Kube::Cluster::Middleware

build, #filter

Constructor Details

#initialize(namespace) ⇒ SetNamespace

Returns a new instance of SetNamespace.



16
17
18
19
# File 'lib/kube/cluster/middleware/set_namespace.rb', line 16

def initialize(namespace)
  super(filter: ->(r) { r.kind != 'HelmChart' })
  @namespace = namespace
end

Instance Method Details

#call(manifest) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kube/cluster/middleware/set_namespace.rb', line 21

def call(manifest)
  manifest.resources.map! { |resource|
    filter(resource) {
      next resource if resource.cluster_scoped?

      h = resource.to_h
      h[:metadata] ||= {}

      unless h[:metadata][:namespace] && h[:metadata][:namespace] != 'default'
        h[:metadata][:namespace] = @namespace
      end

      # A RoleBinding's ServiceAccount subjects need an explicit
      # namespace; fill in any left blank with the target namespace so
      # same-namespace bindings (e.g. ServiceAccountWithRole) resolve.
      if h[:kind] == 'RoleBinding' && h[:subjects].is_a?(Array)
        h[:subjects] = h[:subjects].map { |subject|
          if subject[:kind] == 'ServiceAccount' &&
             (subject[:namespace].nil? || subject[:namespace].to_s.empty?)
            subject.merge(namespace: @namespace)
          else
            subject
          end
        }
      end

      resource.rebuild(h)
    }
  }
end