kubernetes_helper 1.13.2 → 1.15.2

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: 9257b84ef107ed3b39ce537325de4357b348023becbf0f14fcf761bdf652d3f0
4
- data.tar.gz: 77abef0bae873a41b1ed2fce90d49c55f07b43acbef6b578ae9b0e7f45716893
3
+ metadata.gz: 6b09fe0251ca84cfc1251acbf3f4079d02b5830abe77d3dc34c49b71ae2813ef
4
+ data.tar.gz: fff66310d7051331a334eb1e3a250c92413348f17818dda5426fcaca0581a3b1
5
5
  SHA512:
6
- metadata.gz: f2798fb189f4e2ab77507dd8095d0f2741ef70d163a03d98f734f6d4772395b7521c77aed051a97a9d5241fca3ef0d8cdc0d19c4ea430ea81ed0af7e4d9f89ca
7
- data.tar.gz: 4446e493e3cb37ec030d99e1ede09738bce1e8cd17f16f35c95ded98c1a18213cb7997d03d7b3e6c6c59d273fcf5468a09cd709aaaca6f87859bfa5f44d419f5
6
+ metadata.gz: ae4c26493643bc9fd8897ef362a476457e801868d8c3b12202889a1e7276fc8148b6bc247caec5a891985a81855296d142253b46517c7edd0f0e823fc4b2f77e
7
+ data.tar.gz: ee94f2b9c13ecae82253900502e3c00c20f0cc72fbf2e9046a31bbc2a56391f4098819b7725f404f220a8a38898398658f0dfc1d2524093350dd47c02ea81435
data/README.md CHANGED
@@ -3,69 +3,30 @@
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
- ## Installation
7
- ```bash
8
- cd my_app/
9
- gem install kubernetes_helper -v '~> 1.0'
10
- kubernetes_helper generate_templates
11
- ```
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.
21
-
22
- ## Deployment
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.
24
-
25
- ## API
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
- 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` (deprecated): Docker command to build the corresponding image. Sample: `build --target production -f Dockerfile `
63
- - `continuous_deployment.docker_cmd` (String): Docker command to build the corresponding image.
64
- Simple docker image: `docker build -f Dockerfile -t $DEPLOY_NAME .`
65
- Docker image with target: `docker build --target production -f Dockerfile -t $DEPLOY_NAME .`
66
- - `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)
67
-
68
- Below settings are used when configuring the application in the k8s environment
29
+ ### Application deployment.yml
69
30
  - `deployment.name` (String): Web deployment name (Note: Underscores are not accepted). Sample: `my-app`
70
31
  - `deployment.replicas` (Integer): Quantity of replicas. Sample: `1`
71
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]`
@@ -74,7 +35,6 @@ Below settings are used when configuring the application in the k8s environment
74
35
  - `deployment.env_vars` (Hash, optional): List of static or external env variables (Note: Not recommended for sensitive values).
75
36
  Sample: `{ 'RAILS_ENV' => 'production' }`
76
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`
77
-
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.
@@ -83,27 +43,49 @@ Below settings are used when configuring the application in the k8s environment
83
43
  - `deployment.log_container` (Boolean, default true): Permits to auto include logs container to print all logs from logs/*.log to stdout (required for papertrail using fluentd)
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
46
+ - `deployment.resources` (Hash, optional): Configure depending on the web app requirements. Sample: `{ cpu: { max: '1', min: '500m' }, mem: { max: '1Gi', min: '500Mi' } }`
86
47
 
48
+ - `deployment.cloudsql_resources` (Hash, optional): Configure depending on the app requirements. Default: `{ cpu: { max: '300m', min: '100m' }, mem: { max: '500Mi', min: '200Mi' } }`
49
+ - `deployment.logs_resources` (Hash, optional): Configure depending on the app requirements. Default: `{ cpu: { max: '200m', min: '50m' }, mem: { max: '200Mi', min: '50Mi' } }`
50
+
51
+ ### Application deployment.yml for jobs or services without internet interaction (Optional)
87
52
  - `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
53
  - `deployment.job_command` (String, optional): Bash command to be used for job container. Sample: `bundle exec sidekiq`
89
54
  - `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
55
  - `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']`
56
+ - `deployment.job_resources` (Hash, optional): Configure depending on the job app requirements. Sample: `{ cpu: { max: '1', min: '500m' }, mem: { max: '1Gi', min: '500Mi' } }`
91
57
 
92
-
58
+ ### Applications secrets.yml (Optional)
93
59
  - `secrets.name` (String): K8s secrets name where env vars will be saved and fetched from. Sample: `my-app-secrets`
94
60
 
61
+ ### Application service.yml (Optional)
95
62
  - `service.name`: K8s service name. Sample: `my-app-service`
96
63
  - `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
64
  - `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
65
+ - `service.type`: K8s service type. By default `NodePort`
98
66
 
67
+ ### Application ingress.yml (Optional)
99
68
  - `ingress.name`: Name of k8s ingress for the app: Sample: `my-app-ingress`
100
69
  - `ingress.ip_name` (Optional): Static ip address is not created nor assigned if empty value. Sample: `my-app-static-ip`
101
- - `ingress.certificate_name` (Optional): Ssl certificate is not created nor assigned if empty value. Sample: `my-app-lets-encrypt`. Note: requires `certificate_domain`
102
- - `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
70
+ - `ingress.certificate_name` (Deprecated): Ssl certificate is not created nor assigned if empty value. Sample: `my-app-lets-encrypt`. Note: requires `certificate_domain`
71
+ - `ingress.certificate_domain` (Optional): Domain name for the certificate. Sample: `myapp.com`. Note: does not support for willcard domains
72
+ To register multiple domains (Certificate names will be auto-generated like `mysite-com-lets-encrypt`): `certificate_domain: ['mysite.com', 'mysite.de', 'mysite.uk']`
103
73
 
104
74
  - `cloud.name` (String, optional): Cloud service name. Default `gcloud`.
105
75
 
106
- ### Partials
76
+ ### Application CD (continuous deployment)
77
+ - `continuous_deployment.image_name` (String): Partial docker image url. Sample: `gcr.io/my-account/my_app_name`
78
+ - `continuous_deployment.image_tag` (String, default 'latest'): Image tag to be used for this application
79
+ - `continuous_deployment.project_name`: Cloud project name. Sample: `my-project-name`
80
+ - `continuous_deployment.cluster_name`: Cluster cluster name. Sample: `my-cluster-name`
81
+ - `continuous_deployment.cluster_region`: Cluster region name. Sample: `europe-west4-a`
82
+ - `continuous_deployment.docker_build_cmd` (deprecated): Docker command to build the corresponding image. Sample: `build --target production -f Dockerfile `
83
+ - `continuous_deployment.docker_cmd` (String): Docker command to build the corresponding image.
84
+ Simple docker image: `docker build -f Dockerfile -t $DEPLOY_NAME .`
85
+ Docker image with target: `docker build --target production -f Dockerfile -t $DEPLOY_NAME .`
86
+ - `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)
87
+
88
+ ### Gem templating partials
107
89
  - `_container_extra_settings.yml` Partial template to add custom container settings. Receives `pod` as local variable (`web` | `job` | `cloudsql` | `logs`). Sample:
108
90
  ```yaml
109
91
  <% if locals[:pod] == 'job' %>
@@ -126,18 +108,50 @@ Below settings are used when configuring the application in the k8s environment
126
108
  ```
127
109
  - `_cd_apply_images.sh` Partial template to customize the process to apply the new version (new docker image)
128
110
 
129
- ## Templating
111
+ ### Gem templating
130
112
  When performing a command or script, the setting variables are replaced based on `DEPLOY_ENV`.
131
113
  All these setting variable values are configured in `.kubernetes/settings.rb` which defines the values based on `DEPLOY_ENV`.
132
114
  These setting variables use [erb](https://github.com/ruby/erb) template gem to define variable replacement and conditional blocks, and so on.
133
115
  Note: Setting variable values are referenced as an object format instead of a hash format for simplicity.
134
116
 
135
117
 
136
- ## Sample
118
+ ### Sample
137
119
  https://owen2345.github.io/kubernetes_helper/
138
120
 
121
+
122
+ ## API
123
+ - Run any kubernetes document
124
+ `DEPLOY_ENV=<env name> kubernetes_helper run_deployment "<document name>" "<bash command>"`
125
+ Evaluates the kubernetes document with the following details:
126
+ - Supports for `- documents` to include multiple documents in a file and share yml variables between them (Sample: `lib/templates/deployment.yml#1`)
127
+ - Replaces all setting values based on `DEPLOY_ENV`
128
+ - Supports for secrets auto importer using `import_secrets: ['secrets.yml', '<%=secrets.name%>']` (Sample: `lib/templates/deployment.yml#29`)
129
+ - Supports for sub templates by `include_template 'template_name.yml.erb', { my_local_var: 10 }`
130
+ Sample: `DEPLOY_ENV=beta kubernetes_helper run_deployment "deployment.yml" "kubectl create"`
131
+
132
+ - Run kubernetes commands
133
+ `DEPLOY_ENV=<env name> rake kubernetes_helper:run_command "<bash or k8s commands>"`
134
+ Replaces all setting variables inside command based on `DEPLOY_ENV` and performs it as a normal bash command.
135
+ Sample: `DEPLOY_ENV=beta rake kubernetes_helper:run_command "gcloud compute addresses create \#{ingress.ip_name} --global"'`
136
+
137
+ - Run kubernetes bash scripts
138
+ `DEPLOY_ENV=<env name> kubernetes_helper run_script "<script name>"`
139
+ Performs the script name located inside `.kubernetes` folder or kubernetes_helper template as the second option.
140
+ All setting variables inside the script will be replaced based on `DEPLOY_ENV`.
141
+ Sample: `DEPLOY_ENV=beta kubernetes_helper run_script "cd.sh"`
142
+
143
+ - Generate templates
144
+ `DEPLOY_ENV=<env name> kubernetes_helper generate_templates "<mode_or_template_name>"`
145
+ Copy files based on mode (`basic|advanced`) or a specific file from templates.
146
+ Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "basic"`
147
+ Sample: `DEPLOY_ENV=beta kubernetes_helper generate_templates "ingress.yml"`
148
+
149
+ When performing a script it looks first for file inside .kubernetes folder, if not exist,
150
+ it looks for the file inside kubernetes_helper template folder.
151
+
139
152
  ## TODO
140
153
  - Add one_step_configuration.sh
154
+ - Change `include_template` into `ERB render partial`
141
155
 
142
156
  ## Contributing
143
157
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KubernetesHelper
4
- VERSION = '1.13.2'
4
+ VERSION = '1.15.2'
5
5
  end
@@ -1,6 +1,6 @@
1
1
  # Kubernetes app configuration
2
2
 
3
- ## Configure a new application environment
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
- - Install helper for the next commands
17
- `gem install kubernetes_helper`
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 .kubernetes/settings.rb
20
- Note: Please do not include sensitive values in this file, ENV values are recommended instead.
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 exists)
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
+ ```
@@ -0,0 +1,24 @@
1
+ # Download and install doctl
2
+ if [ -z "$(which doctl)" ]; then
3
+ wget https://github.com/digitalocean/doctl/releases/download/v1.72.0/doctl-1.72.0-linux-amd64.tar.gz
4
+ tar xf ~/doctl-1.72.0-linux-amd64.tar.gz
5
+ sudo mv ~/doctl /usr/local/bin
6
+ fi
7
+
8
+ if [ ! -z "$KB_AUTH_TOKEN" ]
9
+ then
10
+ doctl auth init --access-token $KB_AUTH_TOKEN
11
+ fi
12
+
13
+ ## Build and push containers
14
+ echo "****** building image..."
15
+ <% if continuous_deployment.docker_cmd %>
16
+ <%= continuous_deployment.docker_cmd %>
17
+ <% else %>
18
+ docker <%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%> -t $DEPLOY_NAME .
19
+ <% end %>
20
+ docker push $DEPLOY_NAME
21
+
22
+ echo "****** tagging image $DEPLOY_NAME as $LATEST_NAME"
23
+ docker tag $DEPLOY_NAME $LATEST_NAME
24
+ docker push $LATEST_NAME
@@ -0,0 +1,37 @@
1
+ if [ ! -z "$KB_AUTH_TOKEN" ]
2
+ then
3
+ AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
4
+ rm -f -- $AUTH_PATH
5
+ echo $KB_AUTH_TOKEN >> $AUTH_PATH
6
+
7
+ ## ***** GOOGLE CONNECTOR
8
+ # Download and install Google Cloud SDK
9
+ if [ -z "$(which gcloud)" ]; then
10
+ 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
11
+ fi
12
+
13
+ # Connect to cluster
14
+ gcloud auth activate-service-account --key-file $AUTH_PATH --project $PROJECT_NAME
15
+ gcloud docker --authorize-only --project $PROJECT_NAME
16
+ gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION
17
+ ## ***** END GOOGLE CONNECTOR
18
+ fi
19
+
20
+
21
+ ALREADY_DEPLOYED="$(gcloud container images list-tags --format='get(tags)' $IMAGE_NAME | grep $CI_COMMIT_SHA || :;)"
22
+ if [ -z $ALREADY_DEPLOYED ]
23
+ then
24
+ ## Build and push containers
25
+ echo "****** image not created yet, building image..."
26
+ <% if continuous_deployment.docker_cmd %>
27
+ <%= continuous_deployment.docker_cmd %>
28
+ <% else %>
29
+ docker <%=continuous_deployment.docker_build_cmd || 'build -f Dockerfile'%> -t $DEPLOY_NAME .
30
+ <% end %>
31
+ docker push $DEPLOY_NAME
32
+ else
33
+ echo "****** image was already created: $ALREADY_DEPLOYED"
34
+ fi
35
+
36
+ echo "****** tagging image $DEPLOY_NAME as $LATEST_NAME"
37
+ gcloud container images add-tag --quiet $DEPLOY_NAME $LATEST_NAME
@@ -0,0 +1,15 @@
1
+ resources:
2
+ limits:
3
+ <% if locals[:cpu] && locals[:cpu][:max] %>
4
+ cpu: <%= locals[:cpu][:max] %>
5
+ <% end %>
6
+ <% if locals[:mem] && locals[:mem][:max] %>
7
+ memory: <%= locals[:mem][:max] %>
8
+ <% end %>
9
+ requests:
10
+ <% if locals[:cpu] && locals[:cpu][:min] %>
11
+ cpu: <%= locals[:cpu][:min] %>
12
+ <% end %>
13
+ <% if locals[:mem] && locals[:mem][:min] %>
14
+ memory: <%= locals[:mem][:min] %>
15
+ <% end %>
data/lib/templates/cd.sh CHANGED
@@ -16,43 +16,8 @@ CI_COMMIT_SHA=${CI_COMMIT_SHA:-$(date +%s) }
16
16
  DEPLOY_NAME="${IMAGE_NAME}:${CI_COMMIT_SHA}"
17
17
  LATEST_NAME="${IMAGE_NAME}:<%= continuous_deployment.image_tag || 'latest' %>"
18
18
 
19
- if [ ! -z "$KB_AUTH_TOKEN" ]
20
- then
21
- AUTH_PATH="$SCRIPT_DIR/k8s-auth-token.json"
22
- rm -f -- $AUTH_PATH
23
- echo $KB_AUTH_TOKEN >> $AUTH_PATH
24
-
25
- ## ***** GOOGLE CONNECTOR
26
- # Download and install Google Cloud SDK
27
- if [ -z "$(which gcloud)" ]; then
28
- 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
29
- fi
30
-
31
- # Connect to cluster
32
- gcloud auth activate-service-account --key-file $AUTH_PATH --project $PROJECT_NAME
33
- gcloud docker --authorize-only --project $PROJECT_NAME
34
- gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION
35
- ## ***** END GOOGLE CONNECTOR
36
- fi
37
-
38
-
39
- ALREADY_DEPLOYED="$(gcloud container images list-tags --format='get(tags)' $IMAGE_NAME | grep $CI_COMMIT_SHA || :;)"
40
- if [ -z $ALREADY_DEPLOYED ]
41
- then
42
- ## Build and push containers
43
- echo "****** image not created yet, building image..."
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 %>
49
- docker push $DEPLOY_NAME
50
- else
51
- echo "****** image was already created: $ALREADY_DEPLOYED"
52
- fi
53
-
54
- echo "****** tagging image $DEPLOY_NAME as $LATEST_NAME"
55
- gcloud container images add-tag --quiet $DEPLOY_NAME $LATEST_NAME
19
+ <%= include_template "_cd_google.sh" if continuous_deployment.image_name.include?('gcr.io/') %>
20
+ <%= include_template "_cd_digital.sh" if continuous_deployment.image_name.include?('digitalocean.com/') %>
56
21
 
57
22
  ## Update new secrets defined in secrets.yml as ENV vars for deployments
58
23
  <% if continuous_deployment.update_deployment %>
@@ -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: '<%=ingress.certificate_name%>'
6
+ name: '<%= cert_name %>'
5
7
  spec:
6
- domains: # does not support for willcard domains
7
- - '<%= ingress.domain_name || ingress.certificate_domain %>'
8
+ domains: # does not support for willcard domains nor multiple domains
9
+ - '<%= domain %>'
10
+
11
+ ---
12
+ <% end %>
@@ -42,6 +42,9 @@ documents:
42
42
  periodSeconds: 15
43
43
  readinessProbe: *liveness_probe
44
44
  <% end %>
45
+ <% if deployment.resources %>
46
+ <%= include_template "_resources.yml", deployment.resources %>
47
+ <% end %>
45
48
  <%= include_template "_container_extra_settings.yml", { pod: 'web' } %>
46
49
 
47
50
  volumeMounts:
@@ -73,6 +76,7 @@ documents:
73
76
  env: []
74
77
  volumeMounts:
75
78
  - *cloud_credentials_volume
79
+ <%= include_template "_resources.yml", deployment.cloudsql_resources || { cpu: { min: '1' }, mem: { min: '1Gi' } } %>
76
80
  <%= include_template "_container_extra_settings.yml", { pod: 'cloudsql' } %>
77
81
  <% end %>
78
82
 
@@ -80,10 +84,11 @@ documents:
80
84
  - &logs_container # print to stdout all log files
81
85
  name: print-logs
82
86
  image: busybox
83
- command: [/bin/sh, -c, 'until find <%= deployment.log_folder %>/*.log; do sleep 1; done; tail -n+1 -f <%= deployment.log_folder %>/*.log']
87
+ command: [/bin/sh, -c, 'until find <%= deployment.log_folder %>/*.log; do sleep 5; done; tail -n+1 -f <%= deployment.log_folder %>/*.log']
84
88
  env: []
85
89
  volumeMounts:
86
90
  - *log_volume
91
+ <%= include_template "_resources.yml", deployment.logs_resources || { cpu: { max: '300m', min: '100m' }, mem: { max: '500Mi', min: '200Mi' } } %>
87
92
  <%= include_template "_container_extra_settings.yml", { pod: 'logs' } %>
88
93
  <% end %>
89
94
 
@@ -117,6 +122,9 @@ documents:
117
122
  <% if deployment.job_command %>
118
123
  command: [ "/bin/bash", "-c", "<%= deployment.job_command %>" ]
119
124
  <% end %>
125
+ <% if deployment.job_resources %>
126
+ <%= include_template "_resources.yml", deployment.job_resources %>
127
+ <% end %>
120
128
  <%= include_template "_container_extra_settings.yml", { pod: 'job' } %>
121
129
 
122
130
  <% if deployment.job_sidekiq_alive_gem %>
@@ -1,5 +1,9 @@
1
- <% if ingress.certificate_name %>
2
- <%= include_template "certificate.yml" %>
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.certificate_name %>
21
- networking.gke.io/managed-certificates: '<%=ingress.certificate_name%>'
24
+ <% if ingress.certificate_domain %>
25
+ networking.gke.io/managed-certificates: '<%= domains.values.join(", ") %>'
22
26
  <% end %>
23
27
 
24
28
  spec:
@@ -21,7 +21,7 @@ metadata:
21
21
  spec:
22
22
  selector:
23
23
  name: '<%=deployment.name%>'
24
- type: NodePort
24
+ type: '<%=service.type || 'NodePort' %>'
25
25
  ports:
26
26
  - port: 80
27
27
  protocol: TCP
@@ -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: {
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.13.2
4
+ version: 1.15.2
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-18 00:00:00.000000000 Z
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erb
@@ -41,11 +41,14 @@ files:
41
41
  - lib/kubernetes_helper/version.rb
42
42
  - lib/templates/README.md
43
43
  - lib/templates/_cd_apply_images.sh
44
+ - lib/templates/_cd_digital.sh
45
+ - lib/templates/_cd_google.sh
44
46
  - lib/templates/_container_extra_settings.yml
45
47
  - lib/templates/_custom_containers.yml
46
48
  - lib/templates/_custom_volumes.yml
47
49
  - lib/templates/_job_liveness.yml
48
50
  - lib/templates/_replicas.yml
51
+ - lib/templates/_resources.yml
49
52
  - lib/templates/_sidekiq_alive_gem.yml
50
53
  - lib/templates/_volumes.yml
51
54
  - lib/templates/cd.sh