Class: EksCli::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/eks_cli/cli.rb

Constant Summary collapse

RED =
31

Instance Method Summary collapse

Instance Method Details

#add_iam_user(iam_arn) ⇒ Object



212
213
214
215
216
217
# File 'lib/eks_cli/cli.rb', line 212

def add_iam_user(iam_arn)
  with_context do
    Config[cluster_name].add_user(iam_arn, options[:username], options[:groups])
    K8s::Auth.new(cluster_name).update if options[:yes]
  end
end

#createObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/eks_cli/cli.rb', line 46

def create
  with_context do

    opts = {region: options[:region],
            kubernetes_version: options[:kubernetes_version],
            open_ports: options[:open_ports],
            cidr: options[:cidr],
            warm_ip_target: options[:warm_ip_target] ? options[:warm_ip_target].to_i : nil,
            subnet1_az: (options[:subnet1_az] || Config::AZS[options[:region]][0]),
            subnet2_az: (options[:subnet2_az] || Config::AZS[options[:region]][1]),
            subnet3_az: (options[:subnet3_az] || Config::AZS[options[:region]][2])}

    config.bootstrap(opts)
    cluster = EKS::Cluster.new(cluster_name).create
    cluster.update_kubeconfig
    wait_for_cluster
    enable_gpu if options[:enable_gpu]
    create_default_storage_class if options[:create_default_storage_class]
    create_dns_autoscaler if options[:create_dns_autoscaler]
    update_cluster_cni if options[:warm_ip_target]
    Log.info "cluster creation completed"
  end
end

#create_default_storage_classObject



98
99
100
# File 'lib/eks_cli/cli.rb', line 98

def create_default_storage_class
  with_context { K8s::Client.new(cluster_name).create_default_storage_class }
end

#create_dns_autoscalerObject



191
192
193
# File 'lib/eks_cli/cli.rb', line 191

def create_dns_autoscaler
  with_context { K8s::Client.new(cluster_name).create_dns_autoscaler }
end

#create_nodegroupObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/eks_cli/cli.rb', line 116

def create_nodegroup
  with_context do
    opts = options.dup 
    opts[:subnets] = opts[:subnets].map(&:to_i)
    Config[cluster_name].update_nodegroup(opts) unless opts[:all]
    if opts[:yes]
      cf_stacks = nodegroups.map {|ng| ng.create(wait_for_completion: false)}
      CloudFormation::Stack.await(cf_stacks)
      K8s::Auth.new(cluster_name).update
    end
  end
end

#delete_clusterObject



149
150
151
152
153
154
155
156
157
# File 'lib/eks_cli/cli.rb', line 149

def delete_cluster
  answer = ask("you are about to delete EKS cluster #{bold(underline(colorize(cluster_name, RED)))} including nodegroups/elastigroups, elbs, kubernetes services and cloudformation stacks.\nare you 100% sure you want to proceed? (y/N)")
  if answer == "y"
    Log.info "deleting cluster #{cluster_name}"
    with_context { EKS::Cluster.new(cluster_name).delete }
  else
    Log.info "aborted"
  end
end

#delete_nodegroupObject



162
163
164
# File 'lib/eks_cli/cli.rb', line 162

def delete_nodegroup
  with_context { nodegroups.each(&:delete) }
end

#enable_gpuObject



88
89
90
# File 'lib/eks_cli/cli.rb', line 88

def enable_gpu
  with_context { K8s::Client.new(cluster_name).enable_gpu }
end

#export_nodegroupObject



204
205
206
# File 'lib/eks_cli/cli.rb', line 204

def export_nodegroup
  with_context { nodegroups.each {|ng| ng.export_to_spotinst(options[:exact_instance_type]) } }
end

#scale_nodegroupObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/eks_cli/cli.rb', line 137

def scale_nodegroup
  with_context do
    nodegroups.each do |ng| 
      min = (options[:min] || config.for_group(ng.name)["min"]).to_i
      max = (options[:max] || config.for_group(ng.name)["max"]).to_i
      ng.scale(min, max, options[:asg], options[:spotinst])
      Config[cluster_name].update_nodegroup(options.slice("min", "max").merge({"group_name" => ng.name})) if options[:update]
    end
  end
end

#set_docker_registry_credentials(username, password, email) ⇒ Object



93
94
95
# File 'lib/eks_cli/cli.rb', line 93

def set_docker_registry_credentials(username, password, email)
  with_context { K8s::Client.new(cluster_name).set_docker_registry_credentials(username, password, email) }
end

#set_iam_policiesObject



173
174
175
# File 'lib/eks_cli/cli.rb', line 173

def set_iam_policies
  with_context { Config[cluster_name].set_iam_policies(options[:policies]) }
end

#set_inter_vpc_networking(to_vpc_id, to_sg_id) ⇒ Object



186
187
188
# File 'lib/eks_cli/cli.rb', line 186

def set_inter_vpc_networking(to_vpc_id, to_sg_id)
  with_context { VPC::Client.new(cluster_name).set_inter_vpc_networking(to_vpc_id, to_sg_id) }
end

#show_configObject



72
73
74
75
76
77
78
79
80
# File 'lib/eks_cli/cli.rb', line 72

def show_config
  with_context do
    if options[:group_name]
      puts JSON.pretty_generate(config.for_group(options[:group_name]))
    else
      puts JSON.pretty_generate(config.read_from_disk)
    end
  end
end

#update_authObject



167
168
169
# File 'lib/eks_cli/cli.rb', line 167

def update_auth
  with_context { K8s::Auth.new(cluster_name).update }
end

#update_cluster_cniObject



83
84
85
# File 'lib/eks_cli/cli.rb', line 83

def update_cluster_cni
  with_context { K8s::Client.new(cluster_name).update_cni }
end

#update_dns(hostname, k8s_service_name) ⇒ Object



181
182
183
# File 'lib/eks_cli/cli.rb', line 181

def update_dns(hostname, k8s_service_name)
  with_context { Route53::Client.new(cluster_name).update_dns(hostname, k8s_service_name, options[:namespace], options[:route53_hosted_zone_id], options[:elb_hosted_zone_id]) }
end

#versionObject



221
222
223
# File 'lib/eks_cli/cli.rb', line 221

def version
  puts EksCli::VERSION
end

#wait_for_clusterObject



196
197
198
# File 'lib/eks_cli/cli.rb', line 196

def wait_for_cluster
  with_context { K8s::Client.new(cluster_name).wait_for_cluster }
end