Class: Gitlab::Kas::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/kas/client.rb

Constant Summary collapse

JWT_AUDIENCE =
'gitlab-kas'
STUB_CLASSES =
{
  server_info: Gitlab::Agent::ServerInfo::Rpc::ServerInfo::Stub,
  agent_tracker: Gitlab::Agent::AgentTracker::Rpc::AgentTracker::Stub,
  configuration_project: Gitlab::Agent::ConfigurationProject::Rpc::ConfigurationProject::Stub,
  autoflow: Gitlab::Agent::AutoFlow::Rpc::AutoFlow::Stub,
  notifications: Gitlab::Agent::Notifications::Rpc::Notifications::Stub,
  managed_resources: Gitlab::Agent::ManagedResources::Rpc::Provisioner::Stub
}.freeze
AUTOFLOW_CI_VARIABLE_ENV_SCOPE =
'autoflow/internal-use'
ConfigurationError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.

Raises:



21
22
23
24
# File 'lib/gitlab/kas/client.rb', line 21

def initialize
  raise ConfigurationError, 'GitLab KAS is not enabled' unless Gitlab::Kas.enabled?
  raise ConfigurationError, 'KAS internal URL is not configured' unless Gitlab::Kas.internal_url.present?
end

Instance Method Details

#delete_environment(managed_resource:) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gitlab/kas/client.rb', line 167

def delete_environment(managed_resource:)
  request = ::Gitlab::Agent::ManagedResources::Rpc::DeleteEnvironmentRequest.new(
    agent_id: managed_resource.cluster_agent_id,
    project_id: managed_resource.project_id,
    environment_slug: managed_resource.environment.slug,
    objects: managed_resource.tracked_objects
  )

  stub_for(:managed_resources).delete_environment(request, metadata: (
    project: ::Feature::Kas.project_actor(managed_resource.project),
    group: ::Feature::Kas.group_actor(managed_resource.project)
  ))
end

#ensure_environment(template:, environment:, build:) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gitlab/kas/client.rb', line 154

def ensure_environment(template:, environment:, build:)
  request = Gitlab::Agent::ManagedResources::Rpc::EnsureEnvironmentRequest.new(
    template: Gitlab::Agent::ManagedResources::RenderedEnvironmentTemplate.new(
      name: template.name,
      data: template.data),
    info: templating_info(environment:, build:))
  stub_for(:managed_resources)
    .ensure_environment(request, metadata: (
      project: ::Feature::Kas.project_actor(environment.project),
      group: ::Feature::Kas.group_actor(environment.project)
    ))
end

#get_connected_agentks_by_agent_ids(agent_ids:) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/gitlab/kas/client.rb', line 38

def get_connected_agentks_by_agent_ids(agent_ids:)
  request = Gitlab::Agent::AgentTracker::Rpc::GetConnectedAgentksByAgentIDsRequest.new(agent_ids: agent_ids)

  stub_for(:agent_tracker)
   .get_connected_agentks_by_agent_i_ds(request, metadata: )
   .agents
   .to_a
end

#get_default_environment_templateObject



133
134
135
136
137
138
# File 'lib/gitlab/kas/client.rb', line 133

def get_default_environment_template
  request = Gitlab::Agent::ManagedResources::Rpc::GetDefaultEnvironmentTemplateRequest.new
  stub_for(:managed_resources)
    .get_default_environment_template(request, metadata: )
    .template
end

#get_environment_template(agent:, template_name:) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gitlab/kas/client.rb', line 114

def get_environment_template(agent:, template_name:)
  project = agent.project

  request = Gitlab::Agent::ManagedResources::Rpc::GetEnvironmentTemplateRequest.new(
    template_name: template_name,
    agent_name: agent.name,
    gitaly_info: gitaly_info(project),
    gitaly_repository: repository(project),
    default_branch: project.default_branch_or_main
  )

  stub_for(:managed_resources)
    .get_environment_template(request, metadata: (
      project: ::Feature::Kas.project_actor(project),
      group: ::Feature::Kas.group_actor(project)
    ))
    .template
end

#get_server_infoObject

Return GitLab KAS server info This method only returns information about a single KAS server instance without taking into account that there are potentially multiple KAS replicas running, which may not have the same server info. This is particularly the case during a rollout.



30
31
32
33
34
35
36
# File 'lib/gitlab/kas/client.rb', line 30

def get_server_info
  request = Gitlab::Agent::ServerInfo::Rpc::GetServerInfoRequest.new

  stub_for(:server_info)
    .get_server_info(request, metadata: )
    .current_server_info
end

#list_agent_config_files(project:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/kas/client.rb', line 47

def list_agent_config_files(project:)
  request = Gitlab::Agent::ConfigurationProject::Rpc::ListAgentConfigFilesRequest.new(
    repository: repository(project),
    gitaly_info: gitaly_info(project)
  )

  stub_for(:configuration_project)
    .list_agent_config_files(request, metadata: (
      project: ::Feature::Kas.project_actor(project),
      group: ::Feature::Kas.group_actor(project)
    ))
    .config_files
    .to_a
end

#render_environment_template(template:, environment:, build:) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gitlab/kas/client.rb', line 140

def render_environment_template(template:, environment:, build:)
  request = Gitlab::Agent::ManagedResources::Rpc::RenderEnvironmentTemplateRequest.new(
    template: Gitlab::Agent::ManagedResources::EnvironmentTemplate.new(
      name: template.name,
      data: template.data),
    info: templating_info(environment:, build:))
  stub_for(:managed_resources)
    .render_environment_template(request, metadata: (
      project: ::Feature::Kas.project_actor(environment.project),
      group: ::Feature::Kas.group_actor(environment.project)
    ))
    .template
end

#send_autoflow_event(project:, type:, id:, data:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gitlab/kas/client.rb', line 79

def send_autoflow_event(project:, type:, id:, data:)
  # We only want to send events if AutoFlow is enabled and no-op otherwise
  return unless Feature.enabled?(:autoflow_enabled, project)

  # retrieve all AutoFlow-relevant variables
  variables = project.variables.by_environment_scope(AUTOFLOW_CI_VARIABLE_ENV_SCOPE)

  project_proto = Gitlab::Agent::Event::Project.new(
    id: project.id,
    full_path: project.full_path
  )
  request = Gitlab::Agent::AutoFlow::Rpc::CloudEventRequest.new(
    event: Gitlab::Agent::Event::CloudEvent.new(
      id: id,
      source: "GitLab",
      spec_version: "v1",
      type: type,
      attributes: {
        datacontenttype: Gitlab::Agent::Event::CloudEvent::CloudEventAttributeValue.new(
          ce_string: "application/json"
        )
      },
      text_data: data.to_json
    ),
    flow_project: project_proto,
    variables: variables.to_h { |v| [v.key, v.value] }
  )

  stub_for(:autoflow)
    .cloud_event(request, metadata: (
      project: ::Feature::Kas.project_actor(project),
      group: ::Feature::Kas.group_actor(project)
    ))
end

#send_git_push_event(project:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/kas/client.rb', line 62

def send_git_push_event(project:)
  request = Gitlab::Agent::Notifications::Rpc::GitPushEventRequest.new(
    event: Gitlab::Agent::Event::GitPushEvent.new(
      project: Gitlab::Agent::Event::Project.new(
        id: project.id,
        full_path: project.full_path
      )
    )
  )

  stub_for(:notifications)
    .git_push_event(request, metadata: (
      project: ::Feature::Kas.project_actor(project),
      group: ::Feature::Kas.group_actor(project)
    ))
end