summaryrefslogtreecommitdiff
path: root/lib/gitlab/kubernetes/helm.rb
blob: 7a50f07f3c5535fad69ff23302f78c909f0243d3 (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
96
module Gitlab
  module Kubernetes
    class Helm
      HELM_VERSION = '2.7.0'.freeze
      NAMESPACE = 'gitlab-managed-apps'.freeze
      INSTALL_DEPS = <<-EOS.freeze
        set -eo pipefail
        apk add -U ca-certificates openssl >/dev/null
        wget -q -O - https://kubernetes-helm.storage.googleapis.com/helm-v${HELM_VERSION}-linux-amd64.tar.gz | tar zxC /tmp >/dev/null
        mv /tmp/linux-amd64/helm /usr/bin/
      EOS

      InstallCommand = Struct.new(:name, :install_helm, :chart) do
        def pod_name
          "install-#{name}"
        end
      end

      def initialize(kubeclient)
        @kubeclient = kubeclient
        @namespace = Namespace.new(NAMESPACE, kubeclient)
      end

      def install(command)
        @namespace.ensure_exists!
        @kubeclient.create_pod(pod_resource(command))
      end

      ##
      # Returns Pod phase
      #
      # https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase
      #
      # values: "Pending", "Running", "Succeeded", "Failed", "Unknown"
      #
      def installation_status(pod_name)
        @kubeclient.get_pod(pod_name, @namespace.name).status.phase
      end

      def installation_log(pod_name)
        @kubeclient.get_pod_log(pod_name, @namespace.name).body
      end

      def delete_installation_pod!(pod_name)
        @kubeclient.delete_pod(pod_name, @namespace.name)
      end

      private

      def pod_resource(command)
        labels = { 'gitlab.org/action': 'install', 'gitlab.org/application': command.name }
        metadata = { name: command.pod_name, namespace: @namespace.name, labels: labels }
        container = {
          name: 'helm',
          image: 'alpine:3.6',
          env: generate_pod_env(command),
          command: %w(/bin/sh),
          args: %w(-c $(COMMAND_SCRIPT))
        }
        spec = { containers: [container], restartPolicy: 'Never' }

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

      def generate_pod_env(command)
        {
          HELM_VERSION: HELM_VERSION,
          TILLER_NAMESPACE: @namespace.name,
          COMMAND_SCRIPT: generate_script(command)
        }.map { |key, value| { name: key, value: value } }
      end

      def generate_script(command)
        [
            INSTALL_DEPS,
            helm_init_command(command),
            helm_install_command(command)
        ].join("\n")
      end

      def helm_init_command(command)
        if command.install_helm
          'helm init >/dev/null'
        else
          'helm init --client-only >/dev/null'
        end
      end

      def helm_install_command(command)
        return if command.chart.nil?

        "helm install #{command.chart} --name #{command.name} --namespace #{@namespace.name} >/dev/null"
      end
    end
  end
end