kubernetes_helper 1.13.0 → 1.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +78 -70
- data/lib/kubernetes_helper/core.rb +4 -16
- data/lib/kubernetes_helper/version.rb +1 -1
- data/lib/templates/README.md +68 -33
- data/lib/templates/cd.sh +5 -4
- data/lib/templates/certificate.yml +8 -3
- data/lib/templates/ingress.yml +8 -4
- data/lib/templates/settings.rb +1 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33da4492b4f95cbd8a47c55b8b2449f99b0c2224e7b52017e12d95c50efa2414
|
4
|
+
data.tar.gz: d7a12f66bfbd86a252acd1d6e57978a1f73497f7578cc0f9c484b1c4f93983a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10cabcc986c70e54adf560d32c834c6421958fdbf0dcaf1d991e9a1bd994cb34c4f9dd126261abc638c2bff1dd899e776840c83da8887c10e0975607cd3641b9
|
7
|
+
data.tar.gz: 306780df8402e223537b35fc02edaf7f8157b91aa7c8ce0512d38e49e46bcb529a3da06eb495fd0ea167e076423f6cd952ed5a5b9d2dbcf5bf14b34a627d561c
|
data/README.md
CHANGED
@@ -3,78 +3,38 @@
|
|
3
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
4
|
Configuration and customization can be done for multiple environments and at any level which permits to deploy simple and complex applications.
|
5
5
|
|
6
|
-
##
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
kubernetes_helper
|
11
|
-
```
|
12
|
-
Note: Requires ruby 1.7+
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
- Run any kubernetes document
|
27
|
-
`DEPLOY_ENV=<env name> kubernetes_helper run_deployment "<document name>" "<bash command>"`
|
28
|
-
Evaluates the kubernetes document with the following details:
|
29
|
-
- Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
|
30
|
-
- Replaces all setting values based on `DEPLOY_ENV`
|
31
|
-
- Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
|
32
|
-
- Supports for sub templates by `include_template 'template_name.yml.erb', { my_local_var: 10 }`
|
33
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
|
34
|
-
|
35
|
-
- Run kubernetes commands
|
36
|
-
`DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
|
37
|
-
Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
|
38
|
-
Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
|
39
|
-
|
40
|
-
- Run kubernetes bash scripts
|
41
|
-
`DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
|
42
|
-
Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
|
43
|
-
All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
|
44
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
|
6
|
+
## Steps
|
7
|
+
1. Install the `kubernetes_helper` gem
|
8
|
+
```bash
|
9
|
+
cd my_app/
|
10
|
+
gem install kubernetes_helper -v '~> 1.0'
|
11
|
+
```
|
12
|
+
Note: Requires ruby 1.7+
|
13
|
+
|
14
|
+
2. Generate helper settings
|
15
|
+
```bash
|
16
|
+
kubernetes_helper generate_templates
|
17
|
+
```
|
18
|
+
Note: `.kubernetes` folder was added. For special applications where default configurations are not enough, you can do the following:
|
19
|
+
- Download the required template from [lib/templates](lib/templates)
|
20
|
+
- Put it inside `.kubernetes` folder
|
21
|
+
- Customize based on your needs (You can add or use your custom variables from `settings.rb`)
|
22
|
+
Note: The local template will be used instead of the default one.
|
23
|
+
|
24
|
+
3. Install/setup the application on kubernetes
|
25
|
+
Open [.kubernetes/README.md](lib/templates/README.md) to see the instructions (customize the file according to your project and keep it updated)
|
45
26
|
|
46
|
-
- Generate templates
|
47
|
-
`DEPLOY_ENV=<env name> kubernetes_helper generate_templates "<mode_or_template_name>"`
|
48
|
-
Copy files based on mode (`basic|advanced`) or a specific file from templates.
|
49
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
|
50
|
-
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
|
51
|
-
|
52
|
-
When performing a script it looks first for file inside .kubernetes folder, if not exist,
|
53
|
-
it looks for the file inside kubernetes_helper template folder.
|
54
27
|
|
55
28
|
## Settings API
|
56
|
-
|
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
|
-
- `continuous_deployment.before_building_image` (String, default: empty): Add the ability to enter commands before building docker image
|
65
|
-
- `continuous_deployment.after_building_image` (String, default: empty): Add the ability to enter commands after building docker image
|
66
|
-
|
67
|
-
Below settings are used when configuring the application in the k8s environment
|
29
|
+
### Application deployment.yml
|
68
30
|
- `deployment.name` (String): Web deployment name (Note: Underscores are not accepted). Sample: `my-app`
|
69
31
|
- `deployment.replicas` (Integer): Quantity of replicas. Sample: `1`
|
70
32
|
- `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]`
|
71
33
|
- `deployment.cloud_secret_name` (String, Optional): K8s credentials name where cloud secrets will be saved (includes permission like DB). Sample: `my-app-cloud-secret`
|
72
34
|
- `deployment.cloud_sql_instance` (String, Optional): Cloud sql instance name. Sample: `my-project:europe-west1:my-instance-name=tcp:5432` (5432 => postgres, 3306 => mysql)
|
73
|
-
- `deployment.env_vars` (Hash, optional): List of static env variables (Note: Not recommended for sensitive values).
|
74
|
-
|
75
|
-
|
76
|
-
Advanced example: `external_secrets: { papertrail_port: { name: 'common_secrets', key: 'paper_trail_port' } }` will import `paper_trail_port` value from `common_secrets` yml as `PAPERTRAIL_PORT`
|
77
|
-
|
35
|
+
- `deployment.env_vars` (Hash, optional): List of static or external env variables (Note: Not recommended for sensitive values).
|
36
|
+
Sample: `{ 'RAILS_ENV' => 'production' }`
|
37
|
+
Example for external secrets: `{ PAPERTRAIL_PORT: { name: 'common_secrets', key: 'paper_trail_port' }` will import `paper_trail_port` value from `common_secrets` yml as `PAPERTRAIL_PORT`
|
78
38
|
- `deployment.command` (String, Optional): Bash command to be used for web containers. Sample: `rails s -b 0.0.0.0`
|
79
39
|
- `deployment.liveness_path` (String, Optional): Relative path to be used for readiness and liveness checker of the web app. Sample: `/check_liveness`
|
80
40
|
- `deployment.custom_volumes` (Hash<name: path>, Optional): Custom volumes to be mounted.
|
@@ -84,26 +44,42 @@ Below settings are used when configuring the application in the k8s environment
|
|
84
44
|
- `deployment.log_folder` (String, default `/app/log`): Logs to be printed from
|
85
45
|
- `deployment.app_port` (Integer, default 3000): Application port number
|
86
46
|
|
47
|
+
### Application deployment.yml for jobs or services without internet interaction (Optional)
|
87
48
|
- `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
|
88
49
|
- `deployment.job_command` (String, optional): Bash command to be used for job container. Sample: `bundle exec sidekiq`
|
89
50
|
- `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)
|
90
51
|
- `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']`
|
91
52
|
|
92
|
-
|
53
|
+
### Applications secrets.yml (Optional)
|
93
54
|
- `secrets.name` (String): K8s secrets name where env vars will be saved and fetched from. Sample: `my-app-secrets`
|
94
55
|
|
56
|
+
### Application service.yml (Optional)
|
95
57
|
- `service.name`: K8s service name. Sample: `my-app-service`
|
96
58
|
- `service.port_name` (String, default `http-port`): Http port name to connect between k8s ingress and service. Sample: `http-port`. Note: max 15 characters
|
97
59
|
- `service.backend_port_name` (String, default `b-port`): Web backend port name to be connected between k8s service and web deployments. Sample: `b-port`. Note: max 15 characters
|
98
60
|
|
61
|
+
### Application ingress.yml (Optional)
|
99
62
|
- `ingress.name`: Name of k8s ingress for the app: Sample: `my-app-ingress`
|
100
63
|
- `ingress.ip_name` (Optional): Static ip address is not created nor assigned if empty value. Sample: `my-app-static-ip`
|
101
|
-
- `ingress.certificate_name` (
|
102
|
-
- `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
|
64
|
+
- `ingress.certificate_name` (Deprecated): Ssl certificate is not created nor assigned if empty value. Sample: `my-app-lets-encrypt`. Note: requires `certificate_domain`
|
65
|
+
- `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
|
66
|
+
To register multiple domains (Certificate names will be auto-generated like `mysite-com-lets-encrypt`): `certificate_domain: ['mysite.com', 'mysite.de', 'mysite.uk']`
|
103
67
|
|
104
68
|
- `cloud.name` (String, optional): Cloud service name. Default `gcloud`.
|
105
69
|
|
106
|
-
###
|
70
|
+
### Application CD (continuous deployment)
|
71
|
+
- `continuous_deployment.image_name` (String): Partial docker image url. Sample: `gcr.io/my-account/my_app_name`
|
72
|
+
- `continuous_deployment.image_tag` (String, default 'latest'): Image tag to be used for this application
|
73
|
+
- `continuous_deployment.project_name`: Cloud project name. Sample: `my-project-name`
|
74
|
+
- `continuous_deployment.cluster_name`: Cluster cluster name. Sample: `my-cluster-name`
|
75
|
+
- `continuous_deployment.cluster_region`: Cluster region name. Sample: `europe-west4-a`
|
76
|
+
- `continuous_deployment.docker_build_cmd` (deprecated): Docker command to build the corresponding image. Sample: `build --target production -f Dockerfile `
|
77
|
+
- `continuous_deployment.docker_cmd` (String): Docker command to build the corresponding image.
|
78
|
+
Simple docker image: `docker build -f Dockerfile -t $DEPLOY_NAME .`
|
79
|
+
Docker image with target: `docker build --target production -f Dockerfile -t $DEPLOY_NAME .`
|
80
|
+
- `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)
|
81
|
+
|
82
|
+
### Gem templating partials
|
107
83
|
- `_container_extra_settings.yml` Partial template to add custom container settings. Receives `pod` as local variable (`web` | `job` | `cloudsql` | `logs`). Sample:
|
108
84
|
```yaml
|
109
85
|
<% if locals[:pod] == 'job' %>
|
@@ -126,18 +102,50 @@ Below settings are used when configuring the application in the k8s environment
|
|
126
102
|
```
|
127
103
|
- `_cd_apply_images.sh` Partial template to customize the process to apply the new version (new docker image)
|
128
104
|
|
129
|
-
|
105
|
+
### Gem templating
|
130
106
|
When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
|
131
107
|
All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
|
132
108
|
These setting variables use [erb](https://github.com/ruby/erb) template gem to define variable replacement and conditional blocks, and so on.
|
133
109
|
Note: Setting variable values are referenced as an object format instead of a hash format for simplicity.
|
134
110
|
|
135
111
|
|
136
|
-
|
112
|
+
### Sample
|
137
113
|
https://owen2345.github.io/kubernetes_helper/
|
138
114
|
|
115
|
+
|
116
|
+
## API
|
117
|
+
- Run any kubernetes document
|
118
|
+
`DEPLOY_ENV=<env name> kubernetes_helper run_deployment "<document name>" "<bash command>"`
|
119
|
+
Evaluates the kubernetes document with the following details:
|
120
|
+
- Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
|
121
|
+
- Replaces all setting values based on `DEPLOY_ENV`
|
122
|
+
- Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
|
123
|
+
- Supports for sub templates by `include_template 'template_name.yml.erb', { my_local_var: 10 }`
|
124
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
|
125
|
+
|
126
|
+
- Run kubernetes commands
|
127
|
+
`DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
|
128
|
+
Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
|
129
|
+
Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
|
130
|
+
|
131
|
+
- Run kubernetes bash scripts
|
132
|
+
`DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
|
133
|
+
Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
|
134
|
+
All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
|
135
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
|
136
|
+
|
137
|
+
- Generate templates
|
138
|
+
`DEPLOY_ENV=<env name> kubernetes_helper generate_templates "<mode_or_template_name>"`
|
139
|
+
Copy files based on mode (`basic|advanced`) or a specific file from templates.
|
140
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
|
141
|
+
Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
|
142
|
+
|
143
|
+
When performing a script it looks first for file inside .kubernetes folder, if not exist,
|
144
|
+
it looks for the file inside kubernetes_helper template folder.
|
145
|
+
|
139
146
|
## TODO
|
140
147
|
- Add one_step_configuration.sh
|
148
|
+
- Change `include_template` into `ERB render partial`
|
141
149
|
|
142
150
|
## Contributing
|
143
151
|
|
@@ -67,26 +67,12 @@ module KubernetesHelper
|
|
67
67
|
def import_secrets(path, secrets_name)
|
68
68
|
path = KubernetesHelper.settings_path(path)
|
69
69
|
data = YAML.load(File.read(path)) # rubocop:disable Security/YAMLLoad
|
70
|
-
|
70
|
+
data['data'].keys.map do |secret|
|
71
71
|
{
|
72
72
|
'name' => secret.upcase,
|
73
73
|
'valueFrom' => { 'secretKeyRef' => { 'name' => secrets_name, 'key' => secret } }
|
74
74
|
}
|
75
75
|
end
|
76
|
-
secrets + external_secrets
|
77
|
-
end
|
78
|
-
|
79
|
-
def external_secrets
|
80
|
-
data = config_values.dig(:deployment, :external_secrets) || {}
|
81
|
-
data.map do |key, source|
|
82
|
-
source = source.is_a?(Hash) ? source : { name: source.to_s, key: key }
|
83
|
-
{
|
84
|
-
'name' => key.upcase.to_s,
|
85
|
-
'valueFrom' => {
|
86
|
-
'secretKeyRef' => { 'name' => source[:name], 'key' => source[:key].to_s }
|
87
|
-
}
|
88
|
-
}
|
89
|
-
end
|
90
76
|
end
|
91
77
|
|
92
78
|
def render_template(template_name, locals = {})
|
@@ -97,9 +83,11 @@ module KubernetesHelper
|
|
97
83
|
|
98
84
|
def static_env_vars
|
99
85
|
(config_values.dig(:deployment, :env_vars) || {}).map do |key, value|
|
86
|
+
external = value.is_a?(Hash)
|
87
|
+
value = { 'secretKeyRef' => { 'name' => value[:name], 'key' => value[:key].to_s } } if external
|
100
88
|
{
|
101
89
|
'name' => key.to_s,
|
102
|
-
'value' => value
|
90
|
+
(external ? 'valueFrom' : 'value') => value
|
103
91
|
}
|
104
92
|
end
|
105
93
|
end
|
data/lib/templates/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Kubernetes app configuration
|
2
2
|
|
3
|
-
##
|
3
|
+
## Cluster connection
|
4
4
|
- Create the project on Gcloud
|
5
5
|
- Set the project where to work on
|
6
6
|
`gcloud config set project my-project`
|
@@ -13,13 +13,14 @@
|
|
13
13
|
- Use the cluster/project as default
|
14
14
|
`gcloud container clusters get-credentials my-cluster --zone europe-west4-a`
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
## App setup
|
17
|
+
- Install kubernetes-helper (if not installed yet)
|
18
|
+
`gem install kubernetes_helper`
|
18
19
|
|
19
|
-
- Verify or update k8s settings in
|
20
|
-
Note: Please do not include sensitive values in this file,
|
20
|
+
- Verify or update k8s settings in `.kubernetes/settings.rb`
|
21
|
+
Note: Please do not include sensitive values in this file, secrets are recommended instead.
|
21
22
|
|
22
|
-
- Register shared cloudsql proxy configuration (only if not
|
23
|
+
- Register shared cloudsql proxy configuration to connect application to the Database/Pubsub (only if it does not exist yet)
|
23
24
|
```bash
|
24
25
|
DEPLOY_ENV=beta kubernetes_helper run_command "kubectl create secret generic <%=deployment.cloud_secret_name%> --from-file=credentials.json=<path-to-downloaded/credentials.json>"
|
25
26
|
```
|
@@ -60,7 +61,67 @@
|
|
60
61
|
# You can start accessing to the app using the generated ip address
|
61
62
|
# `kubectl get ManagedCertificate` # to see the status of ssl provisionning
|
62
63
|
```
|
64
|
+
- Update your domain to point to the new generated ip address (if required) and visit the domain or the ip address to see your application.
|
65
|
+
Note: The domain name propagation can take some time before pointing to the new ip address.
|
66
|
+
Note2: If the application shows "404 not found", check the deployment/pods status by: `kubectl get pods`
|
67
|
+
Note3: If the pod error is `ImagePullBackOff`, it is because the application docker image is missing.
|
68
|
+
You can deploy your application via github actions or similar (see #Configure-continuous-deployment-for-github-actions) or do it manually (see #Deploy-application-manually)
|
63
69
|
|
70
|
+
## Deploy application manually
|
71
|
+
Run the deployment manually with:
|
72
|
+
```bash
|
73
|
+
DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
|
74
|
+
```
|
75
|
+
The application image will be create and uploaded to the configured container registry (application pods should be restarted with the new docker image).
|
76
|
+
Visit the application url to see changes.
|
77
|
+
|
78
|
+
## Configure continuous deployment for github actions
|
79
|
+
This gem comes with continuous deployment script out of the box which can be executed with a single line of code.
|
80
|
+
* Go to github repository settings
|
81
|
+
* Register a new secret variable with content downloaded from (for google cloud) https://console.cloud.google.com/iam-admin/serviceaccounts
|
82
|
+
(Make sure to attach a "Editor", "Storage Admin" and "Kubernetes engine cluster admin" role to the service account)
|
83
|
+
```bash
|
84
|
+
BETA_CLOUD_TOKEN=<secret content here>
|
85
|
+
PROD_CLOUD_TOKEN=<secret content here>
|
86
|
+
```
|
87
|
+
|
88
|
+
* Add github workflow to automatically run deployment when merged into master or staging, something like:
|
89
|
+
```yml
|
90
|
+
name: "Continuous Deployment"
|
91
|
+
on:
|
92
|
+
push:
|
93
|
+
branches:
|
94
|
+
- master
|
95
|
+
- staging
|
96
|
+
|
97
|
+
deployment:
|
98
|
+
runs-on: ubuntu-latest
|
99
|
+
jobs:
|
100
|
+
steps:
|
101
|
+
- uses: actions/checkout@v2
|
102
|
+
with:
|
103
|
+
ref: ${{ env.DEPLOY_BRANCH }}
|
104
|
+
- name: Cancel previous Workflow Actions
|
105
|
+
uses: styfle/cancel-workflow-action@0.6.0
|
106
|
+
with:
|
107
|
+
access_token: ${{ github.token }}
|
108
|
+
|
109
|
+
- name: Set up Cloud SDK
|
110
|
+
uses: google-github-actions/setup-gcloud@master
|
111
|
+
- uses: satackey/action-docker-layer-caching@v0.0.11
|
112
|
+
continue-on-error: true
|
113
|
+
with:
|
114
|
+
key: CD-docker-cache-${{ hashFiles('Dockerfile', 'Gemfile.lock') }}
|
115
|
+
|
116
|
+
#### App deployment
|
117
|
+
- run: sudo gem install kubernetes_helper
|
118
|
+
- name: App deployment
|
119
|
+
env:
|
120
|
+
KB_AUTH_TOKEN: ${{ github.ref_name == 'master' && secrets.PROD_CLOUD_TOKEN || secrets.BETA_CLOUD_TOKEN }}
|
121
|
+
DEPLOY_ENV: ${{ github.ref_name == 'master' && 'production' || 'beta' }}
|
122
|
+
run: kubernetes_helper run_deployment 'cd.sh'
|
123
|
+
```
|
124
|
+
|
64
125
|
## Apply any k8s setting changes
|
65
126
|
- Secrets
|
66
127
|
Open kubernetes secrets and add/edit/remove values and then save it
|
@@ -70,30 +131,4 @@
|
|
70
131
|
- Other settings
|
71
132
|
```bash
|
72
133
|
DEPLOY_ENV=beta kubernetes_helper run_yml 'deployment.yml' 'kubectl apply'
|
73
|
-
```
|
74
|
-
|
75
|
-
## Configure continuous deployment for github actions
|
76
|
-
* Go to github repository settings
|
77
|
-
* Register a new secret variable with content downloaded from https://console.cloud.google.com/iam-admin/serviceaccounts
|
78
|
-
(Make sure to attach a "Editor", "Storage Admin" and "Kubernetes engine cluster admin" role to the service account)
|
79
|
-
```bash
|
80
|
-
beta: BETA_CLOUD_TOKEN=<secret content here>
|
81
|
-
production: PROD_CLOUD_TOKEN=<secret content here>
|
82
|
-
```
|
83
|
-
|
84
|
-
* Add action to run deployment:
|
85
|
-
```bash
|
86
|
-
env:
|
87
|
-
KB_AUTH_TOKEN: secrets.BETA_CLOUD_TOKEN
|
88
|
-
run: DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
|
89
|
-
```
|
90
|
-
|
91
|
-
* Sample:
|
92
|
-
```yml
|
93
|
-
- run: sudo gem install kubernetes_helper
|
94
|
-
- name: Staging deployment
|
95
|
-
env: # Env variable saved in github that contains gcloud credential (json format)
|
96
|
-
KB_AUTH_TOKEN: ${{ secrets.BETA_GOOGLE_AUTH }}
|
97
|
-
run: DEPLOY_ENV=beta kubernetes_helper run_deployment 'cd.sh'
|
98
|
-
if: ${{ !contains(fromJson('["main", "master"]'), env.DEPLOY_BRANCH) }}
|
99
|
-
```
|
134
|
+
```
|
data/lib/templates/cd.sh
CHANGED
@@ -10,7 +10,6 @@ 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
|
-
DOCKER_BUILD_CMD="<%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%>"
|
14
13
|
|
15
14
|
CI_COMMIT_SHA=$(git rev-parse --verify HEAD || :)
|
16
15
|
CI_COMMIT_SHA=${CI_COMMIT_SHA:-$(date +%s) }
|
@@ -42,9 +41,11 @@ if [ -z $ALREADY_DEPLOYED ]
|
|
42
41
|
then
|
43
42
|
## Build and push containers
|
44
43
|
echo "****** image not created yet, building image..."
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
<% if continuous_deployment.docker_cmd %>
|
45
|
+
<%= continuous_deployment.docker_cmd %>
|
46
|
+
<% else %>
|
47
|
+
docker <%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%> -t $DEPLOY_NAME .
|
48
|
+
<% end %>
|
48
49
|
docker push $DEPLOY_NAME
|
49
50
|
else
|
50
51
|
echo "****** image was already created: $ALREADY_DEPLOYED"
|
@@ -1,7 +1,12 @@
|
|
1
|
+
# locals: { domains: Hash(domain => cert_name) }
|
2
|
+
<% locals[:domains].each do |domain, cert_name| %>
|
1
3
|
apiVersion: networking.gke.io/v1beta1
|
2
4
|
kind: ManagedCertificate
|
3
5
|
metadata:
|
4
|
-
name: '<%=
|
6
|
+
name: '<%= cert_name %>'
|
5
7
|
spec:
|
6
|
-
domains: # does not support for willcard domains
|
7
|
-
- '<%=
|
8
|
+
domains: # does not support for willcard domains nor multiple domains
|
9
|
+
- '<%= domain %>'
|
10
|
+
|
11
|
+
---
|
12
|
+
<% end %>
|
data/lib/templates/ingress.yml
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
<%
|
2
|
-
|
1
|
+
<%
|
2
|
+
domains = Array(ingress.domain_name || ingress.certificate_domain)
|
3
|
+
domains = domains.map { |domain| cert_name = "#{domain.downcase.gsub(/[^0-9A-Za-z]/, '-')}-lets-encrypt"; domains.count > 1 ? [domain, cert_name] : [domain, ingress.certificate_name || cert_name] }.to_h
|
4
|
+
%>
|
5
|
+
<% if ingress.certificate_domain %>
|
6
|
+
<%= include_template "certificate.yml", { domains: domains } %>
|
3
7
|
<% end %>
|
4
8
|
|
5
9
|
---
|
@@ -17,8 +21,8 @@ metadata:
|
|
17
21
|
kubernetes.io/ingress.global-static-ip-name: "<%=ingress.ip_name%>"
|
18
22
|
<% end %>
|
19
23
|
|
20
|
-
<% if ingress.
|
21
|
-
networking.gke.io/managed-certificates: '<%=
|
24
|
+
<% if ingress.certificate_domain %>
|
25
|
+
networking.gke.io/managed-certificates: '<%= domains.values.join(", ") %>'
|
22
26
|
<% end %>
|
23
27
|
|
24
28
|
spec:
|
data/lib/templates/settings.rb
CHANGED
@@ -26,7 +26,6 @@ settings = {
|
|
26
26
|
ingress: {
|
27
27
|
name: "#{app_name}-ingress",
|
28
28
|
ip_name: "#{app_name}-static-ip", # nil if static ip is not necessary
|
29
|
-
certificate_name: "#{app_name}-lets-encrypt", # nil if ssl is not required
|
30
29
|
certificate_domain: is_production ? 'myapp.com' : 'beta.myapp.com' # nil if domain is not required
|
31
30
|
},
|
32
31
|
continuous_deployment: {
|
@@ -35,7 +34,7 @@ settings = {
|
|
35
34
|
project_name: 'my-project-name',
|
36
35
|
cluster_name: 'my-cluster-name',
|
37
36
|
cluster_region: 'europe-west4-a',
|
38
|
-
|
37
|
+
docker_cmd: 'docker build -f Dockerfile -t $DEPLOY_NAME .', # using target: 'docker build --target production -f Dockerfile -t $DEPLOY_NAME .'
|
39
38
|
update_deployment: false # permits to reload secrets and re-generate/update deployment yaml
|
40
39
|
},
|
41
40
|
}
|
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.
|
4
|
+
version: 1.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- owen2345
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erb
|