-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
123 lines (109 loc) · 3.76 KB
/
Copy pathoptions.go
File metadata and controls
123 lines (109 loc) · 3.76 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package framework
import (
"path/filepath"
"k8s.io/apimachinery/pkg/runtime"
)
// Option configures a HookExecutionConfig at construction time.
type Option interface {
apply(*execOptions)
}
type optionFunc func(*execOptions)
func (f optionFunc) apply(o *execOptions) { f(o) }
// execOptions holds parsed framework options.
type execOptions struct {
initValues string
initConfigValues string
valuesSchemaPath string
configValuesSchemaPath string
extraSchemeBuilders []runtime.SchemeBuilder
crds []customCRD
}
type customCRD struct {
group string
version string
kind string
namespaced bool
}
// WithInitialValues sets the initial Helm values JSON or YAML.
func WithInitialValues(v string) Option {
return optionFunc(func(o *execOptions) {
o.initValues = v
})
}
// WithInitialConfigValues sets the initial module config values JSON or YAML.
func WithInitialConfigValues(v string) Option {
return optionFunc(func(o *execOptions) {
o.initConfigValues = v
})
}
// WithSchemeBuilder registers an additional runtime.SchemeBuilder so that
// typed CRDs from your module can be used in YAML state and assertions.
func WithSchemeBuilder(builder runtime.SchemeBuilder) Option {
return optionFunc(func(o *execOptions) {
o.extraSchemeBuilders = append(o.extraSchemeBuilders, builder)
})
}
// WithCRD registers a custom resource definition with the fake cluster so
// that resources of this kind can be created/listed via the dynamic client.
//
// Use this when your hook reads or writes CRs not registered through a
// runtime.SchemeBuilder.
func WithCRD(group, version, kind string, namespaced bool) Option {
return optionFunc(func(o *execOptions) {
o.crds = append(o.crds, customCRD{
group: group, version: version, kind: kind, namespaced: namespaced,
})
})
}
// WithValuesSchema reads an OpenAPI v3 schema from the given file path,
// extracts a values document populated with all `default:` values, and
// uses it as the baseline for the framework's `Values`. Anything passed
// via WithInitialValues (or HookExecutionConfigInit's initValues) is then
// deep-merged on top, so test-supplied values override schema defaults.
//
// The schema may use the addon-operator `x-extend` extension to inherit
// `properties` / `required` from a sibling schema (typically
// config-values.yaml). See LoadOpenAPISchema for details.
//
// Construction fails the test (via testing.TB.Fatalf) if the file is
// missing or malformed. Use WithOpenAPIDir if you want missing files to
// be silently ignored.
func WithValuesSchema(path string) Option {
return optionFunc(func(o *execOptions) {
o.valuesSchemaPath = path
})
}
// WithConfigValuesSchema does the same as WithValuesSchema for the
// module's config values schema (typically `openapi/config-values.yaml`).
func WithConfigValuesSchema(path string) Option {
return optionFunc(func(o *execOptions) {
o.configValuesSchemaPath = path
})
}
// WithOpenAPIDir is a convenience wrapper that points the framework at a
// directory containing the standard module OpenAPI files:
//
// <dir>/values.yaml
// <dir>/config-values.yaml
//
// Either file may be absent. Whichever ones are present are loaded and
// their defaults are merged under any test-supplied values.
//
// Example:
//
// hec := framework.NewHookExecutionConfig(t, cfg, handler,
// framework.WithOpenAPIDir("../openapi"),
// framework.WithInitialValues(`{"https": {"mode": "CertManager"}}`),
// )
func WithOpenAPIDir(dir string) Option {
return optionFunc(func(o *execOptions) {
valuesPath := filepath.Join(dir, "values.yaml")
if fileExists(valuesPath) {
o.valuesSchemaPath = valuesPath
}
configPath := filepath.Join(dir, "config-values.yaml")
if fileExists(configPath) {
o.configValuesSchemaPath = configPath
}
})
}