Skip to content

Commit fd5c30a

Browse files
Expose CRDInstallOptions via envtest.Environment
This change provides better control for installing CRDs in `envtest`. Addresses #541
1 parent d7467fc commit fd5c30a

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

pkg/builder/builder_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ func addCRDToEnvironment(env *envtest.Environment, gvks ...schema.GroupVersionKi
103103
},
104104
},
105105
}
106-
env.CRDs = append(env.CRDs, crd)
106+
env.CRDInstallOptions.CRDs = append(env.CRDInstallOptions.CRDs, crd)
107107
}
108108
}

pkg/envtest/crd.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737

3838
// CRDInstallOptions are the options for installing CRDs
3939
type CRDInstallOptions struct {
40-
// Paths is the path to the directory containing CRDs
40+
// Paths is a list of paths to the directories containing CRDs
4141
Paths []string
4242

4343
// CRDs is a list of CRDs to install
@@ -46,11 +46,11 @@ type CRDInstallOptions struct {
4646
// ErrorIfPathMissing will cause an error if a Path does not exist
4747
ErrorIfPathMissing bool
4848

49-
// maxTime is the max time to wait
50-
maxTime time.Duration
49+
// MaxTime is the max time to wait
50+
MaxTime time.Duration
5151

52-
// pollInterval is the interval to check
53-
pollInterval time.Duration
52+
// PollInterval is the interval to check
53+
PollInterval time.Duration
5454
}
5555

5656
const defaultPollInterval = 100 * time.Millisecond
@@ -97,11 +97,11 @@ func readCRDFiles(options *CRDInstallOptions) error {
9797

9898
// defaultCRDOptions sets the default values for CRDs
9999
func defaultCRDOptions(o *CRDInstallOptions) {
100-
if o.maxTime == 0 {
101-
o.maxTime = defaultMaxWait
100+
if o.MaxTime == 0 {
101+
o.MaxTime = defaultMaxWait
102102
}
103-
if o.pollInterval == 0 {
104-
o.pollInterval = defaultPollInterval
103+
if o.PollInterval == 0 {
104+
o.PollInterval = defaultPollInterval
105105
}
106106
}
107107

@@ -132,7 +132,7 @@ func WaitForCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResourc
132132

133133
// Poll until all resources are found in discovery
134134
p := &poller{config: config, waitingFor: waitingFor}
135-
return wait.PollImmediate(options.pollInterval, options.maxTime, p.poll)
135+
return wait.PollImmediate(options.PollInterval, options.MaxTime, p.poll)
136136
}
137137

138138
// poller checks if all the resources have been found in discovery, and returns false if not

pkg/envtest/envtest_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ var _ = Describe("Test", func() {
145145
}},
146146
},
147147
},
148-
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
148+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
149149
)
150150
Expect(err).NotTo(HaveOccurred())
151151

@@ -178,7 +178,7 @@ var _ = Describe("Test", func() {
178178
}},
179179
},
180180
},
181-
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
181+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
182182
)
183183
Expect(err).To(HaveOccurred())
184184

@@ -209,7 +209,7 @@ var _ = Describe("Test", func() {
209209
Plural: "fake",
210210
}},
211211
}},
212-
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
212+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
213213
)
214214
Expect(err).To(HaveOccurred())
215215

pkg/envtest/helper.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package envtest
2+
3+
import apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
4+
5+
// mergePaths merges two string slices containing paths.
6+
// This function makes no guarantees about order of the merged slice.
7+
func mergePaths(s1, s2 []string) []string {
8+
m := make(map[string]struct{})
9+
for _, s := range s1 {
10+
m[s] = struct{}{}
11+
}
12+
for _, s := range s2 {
13+
m[s] = struct{}{}
14+
}
15+
merged := make([]string, len(m))
16+
i := 0
17+
for key, _ := range m {
18+
merged[i] = key
19+
i++
20+
}
21+
return merged
22+
}
23+
24+
// mergeCRDs merges two CRD slices using their names.
25+
// This function makes no guarantees about order of the merged slice.
26+
func mergeCRDs(s1, s2 []*apiextensionsv1beta1.CustomResourceDefinition) []*apiextensionsv1beta1.CustomResourceDefinition {
27+
m := make(map[string]*apiextensionsv1beta1.CustomResourceDefinition)
28+
for _, crd := range s1 {
29+
m[crd.Name] = crd
30+
}
31+
for _, crd := range s2 {
32+
m[crd.Name] = crd
33+
}
34+
merged := make([]*apiextensionsv1beta1.CustomResourceDefinition, len(m))
35+
i := 0
36+
for _, crd := range m {
37+
merged[i] = crd
38+
i++
39+
}
40+
return merged
41+
}

pkg/envtest/server.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,17 @@ type Environment struct {
8181
// loading.
8282
Config *rest.Config
8383

84-
// CRDs is a list of CRDs to install
84+
// CRDInstallOptions are the options for installing CRDs.
85+
CRDInstallOptions CRDInstallOptions
86+
87+
// CRDs is a list of CRDs to install.
88+
// If both this field and CRDs field in CRDInstallOptions are specified, the
89+
// values are merged.
8590
CRDs []*apiextensionsv1beta1.CustomResourceDefinition
8691

8792
// CRDDirectoryPaths is a list of paths containing CRD yaml or json configs.
93+
// If both this field and Paths field in CRDInstallOptions are specified, the
94+
// values are merged.
8895
CRDDirectoryPaths []string
8996

9097
// UseExisting indicates that this environments should use an
@@ -203,10 +210,9 @@ func (te *Environment) Start() (*rest.Config, error) {
203210
}
204211

205212
log.V(1).Info("installing CRDs")
206-
_, err := InstallCRDs(te.Config, CRDInstallOptions{
207-
Paths: te.CRDDirectoryPaths,
208-
CRDs: te.CRDs,
209-
})
213+
te.CRDInstallOptions.CRDs = mergeCRDs(te.CRDInstallOptions.CRDs, te.CRDs)
214+
te.CRDInstallOptions.Paths = mergePaths(te.CRDInstallOptions.Paths, te.CRDDirectoryPaths)
215+
_, err := InstallCRDs(te.Config, te.CRDInstallOptions)
210216
return te.Config, err
211217
}
212218

0 commit comments

Comments
 (0)