kubernetes_helper 0.3.2 → 1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ef481cc4f5afcb7c2bca9605b7ecc72649ac2e2522400a58e2dc964802f22bd
4
- data.tar.gz: 2b2aa0863511f4aa192a1332d87f12c80ce1c30a435248ade3f5437192e83384
3
+ metadata.gz: a680149c9a36ebc58fc0f7560a1257bc48bedfbf8a12ed93cfed112b12799407
4
+ data.tar.gz: b0b0dafefba5594194bed8bf990ace04f04297939f2861575d79b84c716aacca
5
5
  SHA512:
6
- metadata.gz: 7c0250a176f8acc04262bceab8cf550306006b540a4df43590b94f5bbb81e69f768fa023bcf0e88ffbd84dbd1d15964f374cf1950a6ddfc23211b1413487c380
7
- data.tar.gz: bddf79fea09a01c9329592482866f552744ba0113952500cb3d032bf619ae2855613f8b9acc5567a87587c38f780c92e983c20338c68e5ec0289c3b455f1cbfb
6
+ metadata.gz: 2b3829818e7e37a552fe645686b954a27ac7779ab6097aaba16fe3b6b91538c471c4501b9c9d1346230737553772058ae0c4cc5183b4fe61ca1a289cf5422d24
7
+ data.tar.gz: c98898c74f76a1d796387953450360a5d167b070a23461a58f3ae20b93a840906920fc191b196354132ff124925d6c877bd1baf28a60a5d6f766fd7c0af89e60
data/README.md CHANGED
@@ -1,37 +1,61 @@
1
1
  # KubernetesHelper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kubernetes_helper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This gem is a helper to manage easily Kubernetes settings for GCloud (easy customization for other cloud services) where configuring and deploying a new application can be done in a couple of minutes.
4
+ Configuration and customization can be done for multiple environments and at any level which permits to deploy simple and complex applications.
6
5
 
7
6
  ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'kubernetes_helper'
7
+ ```bash
8
+ cd my_app/
9
+ gem install kubernetes_helper -v '~> 1.0'
10
+ kubernetes_helper generate_templates
13
11
  ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install kubernetes_helper
22
-
23
- ## Usage
12
+ Note: Requires ruby 1.7+
13
+
14
+ ## Deployment
15
+ Once you generated the basic templates, it comes with the corresponding [readme.md](/lib/templates/README.md) which includes all the steps to deploy your application.
16
+
17
+ ## API
18
+ - Run any kubernetes document
19
+ `DEPLOY_ENV=<env name> kubernetes_helper run_deployment "<document name>" "<bash command>"`
20
+ Evaluates the kubernetes document with the following details:
21
+ - Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
22
+ - Replaces all setting values based on `DEPLOY_ENV`
23
+ - Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
24
+ - Supports for template including by `include_template 'template_name.yml.erb'`
25
+ Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
26
+
27
+ - Run kubernetes commands
28
+ `DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
29
+ Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
30
+ Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
31
+
32
+ - Run kubernetes bash scripts
33
+ `DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
34
+ Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
35
+ All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
36
+ Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
37
+
38
+ - Generate templates
39
+ `DEPLOY_ENV=<env name> kubernetes_helper generate_templates "<mode_or_template_name>"`
40
+ Copy files based on mode (`basic|advanced`) or a specific file from templates.
41
+ Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
42
+ Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
43
+
44
+ When performing a script it looks first for file inside .kubernetes folder, if not exist,
45
+ it looks for the file inside kubernetes_helper template folder.
46
+
47
+ ## Templating
48
+ When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
49
+ All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
50
+ These setting variables use [erb](https://github.com/ruby/erb) template gem to define variable replacement and conditional blocks, and so on.
51
+ Note: Setting variable values are referenced as an object format instead of a hash format for simplicity.
52
+
53
+
54
+ ## Sample
55
+ https://owen2345.github.io/kubernetes_helper/
24
56
 
25
57
  ## TODO
26
- - Documentation
27
- - Include conditional blocks
28
- - Include hardcoded env values
29
- - Tasks
30
- - Add docs to include partials using `include_template 'sample.yml.erb'`
31
- - Rake verify files
32
58
  - Add one_step_configuration.sh
33
- - Fix if/end_if for blocks
34
- - Ability to copy specific template
35
59
 
36
60
  ## Contributing
37
61
 
@@ -9,8 +9,8 @@ case ARGV[0]
9
9
  when 'run_command'
10
10
  KubernetesHelper::Core.new(ENV['DEPLOY_ENV']).run_command(ARGV[1])
11
11
  # Run the deployment script.
12
- # Sample: DEPLOY_ENV=beta kubernetes_helper run_deployment "cd_gcloud.sh"
13
- when 'run_deployment'
12
+ # Sample: DEPLOY_ENV=beta kubernetes_helper run_deployment "cd.sh"
13
+ when 'run_deployment', 'run_script'
14
14
  script_path = KubernetesHelper.settings_path(ARGV[1], use_template: true)
15
15
  KubernetesHelper::Core.new(ENV['DEPLOY_ENV']).run_script(script_path)
16
16
  # Parses kubernetes yml files (supporting multiple documents, Config variables replacement, include secrets).
@@ -22,6 +22,7 @@ when 'run_yml'
22
22
  .parse_yml_file(KubernetesHelper.settings_path(ARGV[1], use_template: true), output_path)
23
23
  KubernetesHelper.run_cmd("#{ARGV[2]} -f #{output_path}")
24
24
  # Generate template files
25
+ # Sample: DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"
25
26
  when 'generate_templates' # Sample: kubernetes_helper generate basic
26
27
  mode = ARGV[1] || 'basic'
27
28
  KubernetesHelper.copy_templates(mode)
@@ -39,11 +39,14 @@ module KubernetesHelper
39
39
  file_name ? File.join(path, file_name) : path
40
40
  end
41
41
 
42
- # @param mode (basic, advanced)
43
- def self.copy_templates(mode)
42
+ # @param mode_or_file (basic, advanced, String) mode name or any specific template name
43
+ def self.copy_templates(mode_or_file)
44
44
  FileUtils.mkdir(settings_path) unless Dir.exist?(settings_path)
45
+ template_path = templates_path(mode_or_file)
46
+ return FileUtils.cp(template_path, settings_path(mode_or_file)) if File.exist?(template_path)
47
+
45
48
  files = %w[README.md secrets.yml settings.rb]
46
- files += %w[deployment.yml cd.sh ingress.yml service.yml] if mode == 'advanced'
49
+ files += %w[deployment.yml cd.sh ingress.yml service.yml] if mode_or_file == 'advanced'
47
50
  files.each do |name|
48
51
  path = settings_path(name)
49
52
  FileUtils.cp(templates_path(name), path) unless File.exist?(path)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KubernetesHelper
4
- VERSION = '0.3.2'
4
+ VERSION = '1.2'
5
5
  end
@@ -2,54 +2,56 @@
2
2
 
3
3
  ## Configure a new application environment
4
4
  - Create the project on Gcloud
5
- - Set the project where to work on
5
+ - Set the project where to work on
6
6
  `gcloud config set project my-project`
7
7
 
8
- - Create the cluster (Only if not exist)
8
+ - Create the cluster (Only if not exist)
9
9
  `gcloud container clusters create my-cluster`
10
10
  `# gcloud container clusters list --region europe-west4-a # to list clusters`
11
+ Note: `--preemptible` recommended for non production environments
11
12
 
12
- - Use the cluster/project as default
13
+ - Use the cluster/project as default
13
14
  `gcloud container clusters get-credentials my-cluster --zone europe-west4-a`
14
15
 
15
- - Install helper for the next commands
16
+ - Install helper for the next commands
16
17
  `gem install kubernetes_helper`
17
18
 
18
- - Verify or update k8s settings in .kubernetes/settings.rb
19
+ - Verify or update k8s settings in .kubernetes/settings.rb
20
+ Note: Please do not include sensitive values in this file, ENV values are recommended instead.
19
21
 
20
- - Register shared cloudsql proxy configuration (only if not exists)
22
+ - Register shared cloudsql proxy configuration (only if not exists)
21
23
  ```bash
22
24
  DEPLOY_ENV=beta kubernetes_helper run_command "kubectl create secret generic <%=deployment.cloud_secret_name%> --from-file=credentials.json=<path-to-downloaded/credentials.json>"
23
25
  ```
24
26
 
25
- - Register manually env vars (values must be encrypted using base64)
27
+ - Register manually env vars (values must be encrypted using base64)
26
28
  Open and register secret values in `.kubernetes/secrets.yml`
27
- Note: Enter base64 encoded values
29
+ Note: Enter base64 encoded values
28
30
  ```bash
29
31
  DEPLOY_ENV=beta kubernetes_helper run_yml 'secrets.yml' 'kubectl create'
30
32
  # kubectl get secrets # to list all secrets registered
31
33
  ```
32
34
 
33
- - Create deployment to run application
35
+ - Create deployment to run application
34
36
  ```bash
35
37
  DEPLOY_ENV=beta kubernetes_helper run_yml 'deployment.yml' 'kubectl create'
36
38
  # kubectl get deployment # to list deployments
37
39
  ```
38
40
 
39
- - Create service to connect pods and ingress
41
+ - Create service to connect pods and ingress
40
42
  ```bash
41
43
  DEPLOY_ENV=beta kubernetes_helper run_yml 'service.yml' 'kubectl create'
42
44
  # kubectl get services # to list all registered services
43
45
  ```
44
46
 
45
- - Create the public ip address (only if static ip is required)
47
+ - Create the public ip address (only if static ip is required)
46
48
  ```bash
47
49
  DEPLOY_ENV=beta kubernetes_helper run_command "gcloud compute addresses create <%=ingress.ip_name%> --global"
48
50
  # gcloud compute addresses list # to list static ips generated
49
51
  # Copy new external ip generated by the previous command and point your domain to it
50
52
  ```
51
53
 
52
- - Register ingress to receive external http calls (includes ssl certificates if defined)
54
+ - Register ingress to receive external http calls (includes ssl certificates if defined)
53
55
  ```bash
54
56
  DEPLOY_ENV=beta kubernetes_helper run_yml 'ingress.yml' 'kubectl create'
55
57
  # kubectl get ingress # to list all registered ingresses
@@ -60,18 +62,18 @@
60
62
  ```
61
63
 
62
64
  ## Apply any k8s setting changes
63
- - Secrets
65
+ - Secrets
64
66
  Open kubernetes secrets and add/edit/remove values and then save it
65
67
  `kubectl edit secret ...`
66
68
  Once secrets were updated, then restart all related pods, see: https://medium.com/devops-dudes/how-to-propagate-a-change-in-kubernetes-secrets-by-restarting-dependent-pods-b71231827656
67
69
 
68
- - Other settings
70
+ - Other settings
69
71
  ```bash
70
72
  DEPLOY_ENV=beta kubernetes_helper run_yml 'deployment.yml' 'kubectl apply'
71
73
  ```
72
74
 
73
75
  ## Configure continuous deployment for github actions
74
- * Go to github repository settings
76
+ * Go to github repository settings
75
77
  * Register a new secret variable with content downloaded from https://console.cloud.google.com/iam-admin/serviceaccounts
76
78
  (Make sure to attach a "Editor", "Storage Admin" and "Kubernetes engine cluster admin" role to the service account)
77
79
  ```bash
@@ -79,14 +81,14 @@
79
81
  production: PROD_CLOUD_TOKEN=<secret content here>
80
82
  ```
81
83
 
82
- * Add action to run deployment:
84
+ * Add action to run deployment:
83
85
  ```bash
84
86
  env:
85
87
  KB_AUTH_TOKEN: secrets.BETA_CLOUD_TOKEN
86
88
  run: DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
87
89
  ```
88
90
 
89
- * Sample:
91
+ * Sample:
90
92
  ```yml
91
93
  - run: sudo gem install kubernetes_helper
92
94
  - name: Staging deployment
File without changes
data/lib/templates/cd.sh CHANGED
@@ -5,33 +5,37 @@ set -e
5
5
  SCRIPT_DIR=`dirname "$(realpath -s "$0")"` # app_dir/.kubernetes/
6
6
  cd "$SCRIPT_DIR/../" # project directory
7
7
 
8
- DEPLOYMENTS="<%=[deployment.name, deployment.job_name].join(',')%>"
8
+ DEPLOYMENTS="<%=[deployment.job_name, deployment.name].join(',')%>"
9
9
  IMAGE_NAME="<%=continuous_deployment.image_name%>"
10
10
  CLUSTER_NAME="<%=continuous_deployment.cluster_name%>"
11
11
  PROJECT_NAME="<%=continuous_deployment.project_name%>"
12
12
  CLUSTER_REGION="<%=continuous_deployment.cluster_region%>"
13
13
  DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%>"
14
14
 
15
- CI_COMMIT_SHA=$(git rev-parse --verify HEAD)
15
+ CI_COMMIT_SHA=$(git rev-parse --verify HEAD || :)
16
+ CI_COMMIT_SHA=${CI_COMMIT_SHA:-$(date +%s) }
16
17
  DEPLOY_NAME="${IMAGE_NAME}:${CI_COMMIT_SHA}"
17
18
  LATEST_NAME="${IMAGE_NAME}:latest"
18
- AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
19
19
 
20
- rm -f -- $AUTH_PATH
21
- echo $KB_AUTH_TOKEN >> $AUTH_PATH
22
-
23
- ## ***** GOOGLE CONNECTOR
24
- # Download and install Google Cloud SDK
25
- if [ -z "$(which gcloud)" ]; then
26
- export CLOUDSDK_CORE_DISABLE_PROMPTS=1; curl https://sdk.cloud.google.com | bash && source /home/runner/google-cloud-sdk/path.bash.inc && gcloud --quiet components update kubectl
20
+ if [ ! -z "$KB_AUTH_TOKEN" ]
21
+ then
22
+ AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
23
+ rm -f -- $AUTH_PATH
24
+ echo $KB_AUTH_TOKEN >> $AUTH_PATH
25
+
26
+ ## ***** GOOGLE CONNECTOR
27
+ # Download and install Google Cloud SDK
28
+ if [ -z "$(which gcloud)" ]; then
29
+ export CLOUDSDK_CORE_DISABLE_PROMPTS=1; curl https://sdk.cloud.google.com | bash && source /home/runner/google-cloud-sdk/path.bash.inc && gcloud --quiet components update kubectl
30
+ fi
31
+
32
+ # Connect to cluster
33
+ gcloud auth activate-service-account --key-file $AUTH_PATH --project $PROJECT_NAME
34
+ gcloud docker --authorize-only --project $PROJECT_NAME
35
+ gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION
36
+ ## ***** END GOOGLE CONNECTOR
27
37
  fi
28
38
 
29
- # Connect to cluster
30
- gcloud auth activate-service-account --key-file $AUTH_PATH --project $PROJECT_NAME
31
- gcloud docker --authorize-only --project $PROJECT_NAME
32
- gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION
33
- ## ***** END GOOGLE CONNECTOR
34
-
35
39
 
36
40
  ## Build and push containers
37
41
  docker $DOCKER_BUILD_CMD -t $DEPLOY_NAME .
@@ -0,0 +1,7 @@
1
+ apiVersion: networking.gke.io/v1beta1
2
+ kind: ManagedCertificate
3
+ metadata:
4
+ name: '<%=ingress.certificate_name%>'
5
+ spec:
6
+ domains: # does not support for willcard domains
7
+ - '<%=ingress.domain_name%>'
@@ -47,29 +47,41 @@ documents:
47
47
  name: applog
48
48
  mountPath: /app/log
49
49
 
50
+ <% if deployment.cloud_secret_name %>
51
+ - &cloud_credentials_volume
52
+ name: gcloud-creds
53
+ mountPath: /secrets/gcloud
54
+ readOnly: true
55
+ <% end %>
56
+
57
+ <% if deployment.cloud_secret_name %>
50
58
  - &cloudsql_container
51
59
  image: gcr.io/cloudsql-docker/gce-proxy:1.09 # Cloud sql proxy
52
60
  name: cloudsql-proxy
53
61
  command: ["/cloud_sql_proxy", "--dir=/cloudsql",
54
62
  "-instances=<%=deployment.cloud_sql_instance%>",
55
- "-credential_file=/secrets/cloudsql/credentials.json"]
63
+ "-credential_file=/secrets/gcloud/credentials.json"]
64
+ env: []
56
65
  volumeMounts:
57
- - name: cloudsql-creds
58
- mountPath: /secrets/cloudsql
59
- readOnly: true
66
+ - *cloud_credentials_volume
67
+ <% end %>
60
68
 
61
69
  - &logs_container # print to stdout all log files
62
70
  name: print-logs
63
71
  image: busybox
64
- command: [/bin/sh, -c, 'until find log/*.log; do sleep 1; done; tail -n+1 -f log/*.log']
72
+ command: [/bin/sh, -c, 'until find /app/log/*.log; do sleep 1; done; tail -n+1 -f /app/log/*.log']
73
+ env: []
65
74
  volumeMounts:
66
75
  - *log_volume
67
76
 
68
77
  terminationGracePeriodSeconds: 120
69
78
  volumes:
70
- - name: cloudsql-creds
79
+ <% if deployment.cloud_secret_name %>
80
+ - name: gcloud-creds
71
81
  secret:
72
82
  secretName: '<%=deployment.cloud_secret_name%>'
83
+ <% end %>
84
+
73
85
  - name: applog
74
86
  emptyDir: {}
75
87
 
@@ -98,7 +110,7 @@ documents:
98
110
  <% end %>
99
111
 
100
112
  <% if deployment.job_sidekiq_alive_gem %>
101
- <%= include_template "_sidekiq_alive_gem.yml.erb" %>
113
+ <%= include_template "_sidekiq_alive_gem.yml" %>
102
114
  <% else %>
103
115
  ports: [ ]
104
116
  <% if (deployment.job_services || []).any? %>
@@ -114,10 +126,13 @@ documents:
114
126
  <% end %>
115
127
  <% end %>
116
128
 
129
+ <% if deployment.cloud_secret_name %>
117
130
  - *cloudsql_container
131
+ <% end %>
132
+
118
133
  - <<: *logs_container
119
134
  <% end %>
120
135
 
121
136
  <% if deployment.replicas_range %>
122
- <%= include_template "_replicas.yml.erb" %>
137
+ <%= include_template "_replicas.yml" %>
123
138
  <% end %>
@@ -1,11 +1,5 @@
1
1
  <% if ingress.domain_name %>
2
- apiVersion: networking.gke.io/v1beta1
3
- kind: ManagedCertificate
4
- metadata:
5
- name: '<%=ingress.certificate_name%>'
6
- spec:
7
- domains: # does not support for willcard domains
8
- - '<%=ingress.domain_name%>'
2
+ <%= include_template "certificate.yml" %>
9
3
  <% end %>
10
4
 
11
5
  ---
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  is_production = ENV['DEPLOY_ENV'] == 'production'
4
- app_name = is_production ? 'my_app' : 'my_beta_app'
4
+ app_name = is_production ? 'my-app' : 'my-beta-app' # underscore not accepted
5
5
  settings = {
6
6
  deployment: {
7
7
  name: app_name,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - owen2345
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-04 00:00:00.000000000 Z
11
+ date: 2021-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erb
@@ -40,9 +40,10 @@ files:
40
40
  - lib/kubernetes_helper/railtie.rb
41
41
  - lib/kubernetes_helper/version.rb
42
42
  - lib/templates/README.md
43
- - lib/templates/_replicas.yml.erb
44
- - lib/templates/_sidekiq_alive_gem.yml.erb
43
+ - lib/templates/_replicas.yml
44
+ - lib/templates/_sidekiq_alive_gem.yml
45
45
  - lib/templates/cd.sh
46
+ - lib/templates/certificate.yml
46
47
  - lib/templates/deployment.yml
47
48
  - lib/templates/ingress.yml
48
49
  - lib/templates/secrets.yml