Skip to content

Commit c542008

Browse files
committed
- improve documentation
- set templateBody instead of spec - add basic tests to controller
1 parent 6d4fea4 commit c542008

14 files changed

+402
-154
lines changed

README.md

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This operator can be used to create any kubernetes object dynamically. Build your templates and set parameters to create new k8s objects.
33

44
## Use case
5-
Many kubernetes clusters are shared among many applications and teams. Sometimes services are available within the cluster scope and teams can use it to create or configure services using kubernetes spec (such as PrometheusRule, ExternalDNS, etc.). Some of these specs are too complex or contains some configurations that we do not want to expose. You can automate it's creation using templates.
5+
Many kubernetes clusters are shared among many applications and teams. Sometimes services are available within the cluster scope and teams can use it to create or configure services using kubernetes spec (such as ConfigMap, Secret, PrometheusRule, ExternalDNS, etc.). Some of these specs are too complex or contains some configurations that we do not want to expose. You can automate it's creation using templates.
66

77
This operator can create kubernete objects based on templates and simple namespaced parameters. You can give permissions to user create parameters but hide templates and created objects from developers or users using the Kubernetes RBAC system.
88

@@ -16,7 +16,7 @@ kubectl apply -f https://raw.githubusercontent.com/ericogr/k8s-object-template-o
1616
## Additionals Kubernetes Roles
1717
This operator should be allowed to create objects defined in templates. With default permission, it can create any object, but it can be a bit tricky. The ClusterRole ```k8s-ot-manager-role``` can be used to set permissions as needed.
1818

19-
See this example to add PrometheusRules permission to this operator:
19+
See this example to add ConfigMap permission to this operator:
2020

2121
```yaml
2222
---
@@ -27,9 +27,9 @@ metadata:
2727
name: k8s-ot-manager-role
2828
rules:
2929
- apiGroups:
30-
- monitoring.coreos.com
30+
- ""
3131
resources:
32-
- prometheusrules
32+
- configmaps
3333
verbs:
3434
- create
3535
- get
@@ -94,39 +94,28 @@ Use templates as a base to create kubernetes objects. Users can define your own
9494
apiVersion: template.ericogr.github.com/v1
9595
kind: ObjectTemplate
9696
metadata:
97-
name: objecttemplate-prometheus-rules-default
97+
name: objecttemplate-configmap-test
9898
spec:
99-
description: Default prometheus rule
99+
description: ConfigMap test
100100
objects:
101-
- kind: PrometheusRule
102-
apiVersion: monitoring.coreos.com/v1
101+
- kind: ConfigMap
102+
apiVersion: v1
103103
metadata:
104104
labels:
105105
chave1: valor1
106106
chave2: valor2
107107
annotations:
108108
chave1a: valor1a
109109
chave2a: valor2a
110-
name: prometheus-rule-default
111-
spec: |-
112-
groups:
113-
- name: pods
114-
rules:
115-
- alert: pod_not_ready
116-
annotations:
117-
description: 'Pod not ready : {{"{{ $labels.pod }}"}}'
118-
summary: 'Pod not ready: {{"{{ $labels.pod }}"}}'
119-
expr: sum by(pod) (kube_pod_status_ready{namespace="{{ .__namespace }}"} == 0) != 0
120-
for: 10m
121-
labels:
122-
app_name: {{ .app_name }}
123-
app_route: slack
124-
app_severity: critical
125-
app_slack_channel: '{{ .app_slack_channel }}'
110+
name: configmap-test
111+
templateBody: |-
112+
data:
113+
name: '{{ .name }}'
114+
age: '{{ .age }}'
126115
```
127116
128117
## Basic Template Substitution System
129-
You can use sintax like ```{{ .variable }}``` to replace parameters. Let's say you create ```app_name: myapp```. You can use ```{{ .app_name }}``` inside spec template to be replaced in runtime by this controller. If you need to scape braces, use ```{{"{{anything}}"}}```
118+
You can use sintax like ```{{ .variable }}``` to replace parameters. Let's say you create ```name: foo```. You can use ```{{ .name }}``` inside spec template to be replaced in runtime by this controller. If you need to scape braces, use ```{{"{{anything}}"}}```
130119

131120
### System Runtime Variables
132121

@@ -151,8 +140,8 @@ metadata:
151140
namespace: default
152141
spec:
153142
templates:
154-
- name: objecttemplate-prometheus-rules-default
143+
- name: objecttemplate-configmap-test
155144
values:
156-
app_name: myapp
157-
app_slack_channel: '#slack-channel'
158-
```
145+
name: foo
146+
age: '64'
147+
```

apis/template.ericogr.github.com/v1/objecttemplate_types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ type Metadata struct {
2828

2929
// Object defines a single object to be created
3030
type Object struct {
31-
Kind string `json:"kind"`
32-
APIVersion string `json:"apiVersion"`
33-
Metadata Metadata `json:"metadata,omitempty"`
34-
Name string `json:"name"`
35-
Spec string `json:"spec"`
31+
Kind string `json:"kind"`
32+
APIVersion string `json:"apiVersion"`
33+
Metadata Metadata `json:"metadata,omitempty"`
34+
Name string `json:"name"`
35+
TemplateBody string `json:"templateBody"`
3636
}
3737

3838
// ObjectTemplateSpec defines the desired state of ObjectTemplate

config/crd/bases/template.ericogr.github.com_objecttemplates.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ spec:
6464
type: object
6565
name:
6666
type: string
67-
spec:
67+
templateBody:
6868
type: string
6969
required:
7070
- apiVersion
7171
- kind
7272
- name
73-
- spec
73+
- templateBody
7474
type: object
7575
type: array
7676
required:

config/samples/template.ericogr.github.com_v1_objecttemplate.yaml

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,21 @@
22
apiVersion: template.ericogr.github.com/v1
33
kind: ObjectTemplate
44
metadata:
5-
name: objecttemplate-prometheus-rules-default
5+
name: objecttemplate-configmap-test
66
spec:
7-
description: Default prometheus rule
7+
description: ConfigMap test
88
objects:
9-
- kind: PrometheusRule
10-
apiVersion: monitoring.coreos.com/v1
9+
- kind: ConfigMap
10+
apiVersion: v1
1111
metadata:
1212
labels:
1313
chave1: valor1
1414
chave2: valor2
1515
annotations:
1616
chave1a: valor1a
1717
chave2a: valor2a
18-
name: prometheus-rule-default
19-
spec: |-
20-
groups:
21-
- name: pods
22-
rules:
23-
- alert: pod_not_ready
24-
annotations:
25-
description: 'Pod not ready : {{"{{ $labels.pod }}"}}'
26-
summary: 'Pod not ready: {{"{{ $labels.pod }}"}}'
27-
expr: sum by(pod) (kube_pod_status_ready{namespace="{{ .__namespace }}"} == 0) != 0
28-
for: 10m
29-
labels:
30-
app_name: {{ .app_name }}
31-
app_route: slack
32-
app_severity: critical
33-
app_slack_channel: '{{ .app_slack_channel }}'
18+
name: configmap-test
19+
templateBody: |-
20+
data:
21+
name: '{{ .name }}'
22+
age: '{{ .age }}'

config/samples/template.ericogr.github.com_v1_objecttemplateparams.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
namespace: default
77
spec:
88
templates:
9-
- name: objecttemplate-prometheus-rules-default
9+
- name: objecttemplate-configmap-test
1010
values:
11-
app_name: myapp
12-
app_slack_channel: '#slack-channel'
11+
name: foo
12+
age: '64'

controllers/template.ericogr.github.com/commons.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (c *Common) GetObjectSimplified(groupversion string, kind string, namespace
181181
// ToObject process object from template
182182
func (c *Common) ToObject(obj otv1.Object, owners []metav1.OwnerReference, values map[string]string, namespaceName string) (unstructured.Unstructured, *schema.GroupVersionKind, error) {
183183
templateValues := c.addRuntimeVariablesToMap(values, obj, namespaceName)
184-
templateYAML := getStringObject(obj.APIVersion, obj.Kind, obj.Spec)
184+
templateYAML := getStringObject(obj.APIVersion, obj.Kind, obj.TemplateBody)
185185
templateYAMLExecuted, err := executeTemplate(templateYAML, templateValues)
186186

187187
if err != nil {

controllers/template.ericogr.github.com/object_utilities.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"text/template"
2222
)
2323

24-
func getStringObject(apiVersion string, kind string, spec string) string {
24+
func getStringObject(apiVersion string, kind string, templateBody string) string {
2525
sb := strings.Builder{}
2626

2727
sb.WriteString("---\n")
@@ -31,17 +31,11 @@ func getStringObject(apiVersion string, kind string, spec string) string {
3131
sb.WriteString("kind: ")
3232
sb.WriteString(kind)
3333
sb.WriteRune('\n')
34-
sb.WriteString("spec:\n")
35-
sb.WriteString(addIdentation(spec))
34+
sb.WriteString(templateBody)
3635

3736
return sb.String()
3837
}
3938

40-
func addIdentation(str string) string {
41-
str = " " + str
42-
return strings.ReplaceAll(str, "\n", "\n ")
43-
}
44-
4539
func executeTemplate(templateYAML string, values map[string]string) (string, error) {
4640
template, err := template.New("template").Parse(templateYAML)
4741

controllers/template.ericogr.github.com/object_utilities_test.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

controllers/template.ericogr.github.com/objecttemplate_controller.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"context"
2121
"reflect"
2222

23-
//metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
2523
"github.com/go-logr/logr"
2624
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
2725
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

0 commit comments

Comments
 (0)