Class: ClusterService

Inherits:
Object
  • Object
show all
Extended by:
ApiClient
Defined in:
lib/uffizzi/services/cluster_service.rb

Constant Summary collapse

CLUSTER_STATE_DEPLOYING_NAMESPACE =
'deploying_namespace'
CLUSTER_STATE_DEPLOYING =
'deploying'
CLUSTER_STATE_DEPLOYED =
'deployed'
CLUSTER_STATE_SCALING_DOWN =
'scaling_down'
CLUSTER_STATE_SCALED_DOWN =
'scaled_down'
CLUSTER_STATE_SCALING_UP =
'scaling_up'
CLUSTER_FAILED_SCALING_UP =
'failed_scaling_up'
CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE =
'failed_deploy_namespace'
CLUSTER_STATE_FAILED =
'failed'
CLUSTER_NAME_MAX_LENGTH =
15
MANUAL_CREATION_SOURCE =
'manual'

Class Method Summary collapse

Methods included from ApiClient

bulk_create_secrets, check_can_install, check_credential, create_access_token, create_account_controller_settings, create_ci_session, create_cluster, create_credential, create_deployment, create_project, create_session, delete_account_controller_settings, delete_cluster, delete_credential, delete_deployment, delete_project, delete_secret, deploy_containers, describe_compose_file, describe_deployment, destroy_session, fetch_account, fetch_account_projects, fetch_accounts, fetch_credentials, fetch_deployment_service_logs, fetch_deployment_services, fetch_deployments, fetch_events, fetch_project, fetch_projects, fetch_secrets, get_access_token, get_account_clusters, get_account_controller_settings, get_activity_items, get_cluster, get_cluster_ingresses, get_k8s_container_description, get_project_clusters, scale_down_cluster, scale_up_cluster, set_compose_file, sync_cluster, unset_compose_file, update_account, update_account_controller_settings, update_credential, update_deployment, validate_compose_file

Methods included from ApiRoutes

#access_token_url, #access_tokens_url, #account_can_install_uri, #account_clusters_uri, #account_controller_setting_uri, #account_controller_settings_uri, #account_projects_uri, #account_uri, #accounts_uri, #activity_items_uri, #browser_sign_in_url, #check_credential_uri, #ci_session_uri, #cluster_uri, #compose_file_uri, #create_projects_uri, #credential_uri, #credentials_uri, #deploy_containers_uri, #deployment_uri, #deployments_uri, #events_uri, #k8s_container_description_uri, #preview_service_logs_uri, #preview_services_uri, #project_cluster_ingresses_uri, #project_clusters_uri, #project_uri, #projects_uri, #scale_down_cluster_uri, #scale_up_cluster_uri, #secret_uri, #secrets_uri, #session_uri, #sync_cluster_uri, #validate_compose_file_uri

Class Method Details

.build_render_data(cluster_data) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/uffizzi/services/cluster_service.rb', line 116

def build_render_data(cluster_data)
  {
    name: cluster_data[:name],
    status: cluster_data[:state],
    created: Time.strptime(cluster_data[:created_at], '%Y-%m-%dT%H:%M:%S.%N').strftime('%a %b %d %H:%M:%S %Y'),
    host: cluster_data[:host],
  }
end

.cluster_status_text_mapObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/uffizzi/services/cluster_service.rb', line 125

def cluster_status_text_map
  {
    CLUSTER_STATE_SCALING_UP => 'The cluster is scaling up',
    CLUSTER_STATE_SCALED_DOWN => 'The cluster is scaled down',
    CLUSTER_STATE_SCALING_DOWN => 'The cluster is scaling down',
    CLUSTER_FAILED_SCALING_UP => 'The cluster failed scaling up',
    CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE => 'The cluster failed',
    CLUSTER_STATE_FAILED => 'The cluster failed',
  }
end

.deployed?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/uffizzi/services/cluster_service.rb', line 22

def deployed?(cluster_state)
  cluster_state == CLUSTER_STATE_DEPLOYED
end

.deploying?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/uffizzi/services/cluster_service.rb', line 26

def deploying?(cluster_state)
  [CLUSTER_STATE_DEPLOYING_NAMESPACE, CLUSTER_STATE_DEPLOYING].include?(cluster_state)
end

.failed?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/uffizzi/services/cluster_service.rb', line 30

def failed?(cluster_state)
  [CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE, CLUSTER_STATE_FAILED].include?(cluster_state)
end

.failed_scaling_up?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/uffizzi/services/cluster_service.rb', line 46

def failed_scaling_up?(cluster_state)
  cluster_state === CLUSTER_FAILED_SCALING_UP
end

.fetch_cluster_data(cluster_name, server:, project_slug:, oidc_token:) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/uffizzi/services/cluster_service.rb', line 92

def fetch_cluster_data(cluster_name, server:, project_slug:, oidc_token:)
  params = {
    cluster_name: cluster_name,
    oidc_token: oidc_token,
  }
  response = get_cluster(server, project_slug, params)

  if Uffizzi::ResponseHelper.ok?(response)
    response.dig(:body, :cluster)
  else
    Uffizzi::ResponseHelper.handle_failed_response(response)
  end
end

.generate_nameObject



77
78
79
80
81
82
83
# File 'lib/uffizzi/services/cluster_service.rb', line 77

def generate_name
  name = Faker::Internet.domain_word[0..CLUSTER_NAME_MAX_LENGTH]

  return name if valid_name?(name)

  generate_name
end

.scaled_down?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/uffizzi/services/cluster_service.rb', line 42

def scaled_down?(cluster_state)
  cluster_state === CLUSTER_STATE_SCALED_DOWN
end

.scaling_down?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/uffizzi/services/cluster_service.rb', line 38

def scaling_down?(cluster_state)
  cluster_state === CLUSTER_STATE_SCALING_DOWN
end

.scaling_up?(cluster_state) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/uffizzi/services/cluster_service.rb', line 34

def scaling_up?(cluster_state)
  cluster_state === CLUSTER_STATE_SCALING_UP
end

.sync_cluster_data(cluster_name, server:, project_slug:) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/uffizzi/services/cluster_service.rb', line 106

def sync_cluster_data(cluster_name, server:, project_slug:)
  response = sync_cluster(server, project_slug, cluster_name)

  if Uffizzi::ResponseHelper.ok?(response)
    response.dig(:body, :cluster)
  else
    Uffizzi::ResponseHelper.handle_failed_response(response)
  end
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/uffizzi/services/cluster_service.rb', line 85

def valid_name?(name)
  return false if name.nil?

  regex = /\A[a-zA-Z0-9-]*\z/
  regex.match?(name)
end

.wait_cluster_deploy(cluster_name, cluster_api_connection_params) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/uffizzi/services/cluster_service.rb', line 50

def wait_cluster_deploy(cluster_name, cluster_api_connection_params)
  loop do
    cluster_data = fetch_cluster_data(cluster_name, **cluster_api_connection_params)
    return cluster_data unless deploying?(cluster_data[:state])

    sleep(5)
  end
end

.wait_cluster_scale_down(cluster_name, cluster_api_connection_params) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/uffizzi/services/cluster_service.rb', line 68

def wait_cluster_scale_down(cluster_name, cluster_api_connection_params)
  loop do
    cluster_data = fetch_cluster_data(cluster_name, **cluster_api_connection_params)
    return unless scaling_down?(cluster_data[:state])

    sleep(3)
  end
end

.wait_cluster_scale_up(cluster_name, cluster_api_connection_params) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/uffizzi/services/cluster_service.rb', line 59

def wait_cluster_scale_up(cluster_name, cluster_api_connection_params)
  loop do
    cluster_data = fetch_cluster_data(cluster_name, **cluster_api_connection_params)
    return cluster_data unless scaling_up?(cluster_data[:state])

    sleep(5)
  end
end