diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-18 11:17:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-18 11:17:02 +0300 |
commit | b39512ed755239198a9c294b6a45e65c05900235 (patch) | |
tree | d234a3efade1de67c46b9e5a38ce813627726aa7 /lib/api/internal/kubernetes.rb | |
parent | d31474cf3b17ece37939d20082b07f6657cc79a9 (diff) |
Add latest changes from gitlab-org/gitlab@15-3-stable-eev15.3.0-rc42
Diffstat (limited to 'lib/api/internal/kubernetes.rb')
-rw-r--r-- | lib/api/internal/kubernetes.rb | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/lib/api/internal/kubernetes.rb b/lib/api/internal/kubernetes.rb index f7c6e48e54f..6f964d5636b 100644 --- a/lib/api/internal/kubernetes.rb +++ b/lib/api/internal/kubernetes.rb @@ -4,6 +4,8 @@ module API # Kubernetes Internal API module Internal class Kubernetes < ::API::Base + include Gitlab::Utils::StrongMemoize + feature_category :kubernetes_management before do check_feature_enabled @@ -54,6 +56,27 @@ module API ::Clusters::AgentTokens::TrackUsageService.new(agent_token).execute end + + def agent_has_access_to_project?(project) + Guest.can?(:download_code, project) || agent.has_access_to?(project) + end + + def count_events + strong_memoize(:count_events) do + events = params.slice(:gitops_sync_count, :k8s_api_proxy_request_count) + events.transform_keys! { |event| event.to_s.chomp('_count') } + events = params[:counters]&.slice(:gitops_sync, :k8s_api_proxy_request) unless events.present? + events + end + end + + def increment_unique_events + events = params[:unique_counters]&.slice(:agent_users_using_ci_tunnel) + + events&.each do |event, entity_ids| + increment_unique_values(event, entity_ids) + end + end end namespace 'internal' do @@ -79,6 +102,24 @@ module API default_branch: project.default_branch_or_main } end + + desc 'Gets project info' do + detail 'Retrieves project info (if authorized)' + end + route_setting :authentication, cluster_agent_token_allowed: true + get '/project_info', urgency: :low do + project = find_project(params[:id]) + + not_found! unless agent_has_access_to_project?(project) + + status 200 + { + project_id: project.id, + gitaly_info: gitaly_info(project), + gitaly_repository: gitaly_repository(project), + default_branch: project.default_branch_or_main + } + end end namespace 'kubernetes/agent_configuration', urgency: :low do @@ -103,14 +144,27 @@ module API detail 'Updates usage metrics for agent' end params do + # Todo: Remove gitops_sync_count and k8s_api_proxy_request_count in the next milestone + # https://gitlab.com/gitlab-org/gitlab/-/issues/369489 + # We're only keeping it for backwards compatibility until KAS is released + # using `counts:` instead 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' + optional :counters, type: Hash do + optional :gitops_sync, type: Integer, desc: 'The count to increment the gitops_sync metric by' + optional :k8s_api_proxy_request, type: Integer, desc: 'The count to increment the k8s_api_proxy_request_count metric by' + end + mutually_exclusive :counters, :gitops_sync_count + mutually_exclusive :counters, :k8s_api_proxy_request_count + + optional :unique_counters, type: Hash do + optional :agent_users_using_ci_tunnel, type: Set[Integer], desc: 'A set of user ids that have interacted a CI Tunnel to' + end end post '/' do - events = params.slice(:gitops_sync_count, :k8s_api_proxy_request_count) - events.transform_keys! { |event| event.to_s.chomp('_count') } + Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_event_counts(count_events) if count_events - Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_event_counts(events) + increment_unique_events no_content! rescue ArgumentError => e |