Class: CivoCLI::Kubernetes

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

Instance Method Summary collapse

Instance Method Details

#config(id) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kubernetes.rb', line 141

def config(id)
  CivoCLI::Config.set_api_auth
  cluster = Finder.detect_cluster(id)

  if !cluster.ready
    puts "The cluster isn't ready yet, so the KUBECONFIG isn't available.".colorize(:red)
    exit 1
  elsif cluster.kubeconfig.blank?
    puts "The cluster is being installed, but the KUBECONFIG isn't available yet.".colorize(:red)
    exit 1
  else
    if options[:save]
      save_config(cluster)
    else
      puts cluster.kubeconfig
    end
  end
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#create(name = CivoCLI::NameGenerator.create) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/kubernetes.rb', line 185

def create(name = CivoCLI::NameGenerator.create)
  CivoCLI::Config.set_api_auth

  applications = []
  (options[:applications] || "").split(",").map(&:chomp).each do |name|
    name, plan = name.split(":")
    app = Finder.detect_app(name)
    if app.default # Will be installed by default
      next
    end

    plans = app.plans&.items

    if app && plans.present? && plan.blank?
      if AskService.available?
        plan = AskService.choose("You requested to add #{app.name} but didn't select a plan. Please choose one...", plans.map(&:label))
        if plan.present?
          puts "Thank you, next time you could use \"#{app.name}:#{plan}\" to choose automatically"
        end
      else
        puts "You need to specify a plan".colorize(:red) + " from those available (#{plans.join(", ")} using the syntax \"#{app.name}:plan\""
        exit 1
      end
    end

    if plan.present?
      applications << "#{app.name}:#{plan}"
    else
      applications << app.name
    end
  end

  (options[:remove_applications] || "").split(",").map(&:chomp).each do |name|
    app = Finder.detect_app(name)
    unless app.default # Not a default application, no need to remove
      next
    end
    applications << "-#{app.name}"
  end

  @cluster = Civo::Kubernetes.create(name: name, target_nodes_size: options[:size], num_target_nodes: options[:nodes], applications: applications.join(","), version: options[:version])

  if options[:wait]
    timer = CivoCLI::Timer.new
    timer.start_timer
    print "Building new Kubernetes cluster #{name.colorize(:green)}: "

    spinner = CivoCLI::Spinner.spin(instance: @instance) do |s|
      Civo::Kubernetes.all.items.each do |cluster|
        if cluster.id == @cluster.id && cluster.ready
          s[:final_cluster] = cluster
        end
      end
      s[:final_cluster]
    end

    timer.end_timer
    puts "\b Done\nCreated Kubernetes cluster #{name.colorize(:green)} in #{Time.at(timer.time_elapsed).utc.strftime("%M min %S sec")}"
  elsif !options[:wait] && options[:save]
    puts "Creating Kubernetes cluster #{name.colorize(:green)}. Can only save configuration once cluster is created."
  else
    puts "Created Kubernetes cluster #{name.colorize(:green)}."
  end

  if options[:save] && options[:wait]
    save_config(spinner.final_cluster)
  end
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#listObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kubernetes.rb', line 6

def list
  CivoCLI::Config.set_api_auth
  latest_version = get_latest_k3s_version
  upgrade_available = false

  if options[:quiet]
    Civo::Kubernetes.all.items.each do |cluster|
    puts cluster.id
    end
  else
    rows = []
    Civo::Kubernetes.all.items.each do |cluster|
      version = cluster.kubernetes_version

      if cluster.kubernetes_version != "development"
        latest_version = get_latest_k3s_version.gsub!(/[+]/, "")
        if Gem::Version.new(latest_version) > Gem::Version.new(version)
          upgrade_available = true
          version = "#{version} *".colorize(:red)
        end
      end

      rows << [cluster.id, cluster.name, cluster.num_target_nodes, cluster.target_nodes_size, version, cluster.status]
    end
    puts Terminal::Table.new headings: ['ID', 'Name', '# Nodes', 'Size', 'Version', 'Status'], rows: rows
    if upgrade_available
      puts "\n* An upgrade to v#{latest_version} is available, use - civo k3s upgrade ID - to upgrade it".colorize(:red)
    end
  end
rescue Flexirest::HTTPForbiddenClientException
  reject_user_access
end

#remove(id) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# File 'lib/kubernetes.rb', line 312

def remove(id)
  CivoCLI::Config.set_api_auth
  cluster = Finder.detect_cluster(id)

  puts "Removing Kubernetes cluster #{cluster.name.colorize(:red)}"
  cluster.remove
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#rename(id) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/kubernetes.rb', line 262

def rename(id)
  CivoCLI::Config.set_api_auth
  cluster = Finder.detect_cluster(id)

  if options[:name]
    Civo::Kubernetes.update(id: cluster.id, name: options[:name])
    puts "Kubernetes cluster #{cluster.id} is now named #{options[:name].colorize(:green)}"
  end
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#scale(id) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/kubernetes.rb', line 297

def scale(id)
  CivoCLI::Config.set_api_auth
  cluster = Finder.detect_cluster(id)

  if options[:nodes]
    Civo::Kubernetes.update(id: cluster.id, num_target_nodes: options[:nodes])
    puts "Kubernetes cluster #{cluster.name.colorize(:green)} will now have #{options[:nodes].colorize(:green)} nodes"
  end
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#show(id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# File 'lib/kubernetes.rb', line 65

def show(id)
  CivoCLI::Config.set_api_auth
  rows = []
  cluster = Finder.detect_cluster(id)
  upgrade_available = false

  puts "                ID : #{cluster.id}"
  puts "              Name : #{cluster.name}"
  puts "           # Nodes : #{cluster.num_target_nodes}"
  puts "              Size : #{cluster.target_nodes_size}"
  case cluster.status
  when "ACTIVE"
    puts "            Status : #{cluster.status.colorize(:green)}"
  when /ING$/
    puts "            Status : #{cluster.status.colorize(:orange)}"
  else
    puts "            Status : #{cluster.status.colorize(:red)}"
  end

  if cluster.kubernetes_version == "development"
    puts "           Version : Development"
  else
    latest_version = get_latest_k3s_version
    if Gem::Version.new(latest_version) > Gem::Version.new(cluster.kubernetes_version)
      puts "           Version : " + "#{cluster.kubernetes_version} *".colorize(:red)
      upgrade_available = true
    else
      puts "           Version : #{cluster.kubernetes_version}"
    end
  end

  puts "      API Endpoint : #{cluster.api_endpoint}"
  puts "      DNS A record : #{cluster.id}.k8s.civo.com"
  puts "                     *.#{cluster.id}.k8s.civo.com"

  if upgrade_available
    puts "\n* An upgrade to v#{latest_version} is available, use - civo k3s upgrade ID - to upgrade it".colorize(:red)
  end

  puts ""
  puts "Nodes:"
  rows = []
  cluster.instances.each do |instance|
    rows << [instance.hostname, instance.public_ip, instance.status]
  end
  puts Terminal::Table.new headings: ['Name', 'IP', 'Status'], rows: rows

  if cluster.installed_applications.any?
    puts ""
    puts "Installed marketplace applications:"
    rows = []
    cluster.installed_applications.each do |application|
      name = application.application
      if application.plan
        name += " #{application.plan}"
      end
      installed = application.installed ? "Yes" : "Not yet"
      rows << [name, application.version, installed, application.category]
    end
    puts Terminal::Table.new headings: ['Name', 'Version', 'Installed', 'Category'], rows: rows
  end
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#upgrade(id) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/kubernetes.rb', line 280

def upgrade(id)
  CivoCLI::Config.set_api_auth
  cluster = Finder.detect_cluster(id)

  version = get_latest_k3s_version(options[:version])
  Civo::Kubernetes.update(id: cluster.id, version: version)
  puts "Kubernetes cluster #{cluster.name.colorize(:green)} is upgrading to #{version.colorize(:green)}"
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#versionsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kubernetes.rb', line 42

def versions
  CivoCLI::Config.set_api_auth
  if options[:quiet]
    Civo::Kubernetes.versions.each do |k3s|
      puts k3s.version
    end
  else
    rows = []
    Civo::Kubernetes.versions.each do |k3s|
      if k3s.default
        rows << [k3s.version, k3s.type, "<====="]
      else
        rows << [k3s.version, k3s.type, ""]
      end
    end
    puts Terminal::Table.new headings: ['Version', 'Type', 'Default'], rows: rows
  end
rescue Flexirest::HTTPForbiddenClientException
  reject_user_access
end