Welcome to mirror list, hosted at ThFree Co, Russian Federation.

api.rb « helm « kubernetes « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63f3d863c61b764243f7092a95fb4a6c0e779459 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module Gitlab
  module Kubernetes
    module Helm
      class Api
        def initialize(kubeclient)
          @kubeclient = kubeclient
          @namespace = Gitlab::Kubernetes::Namespace.new(Gitlab::Kubernetes::Helm::NAMESPACE, kubeclient)
        end

        def install(command)
          begin
            namespace.ensure_exists!

            create_service_account(command)
            create_cluster_role_binding(command)
            Gitlab::AppLogger.info("---CREATING CONFIG MAP-----")
            Gitlab::AppLogger.info(command)
            create_config_map(command)
            Gitlab::AppLogger.info("---CREATING K8s POD-----")

            kubeclient.create_pod(command.pod_resource)
          rescue StandardError => e
            Gitlab::AppLogger.info('install_api_error------------------------------------------------')
            Gitlab::AppLogger.error(e)
            Gitlab::AppLogger.error(e.backtrace.join("\n"))
          rescue Exception => e
            Gitlab::AppLogger.info('install_api_exception--------------------------------------------------')
            Gitlab::AppLogger.error(e)
            Gitlab::AppLogger.error(e.backtrace.join("\n"))
          end  
        end

        def update(command)
          namespace.ensure_exists!
          update_config_map(command)
          kubeclient.create_pod(command.pod_resource)
        end

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

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

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

        def get_config_map(config_map_name)
          namespace.ensure_exists!

          kubeclient.get_config_map(config_map_name, namespace.name)
        end

        private

        attr_reader :kubeclient, :namespace

        def create_config_map(command)
          command.config_map_resource.tap do |config_map_resource|
            Gitlab::AppLogger.info(config_map_resource)
            kubeclient.create_config_map(config_map_resource)
          end
        end

        def update_config_map(command)
          command.config_map_resource.tap do |config_map_resource|
            kubeclient.update_config_map(config_map_resource)
          end
        end

        def create_service_account(command)
          command.service_account_resource.tap do |service_account_resource|
            break unless service_account_resource

            if service_account_exists?(service_account_resource)
              kubeclient.update_service_account(service_account_resource)
            else
              kubeclient.create_service_account(service_account_resource)
            end
          end
        end

        def create_cluster_role_binding(command)
          command.cluster_role_binding_resource.tap do |cluster_role_binding_resource|
            break unless cluster_role_binding_resource

            if cluster_role_binding_exists?(cluster_role_binding_resource)
              kubeclient.update_cluster_role_binding(cluster_role_binding_resource)
            else
              kubeclient.create_cluster_role_binding(cluster_role_binding_resource)
            end
          end
        end

        def service_account_exists?(resource)
          resource_exists? do
            kubeclient.get_service_account(resource.metadata.name, resource.metadata.namespace)
          end
        end

        def cluster_role_binding_exists?(resource)
          resource_exists? do
            kubeclient.get_cluster_role_binding(resource.metadata.name)
          end
        end

        def resource_exists?
          yield
        rescue ::Kubeclient::HttpError => e
          raise e unless e.error_code == 404

          false
        end
      end
    end
  end
end