Class: Kustomize::Transform::RefFixupTransform

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

Constant Summary collapse

SUFFIX_JOINER =
"-"
NAME_LENS =
Lens["metadata", "name"]
FINGERPRINT_LENS =
Lens['metadata', 'annotations', 'kustomizer.covalenthq.com/effective-fingerprint']
POD_TEMPLATE_LENSES =
[
    Lens["spec", "template", "spec", "containers", Access.all, "envFrom", Access.all, "configMapRef", "name"],
    Lens["spec", "template", "spec", "containers", Access.all, "env", Access.all, "valueFrom", "configMapKeyRef", "name"],
    Lens["spec", "template", "spec", "volumes", Access.all, "configMap", "name"],

    Lens["spec", "template", "spec", "containers", Access.all, "env", Access.all, "valueFrom", "secretKeyRef", "name"],
    Lens["spec", "template", "spec", "volumes", Access.all, "secret", "name"],
    Lens["spec", "template", "spec", "volumes", Access.all, "secret", "secretName"]
]
CRONJOB_TEMPLATE_LENSES =
POD_TEMPLATE_LENSES.map do |lens|
  Lens["spec", "jobTemplate"] + lens
end
KEY_REF_LENSES_BY_KIND =
{
  "Deployment" => POD_TEMPLATE_LENSES,
  "StatefulSet" => POD_TEMPLATE_LENSES,
  "DaemonSet" => POD_TEMPLATE_LENSES,
  "Job" => POD_TEMPLATE_LENSES,
  "CronJob" => CRONJOB_TEMPLATE_LENSES
}

Instance Method Summary collapse

Methods inherited from Kustomize::Transform

create, #inspect, #rewrite

Instance Method Details

#rewrite_all(rcs) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kustomize/transform/ref_fixup_transform.rb', line 39

def rewrite_all(rcs)
  ref_fixups =
    rcs.flat_map do |rc|
      fingerprint = FINGERPRINT_LENS.get_in(rc)
      next([]) unless fingerprint

      suffixed_name = NAME_LENS.get_in(rc)
      base_name = suffixed_name.gsub(/-#{fingerprint}$/, '')
      [[base_name, suffixed_name]]
    end.to_h

  rcs.map do |rc|
    key_ref_lenses = KEY_REF_LENSES_BY_KIND[rc['kind']]
    next(rc) unless key_ref_lenses

    key_ref_lenses.inject(rc) do |doc, lens|
      lens.update_in(doc) do |base_name|
        if suffixed_name = ref_fixups[base_name]
          [:set, suffixed_name]
        else
          :keep
        end
      end
    end
  end
end