kubes 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/docs/_docs/dsl/multiple-resources.md +7 -6
  4. data/docs/_docs/helpers/aws/secrets.md +1 -1
  5. data/docs/_docs/helpers/google.md +20 -2
  6. data/docs/_docs/helpers/google/gke.md +10 -6
  7. data/docs/_docs/helpers/google/secrets.md +17 -1
  8. data/docs/_docs/plugins/aws.md +6 -2
  9. data/docs/_docs/plugins/google.md +6 -2
  10. data/docs/_docs/vs/custom.md +3 -3
  11. data/docs/_docs/yaml/multiple-files.md +23 -0
  12. data/docs/_docs/yaml/multiple-resources.md +90 -0
  13. data/docs/_includes/config/hooks/options.md +1 -0
  14. data/docs/_includes/layering/layers.md +7 -4
  15. data/docs/_includes/sidebar.html +6 -1
  16. data/docs/_includes/variables/generator.md +12 -1
  17. data/kubes.gemspec +2 -2
  18. data/lib/kubes/command.rb +1 -1
  19. data/lib/kubes/compiler/dsl/core/base.rb +1 -1
  20. data/lib/kubes/compiler/dsl/core/blocks.rb +5 -1
  21. data/lib/kubes/compiler/dsl/syntax/endpoint.rb +34 -0
  22. data/lib/kubes/compiler/layering.rb +3 -7
  23. data/lib/kubes/compiler/strategy.rb +1 -15
  24. data/lib/kubes/compiler/strategy/base.rb +0 -56
  25. data/lib/kubes/compiler/strategy/dispatcher.rb +59 -0
  26. data/lib/kubes/compiler/strategy/erb.rb +6 -7
  27. data/lib/kubes/compiler/strategy/result.rb +6 -2
  28. data/lib/kubes/compiler/util/normalize.rb +2 -2
  29. data/lib/kubes/compiler/util/yaml_dump.rb +7 -4
  30. data/lib/kubes/version.rb +1 -1
  31. data/lib/templates/new/resource/dsl/endpoint.rb +3 -0
  32. data/lib/templates/new/resource/yaml/endpoint.yaml +9 -0
  33. data/spec/kubes/compiler/strategy/{dsl_spec.rb → dispatcher_spec.rb} +10 -9
  34. metadata +14 -9
  35. data/lib/kubes/compiler/strategy/dsl.rb +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce0d27f928a17e1013e97242990213cd4b375aee110dc14d3effc9aa8df2d490
4
- data.tar.gz: 983312b9f64da2641ec8286dc76b9e7582dac9a575f77222ab4e9f9a145d26dd
3
+ metadata.gz: c708370f432e482b105a766bd174dd52f89c790256ea91063a8045af1d970f39
4
+ data.tar.gz: 3b4aef7f7450776cf35b9db54ff7d326a6444e948ab7cf81fac1a7659fca9649
5
5
  SHA512:
6
- metadata.gz: 36dc2af17c87d9c58eca05f8f63848f57d95cccdfcd51894ddb467c7424c7eefade4aed9133c871f400f05997143d1f537aeb38c1d8288815ab05558f25ae31c
7
- data.tar.gz: 3ed8687d0f1af629a7645a002536e7a187057ea2da504e52fb3227da933b4a0c98db8ad0f1170a2a720248cbe517f7206ad8941299467695772d5fca289c1160
6
+ metadata.gz: f157bfaa51b24e2f23327ab02075abdeb331564c28cdcbe0710eeefb8ba2cd7910ea2719e28e1cdedb3b5b2873be902734c27254089ba5e1bc4083e13d33a10f
7
+ data.tar.gz: 48594a55badaba62e2ef7cfe436bc32640a5723221278f7d42c307144a00c300ca9a9c07d1501bc9a10204ff629929272171d8a4fc1cfe878b91bc951f46ba96
@@ -3,6 +3,21 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.7.0] - 2020-11-16
7
+ - [#41](https://github.com/boltops-tools/kubes/pull/41) multiple resources yaml support
8
+
9
+ ## [0.6.8] - 2020-11-14
10
+ - [#40](https://github.com/boltops-tools/kubes/pull/40) fix version check
11
+
12
+ ## [0.6.7] - 2020-11-12
13
+ - dependencies version bump: kubes_google
14
+
15
+ ## [0.6.6] - 2020-11-12
16
+ - dependencies version bump: kubes_aws and kubes_google
17
+
18
+ ## [0.6.5] - 2020-11-12
19
+ - [#39](https://github.com/boltops-tools/kubes/pull/39) google secrets fetcher option
20
+
6
21
  ## [0.6.4] - 2020-11-11
7
22
  - [#38](https://github.com/boltops-tools/kubes/pull/38) fix auto auth for docker login to registry, docs for secret base64, update dependencies
8
23
 
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: Multiple Resources
2
+ title: DSL Multiple Resources
3
3
  ---
4
4
 
5
5
  Kubes encourages a structure with files that matches the resource kind. Example:
@@ -35,7 +35,7 @@ deployment-2.rb | Deployment
35
35
  service-1.rb | Service
36
36
  service-2.rb | Service
37
37
 
38
- Using multiple files is the general recommended approach.
38
+ Using multiple files is the general recommended approach when using the DSL.
39
39
 
40
40
  ## Multiple Resources: Block Form
41
41
 
@@ -86,8 +86,9 @@ You can declare deployment, service, and other resource kinds multiple times.
86
86
 
87
87
  Layering works for both simple and block form. Just create a folder with the corresponding name.
88
88
 
89
- * The layering definitions with the simple form only merge with other simple form layers.
90
- * The layering definitions for block forms only merge with other block form layers.
89
+ * The layering definitions for the prelayers are in singular form.
90
+ * The layering definitions for the post layers are in a folder with plural form.
91
+ * Resources in the main "middle" layer are the only ones that are allowed multiple resource definitions.
91
92
 
92
93
  Simple form layering:
93
94
 
@@ -105,8 +106,8 @@ Block form layering:
105
106
 
106
107
  .kubes/resources/
107
108
  ├── base
108
- │ ├── alls.rb
109
- │ └── deployments.rb
109
+ │ ├── all.rb
110
+ │ └── deployment.rb
110
111
  └── web
111
112
  ├── deployments
112
113
  │ ├── dev.rb
@@ -55,7 +55,7 @@ By default, the values are automatically base64 encoded. You can change the defa
55
55
 
56
56
  ```ruby
57
57
  KubesAws.configure do |config|
58
- config.base64_secrets = false
58
+ config.secrets.base64 = false
59
59
  end
60
60
  ```
61
61
 
@@ -4,7 +4,7 @@ title: Google Helpers
4
4
 
5
5
  List of Google helpers:
6
6
 
7
- {% assign docs = site.docs | where: "categories","helpers-aws" %}
7
+ {% assign docs = site.docs | where: "categories","helpers-google" %}
8
8
  {% for doc in docs -%}
9
9
  * [{{ doc.nav_text }}]({{ doc.url }})
10
10
  {% endfor %}
@@ -14,4 +14,22 @@ List of Google helpers:
14
14
  * By default, `KubeGoogle.logger = Kubes.logger`. This means, you can set `logger.level = "debug"` in `.kubes/config.rb` to see more details.
15
15
  * The `gcloud` cli is used to create IAM roles. So `gcloud` is required.
16
16
  * Note: Would like to use the google sdk, but it wasn't obvious how to do so. PRs are welcomed.
17
- * The Google helpers are provided by the [boltops-tools/kubes_google](https://github.com/boltops-tools/kubes_google) library.
17
+ * The Google helpers are provided by the [boltops-tools/kubes_google](https://github.com/boltops-tools/kubes_google) library.
18
+
19
+ ## Authentication
20
+
21
+ Most of the Google helpers use the SDK to call the Google Cloud API. As such, it needs to be authenticated. You can do this by setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable point to the path with a service account credentials file. IE:
22
+
23
+ .bash_profile
24
+
25
+ export GOOGLE_APPLICATION_CREDENTIALS=~/.gcp/service-account.json
26
+
27
+ If you would like to use a user IAM credentials instead of a service account. You can also run use [application-default login](https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login). Example:
28
+
29
+ gcloud auth application-default login
30
+
31
+ This generates an Application Default Credentials at `.config/gcloud/application_default_credentials.json`. Note, make sure that `GOOGLE_APPLICATION_CREDENTIALS` is not set or else the `application_default_credentials.json` will not be used. Also, the google sdk prints a warning to use a service account instead. You can suppress that warning with this:
32
+
33
+ .bash_profile
34
+
35
+ export GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS=1
@@ -10,7 +10,7 @@ GKE Private Clusters whitelist and only allow authorized IPs to communicate with
10
10
 
11
11
  ## Setup
12
12
 
13
- To enable the GKE IP whitelisting feature, it's a single line:
13
+ To enable the GKE IP whitelisting feature, it's a few simple configurations:
14
14
 
15
15
  .kubes/config/env/dev.rb
16
16
 
@@ -19,14 +19,18 @@ KubesGoogle.configure do |config|
19
19
  config.gke.cluster_name = "dev-cluster"
20
20
  config.gke.google_region = ENV['GOOGLE_REGION']
21
21
  config.gke.google_project = ENV['GOOGLE_PROJECT']
22
- config.gke.enable_get_credentials = true # enable hook to call: gcloud container clusters get-credentials
22
+ config.gke.enable_hooks = KubesGoogle.cloudbuild?
23
+ config.gke.enable_get_credentials = KubesGoogle.cloudbuild? # enable hook to call: gcloud container clusters get-credentials
23
24
  end
24
25
  ```
25
26
 
26
- Note: The use of `KubesGoogle.configure` instead of `Kubes.configure` here.
27
-
28
27
  This enables `kubes apply` before and after hooks to add and remove the current machine IP.
29
28
 
29
+ Notes:
30
+
31
+ * Notice the use of `KubesGoogle.configure` instead of `Kubes.configure` here.
32
+ * The `KubesGoogle.cloudbuild?` checks if kubes is running on a CloudBuild machine. So hooks don't run locally on your machine.
33
+
30
34
  ## Options
31
35
 
32
36
  Here are the `config.gke` settings:
@@ -58,7 +62,7 @@ steps:
58
62
  - 'GOOGLE_PROJECT=$PROJECT_ID' # .kubes/config.rb: config.repo
59
63
  - 'KUBES_ENV=$_KUBES_ENV'
60
64
  - 'KUBES_EXTRA=$_KUBES_EXTRA'
61
- - 'KUBES_REPO_AUTH=0'
65
+ - 'KUBES_REPO_AUTO_AUTH=0'
62
66
 
63
67
  substitutions:
64
68
  _KUBES_ENV: dev
@@ -67,7 +71,7 @@ options:
67
71
  substitution_option: 'ALLOW_LOOSE'
68
72
  ```
69
73
 
70
- Make sure to replace the substitutions with your own values. IE: _GCP_REGION, _GKE_CLUSTER, _KUBES_ENV, etc.
74
+ Make sure to replace the substitutions with your own values. IE: _KUBES_ENV, etc.
71
75
 
72
76
  ## Google CloudBuild IAM Permissions
73
77
 
@@ -63,7 +63,7 @@ By default, the values are automatically base64 encoded. You can change the defa
63
63
 
64
64
  ```ruby
65
65
  KubesGoogle.configure do |config|
66
- config.base64_secrets = true
66
+ config.secrets.base64 = false
67
67
  end
68
68
  ```
69
69
 
@@ -77,3 +77,19 @@ google_secret("demo-#{Kubes.env}-PASS", base64: false)
77
77
  ```
78
78
 
79
79
  {% include helpers/base64.md %}
80
+
81
+ ## Fetcher Strategy
82
+
83
+ Some systems configured with a VPN seem to have issues with the Google secrets SDK. You may see an error:
84
+
85
+ Handshake failed with fatal error SSL_ERROR_SSL: error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER.
86
+
87
+ As a workaround to this error, you can use the `gcloud` instead of the default `sdk` fetcher strategy. To configure it:
88
+
89
+ .kubes/config.rb
90
+
91
+ ```ruby
92
+ KubesGoogle.configure do |config|
93
+ config.secrets.fetcher = "gcloud"
94
+ end
95
+ ```
@@ -8,10 +8,14 @@ The AWS Kubes Plugin adds support helpers like `aws_secret`. You can configure i
8
8
 
9
9
  ```ruby
10
10
  KubesAws.configure do |config|
11
- config.base64_secrets = false
11
+ config.secrets.base64 = false
12
12
  end
13
13
  ```
14
14
 
15
+ ## Options Reference Table
16
+
17
+ Here's a table with the options:
18
+
15
19
  Name | Description | Default
16
20
  ---|---|---
17
- base64_secrets | Whether or not to automatically base64 encoded values returned by the `aws_secret` helper. | true
21
+ secrets.base64 | Whether or not to automatically base64 encoded values returned by the `aws_secret` helper. | true
@@ -8,11 +8,15 @@ The Google Kubes Plugin adds support helpers like `google_secret`. You can confi
8
8
 
9
9
  ```ruby
10
10
  KubesGoogle.configure do |config|
11
- config.base64_secrets = true
11
+ config.secrets.base64 = true
12
12
  end
13
13
  ```
14
14
 
15
+ ## Options Reference Table
16
+
17
+ Here's a table with the options:
18
+
15
19
  Name | Description | Default
16
20
  ---|---|---
17
- base64_secrets | Whether or not to automatically base64 encoded values returned by the `google_secret` helper. | true
21
+ secrets.base64 | Whether or not to automatically base64 encoded values returned by the `google_secret` helper. | true
18
22
  {% include plugins/gke-config.md %}
@@ -4,7 +4,7 @@ nav_text: Custom Solutions
4
4
  categories: vs
5
5
  ---
6
6
 
7
- Kubernetes provide a great platform to run and manage Docker containers. The `kubectl` command how you usually interact with a Kubernetes cluster. It does its job well and is quite a powerful tool.
7
+ Kubernetes provides a great platform to run and manage Docker containers. The `kubectl` command is usually how you interact with a Kubernetes cluster. It does its job well and is quite a powerful tool.
8
8
 
9
9
  {% include vs/article.md %}
10
10
 
@@ -51,7 +51,7 @@ We've duplicated `service.yaml` and `deployment.yaml`, though. Instead, it'll be
51
51
 
52
52
  ## PreBuilt Docker Image
53
53
 
54
- Additionally, the Docker image is expected to be prebuilt. Because you must first build the Docker image, folks will usually write bash script that perform these additional steps and then glue things together.
54
+ Additionally, the Docker image is expected to be prebuilt. Because you must first build the Docker image, folks will usually write bash script that performs these additional steps and then glue things together.
55
55
 
56
56
  ## Kubernetes Resources Galore
57
57
 
@@ -82,7 +82,7 @@ The same code is used to create different environments. Kubes achieves this with
82
82
 
83
83
  ## Hooks
84
84
 
85
- Kubes support a variety of hooks run scripts at any part of the `kubectl` commands. This allows you customize and add app-specific logic needed. Example:
85
+ Kubes support a variety of hooks run scripts at any part of the `kubectl` commands. This allows you to customize and add app-specific logic needed. Example:
86
86
 
87
87
  .kubes/config/hooks/kubectl.rb
88
88
 
@@ -0,0 +1,23 @@
1
+ ---
2
+ title: YAML Multiple Resources with Multiple Files
3
+ ---
4
+
5
+ You can also create multiple resources of same kind by appending a dash followed by anything. Example:
6
+
7
+ .kubes
8
+ └── resources
9
+ └── web
10
+ ├── deployment-1.yaml
11
+ ├── deployment-2.yaml
12
+ ├── service-1.yaml
13
+ └── service-2.yaml
14
+
15
+ Only words before the dash are used to infer the resource kind.
16
+
17
+ Filename | Resource Kind
18
+ --- | ---
19
+ deployment-1.yaml | Deployment
20
+ deployment-2.yaml | Deployment
21
+ service-1.yaml | Service
22
+ service-2.yaml | Service
23
+
@@ -0,0 +1,90 @@
1
+ ---
2
+ title: YAML Multiple Resources
3
+ ---
4
+
5
+ Kubes encourages a structure with files that matches the resource kind. Example:
6
+
7
+ .kubes
8
+ └── resources
9
+ └── web
10
+ ├── deployment.yaml
11
+ └── service.yaml
12
+
13
+ This structure is nicely organized and covers 80% of use cases. An astute user may point out that this struture assumes one resource of each kind.
14
+
15
+ Next, we'll cover how to create multiple resource of the same kinds.
16
+
17
+ ## Multiple Resources in Same YAML
18
+
19
+ You can simply define multiple resources in th same YAML file. Conventionally, you should name the resource files with plural names. An example helps explain:
20
+
21
+ .kubes
22
+ └── resources
23
+ └── deployments.yaml
24
+
25
+ .kubes/resources/web/deployments.yaml
26
+
27
+ ```yaml
28
+ - apiVersion: apps/v1
29
+ kind: Deployment
30
+ metadata:
31
+ name: web-1
32
+ labels:
33
+ role: web
34
+ spec:
35
+ selector:
36
+ matchLabels:
37
+ role: web
38
+ template:
39
+ metadata:
40
+ labels:
41
+ role: web
42
+ spec:
43
+ containers:
44
+ - name: web
45
+ image: <%= docker_image %>
46
+ - apiVersion: apps/v1
47
+ kind: Deployment
48
+ metadata:
49
+ name: web-2
50
+ labels:
51
+ role: web
52
+ spec:
53
+ selector:
54
+ matchLabels:
55
+ role: web
56
+ template:
57
+ metadata:
58
+ labels:
59
+ role: web
60
+ spec:
61
+ containers:
62
+ - name: web
63
+ image: <%= docker_image %>
64
+ ```
65
+
66
+ Notice that the YAML contains an Array of definitions now.
67
+
68
+ ## Layering
69
+
70
+ Layering works just fine with multiple resource definitions. The layering is processed on each item of the Array.
71
+
72
+ Notes:
73
+
74
+ * The layering definitions for the pre layers must be in singular form.
75
+ * The layering definitions for the post layers must be in a folder with plural form, but define a singlar resource override.
76
+ * Resources in the main "middle" layer is the only one that allows for multiple resource definitions.
77
+
78
+ Multiple resources layering structure.
79
+
80
+ .kubes/resources/
81
+ ├── base
82
+ │ ├── all.rb
83
+ │ └── deployment.rb # SINGULAR
84
+ └── web
85
+ ├── deployments # PLURAL
86
+ │ ├── dev.rb
87
+ │ └── prod.rb
88
+ └── deployments.rb # PLURAL
89
+
90
+ The main difference is the pluralized filenames.
@@ -18,3 +18,4 @@ exit_on_fail | Whether or not to continue process if the script returns an faile
18
18
  ## Ruby Hooks
19
19
 
20
20
  Instead of using a script for the hook `execute` option, you can also use a Ruby object. This provides some more control over the current process. See: [Ruby Hooks]({% link _docs/config/hooks/ruby.md %})
21
+
@@ -21,11 +21,14 @@ Here's an example structure, so we can understand how layering works.
21
21
 
22
22
  To explain the layering, here's the general processing order that Kubes takes.
23
23
 
24
- 1. The `.kubes/resources/base` folder is treated as a base layer. It gets processed as pre-layers by Kubes.
25
- 2. Then Kubes will process your `.kubes/resources/ROLE` definitions.
26
- 3. Lastly, Kubes processes any post-layers in the `.kubes/resources/ROLE/KIND` folders.
24
+ 1. **Pre Layers**: The `.kubes/resources/base` folder is treated as a base layer. It gets processed as pre-layers by Kubes.
25
+ 2. **Main Layer**: Then Kubes will process your `.kubes/resources/ROLE` definitions.
26
+ 3. **Post Layers**: Lastly, Kubes processes any post-layers in the `.kubes/resources/ROLE/KIND` folders.
27
27
 
28
- Note, both YAML and DSL forms support layering. They can be mixed together.
28
+ Notes
29
+
30
+ * Both YAML and DSL forms support layering. They can be mixed together.
31
+ * In the Main Layer you can define single or multiple resource definitions.
29
32
 
30
33
  ## Full Layering
31
34
 
@@ -86,7 +86,12 @@
86
86
  </ul>
87
87
  </li>
88
88
  <li><a href="{% link _docs/generators.md %}">Generators</a></li>
89
- <li><a href="{% link _docs/yaml.md %}">YAML</a></li>
89
+ <li><a href="{% link _docs/yaml.md %}">YAML</a>
90
+ <ul>
91
+ <li><a href="{% link _docs/yaml/multiple-resources.md %}">Multiple Resources</a></li>
92
+ <li><a href="{% link _docs/yaml/multiple-files.md %}">Multiple Files</a></li>
93
+ </ul>
94
+ </li>
90
95
  <li><a href="{% link _docs/layering.md %}">Layering</a>
91
96
  <ul>
92
97
  <li><a href="{% link _docs/layering/yaml.md %}">YAML</a></li>
@@ -9,4 +9,15 @@ To help you get started quickly, you can generate starter variable code.
9
9
 
10
10
  ```ruby
11
11
  @example = "dev-value"
12
- ```
12
+ ```
13
+
14
+ To create the prod variables, set `KUBES_ENV=prod`.
15
+
16
+ $ KUBES_ENV=prod kubes new variable
17
+ create .kubes/variables/prod.rb
18
+
19
+ .kubes/variables/prod.rb
20
+
21
+ ```ruby
22
+ @example = "prod-value"
23
+ ```
@@ -29,8 +29,8 @@ Gem::Specification.new do |spec|
29
29
  spec.add_dependency "zeitwerk"
30
30
 
31
31
  # core helper libs
32
- spec.add_dependency "kubes_aws", "~> 0.3.0"
33
- spec.add_dependency "kubes_google", "~> 0.3.2"
32
+ spec.add_dependency "kubes_aws", "~> 0.3.1"
33
+ spec.add_dependency "kubes_google", "~> 0.3.5"
34
34
 
35
35
  spec.add_development_dependency "bundler"
36
36
  spec.add_development_dependency "byebug"
@@ -57,7 +57,7 @@ module Kubes
57
57
  end
58
58
 
59
59
  def check_project!(command_name)
60
- return if %w[-h help init new].include?(command_name)
60
+ return if %w[-h -v completion completion_script help init new version].include?(command_name)
61
61
  Kubes.check_project!
62
62
  end
63
63
 
@@ -18,7 +18,7 @@ module Kubes::Compiler::Dsl::Core
18
18
 
19
19
  def run
20
20
  evaluate_file(@path) # main resource definition
21
- result if respond_to?(:result) # block form doesnt have result
21
+ result
22
22
  end
23
23
  end
24
24
  end
@@ -5,7 +5,11 @@ module Kubes::Compiler::Dsl::Core
5
5
  @results = {} # Hash key is the name of resource, using it so we can keep a map to handle layering
6
6
  @block_form = true # pluralizes the layer names
7
7
  super # handles layering and evaluating the main DSL file
8
- self
8
+ result # Array
9
+ end
10
+
11
+ def result
12
+ @results.values # Array
9
13
  end
10
14
 
11
15
  def syntax_instance(meth, name)
@@ -0,0 +1,34 @@
1
+ module Kubes::Compiler::Dsl::Syntax
2
+ class Endpoint < Resource
3
+ fields :subsets
4
+
5
+ # kubectl explain endpoints.subsets
6
+ fields :addresses, # <[]Object>
7
+ :notReadyAddresses, # <[]Object>
8
+ :ports # <[]Object>
9
+
10
+ def default_kind
11
+ return @kind_from_block if @kind_from_block
12
+ "Endpoints" # always plural
13
+ end
14
+
15
+ def default_apiVersion
16
+ "v1"
17
+ end
18
+
19
+ def default_top
20
+ top = super
21
+ top.merge(
22
+ subsets: subsets
23
+ )
24
+ end
25
+
26
+ def default_subsets
27
+ [{
28
+ addresses: addresses,
29
+ notReadyAddresses: notReadyAddresses,
30
+ ports: ports,
31
+ }]
32
+ end
33
+ end
34
+ end
@@ -5,14 +5,10 @@ class Kubes::Compiler
5
5
 
6
6
  ext = File.extname(@path)
7
7
  kind = File.basename(@path).sub(ext,'') # IE: deployment
8
- all = "all"
9
- if @block_form
10
- kind = kind.pluralize
11
- all = all.pluralize
12
- end
8
+ kind = kind.pluralize if @block_form
13
9
  layers = [
14
- "#{all}",
15
- "#{all}/#{Kubes.env}",
10
+ "all",
11
+ "all/#{Kubes.env}",
16
12
  "#{kind}",
17
13
  "#{kind}/#{Kubes.env}",
18
14
  ]
@@ -6,23 +6,9 @@ class Kubes::Compiler
6
6
  end
7
7
 
8
8
  def compile
9
- klass = strategy_class
10
- return false unless klass
11
-
12
- strategy = klass.new(@options.merge(path: @path)) # Dsl or Erb
13
- result = strategy.run
9
+ result = Dispatcher.new(@options.merge(path: @path)).dispatch
14
10
  result.decorate!(:pre) # compile pre phase decoration
15
11
  result
16
12
  end
17
-
18
- def strategy_class
19
- ext = File.extname(@path).sub('.','').to_sym
20
- map = {
21
- rb: Dsl,
22
- yaml: Erb,
23
- yml: Erb,
24
- }
25
- map[ext]
26
- end
27
13
  end
28
14
  end
@@ -9,62 +9,6 @@ class Kubes::Compiler::Strategy
9
9
  @options = options
10
10
  @path = options[:path]
11
11
  @save_file = save_file(@path)
12
- @data = @options[:data] || {}
13
- end
14
-
15
- def run
16
- render_files(pre_layers)
17
- render(@path) # main resource definition
18
- render_files(post_layers)
19
-
20
- Result.new(@save_file, @data)
21
- end
22
-
23
- def render_files(paths)
24
- paths.each do |path|
25
- next unless File.exist?(path) # layers may not exist
26
- render(path)
27
- end
28
- end
29
-
30
- # render and merge
31
- def render(path)
32
- result = render_strategy(path)
33
- if result.is_a?(Kubes::Compiler::Dsl::Core::Blocks)
34
- result = result.results
35
- end
36
- @data.deeper_merge!(result)
37
- end
38
-
39
- # Delegate to Dsl or Erb strategy again and pass @data down to allow rendering of a mix of yaml and rb files.
40
- def render_strategy(path)
41
- if path.include?('.rb')
42
- dsl_class.new(@options.merge(path: path, data: @data)).run
43
- else
44
- Erb.new(@options.merge(data: @data)).render_result(path)
45
- end
46
- end
47
-
48
- # Must be defined here in case coming from Kubes::Compiler::Strategy::Erb#render_strategy
49
- def dsl_class
50
- if block_form?
51
- Kubes::Compiler::Dsl::Core::Blocks
52
- else
53
- syntax_class
54
- end
55
- end
56
-
57
- def syntax_class
58
- klass_name = normalize_kind(@save_file) # IE: @save_file: web/service.yaml
59
- "Kubes::Compiler::Dsl::Syntax::#{klass_name}".constantize
60
- rescue NameError
61
- logger.debug "Using default resource for: #{klass_name}"
62
- Kubes::Compiler::Dsl::Syntax::Resource # default
63
- end
64
-
65
- def block_form?
66
- type = extract_type(@save_file)
67
- type.pluralize == type
68
12
  end
69
13
  end
70
14
  end
@@ -0,0 +1,59 @@
1
+ class Kubes::Compiler::Strategy
2
+ class Dispatcher < Base
3
+ def dispatch
4
+ result = render(@path) # main
5
+ results = [result].flatten # ensure array
6
+ data = results.map! do |main|
7
+ hash = pre_layer.deeper_merge!(main) # need the ! or deep_merge returns original hash
8
+ hash.deeper_merge!(post_layer)
9
+ end
10
+ Result.new(@save_file, data)
11
+ end
12
+
13
+ # Render via to Erb or one of the DSL syntax classes or Core/Blocks class
14
+ def render(path)
15
+ if path.include?('.rb')
16
+ klass = dsl_class(path) # IE: Kubes::Compiler::Dsl::Syntax::Deployment or Kubes::Compiler::Dsl::Core::Blocks
17
+ klass.new(@options.merge(path: path, data: @data)).run
18
+ else
19
+ Erb.new(@options.merge(data: @data)).render_result(path)
20
+ end
21
+ end
22
+
23
+ # Must be defined here in case coming from Kubes::Compiler::Strategy::Erb#render
24
+ def dsl_class(path)
25
+ if block_form?(path)
26
+ Kubes::Compiler::Dsl::Core::Blocks
27
+ else
28
+ syntax_class
29
+ end
30
+ end
31
+
32
+ def syntax_class
33
+ klass_name = normalize_kind(@save_file) # IE: @save_file: web/service.yaml
34
+ "Kubes::Compiler::Dsl::Syntax::#{klass_name}".constantize
35
+ rescue NameError
36
+ logger.debug "Using default resource for: #{klass_name}"
37
+ Kubes::Compiler::Dsl::Syntax::Resource # default
38
+ end
39
+
40
+ def block_form?(path)
41
+ type = extract_type(path)
42
+ type.pluralize == type
43
+ end
44
+
45
+ def pre_layer
46
+ merge_layers(pre_layers)
47
+ end
48
+
49
+ def post_layer
50
+ merge_layers(post_layers)
51
+ end
52
+
53
+ def merge_layers(layers)
54
+ layers.inject({}) do |hash, layer|
55
+ render(layer)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -20,13 +20,12 @@ class Kubes::Compiler::Strategy
20
20
  end
21
21
 
22
22
  def render_result(path)
23
- if File.exist?(path)
24
- yaml = RenderMePretty.result(path, context: self)
25
- result = yaml_load(path, yaml)
26
- result.is_a?(Hash) ? result : {} # in case of blank yaml doc a Boolean false is returned
27
- else
28
- {}
29
- end
23
+ return unless File.exist?(path)
24
+
25
+ yaml = RenderMePretty.result(path, context: self)
26
+ result = yaml_load(path, yaml)
27
+ # in case of blank yaml doc a Boolean false is returned. else Hash or Array is returned
28
+ %w[Array Hash].include?(result.class.to_s) ? result : {}
30
29
  end
31
30
 
32
31
  def yaml_load(path, yaml)
@@ -14,11 +14,15 @@ class Kubes::Compiler::Strategy
14
14
  # decorate(:pre) or decorate(:post)
15
15
  def decorate!(phase)
16
16
  klass = "Kubes::Compiler::Decorator::#{phase.to_s.camelize}".constantize
17
- @data = klass.new(@data).result
17
+ results = [@data].flatten
18
+ results.map! do |r|
19
+ klass.new(r).result
20
+ end
18
21
  end
19
22
 
20
23
  def content
21
- yaml_dump(@data)
24
+ result = @data.size == 1 ? @data.first : @data
25
+ yaml_dump(result)
22
26
  end
23
27
  end
24
28
  end
@@ -1,12 +1,12 @@
1
1
  module Kubes::Compiler::Util
2
2
  module Normalize
3
3
  def normalize_kind(path)
4
- info = path.sub(%r{.*/.kubes/resources/}, '')
5
- extract_type(info).underscore.camelize # Deployment, Service, Ingress, ManagedCertificate, etc
4
+ extract_type(path).underscore.camelize # Deployment, Service, Ingress, ManagedCertificate, etc
6
5
  end
7
6
 
8
7
  # info: web/service.yaml
9
8
  def extract_type(info)
9
+ info = info.sub(%r{.*/.kubes/resources/}, '')
10
10
  _, kind = info.split('/')
11
11
  kind.sub('.yaml','').sub('.yml','').sub('.rb','').sub(/-.*/,'')
12
12
  end
@@ -4,12 +4,15 @@ require "yaml"
4
4
  module Kubes::Compiler::Util
5
5
  module YamlDump
6
6
  def yaml_dump(data)
7
- if data.key?("kind") # single resource in YAML
7
+ case data
8
+ when Array
9
+ items = data.map do |i|
10
+ standardize_yaml(i)
11
+ end
12
+ items.map(&:to_yaml).join("")
13
+ else # single resource in YAML
8
14
  data = standardize_yaml(data)
9
15
  data.to_yaml
10
- else
11
- items = data.map { |k,v| standardize_yaml(v) }
12
- items.map(&:to_yaml).join("")
13
16
  end
14
17
  end
15
18
 
@@ -1,3 +1,3 @@
1
1
  module Kubes
2
- VERSION = "0.6.4"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ name "<%= app %>"
2
+ addresses(["1.1.1.1"])
3
+ ports(["80"])
@@ -0,0 +1,9 @@
1
+ apiVersion: v1
2
+ kind: Endpoints
3
+ metadata:
4
+ name: <%= app %>
5
+ subsets:
6
+ - addresses:
7
+ - ip: 192.0.2.42
8
+ ports:
9
+ - port: 9376
@@ -1,12 +1,13 @@
1
- describe Kubes::Compiler::Strategy::Dsl do
2
- let(:dsl) { described_class.new(options) }
3
- let(:options) { {path: fixture(resource) } }
1
+ describe Kubes::Compiler::Strategy::Dispatcher do
2
+ let(:dispatcher) { described_class.new(options) }
3
+ let(:options) { {path: path } }
4
+ let(:path) { fixture(resource) }
4
5
 
5
6
  context "standard" do
6
7
  let(:resource) { "project/.kubes/resources/web/deployment" }
7
8
  it "run" do
8
- result = dsl.run
9
- expect(dsl.dsl_class).to eq(Kubes::Compiler::Dsl::Syntax::Deployment)
9
+ result = dispatcher.dispatch
10
+ expect(dispatcher.dsl_class(path)).to eq(Kubes::Compiler::Dsl::Syntax::Deployment)
10
11
  data = YAML.load(result.content)
11
12
  expect(data['kind']).to eq "Deployment"
12
13
  end
@@ -15,8 +16,8 @@ describe Kubes::Compiler::Strategy::Dsl do
15
16
  context "blocks" do
16
17
  let(:resource) { "blocks/deployments" }
17
18
  it "run" do
18
- result = dsl.run
19
- expect(dsl.dsl_class).to eq(Kubes::Compiler::Dsl::Core::Blocks)
19
+ result = dispatcher.dispatch
20
+ expect(dispatcher.dsl_class(path)).to eq(Kubes::Compiler::Dsl::Core::Blocks)
20
21
  resource = result.content.split('---').last
21
22
  data = YAML.load(resource)
22
23
  expect(data['kind']).to eq "Deployment"
@@ -27,8 +28,8 @@ describe Kubes::Compiler::Strategy::Dsl do
27
28
  context "multiple files" do
28
29
  let(:resource) { "multiple-files/.kubes/resources/web/deployment-1" }
29
30
  it "run" do
30
- result = dsl.run
31
- expect(dsl.dsl_class).to eq(Kubes::Compiler::Dsl::Syntax::Deployment)
31
+ result = dispatcher.dispatch
32
+ expect(dispatcher.dsl_class(path)).to eq(Kubes::Compiler::Dsl::Syntax::Deployment)
32
33
  data = YAML.load(result.content)
33
34
  expect(data['kind']).to eq "Deployment"
34
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-11 00:00:00.000000000 Z
11
+ date: 2020-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -142,28 +142,28 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.3.0
145
+ version: 0.3.1
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.3.0
152
+ version: 0.3.1
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: kubes_google
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: 0.3.2
159
+ version: 0.3.5
160
160
  type: :runtime
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: 0.3.2
166
+ version: 0.3.5
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: bundler
169
169
  requirement: !ruby/object:Gem::Requirement
@@ -377,6 +377,8 @@ files:
377
377
  - docs/_docs/vs/helm.md
378
378
  - docs/_docs/vs/kustomize.md
379
379
  - docs/_docs/yaml.md
380
+ - docs/_docs/yaml/multiple-files.md
381
+ - docs/_docs/yaml/multiple-resources.md
380
382
  - docs/_includes/commands.html
381
383
  - docs/_includes/config/hooks/options.md
382
384
  - docs/_includes/content.html
@@ -593,6 +595,7 @@ files:
593
595
  - lib/kubes/compiler/dsl/syntax/config_map.rb
594
596
  - lib/kubes/compiler/dsl/syntax/daemon_set.rb
595
597
  - lib/kubes/compiler/dsl/syntax/deployment.rb
598
+ - lib/kubes/compiler/dsl/syntax/endpoint.rb
596
599
  - lib/kubes/compiler/dsl/syntax/ingress.rb
597
600
  - lib/kubes/compiler/dsl/syntax/job.rb
598
601
  - lib/kubes/compiler/dsl/syntax/managed_certificate.rb
@@ -613,7 +616,7 @@ files:
613
616
  - lib/kubes/compiler/shared/plugin_helpers.rb
614
617
  - lib/kubes/compiler/strategy.rb
615
618
  - lib/kubes/compiler/strategy/base.rb
616
- - lib/kubes/compiler/strategy/dsl.rb
619
+ - lib/kubes/compiler/strategy/dispatcher.rb
617
620
  - lib/kubes/compiler/strategy/erb.rb
618
621
  - lib/kubes/compiler/strategy/erb/yaml_error.rb
619
622
  - lib/kubes/compiler/strategy/pass.rb
@@ -674,6 +677,7 @@ files:
674
677
  - lib/templates/new/resource/dsl/config_map.rb
675
678
  - lib/templates/new/resource/dsl/daemon_set.rb
676
679
  - lib/templates/new/resource/dsl/deployment.rb
680
+ - lib/templates/new/resource/dsl/endpoint.rb
677
681
  - lib/templates/new/resource/dsl/ingress.rb
678
682
  - lib/templates/new/resource/dsl/job.rb
679
683
  - lib/templates/new/resource/dsl/managed_certificate.rb
@@ -689,6 +693,7 @@ files:
689
693
  - lib/templates/new/resource/yaml/config_map.yaml
690
694
  - lib/templates/new/resource/yaml/daemon_set.yaml
691
695
  - lib/templates/new/resource/yaml/deployment.yaml
696
+ - lib/templates/new/resource/yaml/endpoint.yaml
692
697
  - lib/templates/new/resource/yaml/ingress.yaml
693
698
  - lib/templates/new/resource/yaml/job.yaml
694
699
  - lib/templates/new/resource/yaml/managed_certificate.yaml
@@ -748,7 +753,7 @@ files:
748
753
  - spec/kubes/cli/prune_spec.rb
749
754
  - spec/kubes/compiler/decorator/post/deployment_spec.rb
750
755
  - spec/kubes/compiler/decorator/post/pod_spec.rb
751
- - spec/kubes/compiler/strategy/dsl_spec.rb
756
+ - spec/kubes/compiler/strategy/dispatcher_spec.rb
752
757
  - spec/kubes/compiler_spec.rb
753
758
  - spec/kubes/dsl/daemon_set.rb
754
759
  - spec/kubes/dsl/deployment_spec.rb
@@ -823,7 +828,7 @@ test_files:
823
828
  - spec/kubes/cli/prune_spec.rb
824
829
  - spec/kubes/compiler/decorator/post/deployment_spec.rb
825
830
  - spec/kubes/compiler/decorator/post/pod_spec.rb
826
- - spec/kubes/compiler/strategy/dsl_spec.rb
831
+ - spec/kubes/compiler/strategy/dispatcher_spec.rb
827
832
  - spec/kubes/compiler_spec.rb
828
833
  - spec/kubes/dsl/daemon_set.rb
829
834
  - spec/kubes/dsl/deployment_spec.rb
@@ -1,4 +0,0 @@
1
- class Kubes::Compiler::Strategy
2
- class Dsl < Base
3
- end
4
- end