Class: HubClustersCreator::Agent

Inherits:
Object
  • Object
show all
Includes:
Errors, Logging
Defined in:
lib/hub-clusters-creator/agent.rb

Overview

Agent is the main agent class

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#error, #info, #warn

Constructor Details

#initialize(provider) ⇒ Agent

rubocop:disable Metrics/AbcSize



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

Class Method Details

.cluster_schema(name) ⇒ Object

cluster_schema returns a cluster schema for a specific provider



87
88
89
# File 'lib/hub-clusters-creator/agent.rb', line 87

def self.cluster_schema(name)
  schemas(name).last
end

.defaults(name) ⇒ Object

defaults builds the default from the schema



69
70
71
72
73
74
75
76
77
78
79
# 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

.provider_schema(name) ⇒ Object

provider_schema returns the provider schema



82
83
84
# File 'lib/hub-clusters-creator/agent.rb', line 82

def self.provider_schema(name)
  schemas(name).first
end

.providersObject

providers provides a list of providers



64
65
66
# File 'lib/hub-clusters-creator/agent.rb', line 64

def self.providers
  %w[aks gke]
end

.schemas(name) ⇒ Object

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

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

Instance Method Details

#destroy(name, options) ⇒ Object

destroy is responsible is tearing down the cluster



108
109
110
# File 'lib/hub-clusters-creator/agent.rb', line 108

def destroy(name, options)
  @provider.destroy(name, options)
end

#provision(options) ⇒ Object

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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