Skip to content

Commit

Permalink
Add crd (#306)
Browse files Browse the repository at this point in the history
* Resource and tests
* Add warning about non-global resource
* PR feedback to provide better cli text
* Make CRD tests run serially since they use a global resource
  • Loading branch information
dturn committed Jul 31, 2018
1 parent 7b47ed8 commit 62bfcd6
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 2 deletions.
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/crd/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: mail.stable.example.io
spec:
group: stable.example.io
names:
kind: Mail
listKind: MailList
plural: mail
singular: mail
scope: Namespaced
version: v1
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: 40 additions & 0 deletions test/integration-serial/run_serial_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,44 @@ def test_multiple_configuration_files
ensure
ENV['KUBECONFIG'] = old_config
end

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

def test_crd_can_fail
result = deploy_fixtures("crd") 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("crd") do |f|
crd = f.dig("crd.yml.erb", "CustomResourceDefinition").first
names = crd.dig("spec", "names")
names["listKind"] = "Conflict"
names["plural"] = "others"
crd["metadata"]["name"] = "others.stable.example.io"
end
assert_deploy_failure(result)
assert_logs_match_all([
"Deploying CustomResourceDefinition/others.stable.example.io (timeout: 120s)",
"CustomResourceDefinition/others.stable.example.io: FAILED",
'Final status: ListKindConflict ("Conflict" is already in use)'
])
ensure
apiextensions_v1beta1_kubeclient.delete_custom_resource_definition("mail.stable.example.io")
apiextensions_v1beta1_kubeclient.delete_custom_resource_definition("others.stable.example.io")
end
end
4 changes: 2 additions & 2 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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)},
])

# Verify that success section isn't duplicated for predeployed resources
Expand Down Expand Up @@ -72,7 +72,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

0 comments on commit 62bfcd6

Please sign in to comment.