Commit a5c6c8b3 authored by Tiger's avatar Tiger

Add template classes for creating Kubeconfig files

Provides a simple way to generate config files with multiple users
and/or contexts, which will be used when communicating with Kubernetes
Agents (each applicable agent will have a separate context).

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/67089
parent f98a5d04
# frozen_string_literal: true
module Gitlab
module Kubernetes
module Kubeconfig
module Entry
class Cluster
attr_reader :name
def initialize(name:, url:, ca_pem: nil)
@name = name
@url = url
@ca_pem = ca_pem
end
def to_h
{
name: name,
cluster: cluster
}
end
private
attr_reader :url, :ca_pem
def cluster
{
server: url,
'certificate-authority-data': certificate_authority_data
}.compact
end
def certificate_authority_data
return unless ca_pem.present?
Base64.strict_encode64(ca_pem)
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Kubernetes
module Kubeconfig
module Entry
class Context
attr_reader :name
def initialize(name:, cluster:, user:, namespace: nil)
@name = name
@cluster = cluster
@user = user
@namespace = namespace
end
def to_h
{
name: name,
context: context
}
end
private
attr_reader :cluster, :user, :namespace
def context
{
cluster: cluster,
namespace: namespace,
user: user
}.compact
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Kubernetes
module Kubeconfig
module Entry
class User
attr_reader :name
def initialize(name:, token:)
@name = name
@token = token
end
def to_h
{
name: name,
user: { token: token }
}
end
private
attr_reader :token
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Kubernetes
module Kubeconfig
class Template
ENTRIES = {
cluster: Gitlab::Kubernetes::Kubeconfig::Entry::Cluster,
user: Gitlab::Kubernetes::Kubeconfig::Entry::User,
context: Gitlab::Kubernetes::Kubeconfig::Entry::Context
}.freeze
def initialize
@clusters = []
@users = []
@contexts = []
end
def valid?
contexts.present?
end
def add_cluster(**args)
clusters << new_entry(:cluster, **args)
end
def add_user(**args)
users << new_entry(:user, **args)
end
def add_context(**args)
contexts << new_entry(:context, **args)
end
def to_h
{
apiVersion: 'v1',
kind: 'Config',
clusters: clusters.map(&:to_h),
users: users.map(&:to_h),
contexts: contexts.map(&:to_h)
}
end
def to_yaml
YAML.dump(to_h.deep_stringify_keys)
end
private
attr_reader :clusters, :users, :contexts
def new_entry(entry, **args)
ENTRIES.fetch(entry).new(**args)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Kubernetes::Kubeconfig::Entry::Cluster do
describe '#to_h' do
let(:name) { 'name' }
let(:url) { 'url' }
subject { described_class.new(name: name, url: url).to_h }
it { is_expected.to eq({ name: name, cluster: { server: url } }) }
context 'with a certificate' do
let(:cert) { 'certificate' }
let(:cert_encoded) { Base64.strict_encode64(cert) }
subject { described_class.new(name: name, url: url, ca_pem: cert).to_h }
it { is_expected.to eq({ name: name, cluster: { server: url, 'certificate-authority-data': cert_encoded } }) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Kubernetes::Kubeconfig::Entry::Context do
describe '#to_h' do
let(:name) { 'name' }
let(:user) { 'user' }
let(:cluster) { 'cluster' }
subject { described_class.new(name: name, user: user, cluster: cluster).to_h }
it { is_expected.to eq({ name: name, context: { cluster: cluster, user: user } }) }
context 'with a namespace' do
let(:namespace) { 'namespace' }
subject { described_class.new(name: name, user: user, cluster: cluster, namespace: namespace).to_h }
it { is_expected.to eq({ name: name, context: { cluster: cluster, user: user, namespace: namespace } }) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Kubernetes::Kubeconfig::Entry::User do
describe '#to_h' do
let(:name) { 'name' }
let(:token) { 'token' }
subject { described_class.new(name: name, token: token).to_h }
it { is_expected.to eq({ name: name, user: { token: token } }) }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Kubernetes::Kubeconfig::Template do
let(:template) { described_class.new }
describe '#valid?' do
subject { template.valid? }
it { is_expected.to be_falsey }
context 'with configuration added' do
before do
template.add_context(name: 'name', cluster: 'cluster', user: 'user')
end
it { is_expected.to be_truthy }
end
end
describe '#to_h' do
subject { described_class.new.to_h }
it do
is_expected.to eq(
apiVersion: 'v1',
kind: 'Config',
clusters: [],
users: [],
contexts: []
)
end
end
describe '#to_yaml' do
subject { template.to_yaml }
it { is_expected.to eq(YAML.dump(template.to_h.deep_stringify_keys)) }
end
describe 'adding entries' do
let(:entry) { instance_double(entry_class, to_h: attributes) }
let(:attributes) do
{ name: 'name', other: 'other' }
end
subject { template.to_h }
before do
expect(entry_class).to receive(:new).with(attributes).and_return(entry)
end
describe '#add_cluster' do
let(:entry_class) { Gitlab::Kubernetes::Kubeconfig::Entry::Cluster }
before do
template.add_cluster(**attributes)
end
it { is_expected.to include(clusters: [attributes]) }
end
describe '#add_user' do
let(:entry_class) { Gitlab::Kubernetes::Kubeconfig::Entry::User }
before do
template.add_user(**attributes)
end
it { is_expected.to include(users: [attributes]) }
end
describe '#add_context' do
let(:entry_class) { Gitlab::Kubernetes::Kubeconfig::Entry::Context }
before do
template.add_context(**attributes)
end
it { is_expected.to include(contexts: [attributes]) }
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