Skip to content

Commit

Permalink
Fix kontena-lens create-config success check (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakolehm authored Nov 16, 2018
1 parent bdd2624 commit efbf486
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions non-oss/pharos_pro/addons/kontena-lens/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ def pastel
@pastel ||= Pastel.new
end

# @return [Pharos::Configuration::Host]
def gateway_node
cluster_config.worker_hosts.first || cluster_config.master_hosts.first
end

# @return [String, NilClass]
def gateway_node_ip
gateway_node&.address
end

# @return [String, NilClass]
def master_host_ip
cluster_config.master_host&.address
end
Expand All @@ -70,6 +73,7 @@ def user_management_enabled?
config.user_management&.enabled != false
end

# @return [Pharos::Configuration::TokenWebhook]
def token_authentication_webhook_config
Pharos::Configuration::TokenWebhook.new(
config: {
Expand All @@ -84,9 +88,11 @@ def token_authentication_webhook_config
)
end

# @param protocol [String]
# @param host [String]
def wait_for_dashboard(protocol, host)
puts " Waiting for Kontena Lens to get up and running ..."
command = "sudo curl -LkIs -o /dev/null -w \"%{http_code}\" -H \"Host: #{host}\" #{protocol}://localhost/" # rubocop:disable Style/FormatStringToken
command = "curl -LkIs -o /dev/null -w \"%{http_code}\" -H \"Host: #{host}\" #{protocol}://localhost/" # rubocop:disable Style/FormatStringToken
response = ssh.exec(command)
i = 1
until response.output.to_i == 200
Expand All @@ -97,6 +103,7 @@ def wait_for_dashboard(protocol, host)
end
end

# @return [String]
def admin_password
@admin_password ||= SecureRandom.hex(8)
end
Expand All @@ -105,21 +112,27 @@ def lens_configured?
!configmap.nil?
end

# @return [K8s::Resource, NilClass]
def configmap
@configmap ||= kube_client.api('v1').resource('configmaps', namespace: 'kontena-lens').get('config')
rescue K8s::Error::NotFound
nil
end

# @param name [String]
# @param protocol [String]
# @param host [String]
# @param admin_password [String]
# @raise [Pharos::InvalidAddonError]
def create_lens_config(name, protocol, host, admin_password)
cluster_config = {
clusterName: name,
clusterUrl: "https://#{master_host_ip}:6443",
adminPassword: admin_password
}
command = "sudo curl -iksL -X POST -d '#{cluster_config.to_json}' -H \"Host: #{host}\" -H \"Content-Type: application/json\" #{protocol}://localhost/api/cluster"
result = ssh.exec(command)
raise Pharos::InvalidAddonError, "Could not create Kontena Lens configuration" unless result.output.lines.include?("HTTP/1.1 200 OK\r\n")
command = "curl -iksL -o /dev/null -w \"%{http_code}\" -X POST -d '#{cluster_config.to_json}' -H \"Host: #{host}\" -H \"Content-Type: application/json\" #{protocol}://localhost/api/cluster" # rubocop:disable Style/FormatStringToken
response = ssh.exec(command)
raise Pharos::InvalidAddonError, "Could not create Kontena Lens configuration" unless response.output.to_i == 200
rescue Pharos::InvalidAddonError => e
if @retries <= @max_retries
@retries += 1
Expand All @@ -132,6 +145,8 @@ def create_lens_config(name, protocol, host, admin_password)
raise Pharos::InvalidAddonError, e.message
end

# @param new_name [String]
# @return [K8s::Resource]
def update_lens_name(new_name)
configmap.data.clusterName = new_name
kube_client.api('v1').resource('configmaps', namespace: 'kontena-lens').update_resource(configmap)
Expand Down

0 comments on commit efbf486

Please sign in to comment.