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

Add crd #306

Merged
merged 6 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions lib/kubernetes-deploy/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
stateful_set
cron_job
job
custom_resource_definition
).each do |subresource|
require "kubernetes-deploy/kubernetes_resource/#{subresource}"
end
Expand Down Expand Up @@ -248,6 +249,10 @@ def discover_resources
@logger.info " - #{r.id}"
end
end
if (global = resources.select(&:global?).presence)
@logger.warn("Detected non-namespaced #{'resource'.pluralize(global.count)} which will never be pruned:")
global.each { |r| @logger.warn(" - #{r.id}") }
end
resources
end

Expand Down
8 changes: 8 additions & 0 deletions lib/kubernetes-deploy/kubeclient_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def build_apps_v1beta1_kubeclient(context)
)
end

def build_apiextensions_v1beta1_kubeclient(context)
_build_kubeclient(
api_version: "v1beta1",
context: context,
endpoint_path: "/apis/apiextensions.k8s.io"
)
end

def _build_kubeclient(api_version:, context:, endpoint_path: nil)
# Find a context defined in kube conf files that matches the input context by name
friendly_configs = config_files.map { |f| GoogleFriendlyConfig.read(f) }
Expand Down
5 changes: 5 additions & 0 deletions lib/kubernetes-deploy/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class KubernetesResource
attr_reader :name, :namespace, :context
attr_writer :type, :deploy_started_at

GLOBAL = false
TIMEOUT = 5.minutes
LOG_LINE_COUNT = 250

Expand Down Expand Up @@ -315,6 +316,10 @@ def to_s
end
end

def global?
self.class::GLOBAL
end

private

def validate_timeout_annotation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true
module KubernetesDeploy
class CustomResourceDefinition < KubernetesResource
TIMEOUT = 2.minutes
GLOBAL = true

def deploy_succeeded?
names_accepted_status == "True"
end

def deploy_failed?
names_accepted_status == "False"
end

def timeout_message
"The names this CRD is attempting to register were neither accepted nor rejected in time"
end

def status
if !exists?
super
elsif deploy_succeeded?
"Names accepted"
else
"#{names_accepted_condition['reason']} (#{names_accepted_condition['message']})"
end
end

private

def names_accepted_condition
conditions = @instance_data.dig("status", "conditions") || []
conditions.detect { |c| c["type"] == "NamesAccepted" } || {}
end

def names_accepted_status
names_accepted_condition["status"]
end
end
end
13 changes: 13 additions & 0 deletions test/fixtures/hello-cloud/crd.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: mails-<%= deployment_id %>.stable.example.io
spec:
group: stable.example.io
names:
kind: Mail-<%= deployment_id %>
listKind: MailList-<%= deployment_id %>
plural: mails-<%= deployment_id %>
singular: mail-<%= deployment_id %>
scope: Namespaced
version: v1
6 changes: 6 additions & 0 deletions test/helpers/fixture_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ def assert_stateful_set_present(name)
desired = stateful_sets.find { |ss| ss.metadata.name == name }
assert desired.present?, "Stateful set #{name} does not exist"
end

def assert_crd_present(name)
crds = apiextensions_v1beta1_kubeclient.get_custom_resource_definitions
desired = crds.find { |crd| crd.metadata.name =~ name }
assert desired.present?, "CRD #{name} does not exist"
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/helpers/fixture_sets/hello_cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def assert_all_up
assert_daemon_set_up
assert_stateful_set_up
assert_job_up
assert_crd_up
end

def assert_unmanaged_pod_statuses(status, count = 1)
Expand Down Expand Up @@ -99,5 +100,10 @@ def assert_stateful_set_up
def assert_job_up
assert_job_exists("hello-job")
end

def assert_crd_up
crd_name = /mails[-\w]+.stable.example.io/
assert_crd_present(crd_name)
end
end
end
4 changes: 4 additions & 0 deletions test/helpers/kubeclient_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ def batch_v1beta1_kubeclient
def batch_v1_kubeclient
@batch_v1_kubeclient ||= build_batch_v1_kubeclient(MINIKUBE_CONTEXT)
end

def apiextensions_v1beta1_kubeclient
@apiextensions_v1beta1_kubeclient ||= build_apiextensions_v1beta1_kubeclient(MINIKUBE_CONTEXT)
end
end
40 changes: 37 additions & 3 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_full_hello_cloud_set_deploy_succeeds
%r{Deploying Pod/unmanaged-pod-[-\w]+ \(timeout: 60s\)}, # annotation timeout override
"Hello from the command runner!", # unmanaged pod logs
"Result: SUCCESS",
"Successfully deployed 19 resources"
"Successfully deployed 20 resources"
], in_order: true)

assert_logs_match_all([
Expand All @@ -23,7 +23,8 @@ def test_full_hello_cloud_set_deploy_succeeds
%r{StatefulSet/stateful-busybox},
%r{Service/redis-external\s+Doesn't require any endpoint},
"- Job/hello-job (timeout: 600s)",
%r{Job/hello-job\s+(Succeeded|Started)}
%r{Job/hello-job\s+(Succeeded|Started)},
%r{CustomResourceDefinition/mails[-\w]+.stable.example.io\s+Names accepted},
])

# Verify that success section isn't duplicated for predeployed resources
Expand Down Expand Up @@ -72,7 +73,7 @@ def test_pruning_works
'ingress(\.extensions)? "web"',
'daemonset(\.extensions)? "ds-app"',
'statefulset(\.apps)? "stateful-busybox"',
'job "hello-job"',
'job(\.batch)? "hello-job"',
] # not necessarily listed in this order
expected_msgs = [/Pruned 9 resources and successfully deployed 6 resources/]
expected_pruned.map do |resource|
Expand Down Expand Up @@ -1053,4 +1054,37 @@ def test_raise_on_yaml_missing_kind
" datapoint: value1"
], in_order: true)
end

def test_crd_can_be_successful
assert_deploy_success(deploy_fixtures("hello-cloud", subset: ["crd.yml.erb"]))
assert_logs_match_all([
"Phase 1: Initializing deploy",
"Detected non-namespaced resource which will never be pruned:",
%r{ - CustomResourceDefinition/mails[-\w]+.stable.example.io},
"Phase 2: Checking initial resource statuses",
%r{Deploying CustomResourceDefinition/mails[-\w]+.stable.example.io \(timeout: 120s\)},
%r{CustomResourceDefinition/mails[-\w]+.stable.example.io\s+Names accepted}
])
end

def test_crd_can_fail
result = deploy_fixtures("hello-cloud", subset: ["crd.yml.erb"]) do |f|
crd = f.dig("crd.yml.erb", "CustomResourceDefinition").first
names = crd.dig("spec", "names")
names["listKind"] = 'Conflict'
end
assert_deploy_success(result)

result = deploy_fixtures("hello-cloud", subset: ["crd.yml.erb"]) do |f|
crd = f.dig("crd.yml.erb", "CustomResourceDefinition").first
names = crd.dig("spec", "names")
names["listKind"] = 'Conflict'
end
assert_deploy_failure(result)
assert_logs_match_all([
%r{Deploying CustomResourceDefinition/mails[-\w]+.stable.example.io \(timeout: 120s\)},
%r{CustomResourceDefinition/mails[-\w]+.stable.example.io: FAILED},
'Final status: ListKindConflict ("Conflict" is already in use)'
])
end
end