Class: KuberKit::Shell::Commands::KubectlCommands
- Defined in:
- lib/kuber_kit/shell/commands/kubectl_commands.rb
Instance Method Summary collapse
- #apply_file(shell, file_path, kubeconfig_path: nil, namespace: nil) ⇒ Object
- #delete_resource(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) ⇒ Object
- #exec(shell, pod_name, command, args: nil, kubeconfig_path: nil, interactive: false, namespace: nil) ⇒ Object
- #find_resources(shell, resource_type, resource_name, jsonpath: ".items[*].metadata.name", kubeconfig_path: nil, namespace: nil) ⇒ Object
- #kubectl_run(shell, command_list, kubeconfig_path: nil, namespace: nil, interactive: false) ⇒ Object
- #patch_deployment(shell, deployment_name, specs, kubeconfig_path: nil, namespace: nil) ⇒ Object
- #resource_exists?(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) ⇒ Boolean
- #rolling_restart(shell, deployment_name, kubeconfig_path: nil, namespace: nil) ⇒ Object
Instance Method Details
#apply_file(shell, file_path, kubeconfig_path: nil, namespace: nil) ⇒ Object
27 28 29 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 27 def apply_file(shell, file_path, kubeconfig_path: nil, namespace: nil) kubectl_run(shell, "apply -f #{file_path}", kubeconfig_path: kubeconfig_path, namespace: namespace) end |
#delete_resource(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) ⇒ Object
55 56 57 58 59 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 55 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 |
#exec(shell, pod_name, command, args: nil, kubeconfig_path: nil, interactive: false, namespace: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 31 def exec(shell, pod_name, command, args: nil, kubeconfig_path: nil, interactive: false, namespace: nil) command_parts = [] command_parts << "exec" if args command_parts << args end command_parts << pod_name command_parts << "-- #{command}" kubectl_run(shell, command_parts, kubeconfig_path: kubeconfig_path, interactive: interactive, namespace: namespace) end |
#find_resources(shell, resource_type, resource_name, jsonpath: ".items[*].metadata.name", kubeconfig_path: nil, namespace: nil) ⇒ Object
49 50 51 52 53 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 49 def find_resources(shell, resource_type, resource_name, jsonpath: ".items[*].metadata.name", kubeconfig_path: nil, namespace: nil) command = %Q{get #{resource_type} --field-selector=metadata.name=#{resource_name} -o jsonpath='{#{jsonpath}}'} kubectl_run(shell, command, kubeconfig_path: kubeconfig_path, namespace: namespace) end |
#kubectl_run(shell, command_list, kubeconfig_path: nil, namespace: nil, interactive: false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 5 def kubectl_run(shell, command_list, kubeconfig_path: nil, namespace: nil, interactive: false) command_parts = [] if kubeconfig_path command_parts << "KUBECONFIG=#{kubeconfig_path}" end command_parts << "kubectl" if namespace command_parts << "-n #{namespace}" end command_parts += Array(command_list) # TODO: investigate how to do it with shell. if interactive system(command_parts.join(" ")) else shell.exec!(command_parts.join(" ")) end end |
#patch_deployment(shell, deployment_name, specs, kubeconfig_path: nil, namespace: nil) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 75 def patch_deployment(shell, deployment_name, specs, kubeconfig_path: nil, namespace: nil) specs_json = JSON.dump(specs).gsub('"', '\"') command = %Q{patch deployment #{deployment_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
44 45 46 47 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 44 def resource_exists?(shell, resource_type, resource_name, kubeconfig_path: nil, namespace: nil) result = find_resources(shell, resource_type, resource_name, kubeconfig_path: kubeconfig_path, namespace: namespace) result && result != "" end |
#rolling_restart(shell, deployment_name, kubeconfig_path: nil, namespace: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/kuber_kit/shell/commands/kubectl_commands.rb', line 61 def rolling_restart(shell, deployment_name, kubeconfig_path: nil, namespace: nil) patch_deployment(shell, deployment_name, { spec: { template: { metadata: { labels: { redeploy: "$(date +%s)" } } } } }, kubeconfig_path: kubeconfig_path, namespace: namespace) end |