Class: KuberKit::Shell::Commands::KubectlCommands

Inherits:
Object
  • Object
show all
Defined in:
lib/kuber_kit/shell/commands/kubectl_commands.rb

Instance Method Summary collapse

Instance Method Details

#apply_file(shell, file_path, kubeconfig_path: nil, namespace: nil, apply_command: nil) ⇒ Object



42
43
44
45
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 42

def apply_file(shell, file_path, kubeconfig_path: nil, namespace: nil, apply_command: nil)
  apply_command ||= "apply"
  kubectl_run(shell, "#{apply_command} -f #{file_path}", kubeconfig_path: kubeconfig_path, namespace: namespace)
end

#delete_resource(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) ⇒ Object



99
100
101
102
103
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 99

def delete_resource(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil)
  command = %Q{delete #{resource_type} #{resource_name}}

  kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace)
end

#describe(shell, resource_name, args: nil, kubeconfig_path: nil, namespace: nil) ⇒ Object



68
69
70
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 68

def describe(shell, resource_name, args: nil, kubeconfig_path: nil, namespace: nil)
  kubectl_run(shell, ["describe", args, resource_name].compact, kubeconfig_path: kubeconfig_path, interactive: true, namespace: namespace)
end

#exec(shell, pod_name, command, args: nil, kubeconfig_path: nil, interactive: false, namespace: nil, entrypoint: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 47

def exec(shell, pod_name, command, args: nil, kubeconfig_path: nil, interactive: false, namespace: nil, entrypoint: nil)
  command_parts = []
  command_parts << "exec"

  if args
    command_parts << args
  end
  
  if entrypoint
    command = entrypoint.gsub("$@", command)
  end

  command_parts << pod_name
  command_parts << "-- #{command}"
  kubectl_run(shell, command_parts, kubeconfig_path: kubeconfig_path, interactive: interactive, namespace: namespace)
end

#get_resources(shell, resource_type, field_selector: nil, jsonpath: ".items[*].metadata.name", kubeconfig_path: nil, namespace: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 72

def get_resources(shell, resource_type, field_selector: nil, jsonpath: ".items[*].metadata.name", kubeconfig_path: nil, namespace: nil)
  command_parts = []
  command_parts << "get #{resource_type}"

  if field_selector
    command_parts << "--field-selector=#{field_selector}"
  end

  if jsonpath 
    command_parts << "-o jsonpath='{#{jsonpath}}'"
  end

  result = kubectl_run(shell, command_parts, kubeconfig_path: kubeconfig_path, namespace: namespace).to_s

  # Hide warnings manually, until appropriate kubectl option will be available
  result = result.split("\n").reject{|n| n.start_with?("Warning:") }.join("\n")

  Array(result.split(" ")).reject(&:empty?)
end

#kubectl_run(shell, command_list, kubeconfig_path: nil, namespace: nil, interactive: false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 16

def kubectl_run(shell, command_list, kubeconfig_path: nil, namespace: nil, interactive: false)
  command_parts = []

  if kubeconfig_path.is_a?(KuberKit::Core::ArtifactPath)
    kubeconfig_path = artifact_path_resolver.call(kubeconfig_path)
  end

  if kubeconfig_path
    command_parts << "KUBECONFIG=#{kubeconfig_path}"
  end

  command_parts << "kubectl"

  if namespace
    command_parts << "-n #{namespace}"
  end

  command_parts += Array(command_list).compact

  if interactive
    shell.interactive!(command_parts.join(" "))
  else
    shell.exec!(command_parts.join(" "))
  end
end

#logs(shell, pod_name, args: nil, kubeconfig_path: nil, namespace: nil) ⇒ Object



64
65
66
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 64

def logs(shell, pod_name, args: nil, kubeconfig_path: nil, namespace: nil)
  kubectl_run(shell, ["logs", args, pod_name].compact, kubeconfig_path: kubeconfig_path, interactive: true, namespace: namespace)
end

#patch_resource(shell, resource_type, resource_name, specs, kubeconfig_path: nil, namespace: nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 105

def patch_resource(shell, resource_type, resource_name, specs, kubeconfig_path: nil, namespace: nil)
  specs_json = JSON.dump(specs).gsub('"', '\"')

  command = %Q{patch #{resource_type} #{resource_name} -p "#{specs_json}"}

  kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace)
end

#resource_exists?(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 92

def resource_exists?(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil)
  result = get_resources(shell, resource_type, 
    field_selector: "metadata.name=#{resource_name}", kubeconfig_path: kubeconfig_path, namespace: namespace
  )
  result.any?
end

#rolling_restart(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 113

def rolling_restart(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil)
  patch_resource(shell, resource_type, resource_name, {
    spec: {
      template: {
        metadata: {
          labels: {
            redeploy: "$(date +%s)"
          }
        }
      }
    }
  }, kubeconfig_path: kubeconfig_path, namespace: namespace)
end

#rollout_status(shell, resource_type, resource_name, wait: true, kubeconfig_path: nil, namespace: nil) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 127

def rollout_status(shell, resource_type, resource_name, wait: true, kubeconfig_path: nil, namespace: nil)
  command_parts = []
  command_parts << %Q{rollout status #{resource_type} #{resource_name}}
  command_parts << "-w" if wait

  kubectl_run(shell, command_parts, kubeconfig_path: kubeconfig_path, namespace: namespace)
end

#set_namespace(shell, namespace, kubeconfig_path: nil) ⇒ Object



135
136
137
138
139
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 135

def set_namespace(shell, namespace, kubeconfig_path: nil)
  command = %Q{config set-context --current --namespace=#{namespace}}

  kubectl_run(shell, command, kubeconfig_path: kubeconfig_path)
end