Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support all kubernetes resources by default #2981

Merged
merged 1 commit into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions plugins/kubernetes/app/models/kubernetes/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,6 @@ def ignore_404
end
end

class ConfigMap < Base
end

class HorizontalPodAutoscaler < Base
end

class Service < Base
private

Expand Down Expand Up @@ -429,9 +423,6 @@ def request_delete
end
end

class CronJob < Base
end

class Pod < Base
def deploy
delete
Expand All @@ -448,7 +439,8 @@ def deploy
end

def self.build(*args)
"Kubernetes::Resource::#{args.first.fetch(:kind)}".constantize.new(*args)
klass = "Kubernetes::Resource::#{args.first.fetch(:kind)}".safe_constantize || Base
klass.new(*args)
end
end
end
43 changes: 15 additions & 28 deletions plugins/kubernetes/app/models/kubernetes/role_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@
module Kubernetes
class RoleValidator
VALID_LABEL = /\A[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?\z/ # also used in js ... cannot use /i
IGNORED = ['ConfigMap', 'HorizontalPodAutoscaler', 'PodDisruptionBudget'].freeze
SUPPORTED_KINDS = [
['Deployment'],
['DaemonSet'],
['Deployment', 'Service'],
['Service', 'StatefulSet'],
['Job'],
['CronJob'],
['Pod'],
].freeze
ALLOWED_DUPLICATE_KINDS = ['ConfigMap', 'Service'].freeze

def initialize(elements)
@elements = elements.compact
Expand All @@ -22,8 +13,9 @@ def validate
return ["No content found"] if @elements.blank?
return ["Only hashes supported"] unless @elements.all? { |e| e.is_a?(Hash) }
validate_name
validate_name_kinds_are_unique
validate_namespace
validate_kinds
validate_single_primary_kind
validate_api_version
validate_containers
validate_container_name
Expand Down Expand Up @@ -74,16 +66,19 @@ def validate_namespace
@errors << "Namespaces need to be unique" if map_attributes([:metadata, :namespace]).uniq.size != 1
end

def validate_kinds
# multiple pods in a single role will make validations misbehave (recommend they all have the same role etc)
def validate_single_primary_kind
kinds = map_attributes([:kind])
IGNORED.each { |k| kinds.delete k }
uniq_element!(kinds, 'Service') # ignore multiple services
kinds.sort_by!(&:to_s)

return if SUPPORTED_KINDS.include?(kinds)
supported = SUPPORTED_KINDS.map { |c| c.join(' + ') }.join(', ')
@errors << "Unsupported combination of kinds: #{kinds.join(' + ')}" \
", supported combinations are: #{supported} and #{IGNORED.join(", ")}"
return if kinds.count { |k| RoleConfigFile::PRIMARY_KINDS.include?(k) } < 2
@errors << "Only use a maximum of 1 primary kind in a role (#{RoleConfigFile::PRIMARY_KINDS.join(", ")})"
end

# template_filler.rb sets name for everything except for ConfigMaps and Service so we need to make sure
# users dont use the same kind otherwise they get a duplicate name
def validate_name_kinds_are_unique
kinds = map_attributes([:kind]) - ALLOWED_DUPLICATE_KINDS
return if kinds.uniq.size == kinds.size
@errors << "Only use a maximum of 1 of each kind in a role (except #{ALLOWED_DUPLICATE_KINDS.join(" and ")})"
end

def validate_api_version
Expand Down Expand Up @@ -257,14 +252,6 @@ def validate_host_volume_paths

# helpers below

# [1,2,3,1,4] -> [2,3,4,1]
def uniq_element!(array, element)
if array.count(element) > 1
array.delete(element)
array << element
end
end

def find_stateful_set
@elements.detect { |t| t[:kind] == "StatefulSet" }
end
Expand Down
8 changes: 8 additions & 0 deletions plugins/kubernetes/app/models/kubernetes/template_filler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def to_hash(verification: false)

case kind
when 'HorizontalPodAutoscaler'
set_name
set_hpa_scale_target_name
when 'ConfigMap' # rubocop:disable Lint/EmptyWhen
# referenced in other resources so we cannot change the name
# NOTE: may cause multiple projects to override each others ConfigMaps if they chose duplicate names
when *Kubernetes::RoleConfigFile::SERVICE_KINDS
set_service_name
prefix_service_cluster_ip
Expand Down Expand Up @@ -53,6 +57,8 @@ def to_hash(verification: false)
set_image_pull_secrets
set_resource_blue_green if blue_green_color
set_init_containers
else
set_name
end
template
end
Expand Down Expand Up @@ -121,7 +127,9 @@ def set_service_name
end

def generate_service_name(config_name)
# when no service name was chosen we use the name from the config, which could lead to duplication
return config_name unless name = @doc.kubernetes_role.service_name.presence

if name.include?(Kubernetes::Role::GENERATED)
raise(
Samson::Hooks::UserError,
Expand Down
Loading