Commit b06c4581 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'mattkasa/229462-gitaly-kubernetes-api' into 'master'

Return gitaly info in kubernetes internal API

Closes #229462

See merge request gitlab-org/gitlab!38654
parents 5b4dc684 26634f13
---
title: Return gitaly info in kubernetes internal API
merge_request: 38654
author:
type: added
......@@ -5,52 +5,67 @@ module API
module Internal
class Kubernetes < Grape::API::Instance
helpers do
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 gl_repository(project)
repo_type.identifier_for_container(project)
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 gl_repository_path(project)
repo_type.repository_for(project).full_path
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
not_found! unless Feature.enabled?(:kubernetes_agent_internal_api)
end
def check_agent_token
forbidden! unless agent_token
end
end
namespace 'internal' do
namespace 'kubernetes' do
before do
check_feature_enabled
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
check_feature_enabled
agent_token = cluster_agent_token_from_authorization_token
if agent_token
agent = agent_token.agent
project = agent.project
@gl_project_string = "project-#{project.id}"
status 200
{
project_id: project.id,
agent_id: agent.id,
agent_name: agent.name,
storage_name: project.repository_storage,
relative_path: project.disk_path + '.git',
gl_repository: gl_repository(project),
gl_project_path: gl_repository_path(project)
gitaly_info: gitaly_info(project),
gitaly_repository: gitaly_repository(project)
}
else
status 403
end
end
desc 'Gets project info' do
......@@ -58,11 +73,6 @@ module API
end
route_setting :authentication, cluster_agent_token_allowed: true
get '/project_info' do
check_feature_enabled
agent_token = cluster_agent_token_from_authorization_token
if agent_token
project = find_project(params[:id])
# TODO sort out authorization for real
......@@ -71,19 +81,12 @@ module API
not_found!
end
@gl_project_string = "project-#{project.id}"
status 200
{
project_id: project.id,
storage_name: project.repository_storage,
relative_path: project.disk_path + '.git',
gl_repository: gl_repository(project),
gl_project_path: gl_repository_path(project)
gitaly_info: gitaly_info(project),
gitaly_repository: gitaly_repository(project)
}
else
status 403
end
end
end
end
......
......@@ -33,13 +33,24 @@ RSpec.describe API::Internal::Kubernetes do
expect(response).to have_gitlab_http_status(:success)
expect(json_response['project_id']).to eq(project.id)
expect(json_response['agent_id']).to eq(agent.id)
expect(json_response['agent_name']).to eq(agent.name)
expect(json_response['storage_name']).to eq(project.repository_storage)
expect(json_response['relative_path']).to eq(project.disk_path + '.git')
expect(json_response['gl_repository']).to eq("project-#{project.id}")
expect(json_response['gl_project_path']).to eq(project.full_path)
expect(json_response).to match(
a_hash_including(
'project_id' => project.id,
'agent_id' => agent.id,
'agent_name' => agent.name,
'gitaly_info' => a_hash_including(
'address' => match(/\.socket$/),
'token' => 'secret',
'features' => {}
),
'gitaly_repository' => a_hash_including(
'storage_name' => project.repository_storage,
'relative_path' => project.disk_path + '.git',
'gl_repository' => "project-#{project.id}",
'gl_project_path' => project.full_path
)
)
)
end
end
......@@ -92,11 +103,22 @@ RSpec.describe API::Internal::Kubernetes do
expect(response).to have_gitlab_http_status(:success)
expect(json_response['project_id']).to eq(project.id)
expect(json_response['storage_name']).to eq(project.repository_storage)
expect(json_response['relative_path']).to eq(project.disk_path + '.git')
expect(json_response['gl_repository']).to eq("project-#{project.id}")
expect(json_response['gl_project_path']).to eq(project.full_path)
expect(json_response).to match(
a_hash_including(
'project_id' => project.id,
'gitaly_info' => a_hash_including(
'address' => match(/\.socket$/),
'token' => 'secret',
'features' => {}
),
'gitaly_repository' => a_hash_including(
'storage_name' => project.repository_storage,
'relative_path' => project.disk_path + '.git',
'gl_repository' => "project-#{project.id}",
'gl_project_path' => project.full_path
)
)
)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment