Class: Clusters::Applications::Prometheus

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, Concerns::ApplicationCore, Concerns::ApplicationData, Concerns::ApplicationStatus, Concerns::ApplicationVersion, PrometheusAdapter
Defined in:
app/models/clusters/applications/prometheus.rb

Constant Summary collapse

VERSION =
'10.4.1'

Constants included from PrometheusAdapter

PrometheusAdapter::DEFAULT_PROMETHEUS_REQUEST_TIMEOUT_SEC

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from Concerns::ApplicationData

#files, #repository, #values

Methods included from Concerns::ApplicationVersion

#update_available?

Methods included from Concerns::ApplicationStatus

#available?, #status_states, #update_in_progress?, #updateable?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Instance Method Details

#chartObject


53
54
55
# File 'app/models/clusters/applications/prometheus.rb', line 53

def chart
  'stable/prometheus'
end

#configured?Boolean

Returns:

  • (Boolean)

120
121
122
123
124
# File 'app/models/clusters/applications/prometheus.rb', line 120

def configured?
  kube_client.present? && available?
rescue Gitlab::UrlBlocker::BlockedUrlError
  false
end

#files_with_replaced_values(replaced_values) ⇒ Object

Returns a copy of files where the values of 'values.yaml' are replaced by the argument.

See #values for the data format required


99
100
101
# File 'app/models/clusters/applications/prometheus.rb', line 99

def files_with_replaced_values(replaced_values)
  files.merge('values.yaml': replaced_values)
end

#generate_alert_manager_token!Object


126
127
128
129
130
# File 'app/models/clusters/applications/prometheus.rb', line 126

def generate_alert_manager_token!
  unless alert_manager_token.present?
    update!(alert_manager_token: generate_token)
  end
end

#install_commandObject


65
66
67
68
69
70
71
72
73
74
# File 'app/models/clusters/applications/prometheus.rb', line 65

def install_command
  Gitlab::Kubernetes::Helm::InstallCommand.new(
    name: name,
    version: VERSION,
    rbac: cluster.platform_kubernetes_rbac?,
    chart: chart,
    files: files,
    postinstall: install_knative_metrics
  )
end

#patch_command(values) ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'app/models/clusters/applications/prometheus.rb', line 76

def patch_command(values)
  ::Gitlab::Kubernetes::Helm::PatchCommand.new(
    name: name,
    version: version,
    rbac: cluster.platform_kubernetes_rbac?,
    chart: chart,
    files: files_with_replaced_values(values)
  )
end

#prometheus_clientObject


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/clusters/applications/prometheus.rb', line 103

def prometheus_client
  return unless kube_client

  proxy_url = kube_client.proxy_url('service', service_name, service_port, Gitlab::Kubernetes::Helm::NAMESPACE)

  # ensures headers containing auth data are appended to original k8s client options
  options = kube_client.rest_client.options
    .merge(prometheus_client_default_options)
    .merge(headers: kube_client.headers)
  Gitlab::PrometheusClient.new(proxy_url, options)
rescue Kubeclient::HttpError, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ENETUNREACH
  # If users have mistakenly set parameters or removed the depended clusters,
  # `proxy_url` could raise an exception because gitlab can not communicate with the cluster.
  # Since `PrometheusAdapter#can_query?` is eargely loaded on environement pages in gitlab,
  # we need to silence the exceptions
end

#service_nameObject


57
58
59
# File 'app/models/clusters/applications/prometheus.rb', line 57

def service_name
  'prometheus-prometheus-server'
end

#service_portObject


61
62
63
# File 'app/models/clusters/applications/prometheus.rb', line 61

def service_port
  80
end

#uninstall_commandObject


86
87
88
89
90
91
92
93
# File 'app/models/clusters/applications/prometheus.rb', line 86

def uninstall_command
  Gitlab::Kubernetes::Helm::DeleteCommand.new(
    name: name,
    rbac: cluster.platform_kubernetes_rbac?,
    files: files,
    predelete: delete_knative_istio_metrics
  )
end

#updated_since?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
50
51
# File 'app/models/clusters/applications/prometheus.rb', line 47

def updated_since?(timestamp)
  last_update_started_at &&
    last_update_started_at > timestamp &&
    !update_errored?
end