Skip to content

Commit

Permalink
allow all unknown kubernetes resources
Browse files Browse the repository at this point in the history
  • Loading branch information
eatwithforks committed Oct 8, 2018
1 parent aeab2e6 commit f6fb944
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 288 deletions.
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
33 changes: 5 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,6 @@
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

def initialize(elements)
@elements = elements.compact
Expand All @@ -23,7 +13,7 @@ def validate
return ["Only hashes supported"] unless @elements.all? { |e| e.is_a?(Hash) }
validate_name
validate_namespace
validate_kinds
validate_single_primary_kind
validate_api_version
validate_containers
validate_container_name
Expand Down Expand Up @@ -74,16 +64,11 @@ 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

def validate_api_version
Expand Down Expand Up @@ -257,14 +242,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
Loading

0 comments on commit f6fb944

Please sign in to comment.