Commit 615f158e authored by João Alexandre Cunha's avatar João Alexandre Cunha Committed by Bob Van Landuyt

Adds KAS properties to GraphQL

parent d5ab31e7
# frozen_string_literal: true
module Types
module Metadata
class KasType < ::Types::BaseObject
graphql_name 'Kas'
authorize :read_instance_metadata
field :enabled, GraphQL::BOOLEAN_TYPE, null: false,
description: 'Indicates whether the Kubernetes Agent Server is enabled.'
field :version, GraphQL::STRING_TYPE, null: true,
description: 'KAS version.'
field :external_url, GraphQL::STRING_TYPE, null: true,
description: 'The URL used by the Agents to communicate with KAS.'
end
end
end
...@@ -10,5 +10,7 @@ module Types ...@@ -10,5 +10,7 @@ module Types
description: 'Version.' description: 'Version.'
field :revision, GraphQL::STRING_TYPE, null: false, field :revision, GraphQL::STRING_TYPE, null: false,
description: 'Revision.' description: 'Revision.'
field :kas, ::Types::Metadata::KasType, null: false,
description: 'Metadata about KAS.'
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
class InstanceMetadata class InstanceMetadata
attr_reader :version, :revision attr_reader :version, :revision, :kas
def initialize(version: Gitlab::VERSION, revision: Gitlab.revision) def initialize(version: Gitlab::VERSION, revision: Gitlab.revision)
@version = version @version = version
@revision = revision @revision = revision
@kas = ::InstanceMetadata::Kas.new
end end
end end
# frozen_string_literal: true
class InstanceMetadata::Kas
attr_reader :enabled, :version, :external_url
def initialize
@enabled = Gitlab::Kas.enabled?
@version = Gitlab::Kas.version if @enabled
@external_url = Gitlab::Kas.external_url if @enabled
end
def self.declarative_policy_class
"InstanceMetadataPolicy"
end
end
---
title: Expose KAS metadata through GraphQL - enabled, version and externalUrl
merge_request: 59696
author:
type: added
...@@ -9500,6 +9500,16 @@ four standard [pagination arguments](#connection-pagination-arguments): ...@@ -9500,6 +9500,16 @@ four standard [pagination arguments](#connection-pagination-arguments):
| <a id="jobpermissionsreadjobartifacts"></a>`readJobArtifacts` | [`Boolean!`](#boolean) | Indicates the user can perform `read_job_artifacts` on this resource. | | <a id="jobpermissionsreadjobartifacts"></a>`readJobArtifacts` | [`Boolean!`](#boolean) | Indicates the user can perform `read_job_artifacts` on this resource. |
| <a id="jobpermissionsupdatebuild"></a>`updateBuild` | [`Boolean!`](#boolean) | Indicates the user can perform `update_build` on this resource. | | <a id="jobpermissionsupdatebuild"></a>`updateBuild` | [`Boolean!`](#boolean) | Indicates the user can perform `update_build` on this resource. |
### `Kas`
#### Fields
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="kasenabled"></a>`enabled` | [`Boolean!`](#boolean) | Indicates whether the Kubernetes Agent Server is enabled. |
| <a id="kasexternalurl"></a>`externalUrl` | [`String`](#string) | The URL used by the Agents to communicate with KAS. |
| <a id="kasversion"></a>`version` | [`String`](#string) | KAS version. |
### `Label` ### `Label`
#### Fields #### Fields
...@@ -10083,6 +10093,7 @@ four standard [pagination arguments](#connection-pagination-arguments): ...@@ -10083,6 +10093,7 @@ four standard [pagination arguments](#connection-pagination-arguments):
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ---- | ---- | ----------- |
| <a id="metadatakas"></a>`kas` | [`Kas!`](#kas) | Metadata about KAS. |
| <a id="metadatarevision"></a>`revision` | [`String!`](#string) | Revision. | | <a id="metadatarevision"></a>`revision` | [`String!`](#string) | Revision. |
| <a id="metadataversion"></a>`version` | [`String!`](#string) | Version. | | <a id="metadataversion"></a>`version` | [`String!`](#string) | Version. |
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
module Gitlab module Gitlab
module Kas module Kas
INTERNAL_API_REQUEST_HEADER = 'Gitlab-Kas-Api-Request' INTERNAL_API_REQUEST_HEADER = 'Gitlab-Kas-Api-Request'
VERSION_FILE = 'GITLAB_KAS_VERSION'
JWT_ISSUER = 'gitlab-kas' JWT_ISSUER = 'gitlab-kas'
include JwtAuthenticatable include JwtAuthenticatable
...@@ -29,6 +30,27 @@ module Gitlab ...@@ -29,6 +30,27 @@ module Gitlab
Feature.enabled?(:kubernetes_agent_on_gitlab_com, project, default_enabled: :yaml) Feature.enabled?(:kubernetes_agent_on_gitlab_com, project, default_enabled: :yaml)
end end
# Return GitLab KAS version
#
# @return [String] version
def version
@_version ||= Rails.root.join(VERSION_FILE).read.chomp
end
# Return GitLab KAS external_url
#
# @return [String] external_url
def external_url
Gitlab.config.gitlab_kas.external_url
end
# Return whether GitLab KAS is enabled
#
# @return [Boolean] external_url
def enabled?
!!Gitlab.config['gitlab_kas']&.fetch('enabled', false)
end
end end
end end
end end
...@@ -7,7 +7,10 @@ RSpec.describe Resolvers::MetadataResolver do ...@@ -7,7 +7,10 @@ RSpec.describe Resolvers::MetadataResolver do
describe '#resolve' do describe '#resolve' do
it 'returns version and revision' do it 'returns version and revision' do
expect(resolve(described_class)).to have_attributes(version: Gitlab::VERSION, revision: Gitlab.revision) expect(resolve(described_class)).to have_attributes(
version: Gitlab::VERSION,
revision: Gitlab.revision,
kas: kind_of(InstanceMetadata::Kas))
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabSchema.types['Kas'] do
specify { expect(described_class.graphql_name).to eq('Kas') }
specify { expect(described_class).to require_graphql_authorizations(:read_instance_metadata) }
end
...@@ -33,6 +33,46 @@ RSpec.describe Gitlab::Kas do ...@@ -33,6 +33,46 @@ RSpec.describe Gitlab::Kas do
end end
end end
describe '.enabled?' do
before do
allow(Gitlab).to receive(:config).and_return(gitlab_config)
end
subject { described_class.enabled? }
context 'gitlab_config is not enabled' do
let(:gitlab_config) { { 'gitlab_kas' => { 'enabled' => false } } }
it { is_expected.to be_falsey }
end
context 'gitlab_config is enabled' do
let(:gitlab_config) { { 'gitlab_kas' => { 'enabled' => true } } }
it { is_expected.to be_truthy }
end
context 'enabled is unset' do
let(:gitlab_config) { { 'gitlab_kas' => {} } }
it { is_expected.to be_falsey }
end
end
describe '.external_url' do
it 'returns gitlab_kas external_url config' do
expect(described_class.external_url).to eq(Gitlab.config.gitlab_kas.external_url)
end
end
describe '.version' do
it 'returns gitlab_kas version config' do
version_file = Rails.root.join(described_class::VERSION_FILE)
expect(described_class.version).to eq(version_file.read.chomp)
end
end
describe '.ensure_secret!' do describe '.ensure_secret!' do
context 'secret file exists' do context 'secret file exists' do
before do before do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::InstanceMetadata::Kas do
it 'has InstanceMetadataPolicy as declarative policy' do
expect(described_class.declarative_policy_class).to eq("InstanceMetadataPolicy")
end
context 'when KAS is enabled' do
it 'has the correct properties' do
allow(Gitlab::Kas).to receive(:enabled?).and_return(true)
expect(subject).to have_attributes(
enabled: Gitlab::Kas.enabled?,
version: Gitlab::Kas.version,
external_url: Gitlab::Kas.external_url
)
end
end
context 'when KAS is disabled' do
it 'has the correct properties' do
allow(Gitlab::Kas).to receive(:enabled?).and_return(false)
expect(subject).to have_attributes(
enabled: Gitlab::Kas.enabled?,
version: nil,
external_url: nil
)
end
end
end
...@@ -6,7 +6,8 @@ RSpec.describe InstanceMetadata do ...@@ -6,7 +6,8 @@ RSpec.describe InstanceMetadata do
it 'has the correct properties' do it 'has the correct properties' do
expect(subject).to have_attributes( expect(subject).to have_attributes(
version: Gitlab::VERSION, version: Gitlab::VERSION,
revision: Gitlab.revision revision: Gitlab.revision,
kas: kind_of(::InstanceMetadata::Kas)
) )
end end
end end
...@@ -8,16 +8,48 @@ RSpec.describe 'getting project information' do ...@@ -8,16 +8,48 @@ RSpec.describe 'getting project information' do
let(:query) { graphql_query_for('metadata', {}, all_graphql_fields_for('Metadata')) } let(:query) { graphql_query_for('metadata', {}, all_graphql_fields_for('Metadata')) }
context 'logged in' do context 'logged in' do
it 'returns version and revision' do let(:expected_data) do
post_graphql(query, current_user: create(:user)) {
expect(graphql_errors).to be_nil
expect(graphql_data).to eq(
'metadata' => { 'metadata' => {
'version' => Gitlab::VERSION, 'version' => Gitlab::VERSION,
'revision' => Gitlab.revision 'revision' => Gitlab.revision,
'kas' => {
'enabled' => Gitlab::Kas.enabled?,
'version' => expected_kas_version,
'externalUrl' => expected_kas_external_url
}
}
} }
) end
context 'kas is enabled' do
let(:expected_kas_version) { Gitlab::Kas.version }
let(:expected_kas_external_url) { Gitlab::Kas.external_url }
before do
allow(Gitlab::Kas).to receive(:enabled?).and_return(true)
post_graphql(query, current_user: create(:user))
end
it 'returns version, revision, kas_enabled, kas_version, kas_external_url' do
expect(graphql_errors).to be_nil
expect(graphql_data).to eq(expected_data)
end
end
context 'kas is disabled' do
let(:expected_kas_version) { nil }
let(:expected_kas_external_url) { nil }
before do
allow(Gitlab::Kas).to receive(:enabled?).and_return(false)
post_graphql(query, current_user: create(:user))
end
it 'returns version and revision' do
expect(graphql_errors).to be_nil
expect(graphql_data).to eq(expected_data)
end
end end
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