Class: KubernetesDeploy::Runner

Inherits:
Object
  • Object
show all
Includes:
KubeclientBuilder
Defined in:
lib/kubernetes-deploy/runner.rb

Constant Summary collapse

PREDEPLOY_SEQUENCE =
%w(
  Cloudsql
  Redis
  Bugsnag
  ConfigMap
  PersistentVolumeClaim
  Pod
)
PROTECTED_NAMESPACES =
%w(
  default
  kube-system
  kube-public
)
BASE_PRUNE_WHITELIST =

Things removed from default prune whitelist: core/v1/Namespace – not namespaced core/v1/PersistentVolume – not namespaced core/v1/Endpoints – managed by services core/v1/PersistentVolumeClaim – would delete data core/v1/ReplicationController – superseded by deployments/replicasets extensions/v1beta1/ReplicaSet – managed by deployments core/v1/Secret – should not committed / managed by shipit

%w(
  core/v1/ConfigMap
  core/v1/Pod
  core/v1/Service
  batch/v1/Job
  extensions/v1beta1/DaemonSet
  extensions/v1beta1/Deployment
  extensions/v1beta1/Ingress
  apps/v1beta1/StatefulSet
).freeze
PRUNE_WHITELIST_V_1_5 =
%w(extensions/v1beta1/HorizontalPodAutoscaler).freeze
PRUNE_WHITELIST_V_1_6 =
%w(autoscaling/v1/HorizontalPodAutoscaler).freeze

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, context:, current_sha:, template_dir:, logger:, bindings: {}) ⇒ Runner

Returns a new instance of Runner.



70
71
72
73
74
75
76
77
78
79
# File 'lib/kubernetes-deploy/runner.rb', line 70

def initialize(namespace:, context:, current_sha:, template_dir:, logger:, bindings: {})
  @namespace = namespace
  @context = context
  @current_sha = current_sha
  @template_dir = File.expand_path(template_dir)
  @logger = logger
  @bindings = bindings
  # Max length of podname is only 63chars so try to save some room by truncating sha to 8 chars
  @id = current_sha[0...8] + "-#{SecureRandom.hex(4)}" if current_sha
end

Instance Method Details

#run(verify_result: true, allow_protected_ns: false, prune: true) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kubernetes-deploy/runner.rb', line 81

def run(verify_result: true, allow_protected_ns: false, prune: true)
  @logger.reset

  @logger.phase_heading("Initializing deploy")
  validate_configuration(allow_protected_ns: allow_protected_ns, prune: prune)
  confirm_context_exists
  confirm_namespace_exists
  resources = discover_resources

  @logger.phase_heading("Checking initial resource statuses")
  resources.each(&:sync)
  resources.each { |r| @logger.info(r.pretty_status) }

  ejson = EjsonSecretProvisioner.new(
    namespace: @namespace,
    context: @context,
    template_dir: @template_dir,
    logger: @logger
  )
  if ejson.secret_changes_required?
    @logger.phase_heading("Deploying kubernetes secrets from #{EjsonSecretProvisioner::EJSON_SECRETS_FILE}")
    ejson.run
  end

  if deploy_has_priority_resources?(resources)
    @logger.phase_heading("Predeploying priority resources")
    predeploy_priority_resources(resources)
  end

  @logger.phase_heading("Deploying all resources")
  if PROTECTED_NAMESPACES.include?(@namespace) && prune
    raise FatalDeploymentError, "Refusing to deploy to protected namespace '#{@namespace}' with pruning enabled"
  end

  if verify_result
    deploy_resources(resources, prune: prune, verify: true)
    record_statuses(resources)
    success = resources.all?(&:deploy_succeeded?)
  else
    deploy_resources(resources, prune: prune, verify: false)
    @logger.summary.add_action("deployed #{resources.length} #{'resource'.pluralize(resources.length)}")
    warning = <<-MSG.strip_heredoc
      Deploy result verification is disabled for this deploy.
      This means the desired changes were communicated to Kubernetes, but the deploy did not make sure they actually succeeded.
    MSG
    @logger.summary.add_paragraph(ColorizedString.new(warning).yellow)
    success = true
  end
rescue FatalDeploymentError => error
  @logger.summary.add_action(error.message)
  success = false
ensure
  @logger.print_summary(success)
  success
end

#template_variablesObject



137
138
139
140
141
142
# File 'lib/kubernetes-deploy/runner.rb', line 137

def template_variables
  {
    'current_sha' => @current_sha,
    'deployment_id' => @id,
  }.merge(@bindings)
end