Class: Kustomize::Transform::CommonLabelsTransform

Inherits:
Kustomize::Transform show all
Includes:
Accessory
Defined in:
lib/kustomize/transform/common_labels_transform.rb

Constant Summary collapse

LENSES_FOR_ALL =
[
  Lens["metadata", "labels"]
]
LENSES_FOR_KIND =
{
  "Service" => [
    Lens["spec", "selector"]
  ],

  "Deployment" => [
    Lens["spec", "selector", "matchLabels"]
  ],
}

Instance Method Summary collapse

Methods inherited from Kustomize::Transform

create, #inspect, #rewrite_all

Constructor Details

#initialize(new_labels) ⇒ CommonLabelsTransform

Returns a new instance of CommonLabelsTransform.



8
9
10
# File 'lib/kustomize/transform/common_labels_transform.rb', line 8

def initialize(new_labels)
  @new_labels = new_labels
end

Instance Method Details

#rewrite(rc_doc) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kustomize/transform/common_labels_transform.rb', line 26

def rewrite(rc_doc)
  rc_kind = rc_doc['kind']
  use_lenses = LENSES_FOR_ALL

  if lenses_for_doc_kind = LENSES_FOR_KIND[rc_kind]
    use_lenses += lenses_for_doc_kind
  end

  use_lenses.inject(rc_doc) do |doc, lens|
    lens.update_in(doc) do |annots|
      [:set, (annots || {}).merge(@new_labels)]
    end
  end
end