2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-22 12:23:43 -05:00
|
|
|
module Gitlab
|
|
|
|
module Kubernetes
|
|
|
|
module Helm
|
|
|
|
class Pod
|
2018-09-06 06:03:38 -04:00
|
|
|
def initialize(command, namespace_name, service_account_name: nil)
|
2017-12-22 12:23:43 -05:00
|
|
|
@command = command
|
|
|
|
@namespace_name = namespace_name
|
2018-09-06 06:03:38 -04:00
|
|
|
@service_account_name = service_account_name
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
|
|
|
spec = { containers: [container_specification], restartPolicy: 'Never' }
|
2018-01-15 11:27:58 -05:00
|
|
|
|
2018-08-07 08:39:38 -04:00
|
|
|
spec[:volumes] = volumes_specification
|
|
|
|
spec[:containers][0][:volumeMounts] = volume_mounts_specification
|
2018-09-06 06:03:38 -04:00
|
|
|
spec[:serviceAccountName] = service_account_name if service_account_name
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-12-22 12:23:43 -05:00
|
|
|
::Kubeclient::Resource.new(metadata: metadata, spec: spec)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-09-06 06:03:38 -04:00
|
|
|
attr_reader :command, :namespace_name, :service_account_name
|
2017-12-22 12:23:43 -05:00
|
|
|
|
|
|
|
def container_specification
|
2018-03-01 18:46:02 -05:00
|
|
|
{
|
2017-12-22 12:23:43 -05:00
|
|
|
name: 'helm',
|
2020-11-05 04:09:00 -05:00
|
|
|
image: "registry.gitlab.com/gitlab-org/cluster-integration/helm-install-image/releases/#{command.class::HELM_VERSION}-kube-#{Gitlab::Kubernetes::Helm::KUBECTL_VERSION}-alpine-3.12",
|
2017-12-22 12:23:43 -05:00
|
|
|
env: generate_pod_env(command),
|
|
|
|
command: %w(/bin/sh),
|
|
|
|
args: %w(-c $(COMMAND_SCRIPT))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
2018-01-15 11:27:58 -05:00
|
|
|
{
|
|
|
|
'gitlab.org/action': 'install',
|
|
|
|
'gitlab.org/application': command.name
|
|
|
|
}
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def metadata
|
2018-01-15 11:27:58 -05:00
|
|
|
{
|
|
|
|
name: command.pod_name,
|
|
|
|
namespace: namespace_name,
|
|
|
|
labels: labels
|
|
|
|
}
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
2018-03-01 18:46:02 -05:00
|
|
|
def generate_pod_env(command)
|
2020-11-05 04:09:00 -05:00
|
|
|
command.env.merge(
|
|
|
|
HELM_VERSION: command.class::HELM_VERSION,
|
2018-03-01 18:46:02 -05:00
|
|
|
COMMAND_SCRIPT: command.generate_script
|
2020-11-05 04:09:00 -05:00
|
|
|
).map { |key, value| { name: key, value: value } }
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def volumes_specification
|
2018-01-15 11:27:58 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'configuration-volume',
|
|
|
|
configMap: {
|
2018-02-05 07:46:31 -05:00
|
|
|
name: "values-content-configuration-#{command.name}",
|
2018-08-07 08:39:38 -04:00
|
|
|
items: command.file_names.map { |name| { key: name, path: name } }
|
2018-01-15 11:27:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
2018-03-01 18:46:02 -05:00
|
|
|
def volume_mounts_specification
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'configuration-volume',
|
|
|
|
mountPath: "/data/helm/#{command.name}/config"
|
|
|
|
}
|
|
|
|
]
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|