-
Notifications
You must be signed in to change notification settings - Fork 3
/
render.go
85 lines (71 loc) · 1.99 KB
/
render.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package common
import (
"fmt"
"github.com/docker/distribution/reference"
"helm.sh/helm/v3/pkg/cli/values"
"k8s.io/apimachinery/pkg/runtime"
"github.com/gitpod-io/observability/installer/pkg/config"
)
// Renderable turns the config into a set of Kubernetes runtime objects
type RenderFunc func(cfg *RenderContext) ([]runtime.Object, error)
type HelmFunc func(cfg *RenderContext) ([]string, error)
type HelmConfig struct {
Enabled bool
Values *values.Options
}
func CompositeRenderFunc(f ...RenderFunc) RenderFunc {
return func(ctx *RenderContext) ([]runtime.Object, error) {
var res []runtime.Object
for _, g := range f {
obj, err := g(ctx)
if err != nil {
return nil, err
}
if len(obj) == 0 {
// the RenderFunc chose not to render anything, possibly based on config it received
continue
}
res = append(res, obj...)
}
return res, nil
}
}
func CompositeHelmFunc(f ...HelmFunc) HelmFunc {
return func(ctx *RenderContext) ([]string, error) {
var res []string
for _, g := range f {
str, err := g(ctx)
if err != nil {
return nil, err
}
res = append(res, str...)
}
return res, nil
}
}
type RenderContext struct {
Config config.Config
Namespace string
}
func ImageName(imageURL, tag string) string {
ref := fmt.Sprintf("%s:%s", imageURL, tag)
pref, err := reference.ParseNamed(ref)
if err != nil {
panic(fmt.Sprintf("cannot parse image ref %s: %v", ref, err))
}
if _, ok := pref.(reference.Tagged); !ok {
panic(fmt.Sprintf("image ref %s has no tag: %v", ref, err))
}
return ref
}
// NewRenderContext constructor function to create a new RenderContext with the values generated
func NewRenderContext(cfg config.Config, namespace string) (*RenderContext, error) {
ctx := &RenderContext{
Config: cfg,
Namespace: namespace,
}
return ctx, nil
}