debian-mirror-gitlab/app/models/clusters/applications/knative.rb

157 lines
4.7 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
module Clusters
module Applications
2021-03-11 19:13:27 +05:30
# DEPRECATED for removal in %14.0
# See https://gitlab.com/groups/gitlab-org/-/epics/4280
2019-06-05 12:25:43 +05:30
class Knative < ApplicationRecord
2021-03-08 18:12:59 +05:30
VERSION = '0.10.0'
2020-05-24 23:13:21 +05:30
REPOSITORY = 'https://charts.gitlab.io'
METRICS_CONFIG = 'https://gitlab.com/gitlab-org/charts/knative/-/raw/v0.9.0/vendor/istio-metrics.yml'
2019-02-15 15:39:39 +05:30
FETCH_IP_ADDRESS_DELAY = 30.seconds
2020-01-01 13:55:28 +05:30
API_GROUPS_PATH = 'config/knative/api_groups.yml'
2018-12-13 13:39:08 +05:30
self.table_name = 'clusters_applications_knative'
2020-03-13 15:44:24 +05:30
has_one :serverless_domain_cluster, class_name: '::Serverless::DomainCluster', foreign_key: 'clusters_applications_knative_id', inverse_of: :knative
2020-01-01 13:55:28 +05:30
2018-12-13 13:39:08 +05:30
include ::Clusters::Concerns::ApplicationCore
include ::Clusters::Concerns::ApplicationStatus
include ::Clusters::Concerns::ApplicationVersion
include ::Clusters::Concerns::ApplicationData
2019-02-15 15:39:39 +05:30
include AfterCommitQueue
2020-01-01 13:55:28 +05:30
alias_method :original_set_initial_status, :set_initial_status
2019-02-15 15:39:39 +05:30
def set_initial_status
2020-01-01 13:55:28 +05:30
return unless cluster&.platform_kubernetes_rbac?
2019-02-15 15:39:39 +05:30
2020-01-01 13:55:28 +05:30
original_set_initial_status
2019-02-15 15:39:39 +05:30
end
state_machine :status do
after_transition any => [:installed] do |application|
application.run_after_commit do
ClusterWaitForIngressIpAddressWorker.perform_in(
FETCH_IP_ADDRESS_DELAY, application.name, application.id)
end
end
2020-04-08 14:13:33 +05:30
after_transition any => [:installed, :updated] do |application|
application.run_after_commit do
ClusterConfigureIstioWorker.perform_async(application.cluster_id)
end
end
2019-02-15 15:39:39 +05:30
end
2018-12-13 13:39:08 +05:30
default_value_for :version, VERSION
validates :hostname, presence: true, hostname: true
2019-02-15 15:39:39 +05:30
scope :for_cluster, -> (cluster) { where(cluster: cluster) }
2020-04-08 14:13:33 +05:30
has_one :pages_domain, through: :serverless_domain_cluster
2018-12-13 13:39:08 +05:30
def chart
'knative/knative'
end
def values
{ "domain" => hostname }.to_yaml
end
2020-04-08 14:13:33 +05:30
def available_domains
PagesDomain.instance_serverless
end
def find_available_domain(pages_domain_id)
available_domains.find_by(id: pages_domain_id)
end
2019-12-21 20:55:43 +05:30
def allowed_to_uninstall?
!pre_installed?
end
2018-12-13 13:39:08 +05:30
def install_command
2021-01-29 00:20:46 +05:30
helm_command_module::InstallCommand.new(
2018-12-13 13:39:08 +05:30
name: name,
version: VERSION,
rbac: cluster.platform_kubernetes_rbac?,
chart: chart,
files: files,
2019-01-03 12:48:30 +05:30
repository: REPOSITORY,
2020-10-24 23:57:45 +05:30
postinstall: install_knative_metrics
2018-12-13 13:39:08 +05:30
)
end
2019-02-15 15:39:39 +05:30
def schedule_status_update
return unless installed?
return if external_ip
2019-07-07 11:18:12 +05:30
return if external_hostname
2019-02-15 15:39:39 +05:30
ClusterWaitForIngressIpAddressWorker.perform_async(name, id)
end
def ingress_service
2020-03-13 15:44:24 +05:30
cluster.kubeclient.get_service('istio-ingressgateway', Clusters::Kubernetes::ISTIO_SYSTEM_NAMESPACE)
2019-02-15 15:39:39 +05:30
end
2019-10-12 21:52:04 +05:30
def uninstall_command
2021-01-29 00:20:46 +05:30
helm_command_module::DeleteCommand.new(
2019-10-12 21:52:04 +05:30
name: name,
rbac: cluster.platform_kubernetes_rbac?,
files: files,
predelete: delete_knative_services_and_metrics,
2020-10-24 23:57:45 +05:30
postdelete: delete_knative_istio_leftovers
2019-10-12 21:52:04 +05:30
)
end
2018-12-13 13:39:08 +05:30
private
2019-10-12 21:52:04 +05:30
def delete_knative_services_and_metrics
delete_knative_services + delete_knative_istio_metrics
end
def delete_knative_services
cluster.kubernetes_namespaces.map do |kubernetes_namespace|
Gitlab::Kubernetes::KubectlCmd.delete("ksvc", "--all", "-n", kubernetes_namespace.namespace)
end
end
def delete_knative_istio_leftovers
delete_knative_namespaces + delete_knative_and_istio_crds
end
def delete_knative_namespaces
[
Gitlab::Kubernetes::KubectlCmd.delete("--ignore-not-found", "ns", "knative-serving"),
Gitlab::Kubernetes::KubectlCmd.delete("--ignore-not-found", "ns", "knative-build")
]
end
def delete_knative_and_istio_crds
2020-01-01 13:55:28 +05:30
api_groups.map do |group|
Gitlab::Kubernetes::KubectlCmd.delete_crds_from_group(group)
2019-10-12 21:52:04 +05:30
end
end
# returns an array of CRDs to be postdelete since helm does not
# manage the CRDs it creates.
2020-01-01 13:55:28 +05:30
def api_groups
@api_groups ||= YAML.safe_load(File.read(Rails.root.join(API_GROUPS_PATH)))
2019-10-12 21:52:04 +05:30
end
2019-02-15 15:39:39 +05:30
def install_knative_metrics
2019-10-12 21:52:04 +05:30
return [] unless cluster.application_prometheus_available?
[Gitlab::Kubernetes::KubectlCmd.apply_file(METRICS_CONFIG)]
end
def delete_knative_istio_metrics
return [] unless cluster.application_prometheus_available?
[Gitlab::Kubernetes::KubectlCmd.delete("--ignore-not-found", "-f", METRICS_CONFIG)]
2019-02-15 15:39:39 +05:30
end
2018-12-13 13:39:08 +05:30
end
end
end