require 'picsolve_docker_builder/helpers/kubeclient' require 'picsolve_docker_builder/helpers/kubernetes/rc' require 'picsolve_docker_builder/helpers/kubernetes/service' require 'picsolve_docker_builder/helpers/kubernetes/pod' require 'picsolve_docker_builder/helpers/ssh_forward' require 'picsolve_docker_builder/base' module PicsolveDockerBuilder module Helpers # Ruby representation of a kuberentes cluster class KubernetesManager include PicsolveDockerBuilder::Base def create_client Kubeclient::Client.new kubernetes_url, 'v1beta3' end def kubernetes_url @ssh_forward = SshForward.new( ssh_host: kubernetes_host, remote_host: '127.0.0.1', remote_port: 8080 ) port = @ssh_forward.start at_exit do @ssh_forward.stop unless @ssh_forward.nil? end "http://127.0.0.1:#{port}" end def kubernetes_host # TODO: Need to be touched for multi cluster support config['compose']['clusters'].first end def client @client ||= create_client end def service(image) Kubernetes::Service.new(image, self) end def rc(image) Kubernetes::Rc.new(image, self) end def template_labels_pods(i) c = template_labels(i) c['deployment'] = i.composer.hash c end def template_labels(i) { 'name' => i.name, 'app_name' => i.composer.app_name, 'stage' => i.composer.stage } end def config_service(s, i) # workaround a bug on updating TODO: remove when fixed upstream s.name = i.name # configure the service s.metadata = {} unless s.metadata s.metadata.name = i.name s.metadata.namespace = i.composer.namespace s.metadata.labels = template_labels(i) s.spec = {} unless s.spec s.spec.ports = i.ports s.spec.selector = { 'name' => i.name } end def deploy_service(i) existing = false begin s = client.get_service i.name, i.composer.namespace existing = true rescue KubeException s = Kubeclient::Service.new end # config config config_service(s, i) if existing log.debug \ "update service '#{i.composer.namespace}/#{i.name}' on kubernetes" client.update_service s else log.debug \ "create service '#{i.composer.namespace}/#{i.name}' on kubernetes" client.create_service s end end end end end