debian-mirror-gitlab/app/services/ci/generate_kubeconfig_service.rb

70 lines
1.3 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
module Ci
class GenerateKubeconfigService
2022-07-16 23:28:13 +05:30
def initialize(pipeline, token:)
@pipeline = pipeline
@token = token
2021-12-11 22:18:48 +05:30
@template = Gitlab::Kubernetes::Kubeconfig::Template.new
end
def execute
template.add_cluster(
name: cluster_name,
url: Gitlab::Kas.tunnel_url
)
2022-11-25 23:54:43 +05:30
agent_authorizations.each do |authorization|
agent = authorization.agent
2021-12-11 22:18:48 +05:30
user = user_name(agent)
template.add_user(
name: user,
token: agent_token(agent)
)
template.add_context(
name: context_name(agent),
2022-11-25 23:54:43 +05:30
namespace: context_namespace(authorization),
2021-12-11 22:18:48 +05:30
cluster: cluster_name,
user: user
)
end
template
end
private
2022-07-16 23:28:13 +05:30
attr_reader :pipeline, :token, :template
2021-12-11 22:18:48 +05:30
2022-11-25 23:54:43 +05:30
def agent_authorizations
pipeline.cluster_agent_authorizations
2021-12-11 22:18:48 +05:30
end
def cluster_name
'gitlab'
end
def user_name(agent)
['agent', agent.id].join(delimiter)
end
def context_name(agent)
[agent.project.full_path, agent.name].join(delimiter)
end
2022-11-25 23:54:43 +05:30
def context_namespace(authorization)
authorization.config['default_namespace']
end
2021-12-11 22:18:48 +05:30
def agent_token(agent)
2022-07-16 23:28:13 +05:30
['ci', agent.id, token].join(delimiter)
2021-12-11 22:18:48 +05:30
end
def delimiter
':'
end
end
end