kubernetes_helper 1.9.0 → 1.11.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: 6e2e119480c5c91e09f658c135c25adc802c26acfd64255a60d71ef71f1d71f3
4
- data.tar.gz: a47d27732e7c42f80dd00ee32e5a36bf67cf0fbba533f3650cd3eec528ce2ec6
3
+ metadata.gz: 23c41532791e352e8faf3292df90c6b1d6e62846a28037be8d28cb7d92441104
4
+ data.tar.gz: e74e693b4b8c741cb0df1da0e6e4e94a28b1ec20cf9a43469dd09bbb67884e91
5
5
  SHA512:
6
- metadata.gz: bbdb0771ba124950ce8704528349c16e52f29d90af6b6581db3f423a377aba4f91d5ca1f82387e976d9a88394f327037c782185632e3f83eef7338b8cf50eb39
7
- data.tar.gz: e74722f42ec99b9b22ad01d932203bf968a2610f0724032ca83141201ab3db46d231f666878d283fcc1020fc4c122f5388e938bdbb23c2b6dac901af5bc684ed
6
+ metadata.gz: b8654828bde9d8de102165c5b034b1d19aa948e5da709b280a96f922f881792756e1a791878cb2ffa3a3a8fd45baa52ab25399f17f89ecb51b4a9df44e38c667
7
+ data.tar.gz: ff68484362812d0a247f2f42469f731869de61380a60011c2627714e5a94fb585605aa68e9e11fc63ea529e59a64d7fd17ba119ceb0b0c3795dbecd07ce8910f
data/README.md CHANGED
@@ -9,7 +9,15 @@ cd my_app/
9
9
  gem install kubernetes_helper -v '~> 1.0'
10
10
  kubernetes_helper generate_templates
11
11
  ```
12
- Note: Requires ruby 1.7+
12
+ Note: Requires ruby 1.7+
13
+
14
+ ## Configuration
15
+ - Edit `.kubernetes/settings.rb` and enter or replace all settings with the valid ones
16
+ - For special applications where default configurations are not enough, you can do the following:
17
+ - Download the corresponding template from [lib/templates](lib/templates)
18
+ - Put it inside `.kubernetes` folder
19
+ - Customize based on your needs (You can add or use your custom variables from `settings.rb`)
20
+ Note: The local template will be used instead of the default.
13
21
 
14
22
  ## Deployment
15
23
  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.
@@ -21,7 +29,7 @@ Once you generated the basic templates, it comes with the corresponding [readme.
21
29
  - Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
22
30
  - Replaces all setting values based on `DEPLOY_ENV`
23
31
  - 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'`
32
+ - Supports for sub templates by `include_template 'template_name.yml.erb', { my_local_var: 10 }`
25
33
  Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
26
34
 
27
35
  - Run kubernetes commands
@@ -44,6 +52,71 @@ Once you generated the basic templates, it comes with the corresponding [readme.
44
52
  When performing a script it looks first for file inside .kubernetes folder, if not exist,
45
53
  it looks for the file inside kubernetes_helper template folder.
46
54
 
55
+ ## Settings API
56
+ Below settings are used when running Continuous Deployment
57
+ - `continuous_deployment.image_name` (String): Partial docker image url. Sample: `gcr.io/my-account/my_app_name`
58
+ - `continuous_deployment.image_tag` (String, default 'latest'): Image tag to be used for this application
59
+ - `continuous_deployment.project_name`: Cloud project name. Sample: `my-project-name`
60
+ - `continuous_deployment.cluster_name`: Cluster cluster name. Sample: `my-cluster-name`
61
+ - `continuous_deployment.cluster_region`: Cluster region name. Sample: `europe-west4-a`
62
+ - `continuous_deployment.docker_build_cmd`: Docker command to build the corresponding image. Sample: `build --target production -f Dockerfile `
63
+ - `continuous_deployment.update_deployment` (Boolean, default: false): If true permits to re-generate and update the k8s deployment(s) before applying the new version (new docker image)
64
+
65
+ Below settings are used when configuring the application in the k8s environment
66
+ - `deployment.name` (String): Web deployment name (Note: Underscores are not accepted). Sample: `my-app`
67
+ - `deployment.replicas` (Integer): Quantity of replicas. Sample: `1`
68
+ - `deployment.replicas_range` (Array<min, max, cpu_percentage>, Optional): Defines the minimum and the maximum number of pods that could automatically be created when `CPUUtilizationPercentage` is above than defined. Sample: `[1, 3, 50]`
69
+ - `deployment.cloud_secret_name` (String, Optional): K8s credentials name where cloud secrets will be saved (includes permission like DB). Sample: `my-app-cloud-secret`
70
+ - `deployment.cloud_sql_instance` (String, Optional): Cloud sql instance name. Sample: `my-project:europe-west1:my-instance-name=tcp:5432` (5432 => postgres, 3306 => mysql)
71
+ - `deployment.env_vars` (Hash, optional): List of static env variables (Note: Not recommended for sensitive values). Sample: `{ 'RAILS_ENV' => 'production' }`
72
+ - `deployment.command` (String, Optional): Bash command to be used for web containers. Sample: `rails s -b 0.0.0.0`
73
+ - `deployment.liveness_path` (String, Optional): Relative path to be used for readiness and liveness checker of the web app. Sample: `/check_liveness`
74
+ - `deployment.custom_volumes` (Hash<name: path>, Optional): Custom volumes to be mounted.
75
+ Sample volume: `{ my_volume: { kind: 'hostPath', mount_path: '/', settings: { path: '..', type: 'Directory' } } }`
76
+ Sample secret: `{ pubsub_secret: { kind: 'secret', mount_path: '/secrets/pubsub', settings: { secretName: 'my_secret_name' } } }`
77
+
78
+ - `deployment.job_name` (String, optional): Job deployment name (Note: Underscores are not accepted). Sample: `my-app-job`. Note: This deployment is created only if this value is present
79
+ - `deployment.job_command` (String, optional): Bash command to be used for job container. Sample: `bundle exec sidekiq`
80
+ - `deployment.job_sidekiq_alive_gem` (Boolean, default false): If true will add liveness checker settings using `sidekiq_alive_gem` (`sidekiq_alive` gem needs to be present in your Gemfile)
81
+ - `deployment.job_services` (Array, Optional, only `job_sidekiq_alive_gem` or `job_services` is allowed): List of linux service names that are required for a healthy job container. Sample: `['sidekiq', 'cron']`
82
+
83
+
84
+ - `secrets.name` (String): K8s secrets name where env vars will be saved and fetched from. Sample: `my-app-secrets`
85
+
86
+ - `service.name`: K8s service name. Sample: `my-app-service`
87
+ - `service.port_name`: Http port name to connect between k8s ingress and service. Sample: `http-port`. Note: max 15 characters
88
+ - `service.backend_port_name` (String): Web backend port name to be connected between k8s service and web deployments. Sample: `b-port`. Note: max 15 characters
89
+
90
+ - `ingress.name`: Name of k8s ingress for the app: Sample: `my-app-ingress`
91
+ - `ingress.ip_name` (Optional): Static ip address is not created nor assigned if empty value. Sample: `my-app-static-ip`
92
+ - `ingress.certificate_name` (Optional): Ssl certificate is not created nor assigned if empty value. Sample: `my-app-lets-encrypt`. Note: requires `certificate_domain`
93
+ - `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
94
+
95
+ - `cloud.name` (String, optional): Cloud service name. Default `gcloud`.
96
+
97
+ ### Partials
98
+ - `_container_extra_settings.yml` Partial template to add custom container settings. Receives `pod` as local variable (`web` | `job` | `cloudsql` | `logs`). Sample:
99
+ ```yaml
100
+ <% if locals[:pod] == 'job' %>
101
+ resources:
102
+ requests:
103
+ cpu: 50m
104
+ memory: 256Mi
105
+ limits:
106
+ cpu: 500m
107
+ memory: 1Gi
108
+ <% end %>
109
+ ```
110
+ - `_custom_containers.yml` Partial template to add extra containers (Receives `pod` as local variable: `web` | `job`). Sample:
111
+ ```yaml
112
+ <% if locals[:pod] == 'job' %>
113
+ - name: scraper
114
+ image: owencio/easy_scraper
115
+ ...
116
+ <% end %>
117
+ ```
118
+ - `_cd_apply_images.sh` Partial template to customize the process to apply the new version (new docker image)
119
+
47
120
  ## Templating
48
121
  When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
49
122
  All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KubernetesHelper
4
- VERSION = '1.9.0'
4
+ VERSION = '1.11.2'
5
5
  end
@@ -3,7 +3,7 @@
3
3
  livenessProbe: &liveness_probe
4
4
  exec:
5
5
  command: [ /bin/sh, -c,
6
- 'if [ $(ps -ef | grep "<%= deployment.job_services.join("\|") %>" | grep -v "grep" | wc -l) -lt <%= deployment.job_services.count %> ]; then
6
+ 'if [ $(ps -ef | grep "<%= deployment.job_services.join("\\|") %>" | grep -v "grep" | wc -l) -lt <%= deployment.job_services.count %> ]; then
7
7
  echo "Some required services are not running"; exit 1;
8
8
  fi' ]
9
9
  initialDelaySeconds: 120
@@ -9,4 +9,4 @@
9
9
  name: <%= deployment.name %>
10
10
  minReplicas: <%= deployment.replicas_range[0] %>
11
11
  maxReplicas: <%= deployment.replicas_range[1] %>
12
- targetCPUUtilizationPercentage: 50
12
+ targetCPUUtilizationPercentage: <%= deployment.replicas_range[2] || 50 %>
@@ -8,7 +8,6 @@
8
8
  emptyDir: {}
9
9
 
10
10
  # Custom volumes
11
- # { my_volume: { kind: 'hostPath', mount_path: '/', settings: { path: '..', type: 'Directory' } } }
12
11
  <% (deployment.custom_volumes || {}).each do |name, vol| %>
13
12
  - name: <%= name %>
14
13
  <%= vol[:kind] %>:
data/lib/templates/cd.sh CHANGED
@@ -15,7 +15,7 @@ DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd || 'build -f Dockerf
15
15
  CI_COMMIT_SHA=$(git rev-parse --verify HEAD || :)
16
16
  CI_COMMIT_SHA=${CI_COMMIT_SHA:-$(date +%s) }
17
17
  DEPLOY_NAME="${IMAGE_NAME}:${CI_COMMIT_SHA}"
18
- LATEST_NAME="${IMAGE_NAME}:latest"
18
+ LATEST_NAME="${IMAGE_NAME}:<%= continuous_deployment.image_tag || 'latest' %>"
19
19
 
20
20
  if [ ! -z "$KB_AUTH_TOKEN" ]
21
21
  then
@@ -37,10 +37,14 @@ then
37
37
  fi
38
38
 
39
39
 
40
- ## Build and push containers
41
- docker $DOCKER_BUILD_CMD -t $DEPLOY_NAME .
40
+ ALREADY_DEPLOYED="$(gcloud container images list-tags --format='get(tags)' $IMAGE_NAME | grep $CI_COMMIT_SHA || :;)"
41
+ if [ -z $ALREADY_DEPLOYED ]
42
+ then
43
+ ## Build and push containers
44
+ docker $DOCKER_BUILD_CMD -t $DEPLOY_NAME .
45
+ docker push $DEPLOY_NAME
46
+ fi
42
47
  docker tag $DEPLOY_NAME $LATEST_NAME
43
- docker push $DEPLOY_NAME
44
48
  docker push $LATEST_NAME
45
49
 
46
50
  ## Update new secrets defined in secrets.yml as ENV vars for deployments
@@ -54,5 +58,4 @@ for deployment in "${deployments[@]}"; do
54
58
  [ -z "$deployment" ] && continue # if empty value
55
59
 
56
60
  <%= include_template "_cd_apply_images.sh" %>
57
- done
58
-
61
+ done
@@ -4,4 +4,4 @@ metadata:
4
4
  name: '<%=ingress.certificate_name%>'
5
5
  spec:
6
6
  domains: # does not support for willcard domains
7
- - '<%=ingress.domain_name%>'
7
+ - '<%= ingress.domain_name || ingress.certificate_domain %>'
@@ -21,7 +21,7 @@ documents:
21
21
  spec: &template_spec
22
22
  containers:
23
23
  - &app_container
24
- image: '<%=continuous_deployment.image_name%>:latest'
24
+ image: '<%= continuous_deployment.image_name %>:<%= continuous_deployment.image_tag || "latest" %>'
25
25
  <% if deployment.command %>
26
26
  command: ["/bin/bash", "-c", "<%= deployment.command %>"]
27
27
  <% end %>
@@ -1,4 +1,4 @@
1
- <% if ingress.domain_name %>
1
+ <% if ingress.certificate_name %>
2
2
  <%= include_template "certificate.yml" %>
3
3
  <% end %>
4
4
 
@@ -17,7 +17,7 @@ metadata:
17
17
  kubernetes.io/ingress.global-static-ip-name: "<%=ingress.ip_name%>"
18
18
  <% end %>
19
19
 
20
- <% if ingress.domain_name %>
20
+ <% if ingress.certificate_name %>
21
21
  networking.gke.io/managed-certificates: '<%=ingress.certificate_name%>'
22
22
  <% end %>
23
23
 
@@ -4,7 +4,7 @@
4
4
  apiVersion: cloud.google.com/v1beta1
5
5
  kind: BackendConfig
6
6
  metadata:
7
- name: '<%=service.config_name%>'
7
+ name: '<%=service.name%>-bconfig'
8
8
  spec:
9
9
  timeoutSec: 1800
10
10
  ---
@@ -16,7 +16,7 @@ metadata:
16
16
  name: '<%=service.name%>'
17
17
  annotations:
18
18
  <% if cloud.name == 'gcloud' %>
19
- beta.cloud.google.com/backend-config: '{"ports": {"80":"<%=service.config_name%>"}}'
19
+ beta.cloud.google.com/backend-config: '{"ports": {"80":"<%=service.name%>-bconfig"}}'
20
20
  <% end %>
21
21
  spec:
22
22
  selector:
@@ -6,6 +6,7 @@ settings = {
6
6
  deployment: {
7
7
  name: app_name,
8
8
  replicas: is_production ? 2 : 1,
9
+ replicas_range: nil, # [min, max] or nil to ignore, sample: is_production ? [1, 2] : nil
9
10
  cloud_secret_name: "#{is_production ? 'production' : 'beta'}-cloud-secret",
10
11
  cloud_sql_instance: 'xxx:xxx:xxx=tcp:5432', # 5432 => postgres, 3306 => mysql
11
12
  env_vars: {}, # Sample: { 'CUSTOM_VAR' => 'value' }
@@ -20,10 +21,11 @@ settings = {
20
21
  name: "#{app_name}-ingress",
21
22
  ip_name: "#{app_name}-static-ip", # nil if static ip is not necessary
22
23
  certificate_name: "#{app_name}-lets-encrypt", # nil if ssl is not required
23
- domain_name: is_production ? 'myapp.com' : 'beta.myapp.com' # nil if domain is not required
24
+ certificate_domain: is_production ? 'myapp.com' : 'beta.myapp.com' # nil if domain is not required
24
25
  },
25
26
  continuous_deployment: {
26
27
  image_name: "gcr.io/my-account/#{app_name}",
28
+ image_tag: 'latest',
27
29
  project_name: 'my-project-name',
28
30
  cluster_name: 'my-cluster-name',
29
31
  cluster_region: 'europe-west4-a',
@@ -36,8 +38,7 @@ settings = {
36
38
  service: {
37
39
  name: app_name,
38
40
  port_name: 'http-port', # max 15 characters
39
- backend_port_name: 'b-port', # max 15 characters
40
- config_name: "#{app_name}-backend-config"
41
+ backend_port_name: 'b-port' # max 15 characters
41
42
  }
42
43
  }
43
44
 
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: 1.9.0
4
+ version: 1.11.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-11-18 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erb