mina-kubernetes 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7930f648e0b70eeb94a1413fee561b8edcdac3c28918bf003bee6c77749f9537
4
- data.tar.gz: 5df185bd2d8cf21dbe0d520b799e8654d793400822a9d2522a760614dabf4e97
3
+ metadata.gz: 7be64670afaedf5f60cb172d47331af1df66e3e843d21cdeee54851f2aafb5fa
4
+ data.tar.gz: 9eea0c759fd4823586b551413b7a456b780baeb8af24a0464ea962683212023e
5
5
  SHA512:
6
- metadata.gz: 47f3c48b9157ca35688e8b45cee5e20c8c844c05b128e899f5cee1c15b9db60773a16f6b636fcf15c84bcbf27b78b4c66e6c4f5758397c693d277615f7b23abf
7
- data.tar.gz: ce3221d0774d3d386c95af184798c9a98d67559f43481ea84b75a13119a6bb57fc35b5eafa9ca60a9a5d4e879805328b0dc5b47f162ff20d885d5e4aea95228c
6
+ metadata.gz: 59927afc8c3504e3b381b4c749b5fb390fc2c7f20c143674811bbede57f6c2e89d6852dc9b4ecd534fd84a7bf1ef6b3aa35144c1704ec36569f157868feca7e3
7
+ data.tar.gz: 5b96dc9e7d993063be539d294656f2a3337290adbfaef1e104a5877c9f09736b7e3bb8ee35ee6407812ce7072d047f95c84fe04b3194b2ef0d446150b11eaf82
@@ -1,3 +1,16 @@
1
+ ## 2.5.0
2
+
3
+ *Enhancements*
4
+
5
+ - `kubernetes:command` starts pod with identifiable name and allows session reconnection
6
+ - `kubernetes:command` accepts a `kubectl_pod_overrides` option
7
+
8
+ ## 2.4.1
9
+
10
+ *Fixes*
11
+
12
+ - Security: update rake dependency
13
+
1
14
  ## 2.4.0
2
15
 
3
16
  *Enhancements*
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
1
  # mina-kubernetes
2
- Plugin for the [mina](https://github.com/mina-deploy/mina) deployment tool to streamline deployment of resources to Kubernetes clusters, using the [krane](https://github.com/Shopify/krane) gem and [mina-multistage](https://github.com/endoze/mina-multistage) plugin.
2
+ mina-kubernetes is a plugin for the [mina](https://github.com/mina-deploy/mina) deployment tool to streamline deployment of resources to Kubernetes clusters, using the [krane](https://github.com/Shopify/krane) gem with the [mina-multistage](https://github.com/endoze/mina-multistage) plugin.
3
3
 
4
- Requires local Docker and [kubectl](https://cloud.google.com/kubernetes-engine/docs/quickstart) with local authentication set up to connect to the destination Kubernetes cluster as context in your local KUBE_CONFIG. See https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#generate_kubeconfig_entry for example with Google Kubernetes Engine.
4
+ It requires local Docker and [kubectl](https://cloud.google.com/kubernetes-engine/docs/quickstart) with local authentication set up to connect to the destination Kubernetes cluster as context in your local KUBE_CONFIG. See https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#generate_kubeconfig_entry for example with Google Kubernetes Engine.
5
5
 
6
- NB: `docker manifest inspect` is used to check whether the Docker image with requested tag is available. This requires experimental features to be enabled in your local Docker config by adding `"experimental": "enabled"` to `~/.docker/config.json`.
7
- If the image repository is not public authentication will need to be set up for your local Docker, for instance see https://cloud.google.com/container-registry/docs/advanced-authentication#gcloud_as_a_docker_credential_helper for images hosted on the Google Cloud Registry
6
+ NB: `docker manifest inspect` is used to check whether the Docker image with requested tag is available. At the time of writing this is still an experimental feature that needs to be enabled in your local Docker config by adding `"experimental": "enabled"` to `~/.docker/config.json`.
7
+ If the image to deploy is in a private repository authentication will have to be set up for your local Docker, for instance see https://cloud.google.com/container-registry/docs/advanced-authentication#gcloud_as_a_docker_credential_helper for images hosted on the Google Cloud Registry.
8
8
 
9
9
  ## Usage
10
10
 
11
11
  Add `mina-kubernetes` to your local Gemfile.
12
12
 
13
- Create a configuration file for mina in `config/deploy.rb` like the one below:
13
+ Create a configuration file for mina in `config/deploy.rb` similar to the one below (which replaces the default deploy task):
14
14
  ```ruby
15
15
  require "mina/default"
16
16
  require "mina/multistage"
@@ -28,9 +28,9 @@ set :image_repo, "gcr.io/project-id/myapp"
28
28
  set :kubernetes_context, "kubernetes_context_name"
29
29
  ```
30
30
 
31
- If `set :image_tag, "my_image_tag"` is also defined, it'll be used to deploy the image tagged with this tag on the repository. Otherwise you'll be prompted to pick a branch from current working Git repository and the image to deploy will be assumed to be tagged with the Git commit hash, i.e. `gcr.io/project-123456/my_app:abcd1234`.
31
+ If `set :image_tag, "my_image_tag"` is also defined, it'll be used to deploy the image tagged with this tag on the repository. Otherwise you'll be prompted to pick a branch from the current Git repository and the image to deploy will be assumed to be tagged with the commit hash of that branch, i.e. `gcr.io/project-id/myapp:abcd1234`.
32
32
 
33
- Then add `*.yml.erb` Kubernetes resource definition files in the stage folder, i.e. `config/deploy/production/app.yml.erb`. Occurences of `<%= image_repo %>` and `<%= current_sha %>` in these files will be dynamically replaced on deploy by the image repository URL and the latest commit hash of the selected branch on its git origin.
33
+ Then add `*.yml.erb` Kubernetes resource definition files in the stage folder, for instance `config/deploy/production/webserver.yml.erb` and `config/deploy/production/backgroundjobs.yml.erb`. Occurences of `<%= image_repo %>` and `<%= current_sha %>` in these files will be dynamically replaced on deploy by the image repository URL and the latest commit hash of the selected branch on its git origin.
34
34
 
35
35
  You can also get the RAILS_MASTER_KEY for encrypted credentials deployed as a Kubernetes secrets by adding a secrets.yml.erb like below:
36
36
  ```yml
@@ -42,14 +42,14 @@ data:
42
42
  RAILS_MASTER_KEY: <%= Base64.strict_encode64(File.read("#{Dir.pwd}/config/credentials/production.key").strip) %>
43
43
  ```
44
44
 
45
- When running `mina production deploy`, it'll prompt for a branch and check the image tagged with current commit hash from selected branch is available on the repository. Then the `krane` executable is called to fill in the variables in the resource templates and apply them all to the cluster under the given namespace (see https://github.com/Shopify/krane#deploy-walkthrough for more details)\
45
+ When running `mina production deploy`, it'll check the image is available on the repository and then call the `krane` executable to fill in the variables in the resource templates and apply them all to the cluster under the given namespace (see https://github.com/Shopify/krane#deploy-walkthrough for more details)
46
46
 
47
47
  ### EJSON Encrypted secrets
48
48
 
49
- Krane supports generating Kubernetes secrets from an encrypted EJSON file: https://github.com/Shopify/krane#deploying-kubernetes-secrets-from-ejson. As per current Krane documentation "The ejson file must be included in the resources passed to --filenames it can not be read through stdin.", so
49
+ Krane can dynamically generate Kubernetes secrets from an encrypted EJSON file, see: https://github.com/Shopify/krane#deploying-kubernetes-secrets-from-ejson. As per current Krane documentation "The ejson file must be included in the resources passed to --filenames, it can not be read through stdin.", so
50
50
  following convention-over-configuration principles `mina-kubernetes` checks for the presence of a file named `secrets.ejson` in the stage folder and uses it if available.
51
51
 
52
- ### Passing options to krane
52
+ ### Passing custom options to krane
53
53
 
54
54
  ```ruby
55
55
  invoke :"kubernetes:deploy", "--no-prune"
@@ -57,11 +57,11 @@ following convention-over-configuration principles `mina-kubernetes` checks for
57
57
 
58
58
  Refer to https://github.com/Shopify/krane#usage for a complete set of options
59
59
 
60
- ### Tasks available
60
+ ## Tasks available
61
61
 
62
62
  #### `kubernetes:deploy`
63
63
 
64
- Creates namespace on cluster and assigns it to a local kubectl context, prompts for git branch if no image tag specified, applies all resources to cluster after checking tagged image is available.
64
+ Creates the namespace on cluster if it doesn't exist, prompts for a git branch if no image tag is already specified in stage file, then applies all resources to cluster after checking tagged image is available.
65
65
 
66
66
  #### `kubernetes:bash`
67
67
 
@@ -69,8 +69,26 @@ Prompts for branch unless image tag is set, then spins up a temporary pod with t
69
69
 
70
70
  #### `kubernetes:command`
71
71
 
72
- Prompts for branch unless image tag is set, then spins up a temporary pod with the image and run command given by task variable `command`, for instance with `set :command, "rails console"`. Environment variables can also be given by defining`env_hash`, i.e. `set :env_hash, {"RAILS_ENV" => "production", "MY_VAR" => "abcd123"}`
72
+ Prompts for branch unless image tag is set, then spins up a temporary pod with the image and runs the command given in the task variable `command`, for instance with `set :command, "rails console"`. Environment variables can also be passed by defining`env_hash`, i.e. `set :env_hash, {"RAILS_ENV" => "production", "MY_VAR" => "abcd123"}`
73
+
74
+ The pod will be named `command-username-branch`, and can be reattached/killed in case of disconnection.
75
+
76
+ A `kubectl_pod_overrides` task option is available to pass a value to the `overrides` option of the `kubectl run` command.
73
77
 
74
78
  #### `kubernetes:delete`
75
79
 
76
80
  Confirms and delete all resources on cluster under namespace.
81
+
82
+ ## Example use: run rails console on non-preemptible GKE node
83
+
84
+ Add the following to your `deploy.rb`
85
+ ``` ruby
86
+ task :console do
87
+ set :command, "rails console"
88
+ set :env_hash, "RAILS_ENV" => fetch(:stage), "RAILS_MASTER_KEY" => File.read("#{Dir.pwd}/config/credentials/#{fetch(:stage)}.key").strip
89
+ set :kubectl_pod_overrides, '{"spec": {"affinity": {"nodeAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": {"nodeSelectorTerms": [{"matchExpressions": [{"key": "cloud.google.com/gke-preemptible", "operator": "DoesNotExist"} ] } ] } } } } }'
90
+
91
+ invoke :'kubernetes:command'
92
+ end
93
+ ```
94
+ You can now run `mina production console` to open a rails console in production environment with the image of your choice!
@@ -1,8 +1,7 @@
1
1
  require "tty-prompt"
2
2
  require "tty-spinner"
3
- require "securerandom"
4
3
  require "json"
5
- require "base64"
4
+ require "time"
6
5
 
7
6
  # required by mina
8
7
  set :execution_mode, :pretty
@@ -26,9 +25,9 @@ namespace :kubernetes do
26
25
  end
27
26
 
28
27
  task :command do
28
+ set :skip_report_time, true
29
29
  desc "Spins up temporary pod with image and runs given command in interactive shell, passing given environment variable"
30
30
  set_tag_from_branch_commit unless fetch(:image_tag)
31
- wait_until_image_ready(fetch(:image_tag))
32
31
  run_command(fetch(:command), env_hash_arg)
33
32
  end
34
33
 
@@ -52,7 +51,7 @@ end
52
51
 
53
52
  def set_tag_from_branch_commit
54
53
  run :local do
55
- comment "Updating Git branches..."
54
+ comment "Refreshing Git branches..."
56
55
  end
57
56
  remote_branches = `git fetch --prune && git branch -r --no-merged master --sort=-committerdate | grep origin`.split("\n").collect { |b| b.strip.gsub("origin/", "") }.reject { |b| b == "master" }
58
57
  set :branch, TTY::Prompt.new.select("Which branch?", ["master"].concat(remote_branches))
@@ -69,7 +68,7 @@ end
69
68
 
70
69
  def wait_until_image_ready(commit)
71
70
  run :local do
72
- comment "Check image #{fetch(:image_repo)}:#{commit} is available..."
71
+ comment "Checking image #{fetch(:image_repo)}:#{commit} is available..."
73
72
  end
74
73
  spinner = TTY::Spinner.new
75
74
  spinner.auto_spin
@@ -85,16 +84,37 @@ end
85
84
 
86
85
  def run_command(command, env_hash = {})
87
86
  env = env_hash.collect{|k,v| "--env #{k}=#{v}" }.join(" ")
88
- label = command.downcase.gsub(" ", "-").gsub(":", "-")
87
+ label = command.downcase.gsub(" ", "-").gsub(":", "-")+ "-#{`whoami`}".strip + "-#{fetch(:branch)}"
89
88
  proxy_env = "HTTPS_PROXY=#{fetch(:proxy)}" if fetch(:proxy)
90
89
 
91
- # using system instead of mina's command so tty opens successfully
92
- system "#{proxy_env} kubectl run #{label}-#{SecureRandom.hex(4)} --rm -i --tty --restart=Never --context=#{fetch(:kubernetes_context)} --namespace=#{fetch(:namespace)} --image #{fetch(:image_repo)}:#{fetch(:image_tag)} #{env} -- #{command}"
90
+ run :local do
91
+ comment "Lauching Pod #{color(label, 36)} to run #{color(command, 36)}"
92
+ end
93
+
94
+ pod_description = `#{proxy_env} kubectl get pod #{label} -o json --ignore-not-found --context=#{fetch(:kubernetes_context)} --namespace=#{fetch(:namespace)}`
95
+
96
+ if pod_description.empty?
97
+ wait_until_image_ready(fetch(:image_tag))
98
+ run_command = "#{proxy_env} kubectl run #{label} --rm -i --tty --restart=Never --overrides='#{fetch(:kubectl_pod_overrides)}' --context=#{fetch(:kubernetes_context)} --namespace=#{fetch(:namespace)} --image #{fetch(:image_repo)}:#{fetch(:image_tag)} #{env}"
99
+ system "#{run_command} -- #{command}"
100
+ else
101
+ started_at = Time.parse(JSON.parse(pod_description)["status"]["startTime"]).strftime('%b %e, %H:%M')
102
+ choice = TTY::Prompt.new.select("Pod already exists, running since #{started_at} UTC, what would you like to do?", {"Reattach session" => 1, "Kill it" => 0})
103
+
104
+ attach_command = "#{proxy_env} kubectl attach #{label} -i --tty -c #{label} --context=#{fetch(:kubernetes_context)} --namespace=#{fetch(:namespace)}"
105
+ delete_command = "#{proxy_env} kubectl delete pod #{label} --context=#{fetch(:kubernetes_context)} --namespace=#{fetch(:namespace)}"
106
+
107
+ if choice == 1
108
+ system "#{attach_command} && #{delete_command}"
109
+ else
110
+ system delete_command
111
+ end
112
+ end
93
113
  end
94
114
 
95
115
  def apply_kubernetes_resources(options)
96
116
  run :local do
97
- comment "Apply all Kubernetes resources..."
117
+ comment "Applying all Kubernetes resources..."
98
118
 
99
119
  proxy_env = "HTTPS_PROXY=#{fetch(:proxy)}" if fetch(:proxy)
100
120
  filepaths = options&.[](:filepaths) || "config/deploy/#{fetch(:stage)}"
@@ -1,5 +1,5 @@
1
1
  module Mina
2
2
  module Kubernetes
3
- VERSION = "2.4.1"
3
+ VERSION = "2.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mina-kubernetes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Sabourin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-03 00:00:00.000000000 Z
11
+ date: 2020-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler