Class: KubeLinkSpecs
- Inherits:
-
Object
- Object
- KubeLinkSpecs
- Defined in:
- lib/kube_link_generator.rb
Overview
KubeLinkSpecs provides the information required to generate BOSH links by pretending to be a hash.
Constant Summary collapse
- ANNOTATION_AZ =
ANNOTATION_AZ is the Kube annotation for the (availability) zone
'failure-domain.beta.kubernetes.io/zone'.freeze
- SLEEP_DURATION =
1
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #_get_image_for_pod_spec(pod_spec) ⇒ Object
- #_get_pods_for_role(role_name, sts_image) ⇒ Object
- #_get_sts_image_for_role(role_name) ⇒ Object
- #get_exported_properties(role_name, job_name) ⇒ Object
- #get_pod_instance_info(role_name, pod, job, pods_per_image) ⇒ Object
- #get_pods_for_role(role_name, job, options = {}) ⇒ Object
-
#get_pods_per_image(pods) ⇒ Object
Return the number of pods for each image.
- #get_statefulset_instance_info(role_name, job) ⇒ Object
- #get_svc_instance_info(role_name, job) ⇒ Object
-
#initialize(spec, namespace, kube_client, kube_client_stateful_set) ⇒ KubeLinkSpecs
constructor
A new instance of KubeLinkSpecs.
-
#pod_index(name) ⇒ Object
pod_index returns a number for the given pod name.
- #service?(role_name) ⇒ Boolean
- #this_name ⇒ Object
Constructor Details
#initialize(spec, namespace, kube_client, kube_client_stateful_set) ⇒ KubeLinkSpecs
Returns a new instance of KubeLinkSpecs.
13 14 15 16 17 18 19 |
# File 'lib/kube_link_generator.rb', line 13 def initialize(spec, namespace, kube_client, kube_client_stateful_set) @links = {} @client = kube_client @client_stateful_set = kube_client_stateful_set @namespace = namespace @spec = spec || {} end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
21 22 23 |
# File 'lib/kube_link_generator.rb', line 21 def client @client end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/kube_link_generator.rb', line 21 def namespace @namespace end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
21 22 23 |
# File 'lib/kube_link_generator.rb', line 21 def spec @spec end |
Instance Method Details
#[](key) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/kube_link_generator.rb', line 163 def [](key) return @links[key] if @links.key? key # Resolve the role we're looking for provider = spec['consumes'][key] unless provider warn "No link provider found for #{key}" return @links[key] = nil end if provider['role'] == this_name warn "Resolving link #{key} via self provider #{provider}" pods = get_pods_for_role(provider['role'], provider['job'], wait_for_all: true) pods_per_image = get_pods_per_image(pods) instances = pods.map { |p| get_pod_instance_info(provider['role'], p, provider['job'], pods_per_image) } elsif service? provider['role'] # Getting pods for a different service; since we have kube services, we don't handle it in configgin warn "Resolving link #{key} via service #{provider}" instances = [get_svc_instance_info(provider['role'], provider['job'])] else # If there's no service associated, check the statefulset instead warn "Resolving link #{key} via statefulset #{provider}" instances = get_statefulset_instance_info(provider['role'], provider['job']) end # Underscores aren't valid hostnames, so jobs are transformed in fissile to use dashes job_name = provider['job'].tr('_', '-') service_name = provider['service_name'] || "#{provider['role']}-#{job_name}" @links[key] = { 'address' => "#{service_name}.#{ENV['KUBERNETES_NAMESPACE']}.svc.#{ENV['KUBERNETES_CLUSTER_DOMAIN']}", 'instance_group' => '', # This is probably the role name from the manifest 'default_network' => '', 'deployment_name' => namespace, 'domain' => "#{ENV['KUBERNETES_NAMESPACE']}.svc.#{ENV['KUBERNETES_CLUSTER_DOMAIN']}", 'root_domain' => "#{ENV['KUBERNETES_NAMESPACE']}.svc.#{ENV['KUBERNETES_CLUSTER_DOMAIN']}", 'instances' => instances, 'properties' => instances.first['properties'] } end |
#_get_image_for_pod_spec(pod_spec) ⇒ Object
42 43 44 |
# File 'lib/kube_link_generator.rb', line 42 def _get_image_for_pod_spec(pod_spec) pod_spec.containers.map(&:image).sort.join("\n") end |
#_get_pods_for_role(role_name, sts_image) ⇒ Object
51 52 53 54 55 |
# File 'lib/kube_link_generator.rb', line 51 def _get_pods_for_role(role_name, sts_image) client .get_pods(namespace: namespace, label_selector: "app.kubernetes.io/component=#{role_name}") .select { |pod| _get_image_for_pod_spec(pod.spec) == sts_image } end |
#_get_sts_image_for_role(role_name) ⇒ Object
46 47 48 49 |
# File 'lib/kube_link_generator.rb', line 46 def _get_sts_image_for_role(role_name) statefulset = @client_stateful_set.get_stateful_set(role_name, namespace) _get_image_for_pod_spec statefulset.spec.template.spec end |
#get_exported_properties(role_name, job_name) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kube_link_generator.rb', line 78 def get_exported_properties(role_name, job_name) # Containers are not starting until all the properties they want to import already exist. # This is done using the CONFIGGIN_IMPORT_ROLE environment variables referencing the version # tag in the corresponding secret. secret = client.get_secret(role_name, namespace) begin JSON.parse(Base64.strict_decode64(secret.data["skiff-exported-properties-#{job_name}"])) rescue puts "Role #{role_name} is missing skiff-exported-properties-#{job_name}" fail end end |
#get_pod_instance_info(role_name, pod, job, pods_per_image) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kube_link_generator.rb', line 91 def get_pod_instance_info(role_name, pod, job, pods_per_image) index = pod_index(pod..name) # Use pod DNS name for address field, instead of IP address which may change { 'name' => pod..name, 'index' => index, 'id' => pod..name, 'az' => pod..annotations['failure-domain.beta.kubernetes.io/zone'] || 'az0', 'address' => "#{pod..name}.#{pod.spec.subdomain}.#{ENV['KUBERNETES_NAMESPACE']}.svc.#{ENV['KUBERNETES_CLUSTER_DOMAIN']}", 'properties' => get_exported_properties(role_name, job), 'bootstrap' => pods_per_image[pod..uid] < 2 } end |
#get_pods_for_role(role_name, job, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kube_link_generator.rb', line 57 def get_pods_for_role(role_name, job, = {}) sts_image = _get_sts_image_for_role(role_name) loop do # The 30.times loop exists to print out status messages 30.times do 1.times do pods = _get_pods_for_role(role_name, sts_image) good_pods = pods.select { |pod| pod.status.podIP } if [:wait_for_all] break unless good_pods.length == pods.length end return good_pods unless good_pods.empty? end sleep SLEEP_DURATION end $stdout.puts "Waiting for pods for role #{role_name} and provider job #{job} (at #{Time.now})..." $stdout.flush end end |
#get_pods_per_image(pods) ⇒ Object
Return the number of pods for each image
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kube_link_generator.rb', line 106 def get_pods_per_image(pods) result = {} sets = Hash.new(0) keys = {} pods.each do |pod| next if pod.status.containerStatuses.nil? key = pod.status.containerStatuses.map(&:imageID).sort.join("\n") sets[key] += 1 keys[pod..uid] = key end pods.each do |pod| result[pod..uid] = sets[keys[pod..uid]] end result end |
#get_statefulset_instance_info(role_name, job) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kube_link_generator.rb', line 138 def get_statefulset_instance_info(role_name, job) ss = @client_stateful_set.get_stateful_set(role_name, namespace) pod = get_pods_for_role(role_name, job).first Array.new(ss.spec.replicas) do |i| { 'name' => ss..name, 'index' => i, 'id' => ss..name, 'az' => pod..annotations['failure-domain.beta.kubernetes.io/zone'] || 'az0', 'address' => "#{ss..name}-#{i}.#{ss.spec.serviceName}", 'properties' => get_exported_properties(role_name, job), # XXX not actually correct during updates 'bootstrap' => i.zero? } end end |
#get_svc_instance_info(role_name, job) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/kube_link_generator.rb', line 123 def get_svc_instance_info(role_name, job) svc = client.get_service(role_name, namespace) pod = get_pods_for_role(role_name, job).first { 'name' => svc..name, 'index' => 0, # Completely made up index; there is only ever one service 'id' => svc..name, # XXX bogus, but what can we do? 'az' => pod..annotations['failure-domain.beta.kubernetes.io/zone'] || 'az0', 'address' => svc.spec.clusterIP, 'properties' => get_exported_properties(role_name, job), 'bootstrap' => true } end |
#pod_index(name) ⇒ Object
pod_index returns a number for the given pod name. The number is expected to be unique across all pods for the role.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kube_link_generator.rb', line 30 def pod_index(name) index = name.rpartition('-').last return index.to_i if /^\d+$/ =~ index # The pod name is something like role-abcxyz # Derive the index from the randomness that went into the suffix. # chars are the characters kubernetes might use to generate names # Copied from https://github.com/kubernetes/kubernetes/blob/52a6ad0acb26/staging/src/k8s.io/client-go/pkg/util/rand/rand.go#L73 chars = 'bcdfghjklmnpqrstvwxz0123456789' index.chars.map { |c| chars.index(c) }.reduce(0) { |v, c| v * chars.length + c } end |
#service?(role_name) ⇒ Boolean
156 157 158 159 160 161 |
# File 'lib/kube_link_generator.rb', line 156 def service?(role_name) client.get_service(role_name, namespace) true rescue KubeException false end |
#this_name ⇒ Object
24 25 26 |
# File 'lib/kube_link_generator.rb', line 24 def this_name spec['job']['name'] end |