class Krane::TaskConfigValidator

Constants

DEFAULT_VALIDATIONS

Public Class Methods

new(task_config, kubectl, kubeclient_builder, only: nil) click to toggle source
# File lib/krane/task_config_validator.rb, line 14
def initialize(task_config, kubectl, kubeclient_builder, only: nil)
  @task_config = task_config
  @kubectl = kubectl
  @kubeclient_builder = kubeclient_builder
  @errors = nil
  @validations = only || DEFAULT_VALIDATIONS
end

Public Instance Methods

errors() click to toggle source
# File lib/krane/task_config_validator.rb, line 31
def errors
  valid?
  @errors
end
valid?() click to toggle source
# File lib/krane/task_config_validator.rb, line 22
def valid?
  @errors = []
  @validations.each do |validator_name|
    break if @errors.present?
    send(validator_name)
  end
  @errors.empty?
end

Private Instance Methods

server_version_warning(server_version) click to toggle source
# File lib/krane/task_config_validator.rb, line 91
def server_version_warning(server_version)
  "Minimum cluster version requirement of #{MIN_KUBE_VERSION} not met. "\
  "Using #{server_version} could result in unexpected behavior as it is no longer tested against"
end
validate_context_exists_in_kubeconfig() click to toggle source
# File lib/krane/task_config_validator.rb, line 42
def validate_context_exists_in_kubeconfig
  unless context.present?
    return @errors << "Context can not be blank"
  end

  _, err, st = @kubectl.run("config", "get-contexts", context, "-o", "name",
    use_namespace: false, use_context: false, log_failure: false, attempts: 2)

  unless st.success?
    @errors << if err.match("error: context #{context} not found")
      "Context #{context} missing from your kubeconfig file(s)"
    else
      "Something went wrong. #{err} "
    end
  end
end
validate_context_reachable() click to toggle source
# File lib/krane/task_config_validator.rb, line 59
def validate_context_reachable
  _, err, st = @kubectl.run("get", "namespaces", "-o", "name",
    use_namespace: false, log_failure: false, attempts: 2)

  unless st.success?
    @errors << "Something went wrong connecting to #{context}. #{err} "
  end
end
validate_kubeconfig() click to toggle source
# File lib/krane/task_config_validator.rb, line 38
def validate_kubeconfig
  @errors += @kubeclient_builder.validate_config_files
end
validate_namespace_exists() click to toggle source
# File lib/krane/task_config_validator.rb, line 68
def validate_namespace_exists
  unless namespace.present?
    return @errors << "Namespace can not be blank"
  end

  _, err, st = @kubectl.run("get", "namespace", "-o", "name", namespace,
    use_namespace: false, log_failure: false, attempts: 3)

  unless st.success?
    @errors << if err.match("Error from server [(]NotFound[)]: namespace")
      "Could not find Namespace: #{namespace} in Context: #{context}"
    else
      "Could not connect to kubernetes cluster. #{err}"
    end
  end
end
validate_server_version() click to toggle source
# File lib/krane/task_config_validator.rb, line 85
def validate_server_version
  if @kubectl.server_version < Gem::Version.new(MIN_KUBE_VERSION)
    logger.warn(server_version_warning(@kubectl.server_version))
  end
end