summaryrefslogtreecommitdiff
path: root/lib/gitlab/kubernetes/helm/pod.rb
blob: ca5e06009fa318b6fc2ea50c51827c455758e8b8 (plain)
1
2
3
4
5
6
7
8
9
10
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
module Gitlab
  module Kubernetes
    module Helm
      class Pod
        def initialize(command, namespace_name, kubeclient)
          @command = command
          @namespace_name = namespace_name
          @kubeclient = kubeclient
        end

        def generate
          spec = { containers: [container_specification], restartPolicy: 'Never' }

          if command.chart_values_file
            create_config_map
            spec[:volumes] = volumes_specification
          end

          ::Kubeclient::Resource.new(metadata: metadata, spec: spec)
        end

        private

        attr_reader :command, :namespace_name, :kubeclient

        def container_specification
          container = {
            name: 'helm',
            image: 'alpine:3.6',
            env: generate_pod_env(command),
            command: %w(/bin/sh),
            args: %w(-c $(COMMAND_SCRIPT))
          }
          container[:volumeMounts] = volume_mounts_specification if command.chart_values_file
          container
        end

        def labels
          {
            'gitlab.org/action': 'install',
            'gitlab.org/application': command.name
          }
        end

        def metadata
          {
            name: command.pod_name,
            namespace: namespace_name,
            labels: labels
          }
        end

        def volume_mounts_specification
          [
            {
              name: 'configuration-volume',
              mountPath: "/data/helm/#{command.name}/config"
            }
          ]
        end

        def volumes_specification
          [
            {
              name: 'configuration-volume',
              configMap: {
                name: "values-content-configuration-#{command.name}",
                items: [{ key: 'values', path: 'values.yaml' }]
              }
            }
          ]
        end

        def generate_pod_env(command)
          {
            HELM_VERSION: Gitlab::Kubernetes::Helm::HELM_VERSION,
            TILLER_NAMESPACE: namespace_name,
            COMMAND_SCRIPT: command.generate_script(namespace_name)
          }.map { |key, value| { name: key, value: value } }
        end

        def create_config_map
          resource = ::Kubeclient::Resource.new
          resource.metadata = {
            name: "values-content-configuration-#{command.name}",
            namespace: namespace_name,
            labels: { name: "values-content-configuration-#{command.name}" }
          }
          resource.data = { values: File.read(command.chart_values_file) }
          kubeclient.create_config_map(resource)
        end
      end
    end
  end
end