Sha256: 125fa55b34aa5cf86aebc677e77d00944565a211e8d2b6b900ca3163e0320d64

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

---
title: ERB Comment Syntax PreProcessor
---

Kubes allows you build kubernetes resources by compiling down `.kubes/resources` files with ERB.  This is a powerful ability, but it can get in the way of IDE kubernetes autocompletion tools.

## Pre-Processing Before ERB

To work with IDE kubernetes autocompletion tools, kubes supports a lighter ERB comment-based syntax. It looks like this:

.kubes/resources/web/service.yaml

```yaml
apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    name: web
#ERB if @role_label
    role: #ERB= @role_label
#ERB end
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: <%= dockerfile_port %>
  selector:
    role: web
  type: ClusterIP
```

This allows IDB kubernetes autocompletion plugins and tools to work because the `.kubes/resources/web/service.yaml` source code itself is perfectly valid YAML.

The `#ERB` comments are essentially replaced by `<% ... %>` tags before ERB processing happens. Example:

.kubes/resources/web/service.yaml.erb

```yaml
apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    name: web
<% if @role_label %>
    role: <%= @role_label %>
<% end %>
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: <%= dockerfile_port %>
  selector:
    role: web
  type: ClusterIP
```

If you need to keep around the generated `.erb` file for debugging, use the `KUBES_KEEP_ERB` env var. Example:

    $ KUBES_KEEP_ERB=1 kubes compile
    .kubes/resources/web/service.yaml.erb  # kept around

## Multiple Line ERB Comment Syntax

The ERB Comment syntax works because kubes simply replaces each line with actual ERB. For multiple line ERB syntax:

.kubes/resources/web/service.yaml

```yaml
#ERB if Kubes.env == "dev"
#ERB   env_label = "development"
#ERB end
apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    name: web
    env: #ERB= env_label
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: <%= dockerfile_port %>
  selector:
    role: web
  type: ClusterIP
```

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kubes-0.8.1 docs/_docs/yaml/erb-comment.md
kubes-0.8.0 docs/_docs/yaml/erb-comment.md