forked from grafana/grafana-operator
/
resourceHelper.go
67 lines (55 loc) · 1.75 KB
/
resourceHelper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package grafana
import (
"github.com/ghodss/yaml"
integreatly "github.com/integr8ly/grafana-operator/pkg/apis/integreatly/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
// An abstraction type that allows easier access to the contents of an
// unstructured resource
type UnstructuredResourceMap struct {
Values map[string]interface{}
}
// Go one level deeper in the unstructured resource map
func (m UnstructuredResourceMap) access(key string) *UnstructuredResourceMap {
return &UnstructuredResourceMap{
Values: m.Values[key].(map[string]interface{}),
}
}
// Return value of the current level in the unstructured resource map
func (m UnstructuredResourceMap) get(key string) interface{} {
return m.Values[key]
}
// Set value in an unstructured resource map
func (m UnstructuredResourceMap) set(key string, value interface{}) {
m.Values[key] = value
}
// Create a new unstructured resource map
func newUnstructuredResourceMap(unstructured *unstructured.Unstructured) *UnstructuredResourceMap {
return &UnstructuredResourceMap{
Values: unstructured.UnstructuredContent(),
}
}
// Helps with creating kubernetes resources from yaml templates
type ResourceHelper struct {
templateHelper *TemplateHelper
cr *integreatly.Grafana
}
func newResourceHelper(cr *integreatly.Grafana) *ResourceHelper {
return &ResourceHelper{
templateHelper: newTemplateHelper(cr),
cr: cr,
}
}
func (r *ResourceHelper) createResource(template string) (runtime.Object, error) {
tpl, err := r.templateHelper.loadTemplate(template)
if err != nil {
return nil, err
}
resource := unstructured.Unstructured{}
err = yaml.Unmarshal(tpl, &resource)
if err != nil {
return nil, err
}
return &resource, nil
}