-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
200 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
module KubernetesDeploy | ||
class TaskConfig | ||
attr_reader :context, :namespace | ||
|
||
def initialize(context, namespace, logger = nil) | ||
@context = context | ||
@namespace = namespace | ||
@logger = logger | ||
end | ||
|
||
def logger | ||
@logger ||= KubernetesDeploy::FormattedLogger.build(@namespace, @context) | ||
end | ||
|
||
def kubectl(log_failure_by_default: true) | ||
@kubectl ||= Kubectl.new( | ||
namespace: @namespace, | ||
context: @context, | ||
logger: logger, | ||
log_failure_by_default: log_failure_by_default | ||
) | ||
end | ||
|
||
def kubeclient_builder | ||
@kubeclient ||= KubeclientBuilder.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
module KubernetesDeploy | ||
class Validator | ||
DEFAULT_VALIDATIONS = %i( | ||
validate_kubeconfig | ||
validate_context_exists | ||
validate_namespace_exists | ||
validate_server_version | ||
).freeze | ||
|
||
delegate :context, :namespace, :logger, :kubectl, :kubeclient_builder, to: :@task_config | ||
|
||
def initialize(task_config) | ||
@task_config = task_config | ||
@errors = nil | ||
end | ||
|
||
def valid? | ||
@errors = [] | ||
DEFAULT_VALIDATIONS.each do |validator_name| | ||
send(validator_name) | ||
break if @errors.present? | ||
end | ||
@errors.empty? | ||
end | ||
|
||
def errors | ||
valid? | ||
@errors | ||
end | ||
|
||
private | ||
|
||
def validate_kubeconfig | ||
@errors += kubeclient_builder.validate_config_files | ||
end | ||
|
||
def validate_context_exists | ||
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) | ||
|
||
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 | ||
return | ||
end | ||
|
||
_, err, st = kubectl.run("get", "namespaces", "-o", "name", | ||
use_namespace: false, log_failure: false) | ||
|
||
unless st.success? | ||
@errors << "Something went wrong connectting to #{context}. #{err} " | ||
end | ||
end | ||
|
||
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) | ||
|
||
unless st.success? | ||
@errors << if err.match("Error from server [(]NotFound[)]: namespace") | ||
"Cloud not find Namespace: #{namespace} in Context: #{context}" | ||
else | ||
"Could not connect to kubernetes cluster. #{err}" | ||
end | ||
end | ||
end | ||
|
||
def validate_server_version | ||
if kubectl.server_version < Gem::Version.new(MIN_KUBE_VERSION) | ||
logger.warn(KubernetesDeploy::Errors.server_version_warning(kubectl.server_version)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
require 'integration_test_helper' | ||
|
||
class ValidatorTest < KubernetesDeploy::IntegrationTest | ||
def test_valid_configuration | ||
assert_predicate(validator(context: KubeclientHelper::TEST_CONTEXT, namespace: 'default'), :valid?) | ||
end | ||
|
||
def test_invalid_kubeconfig | ||
assert_match(/Context test-context missing from/, validator.errors.join("\n")) | ||
end | ||
|
||
def test_context_does_not_exists | ||
assert_match("Context test-context missing from your kubeconfig file(s)", | ||
validator.errors.join("\n")) | ||
end | ||
|
||
def test_namespace_does_not_exists | ||
assert_match(/Cloud not find Namespace: test-namespace in Context: #{KubeclientHelper::TEST_CONTEXT}/, | ||
validator(context: KubeclientHelper::TEST_CONTEXT).errors.join("\n")) | ||
end | ||
|
||
def test_invalid_server_version | ||
old_min_version = KubernetesDeploy::MIN_KUBE_VERSION | ||
new_min_version = "99999" | ||
KubernetesDeploy.const_set(:MIN_KUBE_VERSION, new_min_version) | ||
validator(context: KubeclientHelper::TEST_CONTEXT, namespace: 'default', logger: @logger).valid? | ||
assert_logs_match_all([ | ||
"Minimum cluster version requirement of #{new_min_version} not met.", | ||
]) | ||
ensure | ||
KubernetesDeploy.const_set(:MIN_KUBE_VERSION, old_min_version) | ||
end | ||
|
||
private | ||
|
||
def validator(context: nil, namespace: nil, logger: nil) | ||
KubernetesDeploy::Validator.new(task_config(context: context, namespace: namespace, logger: logger)) | ||
end | ||
|
||
def task_config(context: nil, namespace: nil, logger: nil) | ||
KubernetesDeploy::TaskConfig.new(context || "test-context", namespace || "test-namespace", logger) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class TaskConfigTest < KubernetesDeploy::TestCase | ||
def test_responds_to_namespace | ||
assert_equal(task_config.namespace, "test-namespace") | ||
end | ||
|
||
def test_responds_to_context | ||
assert_equal(task_config.context, "test-context") | ||
end | ||
|
||
def test_builds_a_logger_if_none_provided | ||
assert_equal(task_config.logger.class, KubernetesDeploy::FormattedLogger) | ||
end | ||
|
||
def test_uses_provided_logger | ||
logger = KubernetesDeploy::FormattedLogger.build(nil, nil) | ||
assert_equal(task_config(logger: logger).logger, logger) | ||
end | ||
|
||
private | ||
|
||
def task_config(context: nil, namespace: nil, logger: nil) | ||
KubernetesDeploy::TaskConfig.new(context || "test-context", namespace || "test-namespace", logger) | ||
end | ||
end |