Skip to content

Commit

Permalink
fix: initial commit for deploy fix when having duplicate kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
renescheepers committed Jul 13, 2022
1 parent a5f4579 commit 4f87cf4
Show file tree
Hide file tree
Showing 34 changed files with 237 additions and 10 deletions.
22 changes: 22 additions & 0 deletions crds/crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: deployments.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
thiny:
type: string
scope: Cluster
names:
plural: deployments
singular: deployment
kind: Deployment
41 changes: 41 additions & 0 deletions crds/create_crds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "yaml"

1.times do |nr|
crd_yaml = {
"apiVersion" => "apiextensions.k8s.io/v1",
"kind" => "CustomResourceDefinition",
"metadata" => {
"name" => "deploymentss.stable.example.com",
},
"spec" => {
"group" => "stable.example.com",
"versions" => [
{
"name" => "v1",
"served" => true,
"storage" => true,
"schema" => {
"openAPIV3Schema" => {
"type" => "object",
"properties" => {
"thiny" => {
"type" => "string",
}
}
},
},
},
],
"scope" => "Cluster",
"names" => {
"plural" => "deployments",
"singular" => "deployment",
"kind" => "Deployment",
},
},
}

yaml = YAML.dump(crd_yaml)

File.write(File.expand_path("crd.yaml", __dir__), yaml)
end
16 changes: 16 additions & 0 deletions krane-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
deploy: example
template:
metadata:
labels:
deploy: example
spec:
containers:
- name: nginx
image: nginx
1 change: 1 addition & 0 deletions lib/krane/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def resource_hash(path, namespaced, blob)
path_regex = %r{(/apis?/)(?<group>[^/]*)/?(?<version>v.+)}
match = path.match(path_regex)
{
"name" => blob["name"],
"verbs" => blob["verbs"],
"kind" => blob["kind"],
"apigroup" => match[:group],
Expand Down
8 changes: 8 additions & 0 deletions lib/krane/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@

module Krane
MIN_KUBE_VERSION = '1.15.0'

def self.group_from_api_version(input)
input.include?("/") ? input.split("/").first : ""
end

def self.group_kind(group, kind)
"#{kind}.#{group}"
end
end
6 changes: 4 additions & 2 deletions lib/krane/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ def secrets_from_ejson
def discover_resources
@logger.info("Discovering resources:")
resources = []
crds_by_kind = cluster_resource_discoverer.crds.group_by(&:kind)
crds_by_kind = cluster_resource_discoverer.crds.group_by(&:group_kind)
@template_sets.with_resource_definitions(current_sha: @current_sha, bindings: @bindings) do |r_def|
crd = crds_by_kind[r_def["kind"]]&.first
group = Krane.group_from_api_version(r_def["apiVersion"])

crd = crds_by_kind[Krane.group_kind(group, r_def["kind"])]&.first
r = KubernetesResource.build(namespace: @namespace, context: @context, logger: @logger, definition: r_def,
statsd_tags: @namespace_tags, crd: crd, global_names: @task_config.global_kinds)
resources << r
Expand Down
9 changes: 7 additions & 2 deletions lib/krane/kubectl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Kubectl
empty: /\A\z/,
context_deadline: /context deadline exceeded/,
}
DEFAULT_TIMEOUT = 15
DEFAULT_TIMEOUT = 30
MAX_RETRY_DELAY = 16
SERVER_DRY_RUN_MIN_VERSION = "1.13"

Expand All @@ -35,6 +35,7 @@ def run(*args, log_failure: nil, use_context: true, use_namespace: true, output:

(1..attempts).to_a.each do |current_attempt|
logger.debug("Running command (attempt #{current_attempt}): #{cmd.join(' ')}")
cmd_string = cmd.join(' ')
env = { 'KUBECONFIG' => kubeconfig }
out, err, st = Open3.capture3(env, *cmd)

Expand All @@ -43,9 +44,13 @@ def run(*args, log_failure: nil, use_context: true, use_namespace: true, output:
out = out.dup.force_encoding(Encoding::UTF_8)
end

if cmd_string.include?("kubectl get Deploy")
# logger.debug(out)
end

if logger.debug? && !output_is_sensitive
# don't do the gsub unless we're going to print this
logger.debug("Kubectl out: " + out.gsub(/\s+/, ' '))
# logger.debug("Kubectl out: " + out.gsub(/\s+/, ' '))
end

break if st.success?
Expand Down
33 changes: 30 additions & 3 deletions lib/krane/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ class KubernetesResource
class << self
def build(namespace: nil, context:, definition:, logger:, statsd_tags:, crd: nil, global_names: [])
validate_definition_essentials(definition)

group = Krane.group_from_api_version(definition["apiVersion"])
opts = { namespace: namespace, context: context, definition: definition, logger: logger,
statsd_tags: statsd_tags }
if (klass = class_for_kind(definition["kind"]))
if (klass = class_for_group_kind(group, definition["kind"]))
return klass.new(**opts)
end
if crd
CustomResource.new(crd: crd, **opts)
else
type = definition["kind"]
type = Krane.group_kind(definition["group"], definition["kind"])
inst = new(**opts)
inst.type = type
inst.global = global_names.map(&:downcase).include?(type.downcase)
Expand All @@ -70,6 +72,26 @@ def class_for_kind(kind)
nil
end

def class_for_group_kind(group, kind)
if Krane.const_defined?(kind, false)
const = Krane.const_get(kind, false)

if const_defined?("GROUPS")
groups = const.const_get("GROUPS")

if groups.include?(group)
const
else
nil
end
else
const
end
end
rescue NameError
nil
end

def timeout
self::TIMEOUT
end
Expand All @@ -78,6 +100,11 @@ def kind
name.demodulize
end

def group
const_get("GROUPS").first
# self.class.GROUPS.first
end

private

def validate_definition_essentials(definition)
Expand Down Expand Up @@ -218,7 +245,7 @@ def status
end

def type
@type || self.class.kind
@type || Krane.group_kind(self.class.group, self.class.kind)
end

def group
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/config_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class ConfigMap < KubernetesResource
TIMEOUT = 30.seconds
GROUPS = [""]

def deploy_succeeded?
exists?
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/cron_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class CronJob < KubernetesResource
TIMEOUT = 30.seconds
GROUPS = ["batch"]

def deploy_succeeded?
exists?
Expand Down
6 changes: 5 additions & 1 deletion lib/krane/kubernetes_resource/custom_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def status
end

def type
kind
"#{group}.#{kind}"
end

def validate_definition(*, **)
Expand All @@ -80,6 +80,10 @@ def kind
@definition["kind"]
end

def group
definition["apiVersion"].include?("/") ? definition["apiVersion"].split("/").first : ""
end

def rollout_conditions
@crd.rollout_conditions
end
Expand Down
4 changes: 4 additions & 0 deletions lib/krane/kubernetes_resource/custom_resource_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def group
@definition.dig("spec", "group")
end

def group_kind
::Krane.group_kind(group, kind)
end

def prunable?
prunable = krane_annotation_value("prunable")
prunable == "true"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/daemon_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Krane
class DaemonSet < PodSetBase
TIMEOUT = 5.minutes
SYNC_DEPENDENCIES = %w(Pod)
GROUPS = ["apps"]
attr_reader :pods

def sync(cache)
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Deployment < KubernetesResource
REQUIRED_ROLLOUT_ANNOTATION = "required-rollout"
REQUIRED_ROLLOUT_TYPES = %w(maxUnavailable full none).freeze
DEFAULT_REQUIRED_ROLLOUT = 'full'
GROUPS = ["apps"]

def sync(cache)
super
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/horizontal_pod_autoscaler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Krane
class HorizontalPodAutoscaler < KubernetesResource
TIMEOUT = 3.minutes
RECOVERABLE_CONDITION_PREFIX = "FailedGet"
GROUPS = ["autoscaling"]

def deploy_succeeded?
scaling_active_condition["status"] == "True" || scaling_disabled?
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/ingress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class Ingress < KubernetesResource
TIMEOUT = 30.seconds
GROUPS = ["networking.k8s.io"]

def status
exists? ? "Created" : "Not Found"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class Job < KubernetesResource
TIMEOUT = 10.minutes
GROUP = ["batch"]

def deploy_succeeded?
# Don't block deploys for long running jobs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Krane
class MutatingWebhookConfiguration < KubernetesResource
GLOBAL = true
GROUPS= ["admissionregistration.k8s.io"]

class Webhook
EQUIVALENT = 'Equivalent'
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/network_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class NetworkPolicy < KubernetesResource
TIMEOUT = 30.seconds
GROUPS= ["networking.k8s.io"]

def status
exists? ? "Created" : "Not Found"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/persistent_volume_claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class PersistentVolumeClaim < KubernetesResource
TIMEOUT = 5.minutes
GROUPS = [""]

def sync(cache)
super
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/pod.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
module Krane
class Pod < KubernetesResource
GROUPS = [""]
TIMEOUT = 10.minutes

FAILED_PHASE_NAME = "Failed"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/pod_disruption_budget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class PodDisruptionBudget < KubernetesResource
TIMEOUT = 10.seconds
GROUPS = ["policy"]

def status
exists? ? "Available" : "Not Found"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/pod_set_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module Krane
class PodSetBase < KubernetesResource
GROUPS = ["?"] # TODO
def failure_message
pods.map(&:failure_message).compact.uniq.join("\n")
end
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/pod_template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
module Krane
class PodTemplate < KubernetesResource
GROUPS = ""
def status
exists? ? "Available" : "Not Found"
end
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/replica_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module Krane
class ReplicaSet < PodSetBase
GROUPS = ["apps"]
TIMEOUT = 5.minutes
SYNC_DEPENDENCIES = %w(Pod)
attr_reader :pods
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/resource_quota.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class ResourceQuota < KubernetesResource
TIMEOUT = 30.seconds
GROUPS = [""]

def status
exists? ? "In effect" : "Not Found"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class Role < KubernetesResource
TIMEOUT = 30.seconds
GROUPS = ["rbac.authorization.k8s.io"]

def status
exists? ? "Created" : "Not Found"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/role_binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class RoleBinding < KubernetesResource
TIMEOUT = 30.seconds
GROUP = ["rbac.authorization.k8s.io"]

def status
exists? ? "Created" : "Not Found"
Expand Down
1 change: 1 addition & 0 deletions lib/krane/kubernetes_resource/secret.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Krane
class Secret < KubernetesResource
TIMEOUT = 30.seconds
GROUPS = [""]
SENSITIVE_TEMPLATE_CONTENT = true
SERVER_DRY_RUNNABLE = true

Expand Down
Loading

0 comments on commit 4f87cf4

Please sign in to comment.