Class: Dapp::Kube::Helm::Values

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/kube/helm/values.rb

Constant Summary collapse

TEMPLATE_EMPTY_VALUE =
"\\\"-\\\""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Values

Returns a new instance of Values.



115
116
117
# File 'lib/dapp/kube/helm/values.rb', line 115

def initialize(data={})
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



113
114
115
# File 'lib/dapp/kube/helm/values.rb', line 113

def data
  @data
end

Class Method Details

.service_values(*a, &b) ⇒ Object



7
8
9
# File 'lib/dapp/kube/helm/values.rb', line 7

def service_values(*a, &b)
  self.new(service_values_hash(*a, &b))
end

.service_values_hash(dapp, repo, namespace, docker_tag, fake: false, without_registry: false, disable_warnings: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dapp/kube/helm/values.rb', line 11

def service_values_hash(dapp, repo, namespace, docker_tag, fake: false, without_registry: false, disable_warnings: false)
  res = {
    "global" => {
      "namespace" => namespace,
      "dapp" => {
        "name" => dapp.name,
        "repo" => repo,
        "docker_tag" => docker_tag,
      }
    }
  }

  ci_info = {
    "is_tag" => false,
    "is_branch" => false,
    "branch" => TEMPLATE_EMPTY_VALUE,
    "tag" => TEMPLATE_EMPTY_VALUE,
    "ref" => TEMPLATE_EMPTY_VALUE
  }
  res["global"]["dapp"]["ci"] = ci_info

  if fake
  elsif ENV["CI_COMMIT_TAG"]
    ci_info["tag"] = ci_info["ref"] = ENV["CI_COMMIT_TAG"]
    ci_info["is_tag"] = true
  elsif ENV["CI_COMMIT_REF_NAME"]
    ci_info["branch"] = ci_info["ref"] = ENV["CI_COMMIT_REF_NAME"]
    ci_info["is_branch"] = true
  elsif dapp.git_own_repo_exist? and dapp.git_own_repo.branch != "HEAD"
    ci_info["branch"] = ci_info["ref"] = dapp.git_own_repo.branch
    ci_info["is_branch"] = true
  elsif dapp.git_own_repo_exist?
    git = dapp.git_own_repo.send(:git)

    tagref = git.references.find do |r|
      if r.name.start_with?("refs/tags/")
        if r.target.is_a? Rugged::Tag::Annotation
          r.target.target_id == git.head.target_id
        else
          r.target_id == git.head.target_id
        end
      end
    end

    if tagref
      tag = tagref.name.partition("refs/tags/").last
    else
      tag = git.head.target_id
    end

    ci_info["tag"] = ci_info["ref"] = tag
    ci_info["is_tag"] = true
  end

  dimgs = dapp.build_configs.map do |config|
    dapp.dimg(config: config, ignore_git_fetch: true, ignore_signature_auto_calculation: true)
  end.uniq do |dimg|
    dimg.name
  end

  dimgs.each do |dimg|
    dimg_data = {}
    if dimg.name
      res["global"]["dapp"]["is_nameless_dimg"] = false
      res["global"]["dapp"]["dimg"] ||= {}
      res["global"]["dapp"]["dimg"][dimg.name] = dimg_data
    else
      res["global"]["dapp"]["is_nameless_dimg"] = true
      res["global"]["dapp"]["dimg"] = dimg_data
    end

    dimg_labels = {}
    docker_image_id = TEMPLATE_EMPTY_VALUE
    unless fake || without_registry
      begin
        dimg_labels = dapp.dimg_registry(repo).image_labels(docker_tag, dimg.name)
        docker_image_id = dapp.dimg_registry(repo).image_id(docker_tag, dimg.name)
      rescue ::Dapp::Dimg::Error::Registry => err
        unless disable_warnings
          dapp.log_warning "Registry `#{err.net_status[:data][:registry]}` is not availabble: cannot determine <dimg>.docker_image_id and <dimg>.git.<ga>.commit_id helm values of dimg#{dimg.name ? " `#{dimg.name}`" : nil}"
        end
      end
    end

    dimg_data["docker_image"] = [[repo, dimg.name].compact.join("/"), docker_tag].join(":")
    dimg_data["docker_image_id"] = docker_image_id

    [*dimg.local_git_artifacts, *dimg.remote_git_artifacts].each do |ga|
      if ga.as
        commit_id = dimg_labels[dapp.dimgstage_g_a_commit_label(ga.paramshash)] || TEMPLATE_EMPTY_VALUE

        dimg_data["git"] ||= {}
        dimg_data["git"][ga.as] ||= {}
        dimg_data["git"][ga.as]["commit_id"] = commit_id
      end
    end
  end

  res
end

Instance Method Details

#as_set_optionsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dapp/kube/helm/values.rb', line 119

def as_set_options
  options = {}

  queue = [[nil, data]]

  loop do
    option_key, hash = queue.shift
    break unless hash

    hash.each do |k, v|
      new_option_key = [option_key, k].compact.join(".")
      if v.is_a? Hash
        queue << [new_option_key, v]
      else
        options[new_option_key] = v
      end
    end
  end

  options
end

#to_set_optionsObject



141
142
143
# File 'lib/dapp/kube/helm/values.rb', line 141

def to_set_options
  as_set_options.map {|k, v| "--set #{k}=#{v}"}
end