2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
# Kubernetes Internal API
|
|
|
|
module Internal
|
2021-01-03 14:25:43 +05:30
|
|
|
class Kubernetes < ::API::Base
|
2021-01-29 00:20:46 +05:30
|
|
|
feature_category :kubernetes_management
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
before do
|
|
|
|
check_feature_enabled
|
|
|
|
authenticate_gitlab_kas_request!
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
helpers do
|
2020-11-24 15:15:51 +05:30
|
|
|
def authenticate_gitlab_kas_request!
|
|
|
|
unauthorized! unless Gitlab::Kas.verify_api_request(headers)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def agent_token
|
|
|
|
@agent_token ||= cluster_agent_token_from_authorization_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent
|
|
|
|
@agent ||= agent_token.agent
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo_type
|
|
|
|
Gitlab::GlRepository::PROJECT
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_info(project)
|
|
|
|
shard = repo_type.repository_for(project).shard
|
|
|
|
{
|
|
|
|
address: Gitlab::GitalyClient.address(shard),
|
|
|
|
token: Gitlab::GitalyClient.token(shard),
|
|
|
|
features: Feature::Gitaly.server_feature_flags
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_repository(project)
|
|
|
|
{
|
|
|
|
storage_name: project.repository_storage,
|
|
|
|
relative_path: project.disk_path + '.git',
|
|
|
|
gl_repository: repo_type.identifier_for_container(project),
|
|
|
|
gl_project_path: repo_type.repository_for(project).full_path
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_feature_enabled
|
2020-11-24 15:15:51 +05:30
|
|
|
not_found! unless Feature.enabled?(:kubernetes_agent_internal_api, default_enabled: true, type: :ops)
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def check_agent_token
|
|
|
|
forbidden! unless agent_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace 'internal' do
|
|
|
|
namespace 'kubernetes' do
|
|
|
|
before do
|
|
|
|
check_agent_token
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Gets agent info' do
|
|
|
|
detail 'Retrieves agent info for the given token'
|
|
|
|
end
|
|
|
|
route_setting :authentication, cluster_agent_token_allowed: true
|
|
|
|
get '/agent_info' do
|
|
|
|
project = agent.project
|
|
|
|
|
|
|
|
status 200
|
|
|
|
{
|
|
|
|
project_id: project.id,
|
|
|
|
agent_id: agent.id,
|
|
|
|
agent_name: agent.name,
|
|
|
|
gitaly_info: gitaly_info(project),
|
|
|
|
gitaly_repository: gitaly_repository(project)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Gets project info' do
|
|
|
|
detail 'Retrieves project info (if authorized)'
|
|
|
|
end
|
|
|
|
route_setting :authentication, cluster_agent_token_allowed: true
|
|
|
|
get '/project_info' do
|
|
|
|
project = find_project(params[:id])
|
|
|
|
|
|
|
|
# TODO sort out authorization for real
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/220912
|
2020-11-05 12:06:23 +05:30
|
|
|
unless Ability.allowed?(nil, :download_code, project)
|
2020-10-24 23:57:45 +05:30
|
|
|
not_found!
|
|
|
|
end
|
|
|
|
|
|
|
|
status 200
|
|
|
|
{
|
|
|
|
project_id: project.id,
|
|
|
|
gitaly_info: gitaly_info(project),
|
|
|
|
gitaly_repository: gitaly_repository(project)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
namespace 'kubernetes/usage_metrics' do
|
|
|
|
desc 'POST usage metrics' do
|
|
|
|
detail 'Updates usage metrics for agent'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :gitops_sync_count, type: Integer, desc: 'The count to increment the gitops_sync metric by'
|
|
|
|
end
|
|
|
|
post '/' do
|
|
|
|
gitops_sync_count = params[:gitops_sync_count]
|
|
|
|
|
|
|
|
if gitops_sync_count < 0
|
|
|
|
bad_request!('gitops_sync_count must be greater than or equal to zero')
|
|
|
|
else
|
|
|
|
Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_gitops_sync(gitops_sync_count)
|
|
|
|
|
|
|
|
no_content!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|