2020-07-20 05:09:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
# Kubernetes Internal API
|
|
|
|
module Internal
|
2020-10-14 20:08:42 -04:00
|
|
|
class Kubernetes < ::API::Base
|
2020-11-02 10:08:52 -05:00
|
|
|
feature_category :kubernetes_management
|
|
|
|
|
2020-08-21 08:10:22 -04:00
|
|
|
before do
|
2020-09-07 17:08:26 -04:00
|
|
|
check_feature_enabled
|
2020-08-21 08:10:22 -04:00
|
|
|
authenticate_gitlab_kas_request!
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
helpers do
|
2020-08-21 08:10:22 -04:00
|
|
|
def authenticate_gitlab_kas_request!
|
2021-03-31 08:08:55 -04:00
|
|
|
render_api_error!('KAS JWT authentication invalid', 401) unless Gitlab::Kas.verify_api_request(headers)
|
2020-08-21 08:10:22 -04:00
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
def agent_token
|
|
|
|
@agent_token ||= cluster_agent_token_from_authorization_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent
|
|
|
|
@agent ||= agent_token.agent
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
def repo_type
|
|
|
|
Gitlab::GlRepository::PROJECT
|
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
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
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
def gitaly_repository(project)
|
|
|
|
{
|
2022-03-02 10:16:07 -05:00
|
|
|
default_branch: project.default_branch_or_main,
|
2020-08-11 23:10:17 -04:00
|
|
|
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
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_feature_enabled
|
2020-09-14 14:09:48 -04:00
|
|
|
not_found! unless Feature.enabled?(:kubernetes_agent_internal_api, default_enabled: true, type: :ops)
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
2020-08-11 23:10:17 -04:00
|
|
|
|
|
|
|
def check_agent_token
|
2021-03-31 08:08:55 -04:00
|
|
|
unauthorized! unless agent_token
|
2021-02-04 22:09:09 -05:00
|
|
|
|
2022-01-17 16:15:21 -05:00
|
|
|
Clusters::AgentTokens::TrackUsageService.new(agent_token).execute
|
2020-08-11 23:10:17 -04:00
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
namespace 'internal' do
|
|
|
|
namespace 'kubernetes' do
|
2020-08-11 23:10:17 -04:00
|
|
|
before do
|
|
|
|
check_agent_token
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
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
|
2020-08-11 23:10:17 -04:00
|
|
|
project = agent.project
|
2020-07-20 05:09:22 -04:00
|
|
|
|
2020-08-11 23:10:17 -04:00
|
|
|
status 200
|
|
|
|
{
|
|
|
|
project_id: project.id,
|
|
|
|
agent_id: agent.id,
|
|
|
|
agent_name: agent.name,
|
|
|
|
gitaly_info: gitaly_info(project),
|
|
|
|
gitaly_repository: gitaly_repository(project)
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
2020-09-07 17:08:26 -04:00
|
|
|
end
|
2020-09-02 17:10:43 -04:00
|
|
|
|
2021-08-23 05:10:23 -04:00
|
|
|
namespace 'kubernetes/agent_configuration' do
|
|
|
|
desc 'POST agent configuration' do
|
|
|
|
detail 'Store configuration for an agent'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :agent_id, type: Integer, desc: 'ID of the configured Agent'
|
|
|
|
requires :agent_config, type: JSON, desc: 'Configuration for the Agent'
|
|
|
|
end
|
|
|
|
post '/' do
|
|
|
|
agent = Clusters::Agent.find(params[:agent_id])
|
|
|
|
|
|
|
|
Clusters::Agents::RefreshAuthorizationService.new(agent, config: params[:agent_config]).execute
|
|
|
|
|
|
|
|
no_content!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-07 17:08:26 -04:00
|
|
|
namespace 'kubernetes/usage_metrics' do
|
2020-09-02 17:10:43 -04:00
|
|
|
desc 'POST usage metrics' do
|
|
|
|
detail 'Updates usage metrics for agent'
|
|
|
|
end
|
|
|
|
params do
|
2021-05-07 02:10:43 -04:00
|
|
|
optional :gitops_sync_count, type: Integer, desc: 'The count to increment the gitops_sync metric by'
|
|
|
|
optional :k8s_api_proxy_request_count, type: Integer, desc: 'The count to increment the k8s_api_proxy_request_count metric by'
|
2020-09-02 17:10:43 -04:00
|
|
|
end
|
2020-09-07 17:08:26 -04:00
|
|
|
post '/' do
|
2021-05-07 02:10:43 -04:00
|
|
|
events = params.slice(:gitops_sync_count, :k8s_api_proxy_request_count)
|
|
|
|
events.transform_keys! { |event| event.to_s.chomp('_count') }
|
2020-09-02 17:10:43 -04:00
|
|
|
|
2021-05-07 02:10:43 -04:00
|
|
|
Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_event_counts(events)
|
2020-09-02 17:10:43 -04:00
|
|
|
|
2021-05-07 02:10:43 -04:00
|
|
|
no_content!
|
|
|
|
rescue ArgumentError => e
|
|
|
|
bad_request!(e.message)
|
2020-09-02 17:10:43 -04:00
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-12-09 19:09:50 -05:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
API::Internal::Kubernetes.prepend_mod_with('API::Internal::Kubernetes')
|