class HubClustersCreator::Agent

Agent is the main agent class

Public Class Methods

cluster_schema(name) click to toggle source

cluster_schema returns a cluster schema for a specific provider

# File lib/hub-clusters-creator/agent.rb, line 87
def self.cluster_schema(name)
  schemas(name).last
end
defaults(name) click to toggle source

defaults builds the default from the schema

# File lib/hub-clusters-creator/agent.rb, line 69
def self.defaults(name)
  values = {}
  cluster_schema(name)['properties'].reject { |x, _v| x == 'authorized_master_cidrs' }.each do |k, v|
    values[k.to_sym] = v['default']
  end
  # @TODO find a better way of doing this
  unless values[:authorized_master_cidrs]
    values[:authorized_master_cidrs] = [{ name: 'any', cidr: '0.0.0.0/0' }]
  end
  values
end
new(provider) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/hub-clusters-creator/agent.rb, line 35
def initialize(provider)
  @provider_name = provider[:provider]

  # @step: validate the provider configuration
  JsonSchema.parse!(HubClustersCreator::Agent.provider_schema(@provider_name)).validate(provider)

  # @step: create and return a provider instance
  case @provider_name
  when 'gke'
    @provider = HubClustersCreator::Providers::GKE.new(
      account: provider[:account],
      project: provider[:project],
      region: provider[:region]
    )
  when 'aks'
    @provider = HubClustersCreator::Providers::AKS.new(
      client_id: provider[:client_id],
      client_secret: provider[:client_secret],
      region: provider[:region],
      subscription: provider[:subscription],
      tenant: provider[:tenant]
    )
  else
    raise ArgumentError, "cloud provider: #{@provider_name} not supported"
  end
end
provider_schema(name) click to toggle source

provider_schema returns the provider schema

# File lib/hub-clusters-creator/agent.rb, line 82
def self.provider_schema(name)
  schemas(name).first
end
providers() click to toggle source

providers provides a list of providers

# File lib/hub-clusters-creator/agent.rb, line 64
def self.providers
  %w[aks gke]
end
schemas(name) click to toggle source

schemas returns the json schemais defining the providers configuration schema and the cluster schema for tha cloud provider

# File lib/hub-clusters-creator/agent.rb, line 93
def self.schemas(name)
  file = "#{__dir__}/providers/#{name}/schema.yaml"
  raise ArgumentError, "provider: '#{name}' is not supported" unless File.exist?(file)

  # loads and parses both the provider and cluster schema
  provider_schemas = YAML.load_stream(File.read(file))
  # load and parse the base schema which is used across all providers
  provider_base = YAML.safe_load(File.read("#{__dir__}/providers/schema.yaml"))
  # we deep merge the provider with the defaults
  provider_schemas.last.deep_merge(provider_base)

  provider_schemas
end

Public Instance Methods

destroy(name, options) click to toggle source

destroy is responsible is tearing down the cluster

# File lib/hub-clusters-creator/agent.rb, line 108
def destroy(name, options)
  @provider.destroy(name, options)
end
provision(options) click to toggle source

provision is responsible for provisioning the cluster rubocop:disable Lint/RescueException, Metrics/AbcSize

# File lib/hub-clusters-creator/agent.rb, line 114
def provision(options)
  name = options[:name]
  config = HubClustersCreator.defaults(@provider_name).merge(options)

  # @step: provision the cluster if not already there
  begin
    schema = HubClustersCreator::Agent.provider_schema(@provider_name)
    # verify the options
    JsonSchema.parse!(schema).validate(config)
    # provision the cluster
    @provider.create(name, config)
  rescue InfrastructureError => e
    error "failed to provision the infrastructure: #{name}, error: #{e}"
    raise e
  rescue ConfigurationError => e
    error "invalid configuration for cluster: #{name}, error: #{e}"
    raise e
  rescue InitializerError => e
    error "failed to initialize cluster: #{name}, error: #{e}"
    raise e
  rescue Exception => e
    error "failed to provision the cluster: #{name}, error: #{e}"
    raise e
  end
end