class EksCli::K8s::Client
Constants
- TIMEOUTS
Public Class Methods
new(cluster_name)
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 12 def initialize(cluster_name) @cluster_name = cluster_name end
Public Instance Methods
create_default_storage_class()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 35 def create_default_storage_class Log.info "creating default storage class" Log.info self.create_storage_class(resource_from_yaml("k8s/default_storage_class.yaml")) end
create_dns_autoscaler()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 40 def create_dns_autoscaler Log.info "creating kube-dns autoscaler" Log.info self.create_deployment(resource_from_yaml("k8s/dns_autoscaler.dep.yaml")) end
enable_gpu()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 20 def enable_gpu Log.info "installing nvidia device plugin daemon set (GPU support)" self.create_daemon_set(resource_from_yaml("k8s/nvidia_device_plugin.yaml")) end
get_elb(service_name, ns = "default")
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 16 def get_elb(service_name, ns = "default") self.get_service(service_name, ns).status.loadBalancer.ingress.first.hostname end
set_docker_registry_credentials(user, password, email)
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 25 def set_docker_registry_credentials(user, password, email) Log.info "setting docker registry credentials" Log.info `kubectl config use-context #{config["cluster_arn"]} && kubectl create secret docker-registry registrykey --docker-server=https://index.docker.io/v1/ --docker-username=#{user} --docker-password=#{password} --docker-email=#{email} && kubectl --namespace=kube-system create secret docker-registry registrykey --docker-server=https://index.docker.io/v1/ --docker-username=#{user} --docker-password=#{password} --docker-email=#{email}` Log.info client.patch_service_account("default", {imagePullSecrets: [{name: "registrykey"}]}, "default") Log.info client.patch_service_account("default", {imagePullSecrets: [{name: "registrykey"}]}, "kube-system") end
update_cni()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 45 def update_cni Log.info "updating cni" Log.info self.update_daemon_set(resource_from_erb("k8s/cni/ds.yaml.erb", {custom_warm_ip_target: config["warm_ip_target"]})) Log.info `kubectl config use-context #{config["cluster_arn"]} && kubectl apply -f #{file_path("/k8s/cni/rest.yaml")}` end
wait_for_cluster()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 51 def wait_for_cluster Log.info "waiting for cluster #{@cluster_name} to respond" successful_calls = 0 while successful_calls < 3 begin res = self.get_services if res.count > 0 successful_calls += 1 end rescue Kubeclient::HttpError Log.info "couldn't connect to server, retrying..." end end Log.info "#{@cluster_name} is up and running!" end
Private Instance Methods
apps_client()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 97 def apps_client @apps_client ||= client("/apis/apps") end
client(suffix = "")
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 109 def client(suffix = "") Kubeclient::Client.new( [context.api_endpoint, suffix].join, context.api_version, ssl_options: context.ssl_options, auth_options: {bearer_token: token}, timeouts: TIMEOUTS) end
config()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 118 def config @config ||= Config[@cluster_name] end
context()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 130 def context kube_config.context(config["cluster_arn"]) end
file_path(filename)
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 81 def file_path(filename) File.join($root_dir, "/assets/#{filename}") end
kube_config()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 126 def kube_config @kube_config ||= Kubeclient::Config.read("#{ENV['HOME']}/.kube/config") end
method_missing(method, *args, &block)
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 85 def method_missing(method, *args, &block) if v1_client.respond_to?(method) v1_client.send(method, *args, &block) elsif apps_client.respond_to?(method) apps_client.send(method, *args, &block) elsif storage_client.respond_to?(method) storage_client.send(method, *args, &block) else raise "unknown method #{method}" end end
resource_from_erb(filename, bindings)
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 69 def resource_from_erb(filename, bindings) erb = File.read(file_path(filename)) resolved = ERBResolver.render(erb, bindings) yaml = YAML.load(resolved) Kubeclient::Resource.new(yaml) end
resource_from_yaml(filename)
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 76 def resource_from_yaml(filename) yaml = YAML.load_file(file_path(filename)) Kubeclient::Resource.new(yaml) end
storage_client()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 105 def storage_client @storage_client ||= client("/apis/storage.k8s.io") end
token()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 122 def token JSON.parse(`aws-iam-authenticator token -i #{config["cluster_name"]}`)["status"]["token"] end
v1_client()
click to toggle source
# File lib/eks_cli/k8s/client.rb, line 101 def v1_client @v1_client ||= client end